RTOS homework 4

Dependencies:   C12832_lcd mbed

Revision:
12:fab8e658ae76
Parent:
11:e764ed79553a
Child:
13:d380d4ec3c5d
--- a/main.cpp	Sun Aug 18 17:40:11 2013 +0000
+++ b/main.cpp	Sun Aug 18 18:03:10 2013 +0000
@@ -50,7 +50,7 @@
     float fMetroDuty;                           // duration of metro high, in seconds.
     int   dMetroBPM;                            // master parameter.
     long  lUpDownHowMany;                       // count how long up/down joystick pressed.
-    char  cMutex;                               // debounce mutex.
+    char  cMetronomeOn;                         // 1 = allow blink.
 //--global_instances----------------------------//------------------------------ 
     C12832_LCD  lcd;                            // LCD object.
     
@@ -72,6 +72,7 @@
     Ticker      tickerJoystickUD;               // joystick up/down sample.
     Ticker      tickerLCD;                      // display ticker.
     Timeout     timeoutDutyCycle;               // LED duty cycle delay.
+    Timeout     timeoutMetronome;
 //-------prototypes-----------------------------//------------------------------
     void initialization();                      // initialize settings.
     void lcd_display();                         // display on LCD.
@@ -86,6 +87,7 @@
     void ISR_left_rising();
     void ISR_left_falling();
     void ISR_center();
+    void turn_off_metronome();
 //==============================================//==============================
     int main(void) 
     {
@@ -122,7 +124,7 @@
       fMetroDelay    = 60.0 / (float) (dMetroBPM);
       fMetroDuty     = PULSELENGTH;             // initialize LED on-duration.
       lUpDownHowMany = 0;
-      cMutex         = 1;
+      cMetronomeOn   = 1;
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_left_rising(void)                  // increase BPM.
@@ -131,13 +133,13 @@
 
       dMetroBPM++;                              // increase BPM.
       
+                                                // saturate metronome BPM.
+      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
       wait(DEBOUNCE);                           // debounce time.
       
       __enable_irq();
-      
-                                                // saturate metronome BPM.
-      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
-      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_left_falling(void)                 // ignore rising after falling edge.
@@ -156,12 +158,14 @@
 
       dMetroBPM--;                              // decrease BPM.
       
+                                                // saturate metronome BPM.
+      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
       wait(DEBOUNCE);                           // debounce time.
       
       __enable_irq();
-                                                 // saturate metronome BPM.
-      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
-      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_right_falling(void)                // ignore rising after falling edge.
@@ -179,6 +183,9 @@
 /*----------------------------------------------//----------------------------*/
     void ISR_down(void)
     {
+      cMetronomeOn = 1;
+      timeoutMetronome.detach();
+      timeoutMetronome.attach(&turn_off_metronome,10.0);
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_center(void)                       // set BPM = 60.
@@ -215,10 +222,18 @@
 
     void interrupt_service_M()                  // metronome tick.
     {        
-      tickerMetronome.detach();                 // only one attachment.  
-      tickerMetronome.attach(&interrupt_service_M,fMetroDelay);      
-      led3 = 1;
-      timeoutDutyCycle.attach(&led3_off,fMetroDuty);
+      if (cMetronomeOn)
+      {
+        tickerMetronome.detach();                 // only one attachment.  
+        tickerMetronome.attach(&interrupt_service_M,fMetroDelay);      
+        led3_on();
+        timeoutDutyCycle.attach(&led3_off,fMetroDuty);
+      } else led3_off();
+    }
+/*----------------------------------------------//----------------------------*/
+    void turn_off_metronome(void)
+    {
+      cMetronomeOn = 0;
     }
 /*----------------------------------------------//----------------------------*/
 //  this routine measures the number of seconds for which the joystick is