toshiba_ir.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "toshiba_ir.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "esp_log.h"
  5. #include "rxTimer.h"
  6. #include <string.h>
  7. // Toshiba A/C
  8. const uint16_t kToshibaAcHdrMark = 4400;
  9. const uint16_t kToshibaAcHdrSpace = 4480;
  10. const uint16_t kToshibaAcBitMark = 530;
  11. const uint16_t kToshibaAcOneSpace = 1600;
  12. const uint16_t kToshibaAcZeroSpace = 530;
  13. // Some models have a different inter-message gap.
  14. // See: https://github.com/crankyoldgit/IRremoteESP8266/issues/1420
  15. const uint16_t kToshibaAcMinGap = 4600; // WH-UB03NJ remote
  16. const uint16_t kToshibaAcUsualGap = 7400; // Others
  17. #define kToshibaNumberOfBits 72
  18. #define kToshibaNumberOfBytes (kToshibaNumberOfBits/8)
  19. uint8_t data[kToshibaNumberOfBytes];
  20. enum
  21. {
  22. UNKNOWN,
  23. STARTER,
  24. T0,
  25. T1,
  26. DONE
  27. };
  28. static uint8_t rx_state = UNKNOWN;
  29. static uint32_t rx_numBits;
  30. void Toshiba_ir_ResetDecoder()
  31. {
  32. ESP_LOGI("T", "Reset decoder");
  33. rx_numBits = kToshibaNumberOfBits;
  34. rx_state = UNKNOWN;
  35. memset(data,0,kToshibaNumberOfBytes);
  36. }
  37. static void addBit(uint8_t value)
  38. {
  39. if( value == 1 ) {
  40. const uint8_t byteNo = (rx_numBits+7) / 8;
  41. const uint8_t shiftBits = (int)rx_numBits - (byteNo*8) + 7;
  42. //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits);
  43. data[byteNo-1] |= ((uint8_t)1u) << shiftBits;
  44. }
  45. rx_numBits--;
  46. }
  47. #define START_PULSE_MIN (kToshibaAcHdrMark-200)
  48. #define START_PULSE_MAX (kToshibaAcHdrMark+200)
  49. #define T0_PULSE_MIN (kToshibaAcBitMark-100)
  50. #define T0_PULSE_MAX (kToshibaAcBitMark+100)
  51. #define SHORT_PULSE_MIN (kToshibaAcZeroSpace-100)
  52. #define SHORT_PULSE_MAX (kToshibaAcZeroSpace+100)
  53. #define LONG_PULSE_MIN (kToshibaAcOneSpace-100)
  54. #define LONG_PULSE_MAX (kToshibaAcOneSpace+100)
  55. static int32_t rx_decode(uint32_t width)
  56. {
  57. switch (rx_state) {
  58. case UNKNOWN: // Start of frame A
  59. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  60. {
  61. rx_state = STARTER;
  62. //ESP_LOGI("T", "->STARTER");
  63. }
  64. else
  65. {
  66. return -1; // error, reset
  67. }
  68. break;
  69. case STARTER: // Start of frame B
  70. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  71. {
  72. rx_state = T0;
  73. //ESP_LOGI("T", "STARTER");
  74. }
  75. else
  76. {
  77. return -1; // error, reset
  78. }
  79. break;
  80. case T0: // First half of pulse : HIGH around 230us
  81. if(rx_numBits == 0)
  82. { // end of frame
  83. ESP_LOGI("T", "END OF FRAME");
  84. rx_state = DONE;
  85. for(uint8_t i=9; i>0 ;i--) {
  86. ESP_LOGI("DATA","Byte %u : %02x", i-1, data[i-1]);
  87. }
  88. return 1;
  89. }
  90. else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
  91. {
  92. rx_state = T1;
  93. //ESP_LOGI("T", "T0");
  94. }
  95. else
  96. {
  97. return -1; // error, reset
  98. }
  99. break;
  100. case T1:
  101. if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
  102. {
  103. addBit(0);
  104. //ESP_LOGI("T", "Short %u",rx_numBits);
  105. }
  106. else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
  107. {
  108. addBit(1);
  109. //ESP_LOGI("T", "Long %u", rx_numBits);
  110. }
  111. else
  112. {
  113. return -1; // error, reset
  114. }
  115. rx_state = T0;
  116. break;
  117. }
  118. return 0;
  119. }
  120. int64_t nextPulseToshiba_ir(uint32_t width)
  121. {
  122. int64_t retVal = -1;
  123. if (width > 0)
  124. {
  125. if (rx_state != DONE)
  126. {
  127. switch (rx_decode(width))
  128. {
  129. case -1:
  130. Toshiba_ir_ResetDecoder();
  131. break;
  132. case 1:
  133. rx_state = DONE;
  134. break;
  135. }
  136. }
  137. }
  138. if (rx_state == DONE) {
  139. Toshiba_ir_ResetDecoder();
  140. }
  141. return retVal;
  142. }