Forráskód Böngészése

Reception of Toshiba IR Remote works

Thomas Chef 2 éve
szülő
commit
8d53d7d158
4 módosított fájl, 114 hozzáadás és 43 törlés
  1. 2 2
      main/config.h
  2. 5 3
      main/receiver.c
  3. 65 38
      main/toshiba_ir.c
  4. 42 0
      main/toshiba_ir.h

+ 2 - 2
main/config.h

@@ -13,9 +13,9 @@
 
 #define MQTT_DEBUG              // Add an extra debug string to the topic-string
 
-#define ONE_WIRE_BUS_IO           GPIO_NUM_26   // Temp sensors
+#define ONE_WIRE_BUS_IO           GPIO_NUM_16   // Temp sensors
 
-#define GPIO_RX_DATA       16       // IR Receiver
+#define GPIO_RX_DATA       26       // IR Receiver
 
 
 #endif

+ 5 - 3
main/receiver.c

@@ -41,8 +41,10 @@ void receiverTask(void *pvParameter)
 
         while( xQueueReceive( rxQueue, &width, portMAX_DELAY ) == pdTRUE ) {
 
-            /*dataArr[dataArrIdx++] = width;
-            if( dataArrIdx == 1000 ) dataArrIdx=0;
+            //ESP_LOGI("D", "%u", width);
+
+            //dataArr[dataArrIdx++] = width;
+            //if( dataArrIdx == 1000 ) dataArrIdx=0;
 
             // Receive the Toshiba IR
             int64_t data = nextPulseToshiba_ir(width);
@@ -58,7 +60,7 @@ void receiverTask(void *pvParameter)
                 sendUDPMessage(dataStr, true);
 #endif
             }
-            */
+            
 
 
         }

+ 65 - 38
main/toshiba_ir.c

@@ -3,60 +3,96 @@
 #include <stdio.h>
 #include "esp_log.h"
 #include "rxTimer.h"
+#include <string.h>
+
+// 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,
-    T2,
-    T3,
-    TEMP1_OK,
     DONE
 };
 
 static uint8_t rx_state = UNKNOWN;
 
-static uint64_t sensor_data;
 static uint32_t rx_numBits;
 
 
 void Toshiba_ir_ResetDecoder()
 {
-    sensor_data = 0;
-    rx_numBits = 0;
+    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);
 
-    rx_numBits++;
-    sensor_data = (sensor_data << 1) + (value & 0x01);
 
-    rx_state = TEMP1_OK;
+        data[byteNo-1] |= ((uint8_t)1u) << shiftBits;
+    }
+
+    rx_numBits--;
 }
 
-#define START_PULSE_MIN (3880-200)
-#define START_PULSE_MAX (4010+200)
+#define START_PULSE_MIN (kToshibaAcHdrMark-200)
+#define START_PULSE_MAX (kToshibaAcHdrMark+200)
 
-#define T0_PULSE_MIN (380-100)
-#define T0_PULSE_MAX (520+100)
+#define T0_PULSE_MIN (kToshibaAcBitMark-100)
+#define T0_PULSE_MAX (kToshibaAcBitMark+100)
 
-#define SHORT_PULSE_MIN (890-100)
-#define SHORT_PULSE_MAX (990+100)
+#define SHORT_PULSE_MIN (kToshibaAcZeroSpace-100)
+#define SHORT_PULSE_MAX (kToshibaAcZeroSpace+100)
 
-#define LONG_PULSE_MIN (1880-150)
-#define LONG_PULSE_MAX (1980+150)
+#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
+        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
             {
@@ -64,28 +100,27 @@ static int32_t rx_decode(uint32_t width)
             }
             break;
 
+
         case T0: // First half of pulse : HIGH around 230us
 
-            if (rx_numBits == 32)
+            if(rx_numBits == 0)
             { // end of frame
+                ESP_LOGI("T", "END OF FRAME");
                 rx_state = DONE;
-                sensor_data = (sensor_data >> 8); // Mask away some bits at the end
+
+                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
             {
-                if (rx_numBits == 0 && START_PULSE_MIN <= width && width <= START_PULSE_MAX )
-                {
-                    rx_state = T0;
-                }
-                else
-                {
-                    return -1; // error, reset
-                }
+                return -1; // error, reset
             }
             break;
 
@@ -93,10 +128,12 @@ static int32_t rx_decode(uint32_t width)
             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
             {
@@ -110,9 +147,6 @@ static int32_t rx_decode(uint32_t width)
 
 int64_t nextPulseToshiba_ir(uint32_t width)
 {
-    static int64_t previous_data = 0;
-    static uint32_t old_time=0;
-    static uint32_t now;
     int64_t retVal = -1;
 
     if (width > 0)
@@ -131,14 +165,7 @@ int64_t nextPulseToshiba_ir(uint32_t width)
         }
     }
     if (rx_state == DONE) {
-        now = millis();
 
-        if( sensor_data != previous_data || (now > (old_time+1000)) ) {
-            previous_data = sensor_data;
-            retVal = sensor_data;
-            
-        }
-        old_time = now;
         Toshiba_ir_ResetDecoder();
     }
     

+ 42 - 0
main/toshiba_ir.h

@@ -8,4 +8,46 @@ void Toshiba_ir_ResetDecoder ();
 
 int64_t nextPulseToshiba_ir(uint32_t width);
 
+#define  kToshibaACStateLengthLong  10
+
+union ToshibaProtocol{
+  uint8_t raw[kToshibaACStateLengthLong];  ///< The state in code form.
+  struct {
+    // Byte[0] - 0xF2
+    uint8_t :8;
+    // Byte[1] - 0x0D (inverted previous byte's value)
+    uint8_t :8;
+    // Byte[2] - The expected payload length (in bytes) past the Byte[4].
+    ///< Known lengths are:
+    ///<   1 (56 bit message)
+    ///<   3 (72 bit message)
+    ///<   4 (80 bit message)
+    uint8_t Length   :8;
+    // Byte[3] - The bit-inverted value of the "length" byte.
+    uint8_t          :8;
+    // Byte[4]
+    uint8_t          :3;
+    uint8_t LongMsg  :1;
+    uint8_t          :1;
+    uint8_t ShortMsg :1;
+    uint8_t          :2;
+    // Byte[5]
+    uint8_t Swing    :3;
+    uint8_t          :1;
+    uint8_t Temp     :4;
+    // Byte[6]
+    uint8_t Mode     :3;
+    uint8_t          :2;
+    uint8_t Fan      :3;
+    // Byte[7]
+    uint8_t          :4;
+    uint8_t Filter   :1;
+    uint8_t          :3;
+
+    // Byte[8]
+    // (Checksum for 72 bit messages, Eco/Turbo for long 80 bit messages)
+    uint8_t EcoTurbo :8;
+  };
+};
+
 #endif