infrared_common_decoder.c 11 KB

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