RTOS homework 4

Dependencies:   C12832_lcd mbed

Files at this revision

API Documentation at this revision

Comitter:
gatedClock
Date:
Sun Aug 18 19:35:56 2013 +0000
Parent:
17:cd6c76be8046
Child:
19:0db1451d19ef
Commit message:
neatening.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Aug 18 19:26:43 2013 +0000
+++ b/main.cpp	Sun Aug 18 19:35:56 2013 +0000
@@ -7,12 +7,7 @@
     Joystick-Controlled Metronome
     
     features:
-    1. post-initialization functionality all timer-driven.
-    2. IRSs contain no blocking functions.
-    3. LED shows the metronome beat.
-    4. metronome speed controlled with up/down joystick.
-    5. rate-of-speed-change depends on how long the up/down has been active.
-    6. Beat-Per-Minute (BPM) shown on LCD display.
+
     
     controls:
     1. joystick-up     - increase metronome rate.
@@ -22,13 +17,7 @@
     notes:
     
     testing:
-    1. confirm ease of being able to adjust one BPM rate with joystick up/down.
-    2. confirm three joystick up/down change rates, if keeping the stick pressed.
-    3. confirm max/min BPS saturation & display.
-    4. confirm joystick up/down control works to change display & LED BPM.
-    5. confirm joystick center-press sets rate to 60BPM immediately.
-    6. confirm long-test does not result in a crash.
-       -> all items confirmed.
+
 
 -----includes-----------------------------------//----------------------------*/
     #include "mbed.h"                           // mbed class.
@@ -54,19 +43,13 @@
     long  lUpDownHowMany;                       // count how long up/down joystick pressed.
     char  cMetronomeOn;                         // 1 = allow blink.
 //--global_instances----------------------------//------------------------------ 
-    C12832_LCD  lcd;                            // LCD object.
+    C12832_LCD   lcd;                           // LCD object.
     
-    InterruptIn   iJoyStickUp    (p15);         // joystick up rising edge.
-    InterruptIn   iJoyStickDown  (p12);         // joystick down rising edge.
-    InterruptIn   iJoyStickLeft  (p13);         // joystick left rising edge.
-    InterruptIn   iJoyStickRight (p16);         // joystick right rising edge.
-    InterruptIn   iJoyStickCenter(p14);         // 1 if joystick middle pressed.
-    
-    DigitalIn     dJoyStickUp    (p15);         // joystick up sample.
-    DigitalIn     dJoyStickDown  (p12);         // joystick down sample.
-    DigitalIn     dJoyStickLeft  (p13);         // joystick left sample.
-    DigitalIn     dJoyStickRight (p16);         // joystick right sample.
-    DigitalIn     dJoyStickCenter(p14);         // joystick center sample.
+    InterruptIn  iJoyStickUp    (p15);          // joystick up rising edge.
+    InterruptIn  iJoyStickDown  (p12);          // joystick down rising edge.
+    InterruptIn  iJoyStickLeft  (p13);          // joystick left rising edge.
+    InterruptIn  iJoyStickRight (p16);          // joystick right rising edge.
+    InterruptIn  iJoyStickCenter(p14);          // 1 if joystick middle pressed.
 
     DigitalOut  led3(LED1);                     // leftmost LED.
     
@@ -78,46 +61,43 @@
 //-------prototypes-----------------------------//------------------------------
     void initialization();                      // initialize settings.
     void lcd_display();                         // display on LCD.
-    void interrupt_service_M();                 // metronome tick.
-    void interrupt_service_UD();                // joystick up/down sample.                
+    void interrupt_service_M();                 // metronome tick.                
     void led3_off();                            // attachable LED control.
     void led3_on();                             // attachable LED control.
-    void ISR_up();
-    void ISR_down();
-    void ISR_right_rising();
-    void ISR_right_falling();
-    void ISR_left_rising();
-    void ISR_left_falling();
-    void ISR_center();
-    void turn_off_metronome();
+    void ISR_up();                              // stop metronome.
+    void ISR_down();                            // start metronome.
+    void ISR_right_rising();                    // decrease BPM.
+    void ISR_right_falling();                   // bounce protection.
+    void ISR_left_rising();                     // increase BPM.
+    void ISR_left_falling();                    // bounce protection.
+    void ISR_center();                          // set to 60BPM.
+    void turn_off_metronome();                  // turn off blinker.
 //==============================================//==============================
     int main(void) 
     {
-      iJoyStickUp.rise(&ISR_up);    
-      iJoyStickDown.rise(&ISR_down);
+      iJoyStickUp.rise   (&ISR_up);             // metronome stop.
+      iJoyStickDown.rise (&ISR_down);           // metronome start.
       
-      iJoyStickLeft.rise(&ISR_left_rising);
-      iJoyStickLeft.fall(&ISR_left_falling);
+      iJoyStickLeft.rise (&ISR_left_rising);    // increase BPM.
+      iJoyStickLeft.fall (&ISR_left_falling);   // anti-bounce.
       
-      iJoyStickRight.rise(&ISR_right_rising);
-      iJoyStickRight.fall(&ISR_right_falling);
+      iJoyStickRight.rise(&ISR_right_rising);   // decrease BPM.
+      iJoyStickRight.fall(&ISR_right_falling);  // anti-bounce.
       
-      iJoyStickCenter.rise(&ISR_center);
+      iJoyStickCenter.rise(&ISR_center);        // 60BPM.
       
-      initialization();
+      initialization();                         // initialize variables.
       
-                                                      // metronome ticker.
+                                                // metronome ticker.
       tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
       
                                                 // LCD ticker.
       tickerLCD.attach(&lcd_display,LCDSAMPLERATE);  
       
-      while(1)
+      while(1)                                  // all timer/interrupt driven.
       {
-      
         wait(10.0);
       }
-
     }       
 /*----------------------------------------------//----------------------------*/
     void initialization(void)                   // program initializations.
@@ -125,13 +105,13 @@
       dMetroBPM      = 60;                      // initialize to 60BPM.      
       fMetroDelay    = 60.0 / (float) (dMetroBPM);
       fMetroDuty     = PULSELENGTH;             // initialize LED on-duration.
-      lUpDownHowMany = 0;
+ //     lUpDownHowMany = 0;
       cMetronomeOn   = 0;
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_left_rising(void)                  // increase BPM.
     {
-      __disable_irq();
+      __disable_irq();                          // anti-bounce.
 
       dMetroBPM++;                              // increase BPM.
       
@@ -141,22 +121,21 @@
       
       wait(DEBOUNCE);                           // debounce time.
       
-      __enable_irq();
+      __enable_irq();                           // safe by now.
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_left_falling(void)                 // ignore rising after falling edge.
     {
-      __disable_irq();
+      __disable_irq();                          // anti-bounce.
       
       wait(DEBOUNCE);                           // debounce time.
       
-      __enable_irq();
+      __enable_irq();                           // safe by now.
     }
 /*----------------------------------------------//----------------------------*/
-
     void ISR_right_rising(void)                 // decrease BPM.
     {
-      __disable_irq();
+      __disable_irq();                          // anti-bounce.
 
       dMetroBPM--;                              // decrease BPM.
       
@@ -166,25 +145,24 @@
       
       wait(DEBOUNCE);                           // debounce time.
       
-      __enable_irq();
-
+      __enable_irq();                           // safe by now.
     }
 /*----------------------------------------------//----------------------------*/
     void ISR_right_falling(void)                // ignore rising after falling edge.
     {
-      __disable_irq();
+      __disable_irq();                          // anti-bounce.
       
       wait(DEBOUNCE);                           // debounce time.
       
-      __enable_irq();
+      __enable_irq();                           // safe by now.
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_up(void)
+    void ISR_up(void)                           // turn off metronome.
     {
       cMetronomeOn = 0;
     }
 /*----------------------------------------------//----------------------------*/
-    void ISR_down(void)
+    void ISR_down(void)                         // metronome on with timeout.
     {
       cMetronomeOn = 1;
       timeoutMetronome.detach();
@@ -194,7 +172,6 @@
     void ISR_center(void)                       // set BPM = 60.
     {
       dMetroBPM = 60;
-      
     }
 /*----------------------------------------------//----------------------------*/
     void lcd_display(void)                      // display metronome info.
@@ -231,78 +208,18 @@
     {        
       if (cMetronomeOn)
       {
-        tickerMetronome.detach();                 // only one attachment.  
+        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)
+    void turn_off_metronome(void)               // turn off metronome.
     {
       cMetronomeOn = 0;
     }
 /*----------------------------------------------//----------------------------*/
-//  this routine measures the number of seconds for which the joystick is
-//  in the up or down position.  for three ranges of time, three different
-//  BPM rates-of-change are used.  This means that as the user controls the
-//  metronome rate using the joystick, at first it will change slowly, then
-//  it will change at a moderate speed, then it will change quickly.
-//  additionally, pressing the center joystick button will bring the metronome
-//  back to 60BPM immediately, breaking BPM phase continuity.
-    void interrupt_service_UD(void)             // joystick up/down sample
-    {   
-      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)));
-            
-      if (dJoyStickUp)                           // joystick up.
-      {
-                                                // 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.
-      }
-      else
-      if (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.
-      }
-      else lUpDownHowMany = 0;                  // clear when not up or down.
-      
-      if (dJoyStickCenter)
-      {
-        dMetroBPM      = 60;                    // center-button -> 60BPM.
-        cDiscontinuity = 1;                     // pending phase-break.
-      }
-                                                // 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 led3_off(void) {led3 = 0;}             // turn off the LED.
 /*----------------------------------------------//----------------------------*/
     void led3_on( void) {led3 = 1;}             // turn on the led.