Assignment 3 and 4. Both in commits.

Dependencies:   MODSERIAL mbed

Committer:
vsluiter
Date:
Fri Sep 04 07:26:58 2015 +0000
Revision:
2:ae67659d1fca
Parent:
1:958fbf7ad850
Child:
3:b05acad8736e
Assignment4, with enums to make code more readable.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:86aaef96170e 1 #include "mbed.h"
vsluiter 0:86aaef96170e 2 #include "MODSERIAL.h"
vsluiter 0:86aaef96170e 3
vsluiter 1:958fbf7ad850 4 DigitalOut r_led(LED_RED);
vsluiter 1:958fbf7ad850 5 DigitalOut g_led(LED_GREEN);
vsluiter 1:958fbf7ad850 6 DigitalOut b_led(LED_BLUE);
vsluiter 1:958fbf7ad850 7
vsluiter 0:86aaef96170e 8 DigitalIn button(PTA4);
vsluiter 0:86aaef96170e 9 MODSERIAL pc(USBTX, USBRX);
vsluiter 0:86aaef96170e 10
vsluiter 0:86aaef96170e 11 const int baudrate = 115200;
vsluiter 1:958fbf7ad850 12 //const int ms_wait = 100;
vsluiter 0:86aaef96170e 13
vsluiter 1:958fbf7ad850 14 const float period_led = 0.2;
vsluiter 0:86aaef96170e 15
vsluiter 0:86aaef96170e 16 const int led_on = 0;
vsluiter 0:86aaef96170e 17 const int led_off = 1;
vsluiter 0:86aaef96170e 18
vsluiter 0:86aaef96170e 19
vsluiter 1:958fbf7ad850 20 bool blink_red = false;
vsluiter 1:958fbf7ad850 21 bool blink_green = false;
vsluiter 1:958fbf7ad850 22 bool blink_blue = false;
vsluiter 0:86aaef96170e 23
vsluiter 2:ae67659d1fca 24 //with an enum, you can assign integer values to a name. You don't have to
vsluiter 2:ae67659d1fca 25 //remember the numbers, just remember the names.
vsluiter 2:ae67659d1fca 26 //This is very handy if you want to use this with switch statements.
vsluiter 2:ae67659d1fca 27 //If you compare this to the previous version you'll see
vsluiter 2:ae67659d1fca 28 //that this code is much more readable.
vsluiter 2:ae67659d1fca 29
vsluiter 2:ae67659d1fca 30 //first part is creating a new datatype, enum blinkstate
vsluiter 2:ae67659d1fca 31 //second part is defining the names between {}
vsluiter 2:ae67659d1fca 32 //third part is creating a variable of the enum blinkstate type.
vsluiter 2:ae67659d1fca 33 enum blinkstates {BLINKRED,BLINKGREEN,BLINKBLUE,BLINKNONE} blink_state;
vsluiter 2:ae67659d1fca 34
vsluiter 2:ae67659d1fca 35
vsluiter 2:ae67659d1fca 36 //function to flip one LED
vsluiter 1:958fbf7ad850 37 void flip1led(DigitalOut& led)
vsluiter 0:86aaef96170e 38 {
vsluiter 2:ae67659d1fca 39 led.write(!led.read());
vsluiter 1:958fbf7ad850 40 }
vsluiter 1:958fbf7ad850 41
vsluiter 1:958fbf7ad850 42 void blink3Leds()
vsluiter 1:958fbf7ad850 43 {
vsluiter 2:ae67659d1fca 44 //use the enum value!! This is set from the main loop.
vsluiter 2:ae67659d1fca 45 switch(blink_state) {
vsluiter 2:ae67659d1fca 46 case BLINKRED:
vsluiter 1:958fbf7ad850 47 flip1led(r_led);
vsluiter 2:ae67659d1fca 48 g_led.write(led_off);
vsluiter 2:ae67659d1fca 49 b_led.write(led_off);
vsluiter 2:ae67659d1fca 50 break;
vsluiter 2:ae67659d1fca 51 case BLINKGREEN:
vsluiter 1:958fbf7ad850 52 flip1led(g_led);
vsluiter 2:ae67659d1fca 53 r_led.write(led_off);
vsluiter 2:ae67659d1fca 54 b_led.write(led_off);
vsluiter 2:ae67659d1fca 55 break;
vsluiter 2:ae67659d1fca 56 case BLINKBLUE:
vsluiter 2:ae67659d1fca 57 flip1led(b_led);
vsluiter 2:ae67659d1fca 58 g_led.write(led_off);
vsluiter 2:ae67659d1fca 59 r_led.write(led_off);
vsluiter 2:ae67659d1fca 60 break;
vsluiter 2:ae67659d1fca 61 case BLINKNONE:
vsluiter 2:ae67659d1fca 62 r_led.write(led_off);
vsluiter 2:ae67659d1fca 63 g_led.write(led_off);
vsluiter 2:ae67659d1fca 64 b_led.write(led_off);
vsluiter 2:ae67659d1fca 65 break;
vsluiter 2:ae67659d1fca 66 }
vsluiter 0:86aaef96170e 67 }
vsluiter 0:86aaef96170e 68
vsluiter 0:86aaef96170e 69 int main()
vsluiter 0:86aaef96170e 70 {
vsluiter 1:958fbf7ad850 71 Ticker ledtick;
vsluiter 2:ae67659d1fca 72 blink_state = BLINKNONE;//initialize
vsluiter 1:958fbf7ad850 73 ledtick.attach(blink3Leds, period_led/2);
vsluiter 1:958fbf7ad850 74
vsluiter 1:958fbf7ad850 75 r_led.write(led_off);
vsluiter 1:958fbf7ad850 76 g_led.write(led_off);
vsluiter 1:958fbf7ad850 77 b_led.write(led_off);
vsluiter 0:86aaef96170e 78 pc.baud(baudrate);
vsluiter 0:86aaef96170e 79 pc.printf("Hello World!\n");
vsluiter 2:ae67659d1fca 80
vsluiter 0:86aaef96170e 81 while (true) {
vsluiter 2:ae67659d1fca 82 if(pc.readable()) { //if character available. If expresseion is non-zero, it's true
vsluiter 2:ae67659d1fca 83 switch(pc.getc()) { //read a character
vsluiter 1:958fbf7ad850 84 case 'r':
vsluiter 2:ae67659d1fca 85 blink_state = BLINKRED;//using the enum, we can just use the names defined above. This also makes it easier to extend functionality
vsluiter 1:958fbf7ad850 86 break;
vsluiter 1:958fbf7ad850 87 case 'g':
vsluiter 2:ae67659d1fca 88 blink_state = BLINKGREEN;
vsluiter 1:958fbf7ad850 89 break;
vsluiter 1:958fbf7ad850 90 case 'b':
vsluiter 2:ae67659d1fca 91 blink_state = BLINKBLUE;
vsluiter 1:958fbf7ad850 92 break;
vsluiter 1:958fbf7ad850 93 default:
vsluiter 2:ae67659d1fca 94 blink_state = BLINKNONE;
vsluiter 1:958fbf7ad850 95 pc.printf("only r g b allowed.\n");
vsluiter 1:958fbf7ad850 96 }
vsluiter 0:86aaef96170e 97 }
vsluiter 0:86aaef96170e 98 }
vsluiter 0:86aaef96170e 99 }