BraceletUS / Mbed 2 deprecated S05APP3

Dependencies:   ConfigFile EthernetInterface mbed-rtos mbed

Fork of S05APP3 by App S5

Revision:
14:8b59a90725bc
Parent:
13:62404d7dab8d
Child:
15:4c748df01e5b
--- a/main.cpp	Mon Feb 13 17:09:27 2017 +0000
+++ b/main.cpp	Mon Feb 13 18:21:15 2017 +0000
@@ -11,24 +11,27 @@
 Serial pc(USBTX, USBRX); // tx, rx
 TCPSocketConnection sock;
 Ticker ticker1;
+Thread *t1;
 
 /* Mail */
 typedef struct {
-    char strAccelerationX[10];
-    char strAccelerationY[10];
-    char strAccelerationZ[10]; 
-    char strDryContact[10];      
+    uint8_t type;
+    char strAccelerationX[15];
+    char strAccelerationY[15];
+    char strAccelerationZ[15]; 
+    char strDryContact[15];      
 } mail_t;
 
 Mail<mail_t, 100> mail_box;
 
-uint8_t routerAddress[8];
+uint8_t routerAddress[8] = { 0 }; // Should be 00 13 A2 00 40 8B 41 6E
 uint16_t portNumber = 0;
 uint16_t panId = 0;
 char serverAddress[32];
 char panIdChar[5];
 char portNbr[5];
 bool boolLED = false; 
+bool isRouterAddressSet = false;
 
 void printBuffer(uint8_t bufferSize, uint8_t* buffer) {
     for(uint8_t k = 0; k <= bufferSize; k++) {
@@ -61,69 +64,70 @@
 }
 
 uint8_t calculateChecksum(uint8_t frameSize, uint8_t* buffer) {
-    uint32_t checksum = 0;
+    char checksum = 0;
     uint8_t frameTypeIndex = 3;
     
     for (int i = frameTypeIndex; i < frameSize - 1; i++) {
         checksum += buffer[i];
     }
 
-    return (uint8_t)(0xFF - (checksum & 0xFF));      
+    return (0xFF - checksum);      
 }
 
-void toogleLED(uint8_t bufferSize, uint8_t* buffer) {
-    // Addresse 64 bits = 00 13 A2 00 40 8B 41 6E
-    //uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C};
-    //uint8_t low[20]  = {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C};
-    uint8_t command[20] = {0};
-    
-    command[0] = 0x7E;
-    command[1] = 0x00;
-    command[2] = 0x10;
-    command[3] = 0x17;
-    if(boolLED) command[4] = 0x01;
-    else command[4] = 0x02;
-    command[5] = routerAddress[0];
-    command[6] = routerAddress[1];
-    command[7] = routerAddress[2];
-    command[8] = routerAddress[3];
-    command[9] = routerAddress[4];
-    command[10] = routerAddress[5];
-    command[11] = routerAddress[6];
-    command[12] = routerAddress[7];
-    command[13] = 0xFF;
-    command[14] = 0xFE;
-    command[15] = 0x02;
-    command[16] = 0x44;
-    command[17] = 0x34;
-    if(boolLED) command[4] = 0x05;
-    else command[4] = 0x04;
-    command[19] = calculateChecksum(bufferSize, buffer);
-    
- //   for (uint8_t i = 0; i < 20;) {
- //       if (xbee.writeable()) {
- //           if (boolLED) {
- //               xbee.putc(high[i]);
- //           } else {
- //               xbee.putc(low[i]);
- //           }
- //           i++; 
- //       }   
- //   }
-    
-    for (uint8_t i = 0; i < 20;) {
-        if (xbee.writeable()) {
-            xbee.putc(command[i]);
-            i++; 
-        }   
+void toggleLed() {
+    // uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C};
+    // uint8_t low[20]  = {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C};
+    while(1) {
+        Thread::signal_wait(0x1);
+        
+        if (!isRouterAddressSet) { // The router address is unknown
+            return;
+        }
+        
+        uint8_t command[20] = {0};
+        
+        command[0] = 0x7E;
+        command[1] = 0x00;
+        command[2] = 0x10;
+        command[3] = 0x17;
+        if (boolLED) { 
+            command[4] = 0x01;
+        }
+        else {
+            command[4] = 0x02;
+        }
+        command[5] = routerAddress[0];
+        command[6] = routerAddress[1];
+        command[7] = routerAddress[2];
+        command[8] = routerAddress[3];
+        command[9] = routerAddress[4];
+        command[10] = routerAddress[5];
+        command[11] = routerAddress[6];
+        command[12] = routerAddress[7];
+        command[13] = 0xFF;
+        command[14] = 0xFE;
+        command[15] = 0x02;
+        command[16] = 0x44;
+        command[17] = 0x34;
+        if (boolLED) {
+            command[18] = 0x05;
+        }
+        else {
+            command[18] = 0x04;
+        }
+        
+        command[19] = calculateChecksum(20, command);
+        printBuffer(19, command);
+        
+        for (uint8_t i = 0; i < 20;) {
+            if (xbee.writeable()) {
+                xbee.putc(command[i]);
+                i++; 
+            }   
+        }
+        
+        boolLED = !boolLED;
     }
-    
-    if (boolLED) {
-        pc.printf("Et un haut!\n");
-    } else {
-         pc.printf("Et un bas!\n");
-    }
-    boolLED = !boolLED;
 }
 
 void sendDataToServer() {
@@ -135,12 +139,13 @@
             
             int responseSize = 0;
             char buffer[50] = { 0 };
-            for (int i = 0; i < 50; i++) {
-                buffer[i] = 0;
+            
+            if (mail->type == 0) {
+                sprintf(buffer, "%s, %s, %s\0", mail->strAccelerationX, mail->strAccelerationY, mail->strAccelerationZ);
+            } else {
+                sprintf(buffer, "%s\0", mail->strDryContact);
             }
-            sprintf(buffer, "%s, %s, %s\0", mail->strAccelerationX, mail->strAccelerationY, mail->strAccelerationZ);
-            pc.printf("Send to server: %s\n", buffer);
-
+            
             sock.send_all(buffer, sizeof(buffer)-1);
             responseSize = sock.receive(buffer, sizeof(buffer)-1);
                     
@@ -182,14 +187,15 @@
 
 // We are storing the 64 bits address of the router.
 void readRouterAddress(uint8_t* buffer) {    
-    routerAddress[0] = buffer[2];
-    routerAddress[1] = buffer[3];
-    routerAddress[2] = buffer[4];
-    routerAddress[3] = buffer[5];
-    routerAddress[4] = buffer[6];
-    routerAddress[5] = buffer[7];
-    routerAddress[6] = buffer[8];
-    routerAddress[7] = buffer[9];
+    routerAddress[0] = buffer[1];
+    routerAddress[1] = buffer[2];
+    routerAddress[2] = buffer[3];
+    routerAddress[3] = buffer[4];
+    routerAddress[4] = buffer[5];
+    routerAddress[5] = buffer[6];
+    routerAddress[6] = buffer[7];
+    routerAddress[7] = buffer[8];
+    isRouterAddressSet = true;
 }
 
 void readRouterData(uint8_t bufferSize, uint8_t* buffer) {
@@ -204,15 +210,17 @@
         // pc.printf("Acceleration Z: %d\n", acc[2]);
         
         mail_t *mail = mail_box.alloc();
+        mail->type = 0;
         sprintf(mail->strAccelerationX, "Acc. X: %i\0", acc[0]);
         sprintf(mail->strAccelerationY, "Acc. Y: %i\0", acc[1]);
         sprintf(mail->strAccelerationZ, "Acc. Z: %i\0", acc[2]);
         mail_box.put(mail);
                         
     } else if (buffer[0] == 0x90 && bufferSize == 13) { // Dry contact
-        //mail_t *mail = mail_box.alloc();
-        //mail->data = acc[0];
-        //mail_box.put(mail);
+        mail_t *mail = mail_box.alloc();
+        mail->type = 1;
+        sprintf(mail->strDryContact, "Dry Contact: %i\0", buffer[12]);
+        mail_box.put(mail);
     }
 }
 
@@ -233,14 +241,20 @@
             
             printBuffer(bufferSize, buffer);
             if (checksumIsValid(bufferSize, buffer)) {
-                readRouterAddress(buffer);
-                readRouterData(bufferSize, buffer);
+                if (buffer[0] == 0x90) {
+                    readRouterAddress(buffer);
+                    readRouterData(bufferSize, buffer);
+                } else {
+                    
+                }
             }
         }
     }
 }
 
-void isr() {}
+void isr() {
+    t1->signal_set(0x1);
+}
 
 int main() {
     pc.printf("Starting a coordinator... \n");
@@ -276,8 +290,11 @@
     // Démarrage des tâches:
     Thread _readDataFromRouter(readDataFromRouter);
     Thread _sendDataToServer(sendDataToServer);
+    Thread _toggleRouterLed(toggleLed);
+    t1 = &_toggleRouterLed;
     
-    //Thread _toggleRouterLed(toggleLed);
+    ticker1.attach(&isr, 1);
+
     while(1) {}
 }