b4b1.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. const char *sync_patterns[3] = {
  13. "10000000000000000000000000000001", /* 30 zero bits. */
  14. "100000000000000000000000000000001", /* 31 zero bits. */
  15. "1000000000000000000000000000000001", /* 32 zero bits. */
  16. };
  17. uint32_t off;
  18. int j;
  19. for (j = 0; j < 3; j++) {
  20. off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_patterns[j]);
  21. if (off != BITMAP_SEEK_NOT_FOUND) break;
  22. }
  23. if (off == BITMAP_SEEK_NOT_FOUND) return false;
  24. if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 preamble at: %lu",off);
  25. info->start_off = off;
  26. // Seek data setction. Why -1? Last bit is data.
  27. off += strlen(sync_patterns[j])-1;
  28. uint8_t d[3]; /* 24 bits of data. */
  29. uint32_t decoded =
  30. convert_from_line_code(d,sizeof(d),bits,numbytes,off,"1000","1110");
  31. if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 decoded: %lu",decoded);
  32. if (decoded < 24) return false;
  33. off += 24*4; // seek to end symbol offset to calculate the length.
  34. off++; // In this protocol there is a final pulse as terminator.
  35. info->pulses_count = off - info->start_off;
  36. fieldset_add_bytes(info->fieldset,"id",d,5);
  37. fieldset_add_uint(info->fieldset,"button",d[2]&0xf,4);
  38. return true;
  39. }
  40. /* Give fields and defaults for the signal creator. */
  41. static void get_fields(ProtoViewFieldSet *fieldset) {
  42. uint8_t default_id[3]= {0xAB, 0xCD, 0xE0};
  43. fieldset_add_bytes(fieldset,"id",default_id,5);
  44. fieldset_add_uint(fieldset,"button",1,4);
  45. }
  46. /* Create a signal. */
  47. static void build_message(RawSamplesBuffer *samples, ProtoViewFieldSet *fs)
  48. {
  49. uint32_t te = 334; // Short pulse duration in microseconds.
  50. // Sync: 1 te pulse, 31 te gap.
  51. raw_samples_add(samples,true,te);
  52. raw_samples_add(samples,false,te*31);
  53. // ID + button state
  54. uint8_t data[3];
  55. memcpy(data,fs->fields[0]->bytes,3);
  56. data[2] = (data[2]&0xF0) | (fs->fields[1]->uvalue & 0xF);
  57. for (uint32_t j = 0; j < 24; j++) {
  58. if (bitmap_get(data,sizeof(data),j)) {
  59. raw_samples_add(samples,true,te*3);
  60. raw_samples_add(samples,false,te);
  61. } else {
  62. raw_samples_add(samples,true,te);
  63. raw_samples_add(samples,false,te*3);
  64. }
  65. }
  66. // Signal terminator. Just a single short pulse.
  67. raw_samples_add(samples,true,te);
  68. }
  69. ProtoViewDecoder B4B1Decoder = {
  70. .name = "PT/SC remote",
  71. .decode = decode,
  72. .get_fields = get_fields,
  73. .build_message = build_message
  74. };