irda_common_decoder.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "irda_common_decoder_i.h"
  2. #include <stdbool.h>
  3. #include <furi.h>
  4. #include "irda_i.h"
  5. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  6. furi_assert(decoder);
  7. bool result = false;
  8. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  9. // align to start at Mark timing
  10. if (start_level) {
  11. if (decoder->timings_cnt > 0) {
  12. --decoder->timings_cnt;
  13. shift_left_array(decoder->timings, decoder->timings_cnt, 1);
  14. }
  15. }
  16. while ((!result) && (decoder->timings_cnt >= 2)) {
  17. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  18. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  19. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  20. if ((MATCH_PREAMBLE_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
  21. && (MATCH_PREAMBLE_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  22. result = true;
  23. }
  24. decoder->timings_cnt -= 2;
  25. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  26. }
  27. return result;
  28. }
  29. // Pulse Distance-Width Modulation
  30. DecodeStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
  31. furi_assert(decoder);
  32. uint32_t* timings = decoder->timings;
  33. uint16_t index = 0;
  34. uint8_t shift = 0;
  35. DecodeStatus status = DecodeStatusError;
  36. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  37. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  38. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  39. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  40. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  41. while (1) {
  42. // Stop bit
  43. if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
  44. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)) {
  45. decoder->timings_cnt = 0;
  46. status = DecodeStatusReady;
  47. } else {
  48. status = DecodeStatusError;
  49. }
  50. break;
  51. }
  52. if (decoder->timings_cnt >= 2) {
  53. index = decoder->databit_cnt / 8;
  54. shift = decoder->databit_cnt % 8; // LSB first
  55. if (!shift)
  56. decoder->data[index] = 0;
  57. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)
  58. && MATCH_BIT_TIMING(timings[1], bit1_space, bit_tolerance)) {
  59. decoder->data[index] |= (0x1 << shift); // add 1
  60. } else if (MATCH_BIT_TIMING(timings[0], bit0_mark, bit_tolerance)
  61. && MATCH_BIT_TIMING(timings[1], bit0_space, bit_tolerance)) {
  62. (void) decoder->data[index]; // add 0
  63. } else {
  64. status = DecodeStatusError;
  65. break;
  66. }
  67. ++decoder->databit_cnt;
  68. decoder->timings_cnt -= 2;
  69. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  70. } else {
  71. status = DecodeStatusOk;
  72. break;
  73. }
  74. }
  75. return status;
  76. }
  77. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  78. furi_assert(decoder);
  79. IrdaMessage* message = 0;
  80. DecodeStatus status = DecodeStatusError;
  81. if (decoder->level == level) {
  82. furi_assert(0);
  83. decoder->timings_cnt = 0;
  84. }
  85. decoder->level = level; // start with high level (Space timing)
  86. decoder->timings[decoder->timings_cnt] = duration;
  87. decoder->timings_cnt++;
  88. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  89. while(1) {
  90. switch (decoder->state) {
  91. case IrdaCommonStateWaitPreamble:
  92. if (irda_check_preamble(decoder)) {
  93. decoder->state = IrdaCommonStateDecode;
  94. decoder->databit_cnt = 0;
  95. }
  96. break;
  97. case IrdaCommonStateDecode:
  98. status = decoder->protocol->decode(decoder);
  99. if (status == DecodeStatusReady) {
  100. if (decoder->protocol->interpret(decoder)) {
  101. message = &decoder->message;
  102. decoder->state = IrdaCommonStateProcessRepeat;
  103. } else {
  104. decoder->state = IrdaCommonStateWaitPreamble;
  105. }
  106. } else if (status == DecodeStatusError) {
  107. decoder->state = IrdaCommonStateWaitPreamble;
  108. continue;
  109. }
  110. break;
  111. case IrdaCommonStateProcessRepeat:
  112. if (!decoder->protocol->decode_repeat) {
  113. decoder->state = IrdaCommonStateWaitPreamble;
  114. continue;
  115. }
  116. status = decoder->protocol->decode_repeat(decoder);
  117. if (status == DecodeStatusError) {
  118. decoder->state = IrdaCommonStateWaitPreamble;
  119. continue;
  120. } else if (status == DecodeStatusReady) {
  121. decoder->message.repeat = true;
  122. message = &decoder->message;
  123. }
  124. break;
  125. }
  126. break;
  127. }
  128. return message;
  129. }
  130. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  131. furi_assert(protocol);
  132. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  133. + protocol->databit_len / 8
  134. + !!(protocol->databit_len % 8);
  135. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  136. memset(decoder, 0, alloc_size);
  137. decoder->protocol = protocol;
  138. return decoder;
  139. }
  140. void irda_common_decoder_free(void* decoder) {
  141. furi_assert(decoder);
  142. free(decoder);
  143. }
  144. void irda_common_decoder_reset(void* decoder) {
  145. furi_assert(decoder);
  146. IrdaCommonDecoder* common_decoder = decoder;
  147. common_decoder->state = IrdaCommonStateWaitPreamble;
  148. common_decoder->timings_cnt = 0;
  149. common_decoder->databit_cnt = 0;
  150. }