#include "toshiba_ir.h" #include #include #include "esp_log.h" #include "rxTimer.h" #include // 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 #define kToshibaNumberOfBits 72 #define kToshibaNumberOfBytes (kToshibaNumberOfBits/8) uint8_t data[kToshibaNumberOfBytes]; 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; } int64_t nextPulseToshiba_ir(uint32_t width) { int64_t retVal = -1; if (width > 0) { if (rx_state != DONE) { switch (rx_decode(width)) { case -1: Toshiba_ir_ResetDecoder(); break; case 1: rx_state = DONE; break; } } } if (rx_state == DONE) { Toshiba_ir_ResetDecoder(); } return retVal; }