Thomas Chef преди 1 година
родител
ревизия
e525c3a626
променени са 4 файла, в които са добавени 37 реда и са изтрити 19 реда
  1. 4 2
      main/ir_transmit.c
  2. 1 1
      main/main.c
  3. 1 4
      main/receiver.c
  4. 31 12
      main/toshiba_ir.c

+ 4 - 2
main/ir_transmit.c

@@ -122,15 +122,17 @@ void initIrTransmit() {
 	copyDataToRmtArray(1,fixedData,8,false);
 
 	// Setup the the real 152 data bits
-	const uint8_t confData[19]={0x02,0x20,0xE0,0x04,0x00,0x49,0x31,0x86,
+	const uint8_t confData[19]={0x02,0x20,0xE0,0x04,0x00,0x49,0x31,0x86,	// 0x86 is correct
 	                            0xA3,0x06,0x00,0x0E,0xE0,0x00,0x00,0x89,
 								0x00,0x00,0x20 };
 	copyDataToRmtArray(67,confData,19,true);
 
     toshibaTxQueue = xQueueCreate( 5, kPanasonicNumberOfBytes );
-	xTaskCreatePinnedToCore(toshibaTxTask, "toshibaTxTask", 1024*10, NULL, 2, NULL,0);
+	//xTaskCreatePinnedToCore(toshibaTxTask, "toshibaTxTask", 1024*10, NULL, 2, NULL,0);
 
 	ESP_LOGI("IR_TRANSMIT","Init done.");
+
+	ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, &(toshiba_rmt[0]), RMT_BITS, true));
 }
 
 #endif

+ 1 - 1
main/main.c

@@ -60,7 +60,7 @@ vTaskDelay(1000 / portTICK_PERIOD_MS);
     // Do an initial delay to make the different tasks to work out of sync to each other (not send all data at same time)
     vTaskDelayUntil( &vLastWakeTime, 2000 / portTICK_PERIOD_MS );
 
-#ifdef IR_TRANSMIT
+    #ifdef IR_TRANSMIT
 initIrTransmit();
 #endif
 

+ 1 - 4
main/receiver.c

@@ -46,16 +46,13 @@ void receiverTask(void *pvParameter)
         // Here we receive every individual pulse that has been detected by the isr-pulse-rx-function: timer_tg0_isr
         while( xQueueReceive( rxQueue, &width, 100 ) == pdTRUE ) {
 
-            ESP_LOGI("D", "%u", width);     // Debug to show every received pulse as a separate log line
+            //ESP_LOGI("D", "%u", width);     // Debug to show every received pulse as a separate log line
 
             // Receive the Toshiba IR
             // Send the pulse to the receiver that tries to interpret the pulse
             union ToshibaProtocolU* data = (union ToshibaProtocolU*)nextPulseToshiba_ir(width);
             if( data != NULL ) {
 
-                // Panasonic has LSB8 format so we need to bit-reverse each byte
-                for( uint8_t i=0; i<kPanasonicNumberOfBytes; i++ ) data->raw[i] = bitReverse(data->raw[i]);
-
                 char mqtt_s[100];
                 sprintf(mqtt_s,"A %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[0],data->raw[1],data->raw[2],data->raw[3],data->raw[4],data->raw[5],data->raw[6],data->raw[7]);
                 sprintf(mqtt_s,"B %02X%02X%02X%02X%02X%02X%02X%02X",data->raw[8],data->raw[9],data->raw[10],data->raw[11],data->raw[12],data->raw[13],data->raw[14],data->raw[15]);

+ 31 - 12
main/toshiba_ir.c

@@ -6,7 +6,8 @@
 #include <string.h>
 #include "config.h"
 
-uint8_t xorBytes(const uint8_t * const start, const uint16_t length);
+uint8_t sumBytes(const uint8_t * const start, const uint16_t length);
+extern uint8_t bitReverse(uint8_t b);
 
 /**
  * @Analys of Toshiba IR Rx:
@@ -43,6 +44,14 @@ uint8_t xorBytes(const uint8_t * const start, const uint16_t length);
  * 21 deg 4004072000125461 C560007007000091 000018   0101 0100  - 01010  A 10
  * 16 deg 4004072000120461 C560007007000091 000070   0000 0100  - 00000  0 00
  *        0220E00400482086 A306000EE0000089 00000E   0010 0010 17 grader
+ *
+ * Me:    4004072000000060 0220E00400483286 6306000EE00000890000E0
+ * Forum: 0220E00400000006 0220E00400492880 A40D000EE000000100069D
+ * Code has a checksum in the last byte (9D) calculated as follows:
+ * f4 + 02 + 20 + ... + 00 + 06 (= 9D in the case above).
+ * Summing all but the last byte to f4.
+ * Forum: https://www.varmepumpsforum.com/vpforum/index.php?topic=13580.0
+ * For me, the chksum needs to add 0x06 to make it work.
  * 
  * 
  * Fan in byte ([8] >> 4)
@@ -56,6 +65,11 @@ uint8_t xorBytes(const uint8_t * const start, const uint16_t length);
  * Power in data->raw[5]&0x01
  * 0 = OFF
  * 1 = ON
+ * 
+ * Checksum
+ * Byte #19 of frame 2 is the checksum for frame 2. It allows the AC unit to know whether the command sent
+ * is valid or not.
+ * The checksum is the sum (addition) of the previous 18 bytes modulo 256 (frame 2 only).
 */
 
 
@@ -217,16 +231,23 @@ uint8_t* nextPulseToshiba_ir(uint32_t width)
                 break;
             case 1:
                 rx_state = DONE;
+                // Panasonic has LSB8 format so we need to bit-reverse each byte
+                for( uint8_t i=0; i<kPanasonicNumberOfBytes; i++ ) data[i] = bitReverse(data[i]);
                 // Check checksum
-                //if( xorBytes(data,8) == data[8] ) {
+                uint8_t chkSum = sumBytes(data,18) - 6;
+                ESP_LOGI("TOSHIBA","Chksum:0x%02X  B18:0x%02X  Diff:0x%02X", chkSum,data[18],(uint8_t)(chkSum-data[18]));
+
+                if( chkSum == data[18] ) {
+                    ESP_LOGI("TOSHIBA", "CORRECT CHKSUM");
                     memcpy(dataTransfer,data,kPanasonicNumberOfBytes);
                     Toshiba_ir_ResetDecoder();
                     retVal = dataTransfer;
-                /*}
+                }
                 else {
                     ESP_LOGE("TOSHIBA", "WRONG CHKSUM");
+                    ESP_LOGE("TOSHIBA","Chksum:0x%02X  B18:0x%02X  Diff:0x%02X", chkSum,data[18],(uint8_t)(chkSum-data[18]));
                     Toshiba_ir_ResetDecoder();
-                }*/
+                }
                 break;
             }
         }
@@ -235,14 +256,12 @@ uint8_t* nextPulseToshiba_ir(uint32_t width)
     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 sumBytes(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;
+  for (ptr = start; ptr - start < length; ptr++) {
+    checksum += (*ptr);
+    //ESP_LOGI("CS","Byte:0x%02X",(*ptr));
+  }
+  return (uint8_t) (checksum);
 }