X2D lib first commit

Revision:
7:eaa973daaf58
Parent:
6:e373216c80bf
Child:
8:e134863cf7c3
--- a/X2D.cpp	Sun Nov 10 18:11:53 2019 +0000
+++ b/X2D.cpp	Mon Nov 11 17:53:10 2019 +0000
@@ -2,9 +2,9 @@
 #include "X2D.h" 
 
 #define BUF_SIZE    1000
-#define BIT_SIZE    145
+#define BIT_SIZE    160     //16Bytes + 0xFF + 0x00 + 3 bits + (n * 0 bit inserted by the X2D protocole to cut 0xFF
 
-int processData(void);
+int processData(info_t *message);
 
 DigitalOut Reg_Data(PC_1);
 DigitalOut RxTx(PB_0);
@@ -21,7 +21,7 @@
 
 pulse_t timeDiff;
 CircularBuffer<pulse_t, BUF_SIZE> PulseWidth;
-bool dataBits[BIT_SIZE]={0}; // 18 Nibbles +1
+bool dataBits[BIT_SIZE]={0};
 
 
 long startedAt=0;
@@ -153,7 +153,10 @@
         else
             cnt=0;
         if(cnt == 5)
+            {
             SendBit(0);
+            cnt=0;
+            }
         }
 }
 
@@ -161,7 +164,7 @@
 {
 int i;
 
-    pc.printf("\r\n");
+    //pc.printf("\r\n");
     Tx=1;
     for(i=0; i<17; i++)
         SendBit(0);
@@ -188,7 +191,9 @@
 int i, chksum=0;
 
     RxTx = 0;       //set pin rxtx to Tx
-    wait_ms(1);
+    Tx=0;
+//    wait_us(500);
+    //wait_ms(1);
 
     SendPreamble();
 
@@ -203,20 +208,28 @@
     SendByte((char)chksum);
     SendPostamble();
 
+//    Tx=1;
+
     RxTx = 1;       //set pin rxtx to Rx
+    pc.printf("\r\n");
 }
 
-void SendCmd(prog prog, int zone)
+void SendCmd(int prog, int zone)
 {
 char tab[8]={0xF1,0xE6,0x01,0x10,0x01,0x00,0x03,0x64};    // Zone1 Sun
 char maison[2]={0xF1,0xE6};
-char source = 0x01;
+char source = 0x02;
 char dest = 0;
-char trans = 0x01;
+char trans = 0x02;
+// control 1 char
+// information 0-8 char
+
 int i;
 
     dest = 0x10 + zone-1;
+    tab[2] = source;
     tab[3] = dest;
+    tab[4] = trans;
     tab[6] = prog;
 
     for(i=0; i<3; i++)
@@ -227,12 +240,36 @@
         }
 }
 
-int detectPreamble(void)
+void SendCmda(char *tab, int lenght)
+{
+//char tab[8]={0xF1,0xE6,0x02,0x01,0x02,0x00,0x00,0x64};    // Driver Moon
+char buf[16];
+char maison[2]={0xF1,0xE6};
+char source = 0x01;
+char dest = 0;
+char trans = 0x01;
+int i;
+
+/*    dest = 0x10 + zone-1;
+    tab[3] = dest;
+    tab[6] = prog;
+*/
+    memcpy(buf, tab, lenght);
+    for(i=0; i<3; i++)
+        {
+        buf[5] = tab[5] + i;    //message count
+        SendFrame(buf, lenght);
+        wait_ms(30);
+        }
+}
+
+
+int detectPreamble(info_t *message)
 {
 pulse_t pulse;
 int cnt = 0;
 char tmp[32]={0};
-char timing[8*1024]={0};
+char timing[8*BUF_SIZE]={0};
 char state=0;
 char s=0, l=0, bit_ptr=0;
 
@@ -240,7 +277,9 @@
     {
         PulseWidth.pop(pulse);
         sprintf(tmp, "%d, %ld|", pulse.pin, pulse.v);
-        strcat(timing, tmp);
+        
+        //strcat(timing, tmp);
+        //pc.printf("%s ", tmp);
         
         if ((pulse.v > 700) && (pulse.v < 1000))
                 { // short off detected
@@ -297,7 +336,7 @@
                             {
                             state=0;
                             bit_ptr=0;
-                            printf("Frame too long ; dropped");
+                            pc.printf("Frame too long : dropped\r\n");
                             }
                             
                         break;
@@ -305,7 +344,7 @@
                         
         if(pulse.v > 30000 && cnt>0)     // End of frame
               {
-              processData();
+              processData(message);
               //timing[0]=0;
               state=0;
               bit_ptr=0;
@@ -317,32 +356,22 @@
     }
     if (cnt>0)      // if buffer wasn't empty
         {
-        processData();
+        processData(message);
         //pc.printf("%s\r\n", timing);
         }
 
     return(0);
 }
 
-int processData(void)
+int processData(info_t *message)
 {
 int x=0;
 int i = 0;
 int j=  0;
 char nibble[18]={0}, cnt=0;
-int chksum=0, etx_ptr=16;
+int chksum=0, ETX_pos=16;
 
-/*    pc.printf("\r\n");
-    for (x=0;x<128;x++)
-    {
-        if(x%8==0)
-            pc.printf(" ");
-        pc.printf("%d", dataBits[x]);
-    }
-*/
-x=0;
-
-    for (i=0; i<etx_ptr; i++)
+    for (i=0; i<ETX_pos; i++)
         {
         for (j=0;j<8;j++)
             {
@@ -361,22 +390,28 @@
             x++;
             }
         if (cnt >= 8)    // End of Frame detection
-            etx_ptr=i;
+            ETX_pos=i;
         }
                 
 
-    for (i=0; i<etx_ptr-2; i++)   // Calculate Checksum
+    for (i=0; i<ETX_pos-2; i++)   // Calculate Checksum
         chksum += nibble[i];
     chksum = ~chksum +1;
         
 #ifdef __DEBUG__
-    for (i=0; i<etx_ptr; i++)
+    for (i=0; i<ETX_pos; i++)
         pc.printf("%0.2X ",nibble[i]);
-    if ( (char)(chksum>>8) != nibble[etx_ptr-2] || (char)chksum != nibble[etx_ptr-1] )
+    if ( (char)(chksum>>8) != nibble[ETX_pos-2] || (char)chksum != nibble[ETX_pos-1] )
         pc.printf("  CRC Error");
     pc.printf("\r\n");
 #endif
-    
+
+    if((nibble[5]&0xF0) == 0 && nibble[2]==1 && nibble[4]!=nibble[2])
+        {
+        message->zone=nibble[3]-0x10 +1;
+        message->mode=(h_mode)nibble[6];
+        }
+
     return 0;
 }