irda_decoder_nec.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. uint8_t address = decoder->data[0];
  27. uint8_t address_inverse = decoder->data[1];
  28. uint8_t command = decoder->data[2];
  29. uint8_t command_inverse = decoder->data[3];
  30. if ((command == (uint8_t) ~command_inverse) && (address == (uint8_t) ~address_inverse)) {
  31. decoder->message.command = command;
  32. decoder->message.address = address;
  33. decoder->message.repeat = false;
  34. result = true;
  35. }
  36. return result;
  37. }
  38. // timings start from Space (delay between message and repeat)
  39. static DecodeStatus decode_repeat_nec(IrdaCommonDecoder* decoder) {
  40. furi_assert(decoder);
  41. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  42. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  43. DecodeStatus status = DecodeStatusError;
  44. if (decoder->timings_cnt < 4)
  45. return DecodeStatusOk;
  46. if ((decoder->timings[0] > IRDA_NEC_REPEAT_PAUSE_MIN)
  47. && (decoder->timings[0] < IRDA_NEC_REPEAT_PAUSE_MAX)
  48. && MATCH_PREAMBLE_TIMING(decoder->timings[1], IRDA_NEC_REPEAT_MARK, preamble_tolerance)
  49. && MATCH_PREAMBLE_TIMING(decoder->timings[2], IRDA_NEC_REPEAT_SPACE, preamble_tolerance)
  50. && MATCH_BIT_TIMING(decoder->timings[3], decoder->protocol->timings.bit1_mark, bit_tolerance)) {
  51. status = DecodeStatusReady;
  52. decoder->timings_cnt = 0;
  53. } else {
  54. status = DecodeStatusError;
  55. }
  56. return status;
  57. }
  58. void* irda_decoder_nec_alloc(void) {
  59. return irda_common_decoder_alloc(&protocol_nec);
  60. }
  61. IrdaMessage* irda_decoder_nec_decode(void* decoder, bool level, uint32_t duration) {
  62. return irda_common_decode(decoder, level, duration);
  63. }
  64. void irda_decoder_nec_free(void* decoder) {
  65. irda_common_decoder_free(decoder);
  66. }