#include "toshiba_ir.h" #include #include #include "esp_log.h" #include "rxTimer.h" #include /** * @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 * */ // Toshiba A/C const uint16_t kToshibaAcHdrMark = 4400; const uint16_t kToshibaAcHdrSpace = 4480; const uint16_t kToshibaAcBitMark = 530; const uint16_t kToshibaAcOneSpace = 1600; const uint16_t kToshibaAcZeroSpace = 530; // Some models have a different inter-message gap. // See: https://github.com/crankyoldgit/IRremoteESP8266/issues/1420 const uint16_t kToshibaAcMinGap = 4600; // WH-UB03NJ remote const uint16_t kToshibaAcUsualGap = 7400; // Others 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 = kToshibaNumberOfBits; rx_state = UNKNOWN; memset(data,0,kToshibaNumberOfBytes); } static void addBit(uint8_t value) { if( value == 1 ) { const uint8_t byteNo = (rx_numBits+7) / 8; const uint8_t shiftBits = (int)rx_numBits - (byteNo*8) + 7; //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits); data[byteNo-1] |= ((uint8_t)1u) << 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 == 0) { // end of frame //ESP_LOGI("T", "END OF FRAME"); rx_state = DONE; /*for(uint8_t i=9; i>0 ;i--) { ESP_LOGI("DATA","Byte %u : %02x", i-1, data[i-1]); }*/ 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; memcpy(dataTransfer,data,kToshibaNumberOfBytes); Toshiba_ir_ResetDecoder(); retVal = dataTransfer; break; } } } return retVal; }