irda_common_decoder.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include <stdint.h>
  8. static void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder);
  9. static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
  10. furi_assert(len >= shift);
  11. len -= shift;
  12. for (int i = 0; i < len; ++i)
  13. array[i] = array[i + shift];
  14. return len;
  15. }
  16. static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
  17. uint16_t index = decoder->databit_cnt / 8;
  18. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  19. if (!shift)
  20. decoder->data[index] = 0;
  21. if (bit) {
  22. decoder->data[index] |= (0x1 << shift); // add 1
  23. } else {
  24. (void) decoder->data[index]; // add 0
  25. }
  26. ++decoder->databit_cnt;
  27. }
  28. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  29. furi_assert(decoder);
  30. bool result = false;
  31. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  32. // align to start at Mark timing
  33. if (!start_level) {
  34. if (decoder->timings_cnt > 0) {
  35. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  36. }
  37. }
  38. if (decoder->protocol->timings.preamble_mark == 0) {
  39. return true;
  40. }
  41. while ((!result) && (decoder->timings_cnt >= 2)) {
  42. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  43. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  44. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  45. if ((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
  46. && (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  47. result = true;
  48. }
  49. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
  50. }
  51. return result;
  52. }
  53. /* Pulse Distance Modulation */
  54. IrdaStatus irda_common_decode_pdm(IrdaCommonDecoder* decoder) {
  55. furi_assert(decoder);
  56. uint32_t* timings = decoder->timings;
  57. IrdaStatus status = IrdaStatusError;
  58. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  59. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  60. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  61. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  62. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  63. while (1) {
  64. // Stop bit
  65. if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
  66. if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)) {
  67. decoder->timings_cnt = 0;
  68. status = IrdaStatusReady;
  69. } else {
  70. status = IrdaStatusError;
  71. }
  72. break;
  73. }
  74. if (decoder->timings_cnt >= 2) {
  75. if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)
  76. && MATCH_TIMING(timings[1], bit1_space, bit_tolerance)) {
  77. accumulate_lsb(decoder, 1);
  78. } else if (MATCH_TIMING(timings[0], bit0_mark, bit_tolerance)
  79. && MATCH_TIMING(timings[1], bit0_space, bit_tolerance)) {
  80. accumulate_lsb(decoder, 0);
  81. } else {
  82. status = IrdaStatusError;
  83. break;
  84. }
  85. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
  86. } else {
  87. status = IrdaStatusOk;
  88. break;
  89. }
  90. }
  91. return status;
  92. }
  93. /* level switch detection goes in middle of time-quant */
  94. IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
  95. furi_assert(decoder);
  96. IrdaStatus status = IrdaStatusOk;
  97. uint16_t bit = decoder->protocol->timings.bit1_mark;
  98. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  99. while (decoder->timings_cnt) {
  100. uint32_t timing = decoder->timings[0];
  101. bool* switch_detect = &decoder->switch_detect;
  102. furi_assert((*switch_detect == true) || (*switch_detect == false));
  103. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  104. bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
  105. if(!single_timing && !double_timing) {
  106. status = IrdaStatusError;
  107. break;
  108. }
  109. if ((decoder->protocol->manchester_start_from_space) && (decoder->databit_cnt == 0)) {
  110. *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
  111. decoder->data[0] = 0; /* first captured timing should be Mark */
  112. ++decoder->databit_cnt;
  113. }
  114. if (*switch_detect == 0) {
  115. if (double_timing) {
  116. status = IrdaStatusError;
  117. break;
  118. }
  119. /* only single timing - level switch required in the middle of time-quant */
  120. *switch_detect = 1;
  121. } else {
  122. /* double timing means we in the middle of time-quant again */
  123. if (single_timing)
  124. *switch_detect = 0;
  125. }
  126. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  127. status = IrdaStatusOk;
  128. bool level = (decoder->level + decoder->timings_cnt) % 2;
  129. if (decoder->databit_cnt < decoder->protocol->databit_len) {
  130. if (*switch_detect) {
  131. accumulate_lsb(decoder, level);
  132. }
  133. if (decoder->databit_cnt == decoder->protocol->databit_len) {
  134. if (level) {
  135. status = IrdaStatusReady;
  136. break;
  137. }
  138. }
  139. } else {
  140. furi_assert(level);
  141. /* cover case: sequence should be stopped after last bit was received */
  142. if (single_timing) {
  143. status = IrdaStatusReady;
  144. break;
  145. } else {
  146. status = IrdaStatusError;
  147. }
  148. }
  149. }
  150. return status;
  151. }
  152. /* Pulse Width Modulation */
  153. IrdaStatus irda_common_decode_pwm(IrdaCommonDecoder* decoder) {
  154. furi_assert(decoder);
  155. uint32_t* timings = decoder->timings;
  156. IrdaStatus status = IrdaStatusOk;
  157. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  158. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  159. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  160. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  161. while (decoder->timings_cnt) {
  162. bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
  163. if (level) {
  164. if (MATCH_TIMING(timings[0], bit1_mark, bit_tolerance)) {
  165. accumulate_lsb(decoder, 1);
  166. } else if (MATCH_TIMING(timings[0], bit0_mark, bit_tolerance)) {
  167. accumulate_lsb(decoder, 0);
  168. } else {
  169. status = IrdaStatusError;
  170. break;
  171. }
  172. } else {
  173. if (!MATCH_TIMING(timings[0], bit1_space, bit_tolerance)) {
  174. status = IrdaStatusError;
  175. break;
  176. }
  177. }
  178. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  179. if (decoder->databit_cnt == decoder->protocol->databit_len) {
  180. status = IrdaStatusReady;
  181. break;
  182. }
  183. }
  184. return status;
  185. }
  186. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  187. furi_assert(decoder);
  188. IrdaMessage* message = 0;
  189. IrdaStatus status = IrdaStatusError;
  190. if (decoder->level == level) {
  191. irda_common_decoder_reset(decoder);
  192. }
  193. decoder->level = level; // start with low level (Space timing)
  194. decoder->timings[decoder->timings_cnt] = duration;
  195. decoder->timings_cnt++;
  196. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  197. while(1) {
  198. switch (decoder->state) {
  199. case IrdaCommonDecoderStateWaitPreamble:
  200. if (irda_check_preamble(decoder)) {
  201. decoder->state = IrdaCommonDecoderStateDecode;
  202. decoder->databit_cnt = 0;
  203. decoder->switch_detect = false;
  204. continue;
  205. }
  206. break;
  207. case IrdaCommonDecoderStateDecode:
  208. status = decoder->protocol->decode(decoder);
  209. if (status == IrdaStatusReady) {
  210. if (decoder->protocol->interpret(decoder)) {
  211. message = &decoder->message;
  212. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  213. } else {
  214. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  215. }
  216. } else if (status == IrdaStatusError) {
  217. irda_common_decoder_reset_state(decoder);
  218. continue;
  219. }
  220. break;
  221. case IrdaCommonDecoderStateProcessRepeat:
  222. if (!decoder->protocol->decode_repeat) {
  223. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  224. continue;
  225. }
  226. status = decoder->protocol->decode_repeat(decoder);
  227. if (status == IrdaStatusError) {
  228. irda_common_decoder_reset_state(decoder);
  229. continue;
  230. } else if (status == IrdaStatusReady) {
  231. decoder->message.repeat = true;
  232. message = &decoder->message;
  233. }
  234. break;
  235. }
  236. break;
  237. }
  238. return message;
  239. }
  240. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  241. furi_assert(protocol);
  242. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  243. + protocol->databit_len / 8
  244. + !!(protocol->databit_len % 8);
  245. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  246. memset(decoder, 0, alloc_size);
  247. decoder->protocol = protocol;
  248. decoder->level = true;
  249. return decoder;
  250. }
  251. void irda_common_decoder_free(IrdaCommonDecoder* decoder) {
  252. furi_assert(decoder);
  253. free(decoder);
  254. }
  255. void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
  256. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  257. decoder->databit_cnt = 0;
  258. decoder->switch_detect = false;
  259. decoder->message.protocol = IrdaProtocolUnknown;
  260. if (decoder->protocol->timings.preamble_mark == 0) {
  261. if (decoder->timings_cnt > 0) {
  262. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  263. }
  264. }
  265. }
  266. void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
  267. furi_assert(decoder);
  268. irda_common_decoder_reset_state(decoder);
  269. decoder->timings_cnt = 0;
  270. }