Assignment 3 and 4. Both in commits.

Dependencies:   MODSERIAL mbed

Revision:
1:958fbf7ad850
Parent:
0:86aaef96170e
Child:
2:ae67659d1fca
--- a/main.cpp	Wed Sep 02 12:33:55 2015 +0000
+++ b/main.cpp	Wed Sep 02 12:57:47 2015 +0000
@@ -1,55 +1,81 @@
 #include "mbed.h"
 #include "MODSERIAL.h"
 
-DigitalOut led(LED_GREEN);
+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 int ms_wait = 100;
 
-const float period_led = 0.1;
-const float t_on = 0.1;
-const float t_off = period_led - t_on;
+const float period_led = 0.2;
 
 const int led_on = 0;
 const int led_off = 1;
 
-const int button_pressed = 0;
 
+bool blink_red = false;
+bool blink_green = false;
+bool blink_blue  = false;
 
-void blink(int number_of_blinks)
+void flip1led(DigitalOut& led)
 {
-    for(int i = 0; i < 5 ; i++) {
-        led.write(led_on); //turn led on
-        wait(t_on);
-        led.write(led_off); // toggle led
-        wait(t_off);
-    }
+    led = !led;
+}
+
+void blink3Leds()
+{
+        if(blink_red)
+            flip1led(r_led);
+        if(blink_green)
+            flip1led(g_led);
+        if(blink_blue)
+            flip1led(b_led);       
 }
 
 int main()
 {
-    int waittime = 0;
-    led.write(led_off);
+    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) {
-        wait_ms(ms_wait); //const value
-        if(button.read() == button_pressed) //button pressed
-        {
-            waittime++;
-            pc.printf("Pressed for %d ms\n", waittime * ms_wait);  //const used again -> updating it at the top gives consistent results!!
-        }
-        else //button released
-        {
-            waittime = 0;
-        }
         if(pc.readable()) //if character available. If expresseion is non-zero, it's true
         {
-            pc.rxBufferFlush();    //flush the buffer. Otherwise pc.readable() will stay larger than zero
-            blink(1);
+            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");
+            }
         }
     }
 }