Alexandre Lemay / Mbed 2 deprecated APP3_Coordonator

Dependencies:   mbed

Revision:
3:eda76f539da3
Parent:
2:ddaf240af296
Child:
4:0b0dfd5ebf44
--- a/main.cpp	Tue Oct 03 04:48:02 2017 +0000
+++ b/main.cpp	Tue Oct 03 08:58:02 2017 +0000
@@ -14,51 +14,19 @@
 const uint8_t ZERO = 0x00;
 const uint8_t APPLY_CHANGES = 0x02;
 const uint8_t FF_CONST = 0xFF;
-const char OPEN_LED[2] = {'D','5'};
+const char OPEN_LED[2] = {'D','1'};
 
 
 bool atCommand = false;
 
 const int TRANSMIT_REQUEST_DATA = 12;
-uint8_t addresse_64_bit[8] = {0xcd, 0x2b, 0x0, 0x13, 0xa2, 0x0, 0x40, 0x33};
+uint8_t addresse_64_bit[8];
 uint8_t addresse_16_bit[2] = {0xFF,0xFE};
 
 uint8_t pan_id[14]={0x7E, 0x00, 0x0A, 0x08, 0x01, 0x49, 0x44, 0x46, 0x46, 0x39, 0x39, 0x31, 0x31, 0x09};
 
 uint8_t nd_Command[8] =  { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x4E, 0x44, 0x64 };
 
-void afficherErreurLed()
-{
-    
-    uint8_t d5_command[9] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 'D', '5', 5, 0};
-
-    static bool nextState = true;
-    
-    //check sum
-    
-    uint8_t checkSum = 0;
-    for(int i = 3; i<9;i++)
-    {
-        checkSum += d5_command[i];
-    }
-    
-    if(nextState)
-    {
-        d5_command[7] = 5;
-    }
-    else
-    {
-        d5_command[7] = 4;
-    }
-    
-    checkSum = FF_CONST - checkSum;
-    d5_command[8] = checkSum;
-    for(int i = 0 ; i < 9; i++)
-    {
-        xBee.putc(d5_command[i]);
-    }
-    
-}
 
 void sendNDCommand()
 {
@@ -73,17 +41,19 @@
 }
 
 void sendRemoteLedCommandPaquet(){
+    
     static bool nextState = true;
-    uint8_t length = 0xF;
+   // pc.printf("bool %i",nextState);
+    uint8_t length = 0x10;
     uint8_t paquet[25];
     uint8_t checkSum= 0x0;
     uint8_t LSBLength;
     uint8_t MSBLength;
     
-    char type[2] = { 'D','5'};
+    char type[2] = { 'D','1'};
 
     LSBLength = length&0xff;
-    MSBLength = (length &0xff00) >> 8;
+    MSBLength = 0;
     
     paquet[0] = START_DELEMITER; //start
     paquet[1] = MSBLength; //length MSB
@@ -94,11 +64,8 @@
     {
         paquet[i] = addresse_64_bit[i-5];
     }
-    
-    for(int i = 13; i < 15;i++) //16bit address
-    {
-        paquet[i] = addresse_16_bit[i-13];
-    }
+    paquet[13] = 0xFF;
+    paquet[14] = 0xFE;
     
     paquet[15] = APPLY_CHANGES; //BroadCast Radius
     paquet[16] = type[0];
@@ -112,7 +79,7 @@
     {
         paquet[18] = 0x04;
     }
-    
+    nextState = !nextState;
     //check sum
     
     for(int i = 3; i<length+3;i++)
@@ -124,28 +91,13 @@
      
     paquet[length+3] = checkSum;
      
+        pc.printf("\n packet");
      for(int i =0;i<length + 4;i++)
      {
         xBee.putc(paquet[i]);
-        
-    }
-
-}
+        pc.printf("%x ",paquet[i]);
+     }
 
-bool checkForError(int length,uint8_t type, uint8_t* data)
-{
-    data[0] = type;
-    uint8_t origCheckSum;
-    uint8_t checkSum = type;
-    for (int i =1; i < length; i++)
-    {
-        data[i] = xBee.getc();
-        checkSum += data[i];
-    }
-    checkSum = 0xFF - checkSum;
-    origCheckSum = xBee.getc();
-    
-    return checkSum != origCheckSum;
 }
 
 void receiveAtCommandResponse(int length, uint8_t* data)
@@ -166,7 +118,6 @@
     if(status != 0)
     {
         pc.printf("erreur");
-        afficherErreurLed();
         return;
     }
     switch(command)
@@ -175,12 +126,16 @@
     case 0x4e44: // Node Discovery AT Command
     {
         uint64_t serialNumber = 0;
-        for(int i = 5; i < 13; i++)
+        addresse_16_bit[0] = data[5];
+        addresse_16_bit[1] = data[6];
+        pc.printf("\nSerial number :  ");
+        for(int i = 7; i < 15; i++)
         {
             serialNumber += data[i] << (8 * (13-i -1) );
-            addresse_64_bit[i-5] = data[i];
-            pc.printf("\nSerial number %i %x\n",i,data[i]);
+            addresse_64_bit[i-7] = data[i];
+            pc.printf("%x ",data[i]);
         }
+        pc.printf("\n");
         
         return;
     }
@@ -189,39 +144,33 @@
      
 }
 
-void receiveTransmitRequest(int length,uint8_t type){
-    uint8_t data[512];
-    data[0] = type;
-    uint8_t origCheckSum;
-    uint8_t checkSum = type;
-    for (int i =1; i < length; i++)
+void receiveTransmitRequest(uint8_t* data,int length){
+        
+    for(int i = TRANSMIT_REQUEST_DATA; i <length;i++)
     {
-        data[i] = xBee.getc();
-        checkSum += data[i];
+    //    pc.printf("%c", data[i]);
     }
-    
-    checkSum = 0xFF - checkSum;
-    origCheckSum = xBee.getc();
-    
-    if(origCheckSum == checkSum){
-        for(int i = TRANSMIT_REQUEST_DATA; i <length;i++)
-        {
-            pc.printf("%c", data[i]);
-        }
-        pc.printf("\n");
-    }
-    else{
-        
-    }
-    
 }
 
+void stopLed()
+{
+    myled = 0;
+}
+
+void triggerErrorLed()
+{
+    myled = 1;
+    
+    static Timeout t;
+    t.detach();
+    t.attach(stopLed,1.0);
+}
 
 void receiveNewPaquet()
 {
       int length;
-    
-    if(xBee.getc() == START_DELEMITER)
+    uint8_t temp = xBee.getc();
+    if(temp == START_DELEMITER)
     {
         // MSB
         length = (((xBee.getc()) << 8));
@@ -232,28 +181,40 @@
         uint8_t data[512];
         data[0] = frame_type;
         
+        uint8_t checkSum = 0xFF - frame_type;
+        
+        for(int i = 1; i < length; i++)
+        {
+            uint8_t temp = xBee.getc();
+            if(temp == 0x7E){
+                pc.printf("\n Erreur 7e \n");
+                
+                triggerErrorLed();
+                return;                      
+            }
+            data[i] = temp;
+            checkSum-=temp;
+        }
+        // Mauvais CheckSum
+        if(checkSum != xBee.getc())
+        {
+            pc.printf("\nMauvais checksum\n");
+            triggerErrorLed();
+        }
         switch (frame_type){
         case 0x90: 
-               receiveTransmitRequest(length, frame_type);
+            //pc.printf("ReceiveTransmit");
+               receiveTransmitRequest(data,length);
         // AT command Response frame
         break;
         case 0x88:
+        
             pc.printf("\nat response : length %i, frame time %i\n",length,frame_type);
-            bool error = checkForError(length, frame_type,data);
-            if(true)
-            {
-                receiveAtCommandResponse(length, data);
-            }
-            else
-            {
-                pc.printf("Transmit or checksum error");
-            }
+  
+            receiveAtCommandResponse(length, data);
+                
             break;
-        }
-        
-    
-     
-        
+        }       
     }  
 }
 
@@ -266,21 +227,22 @@
     reset=1;
     wait(1);
     xBee.printf("+++");
+    
     wait(1);
-    sendNDCommand();
     //
 //    for(int i =0;i <14;i++){
 //        xBee.putc(pan_id[i]);   
 //    }
-    
+    sendNDCommand();
     while(1) {
+        
         if(xBee.readable()){
                 receiveNewPaquet();
         }
         if (atCommand){
             atCommand = false;
             sendRemoteLedCommandPaquet();
-        }            
+        }
     }
 }