|
@@ -5,6 +5,8 @@
|
|
|
#include "rxTimer.h"
|
|
|
#include <string.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
|
|
@@ -60,7 +62,7 @@ static uint32_t rx_numBits;
|
|
|
void Toshiba_ir_ResetDecoder()
|
|
|
{
|
|
|
//ESP_LOGI("T", "Reset decoder");
|
|
|
- rx_numBits = kToshibaNumberOfBits;
|
|
|
+ rx_numBits = 0;
|
|
|
rx_state = UNKNOWN;
|
|
|
memset(data,0,kToshibaNumberOfBytes);
|
|
|
}
|
|
@@ -69,17 +71,15 @@ static void addBit(uint8_t value)
|
|
|
{
|
|
|
if( value == 1 ) {
|
|
|
|
|
|
+ const uint8_t byteNo = rx_numBits / 8;
|
|
|
+ const uint8_t shiftBits = rx_numBits % 8;
|
|
|
|
|
|
- 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);
|
|
|
+ //ESP_LOGI("BIT RX:","%u %u", byteNo, shiftBits);
|
|
|
|
|
|
-
|
|
|
- data[byteNo-1] |= ((uint8_t)1u) << shiftBits;
|
|
|
+ data[byteNo] |= 1u << (7-shiftBits);
|
|
|
}
|
|
|
|
|
|
- rx_numBits--;
|
|
|
+ rx_numBits++;
|
|
|
}
|
|
|
|
|
|
#define START_PULSE_MIN (kToshibaAcHdrMark-200)
|
|
@@ -125,14 +125,10 @@ static int32_t rx_decode(uint32_t width)
|
|
|
|
|
|
case T0: // First half of pulse : HIGH around 230us
|
|
|
|
|
|
- if(rx_numBits == 0)
|
|
|
+ if(rx_numBits == kToshibaNumberOfBits)
|
|
|
{ // 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 )
|
|
@@ -184,13 +180,32 @@ uint8_t* nextPulseToshiba_ir(uint32_t width)
|
|
|
break;
|
|
|
case 1:
|
|
|
rx_state = DONE;
|
|
|
- memcpy(dataTransfer,data,kToshibaNumberOfBytes);
|
|
|
- Toshiba_ir_ResetDecoder();
|
|
|
- retVal = dataTransfer;
|
|
|
+ // 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;
|
|
|
+}
|