Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

main.cpp

Committer:
oliverfang
Date:
2014-02-06
Revision:
10:f9a1e1dd5de1
Parent:
8:b87b93a62a6a
Child:
11:7af59a3d9ac5

File content as of revision 10:f9a1e1dd5de1:

#include "mbed.h"
#include "PinDetect.h"
#include "MMA8451Q.h"
#include "MAG3110.h"
#include "TSISensor.h"

#define MMA8451_I2C_ADDRESS (0x1d<<1)

// Declare output LEDs
DigitalOut ledgreen(PTD5);
DigitalOut ledred(PTE29);

// Declare USB serial connection
Serial pc(USBTX,USBRX);

// Declare timer interrupt
Ticker timerAcc;
Ticker timerMag;
Ticker timerLight; 
Ticker timerTouch;

// Declare pointer variables
float xAcc;
float yAcc;
float zAcc;
int xMag;
int yMag;
int zMag;
float xLight; 
float xTouch; 

// Sampling rates
float accRate = 0.1;
float magRate = 0.1;
float lightRate = 0.1;
float touchRate = 0.1;

// Receiving Data
const int bufferSize = 255;
char buffer[bufferSize];
int index = 0;
int received = 0;
int readEn = 0;
float temp;

// Declare Accelerometer pins and I2C address
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0);
// Declare Magnetometer pins
MAG3110 mag(PTE25, PTE24);
// Declare touch sensor pin
TSISensor touch;
// Declare light sensor pin
AnalogIn light(PTE22);

// Functions
void init();
void printData();
void receiveHandler();
void processCommand();
void accTime();
void magTime();
void lightTime();
void touchTime();

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

int main() 
{
    // Initialize
    init();

    while(1)
    {
        __disable_irq();
        printData();
        __enable_irq();
        wait(0.05);
        if(received == 1){
            __disable_irq();    
            processCommand();
            __enable_irq();
        }
        //ledgreen = !ledgreen;
    }
}

void printData()
{
    
    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()
{
    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;
            return;
        }
        // if read enable is on, then read in data
        else if (readEn == 1)
        {
            buffer[index] = temp;
            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);
            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);
            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);
            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);
            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 = 0;
    memset(buffer, 0, bufferSize);
    //pc.printf("%s\r\n", buffer);                
}

void accTime() 
{
    xAcc = abs(acc.getAccX());
    yAcc = abs(acc.getAccY());
    zAcc = abs(acc.getAccZ());
    ledred = !ledred;
}

void magTime()
{
    xMag = mag.getXVal();
    yMag = mag.getYVal();
    zMag = mag.getZVal();
    //ledred = !ledred;
}

void lightTime()
{
    xLight = 1 - light.read();
}

void touchTime()
{
    xTouch = 1 - touch.readPercentage();
}