Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

Files at this revision

API Documentation at this revision

Comitter:
oliverfang
Date:
Thu Feb 06 21:35:02 2014 +0000
Parent:
10:f9a1e1dd5de1
Child:
12:d9d577c25961
Commit message:
updated small tweaks to make system more robust

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 06 07:54:58 2014 +0000
+++ b/main.cpp	Thu Feb 06 21:35:02 2014 +0000
@@ -135,59 +135,45 @@
 void processCommand()
 {
     //pc.printf("%s\r\n", buffer);
-    char* commands;
-    commands = strtok(buffer, "x");
-    //pc.printf("%s\r\n", commands);
-    switch(*commands)
+    char* cmd_index;
+    char* cmd_val;
+    // read in first value (0, 1, 2, or 3)
+    cmd_index = strtok(buffer, "x");
+    //pc.printf("%s\r\n", cmd_index);
+    //read in second value (integer larger than 050)
+    cmd_val = strtok(NULL, "x");
+    //pc.printf("%s\r\n", cmd_val);
+    temp = strtod(cmd_val, NULL)/1000;
+    if (temp >= 0.05 && strlen(cmd_val) >= 3)
     {
-        case '0':  
-            commands = strtok(NULL, "x");
-            //pc.printf("%s\r\n", commands);
-            temp = strtod(commands, NULL)/1000;
-            if (temp > 0.05)
-            {
+        switch(*cmd_index)
+        {
+            case '1':  
                 accRate = temp;
                 timerAcc.detach();
-                timerAcc.attach(&accTime, accRate);
-            }    
-            break;
-        case '1':  
-            commands = strtok(NULL, "x");
-            //pc.printf("%s\r\n", commands);
-            temp = strtod(commands, NULL)/1000;
-            if (temp > 0.05)
-            {
+                timerAcc.attach(&accTime, accRate); 
+                break;
+            case '2':  
                 magRate = temp;
                 timerMag.detach();
                 timerMag.attach(&magTime, magRate);
-            }
-            break;
-        case '2':  
-            commands = strtok(NULL, "x");
-            //pc.printf("%s\r\n", commands);
-            temp = strtod(commands, NULL)/1000;
-            if (temp > 0.05)
-            {
+                break;
+            case '3': 
                 lightRate = temp;
                 timerLight.detach();
                 timerLight.attach(&lightTime, lightRate);
-            }
-            break;
-        case '3':  
-            commands = strtok(NULL, "x");
-            //pc.printf("%s\r\n", commands);
-            temp = strtod(commands, NULL)/1000;
-            if (temp > 0.05)
-            {
+                break;
+            case '4':  
                 touchRate = temp;
                 timerTouch.detach();
                 timerTouch.attach(&touchTime, touchRate);
-            }
-            break;
-        default:
-            //pc.printf("incorrect input\r\n");          
-            break;
+                break;
+            default:
+                //pc.printf("incorrect input\r\n");          
+                break;
+        }
     }
+        
     received = 0;
     memset(buffer, 0, bufferSize);
     //pc.printf("%s\r\n", buffer);