irda_decoder_nec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder);
  7. static const IrdaCommonProtocolSpec protocol_nec = {
  8. {
  9. IRDA_NEC_PREAMBULE_MARK,
  10. IRDA_NEC_PREAMBULE_SPACE,
  11. IRDA_NEC_BIT1_MARK,
  12. IRDA_NEC_BIT1_SPACE,
  13. IRDA_NEC_BIT0_MARK,
  14. IRDA_NEC_BIT0_SPACE,
  15. IRDA_NEC_PREAMBLE_TOLERANCE,
  16. IRDA_NEC_BIT_TOLERANCE,
  17. },
  18. 32,
  19. irda_common_decode_pdwm,
  20. interpret_nec,
  21. decode_repeat_nec,
  22. };
  23. static bool interpret_nec(IrdaCommonDecoder* decoder) {
  24. furi_assert(decoder);
  25. bool result = false;
  26. uint16_t address = decoder->data[0] | (decoder->data[1] << 8);
  27. uint8_t command = decoder->data[2];
  28. uint8_t command_inverse = decoder->data[3];
  29. if((command == (uint8_t)~command_inverse)) {
  30. decoder->message.command = command;
  31. decoder->message.address = address;
  32. decoder->message.repeat = false;
  33. result = true;
  34. }
  35. return result;
  36. }
  37. // timings start from Space (delay between message and repeat)
  38. static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder) {
  39. furi_assert(decoder);
  40. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  41. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  42. DecodeStatus status = DecodeStatusError;
  43. if(decoder->timings_cnt < 4) return DecodeStatusOk;
  44. if((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN) &&
  45. (decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX) &&
  46. MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance) &&
  47. MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance) &&
  48. MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
  49. status = DecodeStatusReady;
  50. decoder->timings_cnt = 0;
  51. } else {
  52. status = DecodeStatusError;
  53. }
  54. return status;
  55. }
  56. void* irda_decoder_nec_alloc(void) {
  57. return irda_common_decoder_alloc(&protocol_nec);
  58. }
  59. IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
  60. return irda_common_decode(decoder, level, duration);
  61. }
  62. void irda_decoder_nec_free(void* decoder) {
  63. irda_common_decoder_free(decoder);
  64. }