RTOS homework 4

Dependencies:   C12832_lcd mbed

Revision:
4:67d958cc00e8
Parent:
3:8ff0c9c853be
Child:
5:faa6d8a04501
--- a/main.cpp	Sun Aug 18 15:38:00 2013 +0000
+++ b/main.cpp	Sun Aug 18 15:53:10 2013 +0000
@@ -103,7 +103,7 @@
       lUpDownHowMany = 0;
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_up(void)                           // metronome stop.
+    void ISR_left(void)                         // increase beat rate.
     {
       int  dPressedSeconds;                     // how many seconds joystick pressed.   
       int  dMultiCount;                         // slow count rate.
@@ -114,7 +114,7 @@
                                                 // calculate slow rate period.
       dMultiCount = (int) ((float) (1.0 / ((float) UDSAMPLERATE)));
             
-      if (dJoyStickUp)                           // joystick up.
+      while (dJoyStickUp)                           // joystick up.
       {
                                                 // rate-range calculations.
         dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
@@ -124,22 +124,84 @@
         else dMetroBPM += 5;
         lUpDownHowMany++;                       // joystick holddown time.
       }
+      lUpDownHowMany = 0;                  // clear when not up or down.
+      
+                                                      // saturate metronome BPM.
+      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
+      fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
+      
+      if (cDiscontinuity)                       // implement 60BPS now.
+      {
+        tickerMetronome.detach();               // only one attachment.  
+        tickerMetronome.attach(&interrupt_service_M,fMetroDelay);      
+      }
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_down(void)                         // metronome START.
+    void ISR_right(void)                        // decrease beat rate.
+    {
+      int  dPressedSeconds;                     // how many seconds joystick pressed.   
+      int  dMultiCount;                         // slow count rate.
+      char cDiscontinuity;                      // 1 = break phase & change BPM now.
+      
+      cDiscontinuity = 0;                       // don't break phase.
+      
+                                                // calculate slow rate period.
+      dMultiCount = (int) ((float) (1.0 / ((float) UDSAMPLERATE)));
+      while (dJoyStickDown)                         // joystick down.
+      {
+                                                // rate-range calculations.
+        dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
+        if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM --;}
+        else
+        if (dPressedSeconds < 10) dMetroBPM--;
+        else dMetroBPM -= 5;
+        lUpDownHowMany++;                       // joystick holddown time.
+      }
+      lUpDownHowMany = 0;                  // clear when not up or down.
+      
+                                                 // saturate metronome BPM.
+      if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
+      if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
+      
+      fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
+      
+      if (cDiscontinuity)                       // implement 60BPS now.
+      {
+        tickerMetronome.detach();               // only one attachment.  
+        tickerMetronome.attach(&interrupt_service_M,fMetroDelay);      
+      }     
+    }
+/*----------------------------------------------//----------------------------*/
+    void ISR_up(void)
     {
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_left(void)                         // increase BPM.
-    {
-    }
-/*----------------------------------------------//----------------------------*/
-    void ISR_right(void)                        // decrease BPM.
+    void ISR_down(void)
     {
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_center(void)                       // set BPM = 60.
     {
+          int  dPressedSeconds;                     // how many seconds joystick pressed.   
+      int  dMultiCount;                         // slow count rate.
+      char cDiscontinuity;                      // 1 = break phase & change BPM now.
+      
+          if (dJoyStickCenter)
+      {
+        dMetroBPM      = 60;                    // center-button -> 60BPM.
+        cDiscontinuity = 1;                     // pending phase-break.
+      }
+      
+         fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
+      
+      if (cDiscontinuity)                       // implement 60BPS now.
+      {
+        tickerMetronome.detach();               // only one attachment.  
+        tickerMetronome.attach(&interrupt_service_M,fMetroDelay);      
+      }   
+      
     }
 /*----------------------------------------------//----------------------------*/
     void lcd_display(void)                      // display metronome info.