#include "toshiba_ir.h" #include #include #include "esp_log.h" #include "rxTimer.h" #include #include "config.h" uint8_t xorBytes(const uint8_t * const start, const uint16_t length); /** * @Analys of Toshiba IR Rx: * 0 1 2 3 4 5 6 7 8 * F2 0D 03 FC 01 D0 A3 00 72 30 Grader * F2 0D 03 FC 01 90 A3 00 32 26 Grader * F2 0D 03 FC 01 40 A3 00 E2 21 Grader * F2 0D 03 FC 01 30 A3 00 92 20 Grader * F2 0D 03 FC 01 20 A3 00 82 19 Grader * F2 0D 03 FC 01 10 A3 00 B2 18 * F2 0D 03 FC 01 00 A3 00 A2 17 * * F2 0D 03 FC 01 D0 03 00 D2 Auto Fan 0000 0 (0 is Auto, 2-6 is the speed, 6 is Max) * F2 0D 03 FC 01 D0 43 00 92 1 0100 2 * 2 0110 3 * F2 0D 03 FC 01 D0 83 00 52 3 1000 4 * 4 1010 5 * F2 0D 03 FC 01 D0 C3 00 12 5 1100 6 * * F2 0D 03 FC 01 60 83 00 E2 ON 1000 0011 (3 is ON, 7 is OFF) * F2 0D 03 FC 01 60 87 00 E6 OFF 1000 0111 * */ uint8_t data[kToshibaNumberOfBytes]; // Temp data during rx uint8_t dataTransfer[kToshibaNumberOfBytes]; // Send as pointer to receiver enum { UNKNOWN, STARTER, T0, T1, DONE }; static uint8_t rx_state = UNKNOWN; static uint32_t rx_numBits; void Toshiba_ir_ResetDecoder() { //ESP_LOGI("T", "Reset decoder"); rx_numBits = 0; rx_state = UNKNOWN; memset(data,0,kToshibaNumberOfBytes); } static void addBit(uint8_t value) { if( value == 1 ) { const uint8_t byteNo = rx_numBits / 8; const uint8_t shiftBits = rx_numBits % 8; //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits); data[byteNo] |= 1u << (7-shiftBits); } rx_numBits++; } #define START_PULSE_MIN (kToshibaAcHdrMark-200) #define START_PULSE_MAX (kToshibaAcHdrMark+200) #define T0_PULSE_MIN (kToshibaAcBitMark-100) #define T0_PULSE_MAX (kToshibaAcBitMark+100) #define SHORT_PULSE_MIN (kToshibaAcZeroSpace-100) #define SHORT_PULSE_MAX (kToshibaAcZeroSpace+100) #define LONG_PULSE_MIN (kToshibaAcOneSpace-100) #define LONG_PULSE_MAX (kToshibaAcOneSpace+100) static int32_t rx_decode(uint32_t width) { switch (rx_state) { case UNKNOWN: // Start of frame A if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX ) { rx_state = STARTER; //ESP_LOGI("T", "->STARTER"); } else { return -1; // error, reset } break; case STARTER: // Start of frame B if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX ) { rx_state = T0; //ESP_LOGI("T", "STARTER"); } else { return -1; // error, reset } break; case T0: // First half of pulse : HIGH around 230us if(rx_numBits == kToshibaNumberOfBits) { // end of frame //ESP_LOGI("T", "END OF FRAME"); rx_state = DONE; return 1; } else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX ) { rx_state = T1; //ESP_LOGI("T", "T0"); } else { return -1; // error, reset } break; case T1: if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX ) { addBit(0); //ESP_LOGI("T", "Short %u",rx_numBits); } else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX ) { addBit(1); //ESP_LOGI("T", "Long %u", rx_numBits); } else { return -1; // error, reset } rx_state = T0; break; } return 0; } uint8_t* nextPulseToshiba_ir(uint32_t width) { uint8_t* retVal = NULL; if (width > 0) { if (rx_state != DONE) { switch (rx_decode(width)) { case -1: Toshiba_ir_ResetDecoder(); break; case 1: rx_state = DONE; // Check checksum if( xorBytes(data,8) == data[8] ) { memcpy(dataTransfer,data,kToshibaNumberOfBytes); Toshiba_ir_ResetDecoder(); retVal = dataTransfer; } else { ESP_LOGE("TOSHIBA", "WRONG CHKSUM"); Toshiba_ir_ResetDecoder(); } break; } } } return retVal; } /// Calculate a rolling XOR of all the bytes of an array. /// @param[in] start A ptr to the start of the byte array to calculate over. /// @param[in] length How many bytes to use in the calculation. /// @return The 8-bit calculated result of all the bytes and init value. /// Copied from: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRutils.cpp uint8_t xorBytes(const uint8_t * const start, const uint16_t length) { uint8_t checksum = 0; const uint8_t *ptr; for (ptr = start; ptr - start < length; ptr++) checksum ^= *ptr; return checksum; }