RTOS homework 4

Dependencies:   C12832_lcd mbed

Files at this revision

API Documentation at this revision

Comitter:
gatedClock
Date:
Sun Aug 18 17:05:30 2013 +0000
Parent:
8:95f4f470ae28
Child:
10:4f2fa66cc430
Commit message:
mutex not completely effective for debounce.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Aug 18 16:42:56 2013 +0000
+++ b/main.cpp	Sun Aug 18 17:05:30 2013 +0000
@@ -49,6 +49,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.
 //--global_instances----------------------------//------------------------------ 
     C12832_LCD  lcd;                            // LCD object.
     
@@ -113,24 +114,46 @@
       fMetroDelay    = 60.0 / (float) (dMetroBPM);
       fMetroDuty     = PULSELENGTH;             // initialize LED on-duration.
       lUpDownHowMany = 0;
+      cMutex         = 1;
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_left(void)                         // increase BPM.
     {
-      dMetroBPM++;
+      if (cMutex)                               // grab the mutex.
+      {
+        cMutex = 0;                             // disqualify any bounce.
+        
+        dMetroBPM++;                            // increase BPM.
       
-                                                      // saturate metronome BPM.
-      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
-      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+                                                // saturate metronome BPM.
+        if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+        if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
+        wait(0.01);                             // debounce time.
+        
+        cMutex = 1;                             // allow critical section.
+      }
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_right(void)                        // decrease beat rate.
+//  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.
     {
-      dMetroBPM--;
+      if (cMutex)                               // grab the mutex.
+      {
+        cMutex = 0;                             // disqualify any bounce.
+        
+        dMetroBPM--;                            // decrease BPM.
       
-                                                      // saturate metronome BPM.
-      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
-      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+                                                // saturate metronome BPM.
+        if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+        if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
+        wait(0.01);                             // debounce time.
+        
+        cMutex = 1;                             // allow critical section.
+      }
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_up(void)