irda_common_decoder.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "furi/check.h"
  2. #include "irda.h"
  3. #include "irda_common_i.h"
  4. #include <stdbool.h>
  5. #include <furi.h>
  6. #include "irda_i.h"
  7. static void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder);
  8. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  9. furi_assert(decoder);
  10. bool result = false;
  11. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  12. // align to start at Mark timing
  13. if (!start_level) {
  14. if (decoder->timings_cnt > 0) {
  15. --decoder->timings_cnt;
  16. shift_left_array(decoder->timings, decoder->timings_cnt, 1);
  17. }
  18. }
  19. while ((!result) && (decoder->timings_cnt >= 2)) {
  20. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  21. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  22. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  23. if ((MATCH_PREAMBLE_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
  24. && (MATCH_PREAMBLE_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  25. result = true;
  26. }
  27. decoder->timings_cnt -= 2;
  28. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  29. }
  30. return result;
  31. }
  32. /* Pulse Distance-Width Modulation */
  33. IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
  34. furi_assert(decoder);
  35. uint32_t* timings = decoder->timings;
  36. uint16_t index = 0;
  37. uint8_t shift = 0;
  38. IrdaStatus status = IrdaStatusError;
  39. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  40. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  41. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  42. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  43. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  44. while (1) {
  45. // Stop bit
  46. if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
  47. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)) {
  48. decoder->timings_cnt = 0;
  49. status = IrdaStatusReady;
  50. } else {
  51. status = IrdaStatusError;
  52. }
  53. break;
  54. }
  55. if (decoder->timings_cnt >= 2) {
  56. index = decoder->databit_cnt / 8;
  57. shift = decoder->databit_cnt % 8; // LSB first
  58. if (!shift)
  59. decoder->data[index] = 0;
  60. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)
  61. && MATCH_BIT_TIMING(timings[1], bit1_space, bit_tolerance)) {
  62. decoder->data[index] |= (0x1 << shift); // add 1
  63. } else if (MATCH_BIT_TIMING(timings[0], bit0_mark, bit_tolerance)
  64. && MATCH_BIT_TIMING(timings[1], bit0_space, bit_tolerance)) {
  65. (void) decoder->data[index]; // add 0
  66. } else {
  67. status = IrdaStatusError;
  68. break;
  69. }
  70. ++decoder->databit_cnt;
  71. decoder->timings_cnt -= 2;
  72. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  73. } else {
  74. status = IrdaStatusOk;
  75. break;
  76. }
  77. }
  78. return status;
  79. }
  80. /* level switch detection goes in middle of time-quant */
  81. IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
  82. furi_assert(decoder);
  83. IrdaStatus status = IrdaStatusError;
  84. uint16_t bit = decoder->protocol->timings.bit1_mark;
  85. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  86. while (decoder->timings_cnt) {
  87. uint32_t timing = decoder->timings[0];
  88. bool* switch_detect = &decoder->switch_detect;
  89. furi_assert((*switch_detect == true) || (*switch_detect == false));
  90. bool single_timing = MATCH_BIT_TIMING(timing, bit, tolerance);
  91. bool double_timing = MATCH_BIT_TIMING(timing, 2*bit, tolerance);
  92. if((!single_timing && !double_timing) || (double_timing && !*switch_detect)) {
  93. status = IrdaStatusError;
  94. break;
  95. }
  96. if (*switch_detect == 0) {
  97. /* only single timing - level switch required in the middle of time-quant */
  98. *switch_detect = 1;
  99. } else {
  100. /* double timing means we in the middle of time-quant again */
  101. if (single_timing)
  102. *switch_detect = 0;
  103. }
  104. --decoder->timings_cnt;
  105. shift_left_array(decoder->timings, decoder->timings_cnt, 1);
  106. status = IrdaStatusOk;
  107. if (decoder->databit_cnt < decoder->protocol->databit_len) {
  108. if (*switch_detect) {
  109. uint8_t index = decoder->databit_cnt / 8;
  110. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  111. if (!shift)
  112. decoder->data[index] = 0;
  113. bool inverse_level = decoder->protocol->manchester_inverse_level;
  114. uint8_t logic_value = inverse_level ? !decoder->level : decoder->level;
  115. decoder->data[index] |= (logic_value << shift);
  116. ++decoder->databit_cnt;
  117. }
  118. if (decoder->databit_cnt == decoder->protocol->databit_len) {
  119. if (decoder->level) {
  120. status = IrdaStatusReady;
  121. break;
  122. }
  123. }
  124. } else {
  125. furi_assert(decoder->level);
  126. /* cover case: sequence should be stopped after last bit was received */
  127. if (single_timing) {
  128. status = IrdaStatusReady;
  129. break;
  130. } else {
  131. status = IrdaStatusError;
  132. }
  133. }
  134. }
  135. return status;
  136. }
  137. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  138. furi_assert(decoder);
  139. IrdaMessage* message = 0;
  140. IrdaStatus status = IrdaStatusError;
  141. if (decoder->level == level) {
  142. decoder->timings_cnt = 0;
  143. }
  144. decoder->level = level; // start with low level (Space timing)
  145. decoder->timings[decoder->timings_cnt] = duration;
  146. decoder->timings_cnt++;
  147. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  148. while(1) {
  149. switch (decoder->state) {
  150. case IrdaCommonDecoderStateWaitPreamble:
  151. if (irda_check_preamble(decoder)) {
  152. decoder->state = IrdaCommonDecoderStateDecode;
  153. decoder->databit_cnt = 0;
  154. decoder->switch_detect = false;
  155. }
  156. break;
  157. case IrdaCommonDecoderStateDecode:
  158. status = decoder->protocol->decode(decoder);
  159. if (status == IrdaStatusReady) {
  160. if (decoder->protocol->interpret(decoder)) {
  161. message = &decoder->message;
  162. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  163. } else {
  164. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  165. }
  166. } else if (status == IrdaStatusError) {
  167. irda_common_decoder_reset_state(decoder);
  168. continue;
  169. }
  170. break;
  171. case IrdaCommonDecoderStateProcessRepeat:
  172. if (!decoder->protocol->decode_repeat) {
  173. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  174. continue;
  175. }
  176. status = decoder->protocol->decode_repeat(decoder);
  177. if (status == IrdaStatusError) {
  178. irda_common_decoder_reset_state(decoder);
  179. continue;
  180. } else if (status == IrdaStatusReady) {
  181. decoder->message.repeat = true;
  182. message = &decoder->message;
  183. }
  184. break;
  185. }
  186. break;
  187. }
  188. return message;
  189. }
  190. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  191. furi_assert(protocol);
  192. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  193. + protocol->databit_len / 8
  194. + !!(protocol->databit_len % 8);
  195. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  196. memset(decoder, 0, alloc_size);
  197. decoder->protocol = protocol;
  198. decoder->level = true;
  199. return decoder;
  200. }
  201. void irda_common_decoder_set_context(void* decoder, void* context) {
  202. IrdaCommonDecoder* common_decoder = decoder;
  203. common_decoder->context = context;
  204. }
  205. void irda_common_decoder_free(void* decoder) {
  206. furi_assert(decoder);
  207. free(decoder);
  208. }
  209. void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder) {
  210. common_decoder->state = IrdaCommonDecoderStateWaitPreamble;
  211. common_decoder->databit_cnt = 0;
  212. common_decoder->switch_detect = false;
  213. common_decoder->message.protocol = IrdaProtocolUnknown;
  214. }
  215. void irda_common_decoder_reset(void* decoder) {
  216. furi_assert(decoder);
  217. IrdaCommonDecoder* common_decoder = decoder;
  218. irda_common_decoder_reset_state(common_decoder);
  219. common_decoder->timings_cnt = 0;
  220. }