Pranav Thakur / Mbed 2 deprecated daplink-validation

Dependencies:   mbed

Fork of daplink-validation by Russ Butler

Revision:
1:db2a55107c7e
Parent:
0:c4a3bb148a79
Child:
2:204ef796ee06
--- a/main.cpp	Thu Sep 10 16:03:46 2015 +0000
+++ b/main.cpp	Fri Sep 11 14:50:00 2015 +0000
@@ -1,34 +1,57 @@
 #include "mbed.h"
+#include <stdio.h>
 
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
-DigitalOut myled3(LED3);
-//Serial pc(P0_6, P0_1);
 Serial pc(USBTX, USBRX);
 
-float blink_time = 0.5;
+int main()
+{
+    uint32_t baud;
+    uint32_t count;
+    uint32_t index;
+    uint32_t val;
+    uint8_t str[64];
 
-int main() {
+    count = 0;
+    index = 0;
+
     pc.baud(115200);
-    myled1 = 1;
-    myled3 = 1;
-    myled2 = 1;
+    pc.printf("{init}");
     while(1) {
-        myled1 = 0;
-        wait(blink_time);
-        myled1 = 1;
-        wait(blink_time);
-        
-        myled2 = 0;
-        wait(blink_time);
-        myled2 = 1;
-        wait(blink_time);
-        
-        myled3 = 0;
-        wait(blink_time);
-        myled3 = 1;
-        wait(blink_time);
-        pc.printf("Some data\r\n");
-        
+        while (1) {
+            val = pc.getc();
+
+            // Check for overflow. Leave space for
+            // a null terminating character
+            if (index >= sizeof(str) - 1) {
+                index = 0;
+            }
+
+            // Check for start of frame
+            if ('{' == val) {
+                index = 0;
+            }
+
+            // Check for end of frame
+            str[index] = val;
+            index++;
+
+            // Check for end of frame
+            if ('}' == val && index > 0) {
+                str[index] = 0;
+                count = sscanf((char*)str, "{baud:%i}", &baud);
+            }
+
+            // Echo back character
+            pc.putc(val);
+
+            // Set baud if there is a valid command
+            if (count == 1) {
+                wait(0.01f);
+                pc.baud(baud);
+                wait(0.01f);
+                pc.printf("{change}");
+                count = 0;
+            }
+        }
     }
 }