b4b1.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* PT/SC remotes. Usually 443.92 Mhz OOK.
  2. *
  3. * This line code is used in many remotes such as Princeton chips
  4. * named PT2262, Silian Microelectronics SC5262 and others.
  5. * Basically every 4 pulsee represent a bit, where 1000 means 0, and
  6. * 1110 means 1. Usually we can read 24 bits of data.
  7. * In this specific implementation we check for a prelude that is
  8. * 1 bit high, 31 bits low, but the check is relaxed. */
  9. #include "../app.h"
  10. static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
  11. if (numbits < 30) return false;
  12. /* Test different pulse + gap + first byte possibilities. */
  13. const char *sync_patterns[6] = {
  14. "100000000000000000000000000000011101", /* 30 times gap + one. */
  15. "100000000000000000000000000000010001", /* 30 times gap + zero. */
  16. "1000000000000000000000000000000011101", /* 31 times gap + one. */
  17. "1000000000000000000000000000000010001", /* 31 times gap + zero. */
  18. "10000000000000000000000000000000011101", /* 32 times gap + one. */
  19. "10000000000000000000000000000000010001", /* 32 times gap + zero. */
  20. };
  21. uint32_t off;
  22. int j;
  23. for (j = 0; j < 3; j++) {
  24. off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_patterns[j]);
  25. if (off != BITMAP_SEEK_NOT_FOUND) break;
  26. }
  27. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  28. if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 preamble id:%d at: %lu",j,off);
  29. info->start_off = off;
  30. // Seek data setction. Why -5? Last 5 half-bit-times are data.
  31. off += strlen(sync_patterns[j])-5;
  32. uint8_t d[3]; /* 24 bits of data. */
  33. uint32_t decoded =
  34. convert_from_line_code(d,sizeof(d),bits,numbytes,off,"1000","1110");
  35. if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 decoded: %lu",decoded);
  36. if (decoded < 24) return false;
  37. off += 24*4; // seek to end symbol offset to calculate the length.
  38. off++; // In this protocol there is a final pulse as terminator.
  39. info->pulses_count = off - info->start_off;
  40. fieldset_add_bytes(info->fieldset,"id",d,5);
  41. fieldset_add_uint(info->fieldset,"button",d[2]&0xf,4);
  42. return true;
  43. }
  44. /* Give fields and defaults for the signal creator. */
  45. static void get_fields(ProtoViewFieldSet *fieldset) {
  46. uint8_t default_id[3]= {0xAB, 0xCD, 0xE0};
  47. fieldset_add_bytes(fieldset,"id",default_id,5);
  48. fieldset_add_uint(fieldset,"button",1,4);
  49. }
  50. /* Create a signal. */
  51. static void build_message(RawSamplesBuffer *samples, ProtoViewFieldSet *fs)
  52. {
  53. uint32_t te = 334; // Short pulse duration in microseconds.
  54. // Sync: 1 te pulse, 31 te gap.
  55. raw_samples_add(samples,true,te);
  56. raw_samples_add(samples,false,te*31);
  57. // ID + button state
  58. uint8_t data[3];
  59. memcpy(data,fs->fields[0]->bytes,3);
  60. data[2] = (data[2]&0xF0) | (fs->fields[1]->uvalue & 0xF);
  61. for (uint32_t j = 0; j < 24; j++) {
  62. if (bitmap_get(data,sizeof(data),j)) {
  63. raw_samples_add(samples,true,te*3);
  64. raw_samples_add(samples,false,te);
  65. } else {
  66. raw_samples_add(samples,true,te);
  67. raw_samples_add(samples,false,te*3);
  68. }
  69. }
  70. // Signal terminator. Just a single short pulse.
  71. raw_samples_add(samples,true,te);
  72. }
  73. ProtoViewDecoder B4B1Decoder = {
  74. .name = "PT/SC remote",
  75. .decode = decode,
  76. .get_fields = get_fields,
  77. .build_message = build_message
  78. };