Assignment 3 and 4. Both in commits.

Dependencies:   MODSERIAL mbed

main.cpp

Committer:
vsluiter
Date:
2015-09-02
Revision:
1:958fbf7ad850
Parent:
0:86aaef96170e
Child:
2:ae67659d1fca

File content as of revision 1:958fbf7ad850:

#include "mbed.h"
#include "MODSERIAL.h"

DigitalOut r_led(LED_RED);
DigitalOut g_led(LED_GREEN);
DigitalOut b_led(LED_BLUE);

DigitalIn button(PTA4);
MODSERIAL pc(USBTX, USBRX);

const int baudrate = 115200;
//const int ms_wait = 100;

const float period_led = 0.2;

const int led_on = 0;
const int led_off = 1;


bool blink_red = false;
bool blink_green = false;
bool blink_blue  = false;

void flip1led(DigitalOut& led)
{
    led = !led;
}

void blink3Leds()
{
        if(blink_red)
            flip1led(r_led);
        if(blink_green)
            flip1led(g_led);
        if(blink_blue)
            flip1led(b_led);       
}

int main()
{
    Ticker ledtick;
    ledtick.attach(blink3Leds, period_led/2);

    r_led.write(led_off);
    g_led.write(led_off);
    b_led.write(led_off);
    pc.baud(baudrate);
    pc.printf("Hello World!\n");
    
    while (true) {
        if(pc.readable()) //if character available. If expresseion is non-zero, it's true
        {
            switch(pc.getc())    //read a character
            {
                case 'r':
                    blink_red = true;
                    blink_green = false;
                    blink_blue = false;
                    g_led.write(led_off);
                    b_led.write(led_off);
                    break;
                case 'g':
                    blink_red = false;
                    blink_green = true;
                    blink_blue = false;
                    r_led.write(led_off);
                    b_led.write(led_off);
                    break;
                case 'b':
                    blink_red = false;
                    blink_green = false;
                    blink_blue = true;
                    g_led.write(led_off);
                    r_led.write(led_off);
                    break;
                default:
                    pc.printf("only r g b allowed.\n");
            }
        }
    }
}