Part 2

Dependencies:   MMA8451Q

Revision:
4:efbc5085138a
Parent:
3:d5c746840139
Child:
5:75cd0f7649ca
--- a/main.cpp	Wed Jan 30 21:54:03 2019 +0000
+++ b/main.cpp	Tue Feb 19 17:29:20 2019 +0000
@@ -1,31 +1,144 @@
 #include "mbed.h"
 #include "MMA8451Q.h"
 
-  PinName const SDA = PTE25;
-  PinName const SCL = PTE24;
+                //  rgb (active low)
+#define Red     (0b0011)
+#define Green   (0b0101)
+#define Blue    (0b0110)
+#define Yellow  (0b0001)
+#define Cyan    (0b0100)
+#define Magenta (0b0010)
+#define White   (0b0000)
+#define Black   (0b0111)
+
+#define Xp  Red 
+#define Xn  Yellow 
+#define Yp  Green 
+#define Yn  Cyan 
+#define Zp  Blue 
+#define Zn  Magenta 
+#define IN  Black
+
+// to convert back to string
+char colours[8][8] = {
+    /*0*/"White",
+    /*1*/"Yellow",
+    /*2*/"Magenta",
+    /*3*/"Red",
+    /*4*/"Cyan",
+    /*5*/"Green",
+    /*6*/"Blue",
+    /*7*/"Black"                                                                                                                                       
+                     };
+
+/*
+assuming (x^2 + y^2 + z^2)^0.5 = 1 
+    G-> shared between 2 axes = 0.707 value
+    G-> shared between 3 axes = 0.577 value 
+    Could make threshold for orientation to be 0.9 giving reasonable margin
+*/
+#define TH 0.9 //Threshold for deciding orientation
+
+// IO declarations
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+PinName const SDA = PTE25;
+PinName const SCL = PTE24;
 
-#define MMA8451_I2C_ADDRESS (0x1d<<1)
+DigitalOut rled(LED1);
+DigitalOut gled(LED2);
+DigitalOut bled(LED3);
+
+MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+Serial pc(USBTX, USBRX); // tx, rx
+
+void led_decode(int ip){ // drives LEDs
+        rled = (ip & 0b0100);
+        gled = (ip & 0b0010);
+        bled = (ip & 0b0001);
+}
+
+void state_report(int ip){//reports on state 
+    static int old_state;
+    if(ip != old_state){
+        pc.printf("Orientation state now: %s \n\r", colours[ip]); 
+        old_state = ip;
+        }//end if
+    }//end func
+
+int orientation_find(float x, float y, float z){
+    if      (abs(x) > TH){
+        if (x > 0){
+           return(Xp);
+        }
+        else{
+            return(Xn);
+        }
+    }
+    else if (abs(y) > TH){
+       if (y > 0){
+           return(Yp);
+        }
+        else{
+            return(Yn);
+        }
+    }
+    else if (abs(z) > TH){
+       if (z > 0){
+           return(Zp);
+        }
+        else{
+            return(Zn);
+        }
+    }    
+    else {
+        return(IN);
+    }//end case 
+}//end func
 
 int main(void)
 {
-    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
-    PwmOut rled(LED1);
-    PwmOut gled(LED2);
-    PwmOut bled(LED3);
-    Serial pc(USBTX, USBRX); // tx, rx
+    int state = IN;
+    //MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    //Serial pc(USBTX, USBRX); // tx, rx
 
-
-    pc.printf("MMA8451 ID: %d\n", acc.getWhoAmI());
-
+    pc.printf("MMA8451 ID: %d\n\r", acc.getWhoAmI()); //added carriage return
+    
     while (true) {
         float x, y, z;
         x = acc.getAccX();
         y = acc.getAccY();
         z = acc.getAccZ();
-        rled = 1.0f - abs(x);
-        gled = 1.0f - abs(y);
-        bled = 1.0f - abs(z);
+        
+        state = orientation_find(x,y,z);
+        
+        led_decode(state);
+        
+        int orientation = 1; 
+        int count;
+        
+        switch(orientation){
+            case 1: //no orientation state 
+                if(state != IN){ // if state is not IN:
+                    state_report(state);
+                    orientation = 2;
+                    count = 3;            
+                }
+            break;
+            
+            case 2: //orientation state
+                state_report(state); //always report
+                if(state == IN && count > 0){  //trying to transition back
+                    count--;
+                }
+                else if(state == IN && count == 0){ //transition back to no orientation
+                    orientation = 1;    
+                }
+                else if(state != IN){ //reset as there is an orientation
+                    count = 3;
+                }
+        }// end switch
+        
         wait(0.3);
-        pc.printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
-    }
-}
+    }//end while
+}//end main