RTOS homework 4

Dependencies:   C12832_lcd mbed

Revision:
10:4f2fa66cc430
Parent:
9:ea97a69b9b93
Child:
11:e764ed79553a
--- a/main.cpp	Sun Aug 18 17:05:30 2013 +0000
+++ b/main.cpp	Sun Aug 18 17:34:54 2013 +0000
@@ -43,6 +43,7 @@
     #define UDSAMPLERATE     0.1                // how often to sample U/D joystick.
     #define LCDSAMPLERATE    0.1                // how often to redraw the LCD.
     #define PULSELENGTH      0.0625             // how long the LED-on-time is.
+    #define DEBOUNCE         0.16               // debounce pause duration in S.
 //--global_definitions--------------------------//------------------------------
 //--global_variables----------------------------//------------------------------ 
     float fMetroDelay;                          // time between ticks, in seconds.
@@ -80,7 +81,8 @@
     void led3_on();                             // attachable LED control.
     void ISR_up();
     void ISR_down();
-    void ISR_right();
+    void ISR_right_rising();
+    void ISR_right_falling();
     void ISR_left();
     void ISR_center();
 //==============================================//==============================
@@ -89,7 +91,10 @@
       iJoyStickUp.rise(&ISR_up);    
       iJoyStickDown.rise(&ISR_down);
       iJoyStickLeft.rise(&ISR_left);
-      iJoyStickRight.rise(&ISR_right);
+      
+      iJoyStickRight.rise(&ISR_right_rising);
+      iJoyStickRight.fall(&ISR_right_falling);
+      
       iJoyStickCenter.rise(&ISR_center);
       
       initialization();
@@ -135,25 +140,28 @@
       }
     }
 /*----------------------------------------------//----------------------------*/
-//  using a mutex to squelch this ISR from getting hammered
-//  by switch bounce.  but: could a bounce interrupt sneak in between
-//  the 'if mutex' and 'mutex = 0' statements?
-    void ISR_right(void)                        // decrease BPM.
+    void ISR_right_rising(void)                 // decrease BPM.
     {
-      if (cMutex)                               // grab the mutex.
-      {
-        cMutex = 0;                             // disqualify any bounce.
-        
-        dMetroBPM--;                            // decrease BPM.
+      __disable_irq();
+
+      dMetroBPM--;                              // decrease BPM.
       
                                                 // saturate metronome BPM.
-        if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
-        if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
+      wait(DEBOUNCE);                           // debounce time.
       
-        wait(0.01);                             // debounce time.
-        
-        cMutex = 1;                             // allow critical section.
-      }
+      __enable_irq();
+    }
+/*----------------------------------------------//----------------------------*/
+    void ISR_right_falling(void)                // ignore rising after falling edge.
+    {
+      __disable_irq();
+      
+      wait(DEBOUNCE);                           // debounce time.
+      
+      __enable_irq();
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_up(void)