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 06:10:19 2014 +0000
Parent:
4:0d2eefc2be8e
Child:
9:90567e22c5e1
Child:
10:f9a1e1dd5de1
Commit message:
updated to transmit all data and read in commands from pyserial; currently unreliable serial communication; working on fix

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Feb 03 18:28:32 2014 +0000
+++ b/main.cpp	Thu Feb 06 06:10:19 2014 +0000
@@ -35,6 +35,11 @@
 float lightRate = 0.1;
 float touchRate = 0.1;
 
+// Receiving Data
+const int bufferSize = 255;
+char buffer[bufferSize];
+int index = 0;
+bool received = false;
 
 // Declare Accelerometer pins and I2C address
 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0);
@@ -48,6 +53,8 @@
 // Functions
 void init();
 void printData();
+void receiveHandler();
+void processCommand();
 void accTime();
 void magTime();
 void lightTime();
@@ -56,10 +63,12 @@
 void init()
 {
     // Attach timerAcc
+    //pc.baud(9600);
     timerAcc.attach(&accTime, accRate);
     timerMag.attach(&magTime, magRate);
     timerLight.attach(&lightTime, lightRate);
     timerTouch.attach(&touchTime, touchRate);
+    pc.attach(&receiveHandler, Serial::RxIrq);
     ledred = 0; 
     ledgreen = 0;   
 }
@@ -72,25 +81,89 @@
     while(1)
     {
         printData();
-        wait(0.1);
+        wait(0.05);
+        if(received){
+            __disable_irq();    
+            processCommand();
+            __enable_irq();
+        }
+        //ledgreen = !ledgreen;
     }
 }
 
 void printData()
 {
-    // Read and print data from accelerometer
-    pc.printf("/%f/%f/%f/", xAcc, yAcc, zAcc);
-    // Read data from magnetometer
-    pc.printf("%d/%d/%d/", xMag, yMag, zMag);
-    pc.printf("%f/", xLight);
-    pc.printf("%f/\r\n", xTouch);
-}        
+    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);
+}  
+
+void receiveHandler()
+{
+    index = 0;
+    while (pc.readable() && index < bufferSize){
+        //ledgreen = !ledgreen;
+        buffer[index] = pc.getc();
+        if (buffer[index] == '#'){
+            index = 0;
+            received = true; 
+            //ledred = !ledred;
+            break;
+        }
+        index++;
+    }
+    return;
+}
+
+void processCommand()
+{
+    //pc.printf("%s\r\n", buffer);
+    char* commands;
+    commands = strtok(buffer, "x");
+    //pc.printf("%s\r\n", commands);
+    switch(*commands)
+    {
+        case '0':  
+            commands = strtok(NULL, "x");
+            //pc.printf("%s\r\n", commands);
+            accRate = strtod(commands, NULL)/1000;
+            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);
+            break;
+        case '2':  
+            commands = strtok(NULL, "x");
+            //pc.printf("%s\r\n", commands);
+            lightRate = strtod(commands, NULL)/1000;
+            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);
+            break;
+        default:
+            //pc.printf("incorrect input\r\n");          
+            break;
+    }
+    received = false;
+    memset(buffer, 0, bufferSize);
+    //pc.printf("%s\r\n", buffer);                
+}
+
 void accTime() 
 {
     xAcc = abs(acc.getAccX());
     yAcc = abs(acc.getAccY());
     zAcc = abs(acc.getAccZ());
-    ledgreen = !ledgreen;
+    //ledred = !ledred;
 }
 
 void magTime()
@@ -98,7 +171,7 @@
     xMag = mag.getXVal();
     yMag = mag.getYVal();
     zMag = mag.getZVal();
-    ledred = !ledred;
+    //ledred = !ledred;
 }
 
 void lightTime()
@@ -109,4 +182,4 @@
 void touchTime()
 {
     xTouch = 1 - touch.readPercentage();
-}
\ No newline at end of file
+}