toshiba_ir.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. uint8_t xorBytes(const uint8_t * const start, const uint16_t length);
  8. /**
  9. * @Analys of Toshiba IR Rx:
  10. * 0 1 2 3 4 5 6 7 8
  11. * F2 0D 03 FC 01 D0 A3 00 72 30 Grader
  12. * F2 0D 03 FC 01 90 A3 00 32 26 Grader
  13. * F2 0D 03 FC 01 40 A3 00 E2 21 Grader
  14. * F2 0D 03 FC 01 30 A3 00 92 20 Grader
  15. * F2 0D 03 FC 01 20 A3 00 82 19 Grader
  16. * F2 0D 03 FC 01 10 A3 00 B2 18
  17. * F2 0D 03 FC 01 00 A3 00 A2 17
  18. *
  19. * F2 0D 03 FC 01 D0 03 00 D2 Auto Fan 0000 0 (0 is Auto, 2-6 is the speed, 6 is Max)
  20. * F2 0D 03 FC 01 D0 43 00 92 1 0100 2
  21. * 2 0110 3
  22. * F2 0D 03 FC 01 D0 83 00 52 3 1000 4
  23. * 4 1010 5
  24. * F2 0D 03 FC 01 D0 C3 00 12 5 1100 6
  25. *
  26. * F2 0D 03 FC 01 60 83 00 E2 ON 1000 0011 (3 is ON, 7 is OFF)
  27. * F2 0D 03 FC 01 60 87 00 E6 OFF 1000 0111
  28. *
  29. */
  30. // Toshiba A/C
  31. const uint16_t kToshibaAcHdrMark = 4400;
  32. const uint16_t kToshibaAcHdrSpace = 4480;
  33. const uint16_t kToshibaAcBitMark = 530;
  34. const uint16_t kToshibaAcOneSpace = 1600;
  35. const uint16_t kToshibaAcZeroSpace = 530;
  36. // Some models have a different inter-message gap.
  37. // See: https://github.com/crankyoldgit/IRremoteESP8266/issues/1420
  38. const uint16_t kToshibaAcMinGap = 4600; // WH-UB03NJ remote
  39. const uint16_t kToshibaAcUsualGap = 7400; // Others
  40. uint8_t data[kToshibaNumberOfBytes]; // Temp data during rx
  41. uint8_t dataTransfer[kToshibaNumberOfBytes]; // Send as pointer to receiver
  42. enum
  43. {
  44. UNKNOWN,
  45. STARTER,
  46. T0,
  47. T1,
  48. DONE
  49. };
  50. static uint8_t rx_state = UNKNOWN;
  51. static uint32_t rx_numBits;
  52. void Toshiba_ir_ResetDecoder()
  53. {
  54. //ESP_LOGI("T", "Reset decoder");
  55. rx_numBits = 0;
  56. rx_state = UNKNOWN;
  57. memset(data,0,kToshibaNumberOfBytes);
  58. }
  59. static void addBit(uint8_t value)
  60. {
  61. if( value == 1 ) {
  62. const uint8_t byteNo = rx_numBits / 8;
  63. const uint8_t shiftBits = rx_numBits % 8;
  64. //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits);
  65. data[byteNo] |= 1u << (7-shiftBits);
  66. }
  67. rx_numBits++;
  68. }
  69. #define START_PULSE_MIN (kToshibaAcHdrMark-200)
  70. #define START_PULSE_MAX (kToshibaAcHdrMark+200)
  71. #define T0_PULSE_MIN (kToshibaAcBitMark-100)
  72. #define T0_PULSE_MAX (kToshibaAcBitMark+100)
  73. #define SHORT_PULSE_MIN (kToshibaAcZeroSpace-100)
  74. #define SHORT_PULSE_MAX (kToshibaAcZeroSpace+100)
  75. #define LONG_PULSE_MIN (kToshibaAcOneSpace-100)
  76. #define LONG_PULSE_MAX (kToshibaAcOneSpace+100)
  77. static int32_t rx_decode(uint32_t width)
  78. {
  79. switch (rx_state) {
  80. case UNKNOWN: // Start of frame A
  81. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  82. {
  83. rx_state = STARTER;
  84. //ESP_LOGI("T", "->STARTER");
  85. }
  86. else
  87. {
  88. return -1; // error, reset
  89. }
  90. break;
  91. case STARTER: // Start of frame B
  92. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  93. {
  94. rx_state = T0;
  95. //ESP_LOGI("T", "STARTER");
  96. }
  97. else
  98. {
  99. return -1; // error, reset
  100. }
  101. break;
  102. case T0: // First half of pulse : HIGH around 230us
  103. if(rx_numBits == kToshibaNumberOfBits)
  104. { // end of frame
  105. //ESP_LOGI("T", "END OF FRAME");
  106. rx_state = DONE;
  107. return 1;
  108. }
  109. else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
  110. {
  111. rx_state = T1;
  112. //ESP_LOGI("T", "T0");
  113. }
  114. else
  115. {
  116. return -1; // error, reset
  117. }
  118. break;
  119. case T1:
  120. if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
  121. {
  122. addBit(0);
  123. //ESP_LOGI("T", "Short %u",rx_numBits);
  124. }
  125. else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
  126. {
  127. addBit(1);
  128. //ESP_LOGI("T", "Long %u", rx_numBits);
  129. }
  130. else
  131. {
  132. return -1; // error, reset
  133. }
  134. rx_state = T0;
  135. break;
  136. }
  137. return 0;
  138. }
  139. uint8_t* nextPulseToshiba_ir(uint32_t width)
  140. {
  141. uint8_t* retVal = NULL;
  142. if (width > 0)
  143. {
  144. if (rx_state != DONE)
  145. {
  146. switch (rx_decode(width))
  147. {
  148. case -1:
  149. Toshiba_ir_ResetDecoder();
  150. break;
  151. case 1:
  152. rx_state = DONE;
  153. // Check checksum
  154. if( xorBytes(data,8) == data[8] ) {
  155. memcpy(dataTransfer,data,kToshibaNumberOfBytes);
  156. Toshiba_ir_ResetDecoder();
  157. retVal = dataTransfer;
  158. }
  159. else {
  160. ESP_LOGE("TOSHIBA", "WRONG CHKSUM");
  161. Toshiba_ir_ResetDecoder();
  162. }
  163. break;
  164. }
  165. }
  166. }
  167. return retVal;
  168. }
  169. /// Calculate a rolling XOR of all the bytes of an array.
  170. /// @param[in] start A ptr to the start of the byte array to calculate over.
  171. /// @param[in] length How many bytes to use in the calculation.
  172. /// @return The 8-bit calculated result of all the bytes and init value.
  173. /// Copied from: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRutils.cpp
  174. uint8_t xorBytes(const uint8_t * const start, const uint16_t length) {
  175. uint8_t checksum = 0;
  176. const uint8_t *ptr;
  177. for (ptr = start; ptr - start < length; ptr++) checksum ^= *ptr;
  178. return checksum;
  179. }