irda_common_decoder.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. decoder->timings_cnt = 0;
  83. }
  84. decoder->level = level; // start with high level (Space timing)
  85. decoder->timings[decoder->timings_cnt] = duration;
  86. decoder->timings_cnt++;
  87. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  88. while(1) {
  89. switch (decoder->state) {
  90. case IrdaCommonStateWaitPreamble:
  91. if (irda_check_preamble(decoder)) {
  92. decoder->state = IrdaCommonStateDecode;
  93. decoder->databit_cnt = 0;
  94. }
  95. break;
  96. case IrdaCommonStateDecode:
  97. status = decoder->protocol->decode(decoder);
  98. if (status == DecodeStatusReady) {
  99. if (decoder->protocol->interpret(decoder)) {
  100. message = &decoder->message;
  101. decoder->state = IrdaCommonStateProcessRepeat;
  102. } else {
  103. decoder->state = IrdaCommonStateWaitPreamble;
  104. }
  105. } else if (status == DecodeStatusError) {
  106. decoder->state = IrdaCommonStateWaitPreamble;
  107. continue;
  108. }
  109. break;
  110. case IrdaCommonStateProcessRepeat:
  111. if (!decoder->protocol->decode_repeat) {
  112. decoder->state = IrdaCommonStateWaitPreamble;
  113. continue;
  114. }
  115. status = decoder->protocol->decode_repeat(decoder);
  116. if (status == DecodeStatusError) {
  117. decoder->state = IrdaCommonStateWaitPreamble;
  118. continue;
  119. } else if (status == DecodeStatusReady) {
  120. decoder->message.repeat = true;
  121. message = &decoder->message;
  122. }
  123. break;
  124. }
  125. break;
  126. }
  127. return message;
  128. }
  129. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  130. furi_assert(protocol);
  131. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  132. + protocol->databit_len / 8
  133. + !!(protocol->databit_len % 8);
  134. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  135. memset(decoder, 0, alloc_size);
  136. decoder->protocol = protocol;
  137. return decoder;
  138. }
  139. void irda_common_decoder_free(void* decoder) {
  140. furi_assert(decoder);
  141. free(decoder);
  142. }
  143. void irda_common_decoder_reset(void* decoder) {
  144. furi_assert(decoder);
  145. IrdaCommonDecoder* common_decoder = decoder;
  146. common_decoder->state = IrdaCommonStateWaitPreamble;
  147. common_decoder->timings_cnt = 0;
  148. common_decoder->databit_cnt = 0;
  149. }