My take on the obligatory Hello World app for the FRD-KL25Z platform. This app shows off some of the onboard peripherals. I have commented a chunk of the code, and will work to revise this to add more commenting of all sub-routines.

Dependencies:   FRDM_MMA8451Q TSI mbed

main.cpp

Committer:
milspect18
Date:
2014-06-26
Revision:
0:216148fa726d
Child:
1:6a1079e4e6ab

File content as of revision 0:216148fa726d:

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


// This is the default address as specified by the mfg
#define MMA8451_I2C_ADDRESS (0x1d<<1)


PwmOut myRedLed(LED1);
PwmOut myGreenLed(LED2);
PwmOut myBlueLed(LED3);
PwmOut externalLight(D0);

MMA8451Q accel(PTE25, PTE24, MMA8451_I2C_ADDRESS);    // 3-axis MEMS accelerometer.
TSISensor touchSlider;      // Capacitive touch sensor, defined as touchSlider.

//Function forward declarations
void AccelDemo();
void SliderFadeTimeDemo();
void TouchFadeIn(float val);
void TouchFadeOut(float val);

//
// The following prototypes are all related to the morse code blinking.
//
void morseCodeBlinker(char input[], float speed);
void SendLetter(char input, float speed);
void WordSpace(float speed);
void LetterSpace(float speed);
void Dash(float speed);
void Dot(float speed);


//Main constructor
int main()
{
    // Blink the LED's to morse code "HELLO WORLD"
    morseCodeBlinker("How are you", 0.25f);


    while (1)
    {
        // In the infinite loop of Main, we check to see if the touch slider is being touched
        // and if it is we call the touch slider demo function
        // if it is not we will be performing the accelerometer demo function
        if (touchSlider.readDistance() > 1)
        {
            SliderFadeTimeDemo();
        }
        else
        {
            AccelDemo();
        }
    }

}



void SliderFadeTimeDemo()
{
    float val = 0.00f;

    if (touchSlider.readDistance() > 2)
    {
        val = touchSlider.readDistance() * 0.5f;
        TouchFadeIn(val);
        TouchFadeOut(val);
    }
    else
    {
        if (val > 4)
        {
            TouchFadeIn(val);
            TouchFadeOut(val);
        }
        else
        {
            myRedLed = 1.00f;
            myGreenLed = 1.00f;
            myBlueLed = 1.00f;
        }
    }
}


void TouchFadeIn(float val)
{   
    for (float i = 1.00f; i > 0.00f; i -= 0.01f)
    {
        myRedLed = i;
        myGreenLed = i;
        myBlueLed = i;
        externalLight = i;
        wait_ms(val);
    }
}


void TouchFadeOut(float val)
{   
    for (float i = 0.00f; i < 1.00f; i += 0.01f)
    {
        myRedLed = i;
        myGreenLed = i;
        myBlueLed = i;
        externalLight = i;
        wait_ms(val);
    }
}



void AccelDemo()
{
    float xAxisData = 1 - abs(accel.getAccX());
    float yAxisData = 1 - abs(accel.getAccY());
    float zAxisData = 1 - abs(accel.getAccZ());

    myGreenLed = xAxisData;
    myRedLed = yAxisData;
    myBlueLed = zAxisData;

    wait_ms(100);
}


void morseCodeBlinker(char input[], float speed)
{
    for (unsigned int i = 0; i < sizeof(input); i++)
    {
        if (input[i] == ' ')
        {
            WordSpace(speed);
        }
        else
        {
            SendLetter(input[i], speed);
            LetterSpace(speed);
        }
    }
}


void SendLetter(char input, float speed)
{
    char lowerCaseLetter = tolower(input);

    switch (lowerCaseLetter)
    {
        case 'a' :
            Dot(speed);
            Dash(speed);
            break;
        case 'b' :
            Dash(speed);
            Dot(speed);
            Dot(speed);
            Dot(speed);
            break;
        case 'c' :
            Dash(speed);
            Dot(speed);
            Dash(speed);
            Dot(speed);
            break;
        case 'd' :
            Dash(speed);
            Dot(speed);
            Dot(speed);
            break;
        case 'e' :
            Dot(speed);
            break;
        case 'f' :
            Dot(speed);
            Dot(speed);
            Dash(speed);
            Dot(speed);
            break;
        case 'g' :
            Dash(speed);
            Dash(speed);
            Dot(speed);
            break;
        case 'h' :
            Dot(speed);
            Dot(speed);
            Dot(speed);
            Dot(speed);
            break;
        case 'i' :
            Dot(speed);
            Dot(speed);
            break;
        case 'j' :
            Dot(speed);
            Dash(speed);
            Dash(speed);
            Dash(speed);
            break;
        case 'k' :
            Dash(speed);
            Dot(speed);
            Dash(speed);
            break;
        case 'l' :
            Dot(speed);
            Dash(speed);
            Dot(speed);
            Dot(speed);
            break;
        case 'm' :
            Dash(speed);
            Dash(speed);
            break;
        case 'n' :
            Dash(speed);
            Dot(speed);
            break;
        case 'o' :
            Dash(speed);
            Dash(speed);
            Dash(speed);
            break;
        case 'p' :
            Dot(speed);
            Dash(speed);
            Dash(speed);
            Dot(speed);
            break;
        case 'q' :
            Dash(speed);
            Dash(speed);
            Dot(speed);
            Dash(speed);
            break;
        case 'r' :
            Dot(speed);
            Dash(speed);
            Dot(speed);
            break;
        case 's' :
            Dot(speed);
            Dot(speed);
            Dot(speed);
            break;
        case 't' :
            Dash(speed);
            break;
        case 'u' :
            Dot(speed);
            Dot(speed);
            Dash(speed);
            break;
        case 'v' :
            Dot(speed);
            Dot(speed);
            Dot(speed);
            Dash(speed);
            break;
        case 'w' :
            Dot(speed);
            Dash(speed);
            Dash(speed);
            break;
        case 'x' :
            Dash(speed);
            Dot(speed);
            Dot(speed);
            Dash(speed);
            break;
        case 'y' :
            Dash(speed);
            Dot(speed);
            Dash(speed);
            Dash(speed);
            break;
        case 'z' :
            Dash(speed);
            Dash(speed);
            Dot(speed);
            Dot(speed);
            break;
    }
}

void WordSpace(float speed)
{
    wait(speed * 7);
}

void LetterSpace(float speed)
{
    wait(speed * 3);
}

void Dash(float speed)
{
    myRedLed = 0;
    myGreenLed = 0;
    myBlueLed = 0;
    wait(speed * 3);
    myRedLed = 1;
    myGreenLed = 1;
    myBlueLed = 1;
    wait(speed);
}

void Dot(float speed)
{
    myRedLed = 0;
    myGreenLed = 0;
    myBlueLed = 0;
    wait(speed);
    myRedLed = 1;
    myGreenLed = 1;
    myBlueLed = 1;
    wait(speed);
}