toshiba_ir.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "toshiba_ir.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "esp_log.h"
  5. #include "rxTimer.h"
  6. enum
  7. {
  8. UNKNOWN,
  9. T0,
  10. T1,
  11. T2,
  12. T3,
  13. TEMP1_OK,
  14. DONE
  15. };
  16. static uint8_t rx_state = UNKNOWN;
  17. static uint64_t sensor_data;
  18. static uint32_t rx_numBits;
  19. void Toshiba_ir_ResetDecoder()
  20. {
  21. sensor_data = 0;
  22. rx_numBits = 0;
  23. rx_state = UNKNOWN;
  24. }
  25. static void addBit(uint8_t value)
  26. {
  27. rx_numBits++;
  28. sensor_data = (sensor_data << 1) + (value & 0x01);
  29. rx_state = TEMP1_OK;
  30. }
  31. #define START_PULSE_MIN (3880-200)
  32. #define START_PULSE_MAX (4010+200)
  33. #define T0_PULSE_MIN (380-100)
  34. #define T0_PULSE_MAX (520+100)
  35. #define SHORT_PULSE_MIN (890-100)
  36. #define SHORT_PULSE_MAX (990+100)
  37. #define LONG_PULSE_MIN (1880-150)
  38. #define LONG_PULSE_MAX (1980+150)
  39. static int32_t rx_decode(uint32_t width)
  40. {
  41. switch (rx_state) {
  42. case UNKNOWN: // Start of frame
  43. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  44. {
  45. rx_state = T0;
  46. }
  47. else
  48. {
  49. return -1; // error, reset
  50. }
  51. break;
  52. case T0: // First half of pulse : HIGH around 230us
  53. if (rx_numBits == 32)
  54. { // end of frame
  55. rx_state = DONE;
  56. sensor_data = (sensor_data >> 8); // Mask away some bits at the end
  57. return 1;
  58. }
  59. else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
  60. {
  61. rx_state = T1;
  62. }
  63. else
  64. {
  65. if (rx_numBits == 0 && START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  66. {
  67. rx_state = T0;
  68. }
  69. else
  70. {
  71. return -1; // error, reset
  72. }
  73. }
  74. break;
  75. case T1:
  76. if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
  77. {
  78. addBit(0);
  79. }
  80. else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
  81. {
  82. addBit(1);
  83. }
  84. else
  85. {
  86. return -1; // error, reset
  87. }
  88. rx_state = T0;
  89. break;
  90. }
  91. return 0;
  92. }
  93. int64_t nextPulseToshiba_ir(uint32_t width)
  94. {
  95. static int64_t previous_data = 0;
  96. static uint32_t old_time=0;
  97. static uint32_t now;
  98. int64_t retVal = -1;
  99. if (width > 0)
  100. {
  101. if (rx_state != DONE)
  102. {
  103. switch (rx_decode(width))
  104. {
  105. case -1:
  106. Toshiba_ir_ResetDecoder();
  107. break;
  108. case 1:
  109. rx_state = DONE;
  110. break;
  111. }
  112. }
  113. }
  114. if (rx_state == DONE) {
  115. now = millis();
  116. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  117. previous_data = sensor_data;
  118. retVal = sensor_data;
  119. }
  120. old_time = now;
  121. Toshiba_ir_ResetDecoder();
  122. }
  123. return retVal;
  124. }