1
0

toshiba_ir.c 5.3 KB

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