Dependencies:   MMA8451Q

main.cpp

Committer:
duncangparker
Date:
2019-02-19
Revision:
4:efbc5085138a
Parent:
3:d5c746840139
Child:
5:75cd0f7649ca

File content as of revision 4:efbc5085138a:

#include "mbed.h"
#include "MMA8451Q.h"

                //  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;

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)
{
    int state = IN;
    //MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
    //Serial pc(USBTX, USBRX); // tx, rx

    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();
        
        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);
    }//end while
}//end main