b4b1.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = convert_from_line_code(d, sizeof(d), bits, numbytes, off, "1000", "1110");
  37. if(DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 decoded: %lu", decoded);
  38. if(decoded < 24) return false;
  39. off += 24 * 4; // seek to end symbol offset to calculate the length.
  40. off++; // In this protocol there is a final pulse as terminator.
  41. info->pulses_count = off - info->start_off;
  42. fieldset_add_bytes(info->fieldset, "id", d, 5);
  43. fieldset_add_uint(info->fieldset, "button", d[2] & 0xf, 4);
  44. return true;
  45. }
  46. /* Give fields and defaults for the signal creator. */
  47. static void get_fields(ProtoViewFieldSet* fieldset) {
  48. uint8_t default_id[3] = {0xAB, 0xCD, 0xE0};
  49. fieldset_add_bytes(fieldset, "id", default_id, 5);
  50. fieldset_add_uint(fieldset, "button", 1, 4);
  51. }
  52. /* Create a signal. */
  53. static void build_message(RawSamplesBuffer* samples, ProtoViewFieldSet* fs) {
  54. uint32_t te = 334; // Short pulse duration in microseconds.
  55. // Sync: 1 te pulse, 31 te gap.
  56. raw_samples_add(samples, true, te);
  57. raw_samples_add(samples, false, te * 31);
  58. // ID + button state
  59. uint8_t data[3];
  60. memcpy(data, fs->fields[0]->bytes, 3);
  61. data[2] = (data[2] & 0xF0) | (fs->fields[1]->uvalue & 0xF);
  62. for(uint32_t j = 0; j < 24; j++) {
  63. if(bitmap_get(data, sizeof(data), j)) {
  64. raw_samples_add(samples, true, te * 3);
  65. raw_samples_add(samples, false, te);
  66. } else {
  67. raw_samples_add(samples, true, te);
  68. raw_samples_add(samples, false, te * 3);
  69. }
  70. }
  71. // Signal terminator. Just a single short pulse.
  72. raw_samples_add(samples, true, te);
  73. }
  74. ProtoViewDecoder B4B1Decoder = {
  75. .name = "PT/SC remote",
  76. .decode = decode,
  77. .get_fields = get_fields,
  78. .build_message = build_message};