LE KICKASS TEAM / Mbed 2 deprecated RTOS_Lights_Master

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
alecselfridge
Date:
2015-12-02
Revision:
1:0a93e9e88ad3
Parent:
0:4555c427d7a8
Child:
2:f3ae10decc2f

File content as of revision 1:0a93e9e88ad3:

#include "mbed.h"
#include "rtos.h"
#include "MMA7455.h"

// thresholds for detecting a rotation of 90 deg or more
#define X_TH 60
#define Y_TH 60
#define Z_TH 0

/*
**************************
    Display Functions
**************************
*/
void displayBanner();
void newline();

/*
**************************
    Utility Functions
**************************
*/
void SerialInit();
void AccInit();
void LEDsInit();
void accFeed();
void sampleAcc(int period, int32_t data[3]);
void LEDPush(void);
void LEDS(void const *args);

/*
**************************
       Global Data
**************************
*/
const char banner[37] = "Serial Comm Established with LPC4088";

// x, y, z
// holds current position
int32_t accPos[3]           = {};
// holds up to 20 samples of data
int32_t accData[20][20][20] = {};
// holds calibration offsets
int32_t accCal[3]           = {};
// a counter for LED animations
uint8_t LEDvals = 0;

/*
**************************
         Objects
**************************
*/
// UART connection to PC
Serial terminal(USBTX, USBRX);
// accelerometer on I2C bus
MMA7455 acc(P0_27, P0_28);
// SPI interface for 8 LEDs
SPI leds(p5, NC, p7);
// load for shift register
DigitalOut ld(p30);

int main() {
    /*
    **************************
         Initializations
    **************************
    */
    SerialInit();
    AccInit();
    
    LEDsInit();
    
    RtosTimer refresh_timer(LEDS, osTimerPeriodic, (void *)0);
    refresh_timer.start(60);
    
    terminal.printf("Timer started.");    
    /*
    **************************
          Main Execution
    **************************
    */
    while(1) {
        acc.read(accPos[0], accPos[1], accPos[2]);
        LEDvals = (accPos[2]%4);   
    }
}

/*
**************************
   Function Definitions
**************************
*/
void SerialInit()
{
    // initialize connection to PC
    terminal.baud(19200);
    displayBanner();
    newline();
}

void AccInit()
{
    // configure accelerometer for 2G range
    acc.setMode(MMA7455::ModeMeasurement);
    acc.setRange(MMA7455::Range_2g);
    terminal.printf("Calibrating accelerometer...");
    
    // if we can successfully calibrate the accelerometer...
    if(acc.calibrate()) {
        newline();
        acc.getCalibrationOffsets(accCal[0], accCal[1], accCal[2]);
        terminal.printf("  Offsets are (xyz): (%d) (%d) (%d)", accCal[0], accCal[1], accCal[2]);
        newline(); newline();
    }
    else {
        terminal.printf("failed.");
        newline(); newline();
    }
}

void newline()
{
    // newline = carriage return + line feed
    terminal.putc('\n');
    terminal.putc('\r');
}

/*
    Displays the following header:
************************************************                                
Serial Comm Established with LPC4088                                            
************************************************ 
*/
void displayBanner()
{
    int i = 0;
    for(int j = 0; j < 48; j++)
        terminal.putc('*');  
    newline();
          
    while(i != 36) {
        char c = banner[i];
        terminal.putc(c);
        i++;
    }
    newline();
    
    for(int j = 0; j < 48; j++)
        terminal.putc('*');    
}

// prints the current positional data from the accelerometer
void accFeed()
{
    // returns false if the mode is set to standby or unable to convert
    if(acc.read(accPos[0], accPos[1], accPos[2])) {             
        terminal.printf("x: %d y: %d z: %d", accPos[0], accPos[1], accPos[2]);
        newline();
    }
    else {
        terminal.printf("Unable to access MMA7455");
        newline();
    }
}

/* 
    Samples the accelerometer in 1/4s intervals for the length of "period" (max 5).
    The results are placed in the 3D array. Additionally, the array passed in
    will hold the last reading.
*/
void sampleAcc(int period, int32_t data[3])
{   
    for(int i = 0; i < period*4; i++) {
        //load temps
        acc.read(data[0], data[1], data[2]);
        accData[i][0][0] = data[0]; // x
        accData[0][i][0] = data[1]; // y
        accData[0][0][i] = data[2]; // z
        wait(.25);
    }   
    // if we didn't fill the whole array, we'll clear it to avoid confusion later
    if(period < 5) {
        for(int i = period*4; i < 20; i++)
            accData[i][i][i] = 0;
    }
}

void LEDS(void const *args) 
{
    LEDPush();
}

void LEDsInit()
{
    terminal.printf("Setting format...");
    newline();
    leds.format(8, 3);
    leds.frequency(100000);
    terminal.printf("Creating timer...");
    newline();
    ld = 1;
    LEDvals = 0x00;
    LEDPush();
    LEDvals = 0xFF;
    terminal.printf("LEDvals = %d", LEDvals);
    newline();
    /*LEDvals = 0xAA;
    LEDPush();
    wait(1);
    LEDvals = 0x55;
    LEDPush();*/
    terminal.printf("Starting timer...");
    newline();
    terminal.printf("Timer started.");
}

void LEDPush(void)
{
    ld = 0;
    leds.write(LEDvals);
    ld = 1;
    terminal.printf("LEDvals = %d", LEDvals);
    newline();
}