irda_decoder_nec.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <furi.h>
  4. #include "../irda_i.h"
  5. static bool interpret_nec(IrdaCommonDecoder* decoder);
  6. static bool interpret_necext(IrdaCommonDecoder* decoder);
  7. static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder);
  8. static const IrdaCommonProtocolSpec protocol_nec = {
  9. {
  10. IRDA_NEC_PREAMBULE_MARK,
  11. IRDA_NEC_PREAMBULE_SPACE,
  12. IRDA_NEC_BIT1_MARK,
  13. IRDA_NEC_BIT1_SPACE,
  14. IRDA_NEC_BIT0_MARK,
  15. IRDA_NEC_BIT0_SPACE,
  16. IRDA_NEC_PREAMBLE_TOLERANCE,
  17. IRDA_NEC_BIT_TOLERANCE,
  18. },
  19. 32,
  20. irda_common_decode_pdwm,
  21. interpret_nec,
  22. decode_repeat_nec,
  23. };
  24. static const IrdaCommonProtocolSpec protocol_necext = {
  25. {
  26. IRDA_NEC_PREAMBULE_MARK,
  27. IRDA_NEC_PREAMBULE_SPACE,
  28. IRDA_NEC_BIT1_MARK,
  29. IRDA_NEC_BIT1_SPACE,
  30. IRDA_NEC_BIT0_MARK,
  31. IRDA_NEC_BIT0_SPACE,
  32. IRDA_NEC_PREAMBLE_TOLERANCE,
  33. IRDA_NEC_BIT_TOLERANCE,
  34. },
  35. 32,
  36. irda_common_decode_pdwm,
  37. interpret_necext,
  38. decode_repeat_nec,
  39. };
  40. static bool interpret_nec(IrdaCommonDecoder* decoder) {
  41. furi_assert(decoder);
  42. bool result = false;
  43. uint8_t address = decoder->data[0];
  44. uint8_t address_inverse = decoder->data[1];
  45. uint8_t command = decoder->data[2];
  46. uint8_t command_inverse = decoder->data[3];
  47. if ((command == (uint8_t) ~command_inverse) && (address == (uint8_t) ~address_inverse)) {
  48. decoder->message.command = command;
  49. decoder->message.address = address;
  50. decoder->message.repeat = false;
  51. result = true;
  52. }
  53. return result;
  54. }
  55. // Some NEC's extensions allow 16 bit address
  56. static bool interpret_necext(IrdaCommonDecoder* decoder) {
  57. furi_assert(decoder);
  58. bool result = false;
  59. uint8_t command = decoder->data[2];
  60. uint8_t command_inverse = decoder->data[3];
  61. if(command == (uint8_t)~command_inverse) {
  62. decoder->message.command = command;
  63. decoder->message.address = decoder->data[0] | (decoder->data[1] << 8);
  64. decoder->message.repeat = false;
  65. result = true;
  66. }
  67. return result;
  68. }
  69. // timings start from Space (delay between message and repeat)
  70. static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder) {
  71. furi_assert(decoder);
  72. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  73. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  74. DecodeStatus status = DecodeStatusError;
  75. if(decoder->timings_cnt < 4) return DecodeStatusOk;
  76. if((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN) &&
  77. (decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX) &&
  78. MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance) &&
  79. MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance) &&
  80. MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
  81. status = DecodeStatusReady;
  82. decoder->timings_cnt = 0;
  83. } else {
  84. status = DecodeStatusError;
  85. }
  86. return status;
  87. }
  88. void* irda_decoder_nec_alloc(void) {
  89. return irda_common_decoder_alloc(&protocol_nec);
  90. }
  91. void* irda_decoder_necext_alloc(void) {
  92. return irda_common_decoder_alloc(&protocol_necext);
  93. }
  94. IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
  95. return irda_common_decode(decoder, level, duration);
  96. }
  97. void irda_decoder_nec_free(void* decoder) {
  98. irda_common_decoder_free(decoder);
  99. }
  100. void irda_decoder_nec_reset(void* decoder) {
  101. irda_common_decoder_reset(decoder);
  102. }