infrared_common_decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "infrared_common_i.h"
  2. #include <stdlib.h>
  3. #include <core/check.h>
  4. #include <core/common_defines.h>
  5. static void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder);
  6. static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
  7. furi_assert(len >= shift);
  8. len -= shift;
  9. for(size_t i = 0; i < len; ++i) {
  10. array[i] = array[i + shift];
  11. }
  12. return len;
  13. }
  14. static inline void accumulate_lsb(InfraredCommonDecoder* decoder, bool bit) {
  15. uint16_t index = decoder->databit_cnt / 8;
  16. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  17. if(!shift) decoder->data[index] = 0;
  18. if(bit) {
  19. decoder->data[index] |= (0x1 << shift); // add 1
  20. } else {
  21. (void)decoder->data[index]; // add 0
  22. }
  23. ++decoder->databit_cnt;
  24. }
  25. static bool infrared_check_preamble(InfraredCommonDecoder* decoder) {
  26. furi_assert(decoder);
  27. bool result = false;
  28. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  29. if(decoder->timings_cnt == 0) return false;
  30. // align to start at Mark timing
  31. if(!start_level) {
  32. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  33. }
  34. if(decoder->protocol->timings.preamble_mark == 0) {
  35. return true;
  36. }
  37. while((!result) && (decoder->timings_cnt >= 2)) {
  38. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  39. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  40. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  41. if((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance)) &&
  42. (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  43. result = true;
  44. }
  45. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
  46. }
  47. return result;
  48. }
  49. /**
  50. * decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
  51. * decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
  52. * for some protocol modifications.
  53. */
  54. static InfraredStatus infrared_common_decode_bits(InfraredCommonDecoder* decoder) {
  55. furi_assert(decoder);
  56. InfraredStatus status = InfraredStatusOk;
  57. const InfraredTimings* timings = &decoder->protocol->timings;
  58. while(decoder->timings_cnt && (status == InfraredStatusOk)) {
  59. bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
  60. uint32_t timing = decoder->timings[0];
  61. if(timings->min_split_time && !level) {
  62. if(timing > timings->min_split_time) {
  63. /* long low timing - check if we're ready for any of protocol modification */
  64. for(size_t i = 0; i < COUNT_OF(decoder->protocol->databit_len) &&
  65. decoder->protocol->databit_len[i];
  66. ++i) {
  67. if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  68. return InfraredStatusReady;
  69. }
  70. }
  71. } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  72. /* short low timing for longest protocol - this is signal is longer than we expected */
  73. return InfraredStatusError;
  74. }
  75. }
  76. status = decoder->protocol->decode(decoder, level, timing);
  77. furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
  78. furi_assert(status == InfraredStatusError || status == InfraredStatusOk);
  79. if(status == InfraredStatusError) {
  80. break;
  81. }
  82. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  83. /* check if largest protocol version can be decoded */
  84. if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) && //-V1051
  85. !timings->min_split_time) {
  86. status = InfraredStatusReady;
  87. break;
  88. }
  89. }
  90. return status;
  91. }
  92. /* Pulse Distance-Width Modulation */
  93. InfraredStatus
  94. infrared_common_decode_pdwm(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
  95. furi_assert(decoder);
  96. InfraredStatus status = InfraredStatusOk;
  97. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  98. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  99. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  100. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  101. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  102. bool analyze_timing = level ^ (bit1_mark == bit0_mark);
  103. uint16_t bit1 = level ? bit1_mark : bit1_space;
  104. uint16_t bit0 = level ? bit0_mark : bit0_space;
  105. uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;
  106. if(analyze_timing) {
  107. if(MATCH_TIMING(timing, bit1, bit_tolerance)) {
  108. accumulate_lsb(decoder, 1);
  109. } else if(MATCH_TIMING(timing, bit0, bit_tolerance)) {
  110. accumulate_lsb(decoder, 0);
  111. } else {
  112. status = InfraredStatusError;
  113. }
  114. } else {
  115. if(!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
  116. status = InfraredStatusError;
  117. }
  118. }
  119. return status;
  120. }
  121. /* level switch detection goes in middle of time-quant */
  122. InfraredStatus
  123. infrared_common_decode_manchester(InfraredCommonDecoder* decoder, bool level, uint32_t timing) {
  124. furi_assert(decoder);
  125. uint32_t bit = decoder->protocol->timings.bit1_mark;
  126. uint32_t tolerance = decoder->protocol->timings.bit_tolerance;
  127. bool* switch_detect = &decoder->switch_detect;
  128. furi_assert((*switch_detect == true) || (*switch_detect == false));
  129. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  130. bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);
  131. if(!single_timing && !double_timing) {
  132. return InfraredStatusError;
  133. }
  134. if(decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
  135. *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
  136. accumulate_lsb(decoder, 0);
  137. }
  138. if(*switch_detect == 0) {
  139. if(double_timing) {
  140. return InfraredStatusError;
  141. }
  142. /* only single timing - level switch required in the middle of time-quant */
  143. *switch_detect = 1;
  144. } else {
  145. /* double timing means we're in the middle of time-quant again */
  146. if(single_timing) *switch_detect = 0;
  147. }
  148. if(*switch_detect) {
  149. if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  150. return InfraredStatusError;
  151. }
  152. accumulate_lsb(decoder, level);
  153. }
  154. return InfraredStatusOk;
  155. }
  156. InfraredMessage* infrared_common_decoder_check_ready(InfraredCommonDecoder* decoder) {
  157. InfraredMessage* message = NULL;
  158. bool found_length = false;
  159. for(size_t i = 0;
  160. i < COUNT_OF(decoder->protocol->databit_len) && decoder->protocol->databit_len[i];
  161. ++i) {
  162. if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  163. found_length = true;
  164. break;
  165. }
  166. }
  167. if(found_length && decoder->protocol->interpret(decoder)) {
  168. decoder->databit_cnt = 0;
  169. message = &decoder->message;
  170. if(decoder->protocol->decode_repeat) {
  171. decoder->state = InfraredCommonDecoderStateProcessRepeat;
  172. } else {
  173. decoder->state = InfraredCommonDecoderStateWaitPreamble;
  174. }
  175. }
  176. return message;
  177. }
  178. InfraredMessage*
  179. infrared_common_decode(InfraredCommonDecoder* decoder, bool level, uint32_t duration) {
  180. furi_assert(decoder);
  181. InfraredMessage* message = 0;
  182. InfraredStatus status = InfraredStatusError;
  183. if(decoder->level == level) {
  184. infrared_common_decoder_reset(decoder);
  185. }
  186. decoder->level = level; // start with low level (Space timing)
  187. decoder->timings[decoder->timings_cnt] = duration;
  188. decoder->timings_cnt++;
  189. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  190. while(1) {
  191. switch(decoder->state) {
  192. case InfraredCommonDecoderStateWaitPreamble:
  193. if(infrared_check_preamble(decoder)) {
  194. decoder->state = InfraredCommonDecoderStateDecode;
  195. decoder->databit_cnt = 0;
  196. decoder->switch_detect = false;
  197. continue;
  198. }
  199. break;
  200. case InfraredCommonDecoderStateDecode:
  201. status = infrared_common_decode_bits(decoder);
  202. if(status == InfraredStatusReady) {
  203. message = infrared_common_decoder_check_ready(decoder);
  204. if(message) {
  205. continue;
  206. } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  207. /* error: can't decode largest protocol - begin decoding from start */
  208. decoder->state = InfraredCommonDecoderStateWaitPreamble;
  209. }
  210. } else if(status == InfraredStatusError) {
  211. infrared_common_decoder_reset_state(decoder);
  212. continue;
  213. }
  214. break;
  215. case InfraredCommonDecoderStateProcessRepeat:
  216. status = decoder->protocol->decode_repeat(decoder);
  217. if(status == InfraredStatusError) {
  218. infrared_common_decoder_reset_state(decoder);
  219. continue;
  220. } else if(status == InfraredStatusReady) {
  221. decoder->message.repeat = true;
  222. message = &decoder->message;
  223. }
  224. break;
  225. }
  226. break;
  227. }
  228. return message;
  229. }
  230. void* infrared_common_decoder_alloc(const InfraredCommonProtocolSpec* protocol) {
  231. furi_assert(protocol);
  232. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  233. for(size_t i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  234. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  235. }
  236. uint32_t alloc_size = sizeof(InfraredCommonDecoder) + protocol->databit_len[0] / 8 +
  237. !!(protocol->databit_len[0] % 8);
  238. InfraredCommonDecoder* decoder = malloc(alloc_size);
  239. decoder->protocol = protocol;
  240. decoder->level = true;
  241. return decoder;
  242. }
  243. void infrared_common_decoder_free(InfraredCommonDecoder* decoder) {
  244. furi_assert(decoder);
  245. free(decoder);
  246. }
  247. void infrared_common_decoder_reset_state(InfraredCommonDecoder* decoder) {
  248. decoder->state = InfraredCommonDecoderStateWaitPreamble;
  249. decoder->databit_cnt = 0;
  250. decoder->switch_detect = false;
  251. decoder->message.protocol = InfraredProtocolUnknown;
  252. if(decoder->protocol->timings.preamble_mark == 0) {
  253. if(decoder->timings_cnt > 0) {
  254. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  255. }
  256. }
  257. }
  258. void infrared_common_decoder_reset(InfraredCommonDecoder* decoder) {
  259. furi_assert(decoder);
  260. infrared_common_decoder_reset_state(decoder);
  261. decoder->timings_cnt = 0;
  262. }