1
0

toshiba_ir.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. void sendToshibaIRCode(uint8_t *data) {
  34. gpio_set_level(GPIO_IR_TX_DATA, 1);
  35. delayMicroseconds(kToshibaAcHdrMark);
  36. gpio_set_level(GPIO_IR_TX_DATA, 0);
  37. delayMicroseconds(kToshibaAcHdrSpace);
  38. for(uint8_t b=0;b<kToshibaNumberOfBits;b++) {
  39. const uint8_t byteNo = b / 8;
  40. const uint8_t shiftBits = b % 8;
  41. gpio_set_level(GPIO_IR_TX_DATA, 1);
  42. delayMicroseconds(kToshibaAcBitMark);
  43. gpio_set_level(GPIO_IR_TX_DATA, 0);
  44. const uint8_t bit_data = (1u << (7-shiftBits)) & data[byteNo];
  45. delayMicroseconds( bit_data ? kToshibaAcOneSpace : kToshibaAcZeroSpace );
  46. }
  47. gpio_set_level(GPIO_IR_TX_DATA, 1);
  48. delayMicroseconds(kToshibaAcHdrMark);
  49. gpio_set_level(GPIO_IR_TX_DATA, 0);
  50. delayMicroseconds(kToshibaAcUsualGap);
  51. }
  52. enum
  53. {
  54. UNKNOWN,
  55. STARTER,
  56. T0,
  57. T1,
  58. DONE
  59. };
  60. static uint8_t rx_state = UNKNOWN;
  61. static uint32_t rx_numBits;
  62. void Toshiba_ir_ResetDecoder()
  63. {
  64. //ESP_LOGI("T", "Reset decoder");
  65. rx_numBits = 0;
  66. rx_state = UNKNOWN;
  67. memset(data,0,kToshibaNumberOfBytes);
  68. }
  69. static void addBit(uint8_t value)
  70. {
  71. if( value == 1 ) {
  72. const uint8_t byteNo = rx_numBits / 8;
  73. const uint8_t shiftBits = rx_numBits % 8;
  74. //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits);
  75. data[byteNo] |= 1u << (7-shiftBits);
  76. }
  77. rx_numBits++;
  78. }
  79. #define START_PULSE_MIN (kToshibaAcHdrMark-200)
  80. #define START_PULSE_MAX (kToshibaAcHdrMark+200)
  81. #define T0_PULSE_MIN (kToshibaAcBitMark-100)
  82. #define T0_PULSE_MAX (kToshibaAcBitMark+100)
  83. #define SHORT_PULSE_MIN (kToshibaAcZeroSpace-100)
  84. #define SHORT_PULSE_MAX (kToshibaAcZeroSpace+100)
  85. #define LONG_PULSE_MIN (kToshibaAcOneSpace-100)
  86. #define LONG_PULSE_MAX (kToshibaAcOneSpace+100)
  87. static int32_t rx_decode(uint32_t width)
  88. {
  89. switch (rx_state) {
  90. case UNKNOWN: // Start of frame A
  91. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  92. {
  93. rx_state = STARTER;
  94. //ESP_LOGI("T", "->STARTER");
  95. }
  96. else
  97. {
  98. return -1; // error, reset
  99. }
  100. break;
  101. case STARTER: // Start of frame B
  102. if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
  103. {
  104. rx_state = T0;
  105. //ESP_LOGI("T", "STARTER");
  106. }
  107. else
  108. {
  109. return -1; // error, reset
  110. }
  111. break;
  112. case T0: // First half of pulse : HIGH around 230us
  113. if(rx_numBits == kToshibaNumberOfBits)
  114. { // end of frame
  115. //ESP_LOGI("T", "END OF FRAME");
  116. rx_state = DONE;
  117. return 1;
  118. }
  119. else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
  120. {
  121. rx_state = T1;
  122. //ESP_LOGI("T", "T0");
  123. }
  124. else
  125. {
  126. return -1; // error, reset
  127. }
  128. break;
  129. case T1:
  130. if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
  131. {
  132. addBit(0);
  133. //ESP_LOGI("T", "Short %u",rx_numBits);
  134. }
  135. else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
  136. {
  137. addBit(1);
  138. //ESP_LOGI("T", "Long %u", rx_numBits);
  139. }
  140. else
  141. {
  142. return -1; // error, reset
  143. }
  144. rx_state = T0;
  145. break;
  146. }
  147. return 0;
  148. }
  149. uint8_t* nextPulseToshiba_ir(uint32_t width)
  150. {
  151. uint8_t* retVal = NULL;
  152. if (width > 0)
  153. {
  154. if (rx_state != DONE)
  155. {
  156. switch (rx_decode(width))
  157. {
  158. case -1:
  159. Toshiba_ir_ResetDecoder();
  160. break;
  161. case 1:
  162. rx_state = DONE;
  163. // Check checksum
  164. if( xorBytes(data,8) == data[8] ) {
  165. memcpy(dataTransfer,data,kToshibaNumberOfBytes);
  166. Toshiba_ir_ResetDecoder();
  167. retVal = dataTransfer;
  168. }
  169. else {
  170. ESP_LOGE("TOSHIBA", "WRONG CHKSUM");
  171. Toshiba_ir_ResetDecoder();
  172. }
  173. break;
  174. }
  175. }
  176. }
  177. return retVal;
  178. }
  179. /// Calculate a rolling XOR of all the bytes of an array.
  180. /// @param[in] start A ptr to the start of the byte array to calculate over.
  181. /// @param[in] length How many bytes to use in the calculation.
  182. /// @return The 8-bit calculated result of all the bytes and init value.
  183. /// Copied from: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRutils.cpp
  184. uint8_t xorBytes(const uint8_t * const start, const uint16_t length) {
  185. uint8_t checksum = 0;
  186. const uint8_t *ptr;
  187. for (ptr = start; ptr - start < length; ptr++) checksum ^= *ptr;
  188. return checksum;
  189. }