Przeglądaj źródła

Receiving Clas O Klockradio senor data

Thomas Chef 7 lat temu
rodzic
commit
7596800df2
2 zmienionych plików z 135 dodań i 3 usunięć
  1. 109 0
      clas_o_sensor.ino
  2. 26 3
      wireless_temp_sensor_receiver.ino

+ 109 - 0
clas_o_sensor.ino

@@ -0,0 +1,109 @@
+//#define CLAS_O_DEBUG
+
+enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
+
+static unsigned char clas_o_state = UNKNOWN;
+
+unsigned long clas_o_x_data;
+static unsigned int clas_o_x_numBits;
+
+void clas_o_ResetDecoder () {
+    clas_o_x_data = 0;
+    clas_o_x_numBits = 0;
+    clas_o_state = UNKNOWN;
+}
+
+static boolean clas_o_isDone() {
+    return clas_o_state == DONE;
+}
+
+static void clas_o_done () {
+    clas_o_state = DONE;
+}
+
+static void clas_o_gotBit (char value) {
+
+    clas_o_x_numBits++;
+    clas_o_x_data = (clas_o_x_data << 1) + (value & 0x01);
+
+    clas_o_state = OK;
+}
+
+static int clas_o_decode (unsigned int width) {
+
+    if( clas_o_state != UNKNOWN ) {
+#ifdef CLAS_O_DEBUG
+        Serial.print(width);
+        Serial.print(" - ");
+#endif
+    }
+
+
+    switch (clas_o_state) {
+        case UNKNOWN: // Start of frame
+            if ( 3500 <= width && width <= 4000 ) {
+#ifdef CLAS_O_DEBUG
+                Serial.print(width);
+                Serial.print(" - ");
+#endif
+                clas_o_state = T0;
+            }
+            else {
+                return -1;    // error, reset
+            }
+            break;
+
+        case T0: // First half of pulse : HIGH around 230us
+
+            if ( clas_o_x_numBits == 32 ) {   // end of frame
+                clas_o_state = DONE;
+                clas_o_x_data = (clas_o_x_data >> 8);    // Mask away some bits at the end
+                return 1;
+            }
+            else if ( 340 <= width && width <= 540 ) {
+                clas_o_state = T1;
+            }
+            else {
+                if ( clas_o_x_numBits == 0 && 3400 <= width && width <= 3800 ) {
+                    clas_o_state = T0;
+                }
+                else {
+#ifdef CLAS_O_DEBUG
+                    Serial.println(" X1X ");
+#endif
+                    return -1;  // error, reset
+                }
+            }
+            break;
+
+        case T1:
+            if ( 800 <= width && width <= 1000) {
+                clas_o_gotBit(0);
+            } else if ( 1640 <= width && width <= 1940 ) {
+                clas_o_gotBit(1);
+            } else  {
+#ifdef CLAS_O_DEBUG
+                Serial.println(" X2X ");
+#endif
+                return -1;    // error, reset
+            }
+            clas_o_state = T0;
+            break;
+    }
+    return 0;
+}
+
+boolean nextPulse_clas_o (unsigned int width) {
+
+    if ( width > 0 ) {
+        if (clas_o_state != DONE)
+
+            switch (clas_o_decode(width)) {
+                case -1:
+                    clas_o_ResetDecoder();
+                    break;
+                case 1:  clas_o_done();  break;
+            }
+    }
+    return clas_o_isDone();
+}

+ 26 - 3
wireless_temp_sensor_receiver.ino

@@ -20,21 +20,44 @@ void setup()
     Serial1.begin(9600); // HW-UART (To Raspberry)
 }
 
-// This code is called over and oven again
+extern unsigned long clas_o_x_data;
+
+// Convert from 12-bit unsigned data to 32-bit signed data
+static int32_t convertToSignedTemp(uint16_t value)
+{
+    return ( (value >> 11) == 0 )? value : ((-1 ^ 0xFFF) | value);
+}
+
+// This code is called over and over again
 void loop()
 {
     static uint16_t width;              // Stores the length of the received pulse
     static unsigned long totalRx = 0;   // The total number of received pulses
 
     if ( rcvDeQueue(&width) ) {
+
+        //width = (width / 5) * 4; // 80%
         
         totalRx++;  // Increase the total received pulses
 
+        if( nextPulse_clas_o(width) ) {
+              
+             int16_t value      = convertToSignedTemp( clas_o_x_data & 0xFFF );
+            uint16_t id         = (clas_o_x_data>>12) & 0x007;
+            
+            char rad[200];
+            sprintf(rad,"Id:%u  Temp:%d.%d\n",id,value/10,value%10);
+            Serial.print(rad);
+            
+
+            clas_o_ResetDecoder();
+        }    
+
         // Check if the received pulse is within limits for a start-pulse
-        if ( 3400 <= width && width <= 3800 ) {
+        /*if ( 3400 <= width && width <= 3800 ) {
             Serial.print("Received one !  Total pulses: ");
             Serial.println(totalRx);
-        }
+        }*/
 
     }
 }