Alexandre Lemay / Mbed 2 deprecated APP3_Coordonator

Dependencies:   mbed

Revision:
1:fe18dd0ba172
Parent:
0:53817a293b39
Child:
2:ddaf240af296
--- a/main.cpp	Mon Oct 02 16:00:50 2017 +0000
+++ b/main.cpp	Mon Oct 02 21:58:44 2017 +0000
@@ -2,20 +2,146 @@
 
 DigitalOut myled(LED1);
 
+Ticker flipper;
 Serial pc(USBTX, USBRX);
 
 Serial xBee(p13, p14);
 DigitalOut reset(p8);
 
+const uint8_t START_DELEMITER = 0x7E;
+const uint8_t FRAME_TYPE_AT_REQUEST = 0x17;
+const uint8_t FRAME_ID = 0x01;
+const uint8_t ZERO = 0x00;
+const uint8_t APPLY_CHANGES = 0x02;
+const uint8_t FF_CONST = 0xFF;
+const char OPEN_LED[2] = {'D','1'};
+
+
+bool atCommand = false;
+
+const int TRANSMIT_REQUEST_DATA = 12;
+uint8_t addresse_64_bit[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+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};
 
+void sendAtCommand(){
+       pc.printf("trure");
+       atCommand = true;
+}
+
+void sendAtCommandPaquet(const char* type){
+    uint8_t length = 0xF;
+    uint8_t paquet[25];
+    uint8_t checkSum= 0x0;
+    uint8_t LSBLength;
+    uint8_t MSBLength;
+       pc.printf("in");
+
+    LSBLength = length&0xff;
+    MSBLength = (length &0xff00) >> 8;
+    
+    paquet[0] = START_DELEMITER; //start
+    paquet[1] = MSBLength; //length MSB
+    paquet[2] = LSBLength; //length LSB
+    paquet[3] = FRAME_TYPE_AT_REQUEST; //frame type
+    paquet[4] = FRAME_ID; //frame id
+    for(int i = 5; i < 13;i++) //64 bit addresse
+    {
+        paquet[i] = addresse_64_bit[i-5];
+    }
+    
+    for(int i = 13; i < 15;i++) //16bit address
+    {
+        paquet[i] = addresse_16_bit[i-13];
+    }
+    
+    paquet[15] = APPLY_CHANGES; //BroadCast Radius
+    paquet[16] = type[0];
+    paquet[17] = type[1];
+    paquet[18] = 0x05;
+       pc.printf("in2");
+    
+    //check sum
+    
+    for(int i = 3; i<length+3;i++)
+    {
+        checkSum += paquet[i];
+    }
+    
+    checkSum = FF_CONST - checkSum;
+     
+    paquet[length+3] = checkSum;
+     
+     for(int i =0;i<length + 4;i++)
+     {
+        xBee.putc(paquet[i]);
+        
+    }
+
+}
+
+void receiveTransmitRequest(int length,uint8_t type){
+    uint8_t data[100];
+    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();
+    
+    if(origCheckSum == checkSum){
+        for(int i = TRANSMIT_REQUEST_DATA; i <length;i++)
+        {
+            pc.printf("%x", data[i]);
+        }
+    }
+    else{
+        
+    }
+    
+}
+
+
+void receiveNewPaquet()
+{
+      int length;
+    
+    if(xBee.getc() == START_DELEMITER)
+    {
+        length = (((xBee.getc()) << 8));
+        length +=(xBee.getc());
+        uint8_t frame_type = xBee.getc();
+        
+        switch (frame_type){
+         case 0x90: 
+               receiveTransmitRequest(length, frame_type);
+            
+            
+        }
+        
+    
+     
+        
+    }  
+}
+
+
+
 int main() {
+    flipper.attach (&sendAtCommand,1);
     reset =0;
     wait_ms(500);
     reset=1;
     wait(1);
     xBee.printf("+++");
     wait(1);
+    
     //
 //    for(int i =0;i <14;i++){
 //        xBee.putc(pan_id[i]);   
@@ -24,8 +150,14 @@
     while(1) {
 
         if(xBee.readable()){
-                pc.printf("%x\n",xBee.getc());
-            }
+                receiveNewPaquet();
+        }
+        if (atCommand){
+            atCommand = false;
+            sendAtCommandPaquet(OPEN_LED);
+        }    
+            
+            
     }
 }