b4b1.c 3.4 KB

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