Oregon Scientific Decoder V2 and V3 for Nucleo-F401RE

Dependencies:   Oregon_Decode

Oregon-Scientific Decoder for V2 and V3 protocol. As exemple it handles THGR122NX (V2) and THGR810 (V3) sensors.

The software use interrupts to collect frames in a buffer and decode them in a background task.

Files at this revision

API Documentation at this revision

Comitter:
sev2000
Date:
Sat Jul 18 07:58:54 2020 +0000
Parent:
0:7d471396cb64
Commit message:
Change for Mbed 6

Changed in this revision

OregonBit.cpp Show diff for this revision Revisions of this file
Oregon_Decode.lib Show annotated file Show diff for this revision Revisions of this file
Regul.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/OregonBit.cpp	Sat Oct 12 11:55:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,303 +0,0 @@
-#include "mbed.h" 
-#include "Regul.h"
- 
-int processData(measure_t*, int version);
-
-typedef struct {
-    int v;
-    bool pin;
-}pulse_t;
-
-int onShortLo = 200;
-int onShortHi = 615;
-int offShortLo = 400;
-int offShortHi = 850;
-int onLongLo = 615;
-int onLongHi = 1100;
-int offLongLo = 850;
-int offLongHi = 1400;
-long startedAt;
-long endedAt;
-
-bool dataBits[145]={0}; // 18 Nibbles +1
-
-Thread thread;
-Timer xTime; 
-
-InterruptIn dataPin(PB_8, PullUp);
-
-CircularBuffer<pulse_t, BUF_SIZE> PulseWidth;
-
-measure_t Sensor[NB_CHAN] ={0};
-pulse_t timeDiff;
-
-void getPulseF(void)
-{
-        endedAt = xTime.read_us(); // set timer end for last pin
-        timeDiff.v = endedAt - startedAt;
-        timeDiff.pin = 1;
-        PulseWidth.push(timeDiff);
-        startedAt= endedAt; //  set timer start for this pin
-}
-
-void getPulseR(void)
-{
-        endedAt = xTime.read_us(); // set timer end for last pin
-        timeDiff.v = endedAt - startedAt;
-        timeDiff.pin = 0;
-        PulseWidth.push(timeDiff);
-        startedAt= endedAt; //  set timer start for this pin
-}
-
- 
-
-void getData()
-{
-pulse_t pulse;
-// get next 128 data bits, we determined bit 0 in SYNC
-int i= 0; 
-int l= 0; // long pulse;
-int s= 0; // short pulse
-int bit_ptr= 1; //first bit[0] was derived in Preamble;
-char state=0;
- 
-while(true)
-    {
-    i=0;
-    while(!PulseWidth.empty() && i<100)
-        {
-        PulseWidth.pop(pulse);
-        //pc.printf("%d,", pulse.v);
-        if (pulse.pin == 0) //Pin was OFF
-            {
-            if ((pulse.v > offShortLo) && (pulse.v < offShortHi))
-                { // short off detected
-                    s++;
-                    l=0;
-                }
-            else if ((pulse.v > offLongLo) && (pulse.v < offLongHi))
-                { // long off detected
-                    l++;
-                    s=0;
-                }
-            else
-                {
-                    l=0;
-                    s=0;
-                }
-            }
-        else // Pin was ON
-            {
-            if ((pulse.v > onShortLo) && (pulse.v < onShortHi)) // half-time
-                { // short on detetcted
-                s++;
-                l=0;
-                }
-            else if ((pulse.v > onLongLo) && (pulse.v < onLongHi)) // full-time
-                { // long on detected
-                l++;
-                s=0;
-                }
-            else
-                {
-                    l=0;
-                    s=0;
-                }
-            }
-
-        switch(state)
-            {
-            case 0:     // Detect preamble 
-                        if(l >= 24) // out of 32
-                            state=1;
-                        if(s >= 40) // out of 48
-                            state=11;
-                        //pc.printf("%d ", l);
-                        break;
-            case 1:     // wait start bit (first short in V2.1)
-                        //pc.printf("OK2");
-                        l=0;
-                        if (s==1)
-                            state = 2;
-                        break;
-            case 11:     // wait start bit (first long in V3)
-                        //pc.printf("OK3");
-                        s=0;
-                        if (l==1)
-                            {
-                            state = 21;
-                            dataBits[1]=0; // l => opposite of previous (1)
-                            bit_ptr=2;
-                            l=0;
-                            }
-                        break;
-            case 2: 
-                        //pc.printf(" %d", pulse.v);
-                        if(s==0 && l==0)
-                            {
-                            ERR(" %s : %d\t", (pulse.pin ? "on" : "off"), pulse.v);
-                            if (0 == bit_ptr%2)
-                                l=1; // V2.1 2nd bit is !(bit n-1)
-                            else 
-                                if (pulse.v > 736)
-                                    l=1;
-                                else
-                                    s=1;
-                            }
-                        if (0 == bit_ptr%2 && !l)
-                            {
-                            ERR("%d V2.1 : 2nd pulse should be long",bit_ptr);
-                            s=0;
-                            l=1;
-                            }
-            
-                        if (s == 2)
-                            { // 2 short pulses this bit equals previous bit (we know 1st bit from sync)
-                            dataBits[bit_ptr] = dataBits[bit_ptr-1];
-              /*                  if(dataBits[bit_ptr] != !pulse.pin)
-                                    pc.printf("Error : V2.1 : pin level don't match"); */
-                            bit_ptr++;
-                            s=0;
-                            l=0;
-                            }
-                        if (l == 1 && s==0)
-                            { // 1 long pulse this bit is inverse of previous bit (we know 1st bit from sync)
-                            dataBits[bit_ptr] = !dataBits[bit_ptr-1];
-                            l=0;
-                            s=0;
-                            bit_ptr++;
-                            }
-                        if(bit_ptr>144)
-                            {
-                            processData(Sensor,2);
-                            state=0;
-                            bit_ptr=1;
-                            WARN(" Waiting...");
-                            }
-                        break;
-            case 21: 
-                        //pc.printf(" %d", pulse.v);
-                        if(s==0 && l==0)
-                            {
-                            ERR(" %s : %d\t", (pulse.pin ? "on" : "off"), pulse.v);
-
-                            if (pulse.v > 736)
-                                l=1;
-                            else
-                                s=1;
-                            }
-            
-                        if(s==1 && l==1)
-                            ERR(" %s : %d\t", (pulse.pin ? "on" : "off"), pulse.v);
-                        
-                        if (s == 2)
-                            { // 2 short pulses this bit equals previous bit (we know 1st bit from sync)
-                            dataBits[bit_ptr] = dataBits[bit_ptr-1];
-              /*                  if(dataBits[bit_ptr] != !pulse.pin)
-                                    ERR("Error : V2.1 : pin level don't match");*/
-                            //pc.printf(",[%d]%d",bit_ptr, dataBits[bit_ptr]);
-                            bit_ptr++;
-                            s=0;
-                            l=0;
-                            }
-                        if (l == 1)
-                            { // 1 long pulse this bit is inverse of previous bit (we know 1st bit from sync)
-                            dataBits[bit_ptr] = !dataBits[bit_ptr-1];
-                            l=0;
-                            s=0;
-                            //pc.printf(",[%d]%d",bit_ptr, dataBits[bit_ptr]);
-                            bit_ptr++;
-                            }
-                        if(bit_ptr>72)
-                            {
-                            processData(Sensor,3);
-                            state=0;
-                            bit_ptr=1;
-                            WARN(" Waiting...");
-                            }
-                        break;
-            }
-        i++;
-        }
-    ThisThread::sleep_for(50);
-    }
- //   return 0;
-}
- 
-int processData(measure_t *data, int version)
-{
-int x;
-int i = 0;
-int j=  0;
-char nibble[18]={0}, chksum=0;
-int tmp;
-char channel;
-
-    x= (version==2) ? 2 : 1;
-    for (i=0;i<18;i++)
-        {
-        for (j=0;j<4;j++)
-            {
-            if ( dataBits[x])
-                nibble[i] |= 1<<j;
-            x+= (version==2) ? 2 : 1;
-            }
-        }
-                
-    for (x=1;x<73;x++)
-    {
-//        pc.printf("%d", dataBits[x]);
-        dataBits[x]=0;
-    }
-
-#ifdef __DEBUG__
-    for (i=0;i<18;i++)
-        pc.printf("%X ",nibble[i]);
-    pc.printf("\r\n");
-#endif
-    
-    if( 0x0A == nibble[0])
-        {
-        for (i=1;i<16;i++)
-            chksum += nibble[i]; //no overflow if computed on 15 Nibbles
-    
-        if (chksum == (nibble[17]<<4 | nibble[16]))
-            {
-            channel =  nibble[5];
-            data[channel].deviceID = channel;
-            DBG("Channel: %d", channel);
-        
-            tmp= (nibble[8] & 0x4) ? 10 : 90;
-            DBG("Batterie: %d", tmp);
-        
-            int sign = (nibble[12]>0x0) ? -1 : 1;
-            float temp = nibble[11]*10 + nibble[10] + (float)(nibble[9] / 10.0);
-            temp= sign * temp;
-            data[channel].temp1= temp;
-            DBG("Temperature: %0.1f", temp);
-        
-            tmp= nibble[14] * 10 + nibble[13];
-            data[channel].hum1= (float)tmp;
-            DBG("Humidity: %d", tmp);
-            
-            data[channel].timestamp=time(NULL);
-            }
-        else
-            ERR("Checksum error %X", chksum);
-        }
-    else
-        ERR("Sync error");
-    return 0;
-}
-
-void Init_Oregon()
-{
-    dataPin.fall(&getPulseF);
-    dataPin.rise(&getPulseR);
-    dataPin.enable_irq();
-
-    xTime.start();
-    xTime.reset();
-    dataBits[0] = 1;
-    thread.start(getData);
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Oregon_Decode.lib	Sat Jul 18 07:58:54 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/sev2000/code/Oregon_Decode/#87e9b1cd65c2
--- a/Regul.h	Sat Oct 12 11:55:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-#include "mbed.h"
-
-extern Serial pc;
-
-//#define __DEBUG__
-#define Level 1
-#define DBG(x, ...) if (Level>=3) pc.printf("[DBG]"x"\r\n", ##__VA_ARGS__); 
-#define WARN(x, ...) if (Level>=2) pc.printf("[WARN]"x"\r\n", ##__VA_ARGS__); 
-#define ERR(x, ...) if (Level>=1) pc.printf("[ERR]"x"\r\n", ##__VA_ARGS__); 
-
-#define BUF_SIZE 1024
-
-#define DEVICE_ID 1
-#define FA
-
-#define NB_CHAN 3
-
-typedef struct {
-    char deviceID;
-    unsigned long timestamp;
-    float hum1;
-    float temp1;
-    float hum2;
-    float temp2;
-    unsigned char pwm;
-    int speed;
-}measure_t;
-
-typedef struct {
-    int first;
-    int prev;
-    int last;
-    measure_t data[BUF_SIZE];
-}s_meas_t;
-
-typedef struct {
-    int v;
-    bool update;
-}pwm_t;
-
-int Init_Regul();
-int Mesurement(measure_t *);
-int RegulPWM(measure_t *, pwm_t *);
-//int SendRcvData(NetworkStack *, s_meas_t *, pwm_t *);
-//int SendLoger(NetworkStack *, const char * );
--- a/main.cpp	Sat Oct 12 11:55:05 2019 +0000
+++ b/main.cpp	Sat Jul 18 07:58:54 2020 +0000
@@ -1,16 +1,19 @@
 #include "mbed.h" 
 #include "Regul.h"
+//#include "rtos.h"
 
 void Init_Oregon(void);
 extern measure_t Sensor[];
 
 DigitalOut led1(LED1);
-Serial pc(SERIAL_TX, SERIAL_RX);
+//static BufferedSerial pc(USBTX, USBRX,115200); // tx, rx
 
 int main()
 {
 measure_t Measure;
 
+    //pc.baud(115200);
+    DBG("Start");
     Init_Oregon();
    // NVIC_SetPriority(EXTI9_5_IRQn, 0); //set interupt to highest priority 0
    // NVIC_SetPriority(TIM3_IRQn,1); // set mbed tickers to lower priority than other things
@@ -24,7 +27,7 @@
             if (Sensor[i].timestamp !=0)
                 {
                 Measure=Sensor[i];
-                pc.printf("\r\nCh%d : %0.1f%cC  %0.0f%%\r\n", i , Measure.temp1, 0xC2B0, Measure.hum1);
+                printf("\r\nCh%d : %0.1f%cC  %0.0f%%\r\n", i , Measure.temp1, 0xC2B0, Measure.hum1);
                 Sensor[i].timestamp = 0;
                 }
             }
--- a/mbed-os.lib	Sat Oct 12 11:55:05 2019 +0000
+++ b/mbed-os.lib	Sat Jul 18 07:58:54 2020 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#e83fd322bd9177f60ccfd6712849b7db71869f00
+https://github.com/ARMmbed/mbed-os/#3ab72c71b75cb9cb91160a54fba22ec43b036ed2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Sat Jul 18 07:58:54 2020 +0000
@@ -0,0 +1,7 @@
+{
+    "target_overrides": {
+        "*": {
+            "platform.stdio-baud-rate": 115200
+            }
+    }
+}
\ No newline at end of file