irda-decoder-nec.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "irda-decoder-nec.h"
  2. #include "string.h"
  3. const uint32_t PREAMBULA_HIGH_MIN = 9000 - 900;
  4. const uint32_t PREAMBULA_HIGH_MAX = 9000 + 900;
  5. const uint32_t PREAMBULA_LOW_MIN = 4500 - 450;
  6. const uint32_t PREAMBULA_LOW_MAX = 4500 + 450;
  7. const uint32_t PREAMBULA_RETRY_LOW_MIN = 2500 - 350;
  8. const uint32_t PREAMBULA_RETRY_LOW_MAX = 2500 + 250;
  9. const uint32_t BIT_HIGH_MIN = 560 - 100;
  10. const uint32_t BIT_HIGH_MAX = 560 + 100;
  11. const uint32_t BIT_LOW_ONE_MIN = 1690 - 200;
  12. const uint32_t BIT_LOW_ONE_MAX = 1690 + 200;
  13. const uint32_t BIT_LOW_ZERO_MIN = 560 - 100;
  14. const uint32_t BIT_LOW_ZERO_MAX = 560 + 100;
  15. #define SET_STATE(_state) \
  16. { decoder->state = _state; }
  17. #define TIME_FIT(_prefix) ((time > _prefix##_MIN) && (time < _prefix##_MAX))
  18. #ifndef MIN
  19. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  20. #endif
  21. bool save_decoder_nec_data(IrDANecDecoder* decoder, IrDADecoderOutputData* out) {
  22. bool result = false;
  23. if((decoder->data.simple.cmd + decoder->data.simple.cmd_inverse) == 0xFF) {
  24. if(out->data_length < sizeof(IrDANecDataType)) {
  25. out->flags |= IRDA_TOO_SHORT_BUFFER;
  26. }
  27. memcpy(out->data, &decoder->data.data, MIN(sizeof(IrDANecDataType), out->data_length));
  28. result = true;
  29. } else {
  30. reset_decoder_nec(decoder);
  31. }
  32. return result;
  33. }
  34. bool process_decoder_nec(
  35. IrDANecDecoder* decoder,
  36. bool polarity,
  37. uint32_t time,
  38. IrDADecoderOutputData* out) {
  39. bool error = true;
  40. bool result = false;
  41. switch(decoder->state) {
  42. case(WAIT_PREAMBULA_HIGH):
  43. if(polarity) {
  44. if(TIME_FIT(PREAMBULA_HIGH)) {
  45. SET_STATE(WAIT_PREAMBULA_LOW);
  46. }
  47. }
  48. // any values before preambula start is correct
  49. error = false;
  50. break;
  51. case(WAIT_PREAMBULA_LOW):
  52. if(!polarity) {
  53. if(TIME_FIT(PREAMBULA_LOW)) {
  54. // new data, reset storage
  55. reset_decoder_nec(decoder);
  56. SET_STATE(WAIT_BIT_HIGH);
  57. error = false;
  58. } else if(TIME_FIT(PREAMBULA_RETRY_LOW)) {
  59. // wait for data repeat command
  60. SET_STATE(WAIT_RETRY_HIGH);
  61. error = false;
  62. }
  63. }
  64. break;
  65. case(WAIT_RETRY_HIGH):
  66. if(polarity) {
  67. if(TIME_FIT(BIT_HIGH)) {
  68. SET_STATE(WAIT_PREAMBULA_HIGH);
  69. // repeat event
  70. result = save_decoder_nec_data(decoder, out);
  71. out->flags |= IRDA_REPEAT;
  72. error = false;
  73. }
  74. }
  75. break;
  76. case(WAIT_BIT_HIGH):
  77. if(polarity) {
  78. if(TIME_FIT(BIT_HIGH)) {
  79. SET_STATE(WAIT_BIT_LOW);
  80. error = false;
  81. }
  82. }
  83. break;
  84. case(WAIT_BIT_STOP_HIGH):
  85. if(polarity) {
  86. if(TIME_FIT(BIT_HIGH)) {
  87. SET_STATE(WAIT_PREAMBULA_HIGH);
  88. // message end event
  89. result = save_decoder_nec_data(decoder, out);
  90. error = false;
  91. }
  92. }
  93. break;
  94. case(WAIT_BIT_LOW):
  95. if(!polarity) {
  96. int8_t bit = -1;
  97. if(TIME_FIT(BIT_LOW_ZERO)) {
  98. SET_STATE(WAIT_BIT_HIGH);
  99. bit = 0;
  100. error = false;
  101. } else if(TIME_FIT(BIT_LOW_ONE)) {
  102. SET_STATE(WAIT_BIT_HIGH);
  103. bit = 1;
  104. error = false;
  105. }
  106. if(bit != -1) {
  107. decoder->data.data |= (bit << decoder->current_data_index);
  108. decoder->current_data_index++;
  109. if(decoder->current_data_index > 31) {
  110. decoder->current_data_index = 0;
  111. SET_STATE(WAIT_BIT_STOP_HIGH);
  112. }
  113. }
  114. }
  115. break;
  116. }
  117. if(error) reset_decoder_nec(decoder);
  118. return result;
  119. }
  120. void reset_decoder_nec(IrDANecDecoder* decoder) {
  121. decoder->state = WAIT_PREAMBULA_HIGH;
  122. decoder->data.data = 0;
  123. decoder->current_data_index = 0;
  124. }