DALI RECEIVE CODE

Revision:
0:d01bd4b990f0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jan 16 16:28:04 2021 +0000
@@ -0,0 +1,199 @@
+#include "mbed.h"
+DigitalOut myled(LED1);
+InterruptIn pulse(D8);      //Pulse Monitoring Line
+Ticker timer;
+Timer t;
+Timer s;
+Timer bitime_low;
+Timer bitime_high;
+
+int highcount;
+int lowcount;
+int arr[17];                //Create an array of 8 se
+int bit[8];
+int i;
+int k;
+int startbit;               //Parity bit to check / header bit 
+int addbit;                 // Int address to send bit to array
+int addbit_low;
+int addbit_high;
+int packet_fin;
+int startime;
+int firstbit;
+int fall_end;
+int rise_end;
+
+
+void risephase(){
+    fall_end = 0;
+    bitime_low.stop();
+    bitime_high.start();
+    addbit_high = bitime_low.read_us();
+    
+    if (addbit_high < 855 && i < 17){
+        if(addbit_high > 800) {
+           arr[i] = 0;
+           arr[i+1] = 0;
+           i++;
+           i++;
+        }
+        else{
+            arr[i] = 0;
+            i++; 
+        }
+    
+        }
+    else {
+        packet_fin = 1;
+    }
+    bitime_low.reset();
+    rise_end = 1;            //Switch states on risephase -> fallphase 
+}
+
+
+void fallphase(){
+    rise_end = 0;
+    bitime_high.stop();
+    bitime_low.start();
+    addbit_low = bitime_high.read_us();
+    if(i == 15 && addbit > 416){
+        arr[i+1] = 1;
+        packet_fin = 1;   
+    }
+    if (addbit_low < 855 && i < 16){
+        if(addbit_low > 800) {
+            arr[i] = 1;
+            arr[i+1] = 1;
+            i++;
+            i++;
+        }
+        else{
+            arr[i] = 1;
+            i++;  
+        }
+    }
+    else{
+        packet_fin = 1;
+    
+    }
+    
+    bitime_high.reset();
+    fall_end = 1;
+                //Switch states on fallphase -> risephase / return to previous state
+}
+
+void starthigh() {
+    if (startbit == 0){            
+        startbit = 1;               //Activate next state 
+        bitime_high.reset();        //No error on bittime bcs didn't reset    
+        bitime_high.start();
+        s.stop();
+        startime = s.read_us();
+        
+
+        //pulse.fall(&fallphase); 
+                          ///Reset timer 
+        //if(startime > 416 && startime < 430){
+        //    startbit = 1; 
+        //    }     
+        s.reset();
+        rise_end = 1;
+    }     
+}  
+
+void startlow() {
+    //wait_us(400);    
+    s.start();
+    
+    if (startbit == 0){         
+        pulse.rise(&starthigh);
+    }                                ///Gets starthigh
+}  
+
+
+int main(){
+    while(1){
+        printf("hello world");
+        int j;
+            
+            pulse.fall(&startlow);          // Detect 1st bit as a parity bit / 1st State
+            
+            if (startbit == 1){             // Detect the first fall after parity bit is confirmed, to check the length is X or 2X
+                //pulse.fall(&first_fallphase);     // Enter state to detect phaseshifts 
+                
+                while(i < 17){              //Loop to process if bit index less than 16
+                    if (fall_end == 1){
+                        pulse.rise(&risephase);
+                        }
+                    if (rise_end == 1){
+                        pulse.fall(&fallphase);
+                        }
+                } 
+                
+                //while(bitcount < 8){
+                //    pulse.rise(&red);       //Service RISING EDGE Interrupt to pointer &NAME
+                //    pulse.fall(&grn);       //Service FALLING EDGE Interrupt to pointer &NAME
+                //}
+                
+                if (packet_fin == 1) {        //Finish coding one full packet
+                    
+                    
+                
+                    printf("parity: %d\n", startbit);
+                    printf("startime: %d\n", startime);
+                    printf("firstime: %d\n", firstbit);  
+                    i = 0;                                      //Reset counter to fill a full array 
+                    
+                    startbit = 0;                               //Reset startbit to zero 
+                    //printf("%d\n", lowcount);
+                    j = 0;
+                    
+                    for (j = 1; j < 17; j++) {
+                        printf("%d ", arr[j]);
+                    }
+                    printf("\n");
+                    j = 0;
+                    k = 0;
+                    for (j = 1; j < 17; j++) {
+                        if (arr[j] == 1 && arr[j+1] == 0){
+                            bit[k] = 0; 
+                            k++;
+                            j++;
+                        }
+                        else if (arr[j] == 0 && arr[j+1] == 1){
+                            
+                            bit[k] = 1;
+                            k++;
+                            j++;
+                            
+                            
+                        }          
+                    }
+                    j = 0;
+                    
+                    
+                    for (j = 0; j < 8; j++) {
+                        printf("%d ", bit[j]);
+                    }
+                    
+                    j = 0;                             //Reset Counters
+                    k = 0;                             //Reset Counters
+                    
+                    printf("\n");
+                    highcount = 0;
+                    lowcount = 0; 
+                    
+                    for (j = 0; j < 17; j++) {
+                        arr[j] = 0;
+                    }
+                    for (j = 0; j < 8; j++) {
+                        bit[j] = 0;
+                    }
+                    
+                }
+                
+            }    
+            
+            
+    }
+}
\ No newline at end of file