gbgfa

Dependencies:   mbed-rtos mbed EthernetInterface WebSocketClient

Files at this revision

API Documentation at this revision

Comitter:
ABuche
Date:
Sun Feb 12 22:19:36 2017 +0000
Child:
1:446da73e1118
Commit message:
Coord

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Feb 12 22:19:36 2017 +0000
@@ -0,0 +1,183 @@
+#include "mbed.h"
+#include "rtos.h"
+
+LocalFileSystem local("local");
+
+DigitalOut led1(LED1);
+DigitalOut reset(p8);
+RawSerial xbee(p13, p14);
+
+RawSerial pc(USBTX, USBRX);
+
+Thread* thread1;
+Thread* thread2;
+Thread* thread3;
+
+char LED_Toggle[] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x00, 0x00,
+     0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x02, 0x44, 0x30, 0x00, 0x25}; //Change index 13 & 14 & 18 & 19
+     
+char AT_command[] = {0x7E, 0x00, 0x06, 0x09, 0x01, 0x49 , 0x44, 0x00, 0x00, 0x00}; // +DATA + CS (7 & 8)
+const char AT_WR[] = {0x7E, 0x00, 0x04, 0x09, 0x01, 0x57, 0x52, 0x4C};
+const char AT_AC[] = {0x7E, 0x00, 0x04, 0x09, 0x01, 0x41, 0x43, 0x71};
+
+typedef struct {
+    char msg[25];
+} message;
+
+typedef struct {
+    char al;
+    char ah;
+} address;
+
+address addresses[3];
+char addressCounter = 0;
+
+Queue<message, 25> queue;
+MemoryPool<message, 25> mPool;
+
+char checksum(char array[], char length){
+    char cs = 0;
+    for (int i = 3;i<length - 1;i++){
+        cs += array[i];
+    }
+    return 0xFF - cs;
+}
+
+void hotdog1(){
+    while(1) {
+        message* msg = mPool.alloc();
+        
+        msg->msg[0] = xbee.getc();
+        
+        if(msg->msg[0] == 0x7E){ 
+            msg->msg[1] = xbee.getc();
+            msg->msg[2] = xbee.getc();            
+        
+            for (int i = 3;i < msg->msg[2] + 4;i++){
+                msg->msg[i] = xbee.getc();            
+            }
+            
+            queue.put(msg);      
+        }
+        
+    }
+}
+
+void traitement() {
+    while(1) {
+        message* msg = (message*)queue.get().value.p;
+        
+        if(msg->msg[2] >= 0x0C && msg->msg[3] == 0x90){
+            bool exists = false;
+            
+            for (int i = 0; i< sizeof(addresses);i++){
+                if(addresses[i].ah == msg->msg[12] && addresses[i].al == msg->msg[13]){
+                    exists = true;
+                    break;
+                }
+            }
+            
+            if (!exists) {
+                addresses[addressCounter].ah = msg->msg[12];                
+                addresses[addressCounter].al = msg->msg[13];
+                pc.printf("New address: %02x %02x", addresses[addressCounter].ah, addresses[addressCounter].al);
+                addressCounter++;
+            }
+            
+            //pc.printf("%02x ", msg->msg[15]);  
+            for(int i = 15; i < msg->msg[2] + 3;i++){
+                pc.printf("%02x ", msg->msg[i]);                
+            }
+            pc.printf("\r\n");
+        }            
+        mPool.free(msg);
+    }
+}
+
+void flashLED() {
+    bool high = true;
+    while(1){        
+        Thread::signal_wait(0x1);
+        
+        for (int i = 0; i < addressCounter;i++){            
+            LED_Toggle[13] = addresses[i].ah;
+            LED_Toggle[14] = addresses[i].al;
+            LED_Toggle[18] = high ? 0x05 : 0x04;
+            LED_Toggle[19] = checksum(LED_Toggle, sizeof(LED_Toggle));
+            high = !high;
+            led1 = !led1;
+            
+            for (int j = 0; j < sizeof(LED_Toggle);j++){               
+                xbee.putc(LED_Toggle[j]);
+            }            
+        }
+    }
+}
+
+void LEDSignal(){    
+    thread3->signal_set(0x1);
+}
+
+int main() {
+    reset = 0;
+    wait(0.4);
+    reset = 1;
+    wait(1);
+    
+    int pan;
+    char url[128];
+
+    FILE* f = fopen("/local/coord.cfg", "r");
+    fscanf(f,"%x", &pan);
+    fscanf(f,"%s", &url);
+    fclose(f);
+    
+    pc.printf("URL: %s", url);
+    
+    char buff[2];
+    buff[0]=(pan>>8)&0xff;
+    buff[1]=(pan)&0xff;
+    
+    AT_command[7] = buff[0];
+    AT_command[8] = buff[1];
+    
+    pc.printf("PAN: %02x%02x", AT_command[7],AT_command[8]);
+    
+    char cs = checksum(AT_command, sizeof(AT_command));
+    pc.printf("CS: %02x", cs);
+    
+    AT_command[9] = cs;
+    
+    for(char i = 0;i<sizeof(AT_command);i++){
+        xbee.putc(AT_command[i]);
+    }
+    
+    for(char i = 0;i<sizeof(AT_WR);i++){
+        xbee.putc(AT_WR[i]);
+    }
+    
+    for(char i = 0;i<sizeof(AT_AC);i++){
+        xbee.putc(AT_AC[i]);
+    }    
+    
+    Thread localThread1;
+    thread1 = &localThread1;
+    thread1->start(&hotdog1);
+    
+    Ticker horloge;
+    horloge.attach(&LEDSignal, 1.0);
+    
+    Thread localThread2;
+    thread2 = &localThread2;
+    thread2->start(&traitement);   
+    
+    addresses[0].ah = 0x29;
+    addresses[0].al = 0x21;
+    addressCounter++;
+    
+    Thread localThread3;
+    thread3 = &localThread3;
+    thread3->start(&flashLED);
+    while(1) {
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Feb 12 22:19:36 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Feb 12 22:19:36 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d
\ No newline at end of file