Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

Revision:
10:f9a1e1dd5de1
Parent:
8:b87b93a62a6a
Child:
11:7af59a3d9ac5
--- a/main.cpp	Thu Feb 06 06:10:19 2014 +0000
+++ b/main.cpp	Thu Feb 06 07:54:58 2014 +0000
@@ -39,7 +39,9 @@
 const int bufferSize = 255;
 char buffer[bufferSize];
 int index = 0;
-bool received = false;
+int received = 0;
+int readEn = 0;
+float temp;
 
 // Declare Accelerometer pins and I2C address
 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0);
@@ -63,7 +65,7 @@
 void init()
 {
     // Attach timerAcc
-    //pc.baud(9600);
+    pc.baud(9600);
     timerAcc.attach(&accTime, accRate);
     timerMag.attach(&magTime, magRate);
     timerLight.attach(&lightTime, lightRate);
@@ -80,9 +82,11 @@
 
     while(1)
     {
+        __disable_irq();
         printData();
+        __enable_irq();
         wait(0.05);
-        if(received){
+        if(received == 1){
             __disable_irq();    
             processCommand();
             __enable_irq();
@@ -93,22 +97,37 @@
 
 void printData()
 {
-    pc.printf("/%f/%f/%f/%d/%d/%d/%f/%f/%f/%f/%f/%f/\r\n", xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch, accRate, magRate, lightRate, touchRate);
+    
+    pc.printf("/%f/%f/%f/%d/%d/%d/%f/%f/%f/%f/%f/%f/%d/\r\n", 
+        xAcc, yAcc, zAcc, xMag, yMag, zMag, xLight, xTouch, 
+        accRate, magRate, lightRate, touchRate, received);
+        
 }  
 
 void receiveHandler()
 {
-    index = 0;
-    while (pc.readable() && index < bufferSize){
-        //ledgreen = !ledgreen;
-        buffer[index] = pc.getc();
-        if (buffer[index] == '#'){
+    while (pc.readable()){
+        temp = pc.getc();
+        //begin reading if char is @
+        if (temp == '@')
+        {
+            readEn = 1;
+        }
+        //stop reading if char is #
+        else if (temp == '#' && readEn == 1)
+        {
+            readEn = 0;
+            received = 1;
+            //ledred = 1;
             index = 0;
-            received = true; 
-            //ledred = !ledred;
-            break;
+            return;
         }
-        index++;
+        // if read enable is on, then read in data
+        else if (readEn == 1)
+        {
+            buffer[index] = temp;
+            index++;
+        }
     }
     return;
 }
@@ -124,36 +143,52 @@
         case '0':  
             commands = strtok(NULL, "x");
             //pc.printf("%s\r\n", commands);
-            accRate = strtod(commands, NULL)/1000;
-            timerAcc.detach();
-            timerAcc.attach(&accTime, accRate);
+            temp = strtod(commands, NULL)/1000;
+            if (temp > 0.05)
+            {
+                accRate = temp;
+                timerAcc.detach();
+                timerAcc.attach(&accTime, accRate);
+            }    
             break;
         case '1':  
             commands = strtok(NULL, "x");
             //pc.printf("%s\r\n", commands);
-            magRate = strtod(commands, NULL)/1000;
-            timerMag.detach();
-            timerMag.attach(&magTime, magRate);
+            temp = strtod(commands, NULL)/1000;
+            if (temp > 0.05)
+            {
+                magRate = temp;
+                timerMag.detach();
+                timerMag.attach(&magTime, magRate);
+            }
             break;
         case '2':  
             commands = strtok(NULL, "x");
             //pc.printf("%s\r\n", commands);
-            lightRate = strtod(commands, NULL)/1000;
-            timerLight.detach();
-            timerLight.attach(&lightTime, lightRate);
+            temp = strtod(commands, NULL)/1000;
+            if (temp > 0.05)
+            {
+                lightRate = temp;
+                timerLight.detach();
+                timerLight.attach(&lightTime, lightRate);
+            }
             break;
         case '3':  
             commands = strtok(NULL, "x");
             //pc.printf("%s\r\n", commands);
-            touchRate = strtod(commands, NULL)/1000;
-            timerTouch.detach();
-            timerTouch.attach(&touchTime, touchRate);
+            temp = strtod(commands, NULL)/1000;
+            if (temp > 0.05)
+            {
+                touchRate = temp;
+                timerTouch.detach();
+                timerTouch.attach(&touchTime, touchRate);
+            }
             break;
         default:
             //pc.printf("incorrect input\r\n");          
             break;
     }
-    received = false;
+    received = 0;
     memset(buffer, 0, bufferSize);
     //pc.printf("%s\r\n", buffer);                
 }
@@ -163,7 +198,7 @@
     xAcc = abs(acc.getAccX());
     yAcc = abs(acc.getAccY());
     zAcc = abs(acc.getAccZ());
-    //ledred = !ledred;
+    ledred = !ledred;
 }
 
 void magTime()