RTOS homework 4

Dependencies:   C12832_lcd mbed

Committer:
gatedClock
Date:
Fri Sep 13 04:02:55 2013 +0000
Revision:
22:5d9bcc7f4440
Parent:
21:eb692e90ae8d
Child:
23:99a13f79cdad
phy is powered down ok.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gatedClock 0:1013288b8e43 1 /*----------------------------------------------//------------------------------
gatedClock 0:1013288b8e43 2 student : m-moore
gatedClock 0:1013288b8e43 3 class : rtos
gatedClock 1:5f41e2df0e85 4 directory : RTOS_HW_04
gatedClock 0:1013288b8e43 5 file : main.cpp
gatedClock 0:1013288b8e43 6 ----description---------------------------------//------------------------------
gatedClock 0:1013288b8e43 7 Joystick-Controlled Metronome
gatedClock 0:1013288b8e43 8
gatedClock 0:1013288b8e43 9 features:
gatedClock 19:0db1451d19ef 10 1. joystick-controlled.
gatedClock 19:0db1451d19ef 11 2. turns off after one minute.
gatedClock 19:0db1451d19ef 12 3. ISR's for all joystick inputs.
gatedClock 19:0db1451d19ef 13 4. joystick debounce.
gatedClock 19:0db1451d19ef 14 5. LED and LCD displays.
gatedClock 19:0db1451d19ef 15 6. BPM rate adjustable even when metronome is off.
gatedClock 19:0db1451d19ef 16 7. BPM saturates at defined lower/upper bounds.
gatedClock 18:f19721f1069e 17
gatedClock 0:1013288b8e43 18 controls:
gatedClock 19:0db1451d19ef 19 1. joystick-left - increase metronome rate.
gatedClock 19:0db1451d19ef 20 2. joystick-right - decrease metronome rate.
gatedClock 19:0db1451d19ef 21 3. joystick-down - start metronome.
gatedClock 19:0db1451d19ef 22 4. joystick-up - stop metronome.
gatedClock 19:0db1451d19ef 23 5. joystick-center - set metronome to 60BPM.
gatedClock 0:1013288b8e43 24
gatedClock 0:1013288b8e43 25 notes:
gatedClock 19:0db1451d19ef 26 1. adding the mbed-rtos library to this project prevents the
gatedClock 19:0db1451d19ef 27 LED from blinking and the LCD from displaying, even if
gatedClock 19:0db1451d19ef 28 there is no #include "rtos.h" statement.
gatedClock 19:0db1451d19ef 29 2. i was considering having the BPM up/down ISRs set up self-
gatedClock 19:0db1451d19ef 30 ending threads for continuous button press for rapid rate
gatedClock 19:0db1451d19ef 31 change, but again, rtos.h was causing trouble.
gatedClock 19:0db1451d19ef 32 3. I'm making use of disable-interrupts for bounce suppression.
gatedClock 19:0db1451d19ef 33 this could microscopically delay the metronome while its
gatedClock 19:0db1451d19ef 34 BPM is being adjusted, since I can't turn off only the
gatedClock 19:0db1451d19ef 35 interrupt involved with joystick input.
gatedClock 0:1013288b8e43 36
gatedClock 0:1013288b8e43 37 testing:
gatedClock 19:0db1451d19ef 38 1. start metronome - verify it blinks, and stops after a minute.
gatedClock 19:0db1451d19ef 39 2. stop metronome - verity it stops metronome.
gatedClock 19:0db1451d19ef 40 3. verify display indicates ON and OFF state.
gatedClock 19:0db1451d19ef 41 4. verify display indicates BPM rate.
gatedClock 19:0db1451d19ef 42 5. operate left/right joystick, inspect for BPM jumps (bounce).
gatedClock 19:0db1451d19ef 43 6. verify lower/upper BPM bounds. (only lower-bound tested).
gatedClock 0:1013288b8e43 44 -----includes-----------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 45 #include "mbed.h" // mbed class.
gatedClock 15:7c0a94d2a439 46 // #include "rtos.h"
gatedClock 14:fb4c76ac43a1 47 #include "C12832_lcd.h" // LCD class.
gatedClock 21:eb692e90ae8d 48 #include "PowerControl.h"
gatedClock 21:eb692e90ae8d 49
gatedClock 0:1013288b8e43 50 //---defines------------------------------------//------------------------------
gatedClock 0:1013288b8e43 51 #define LCD1 lcd.locate(0, 0); // LCD line 1.
gatedClock 0:1013288b8e43 52 #define LCD2 lcd.locate(0,11); // LCD line 2.
gatedClock 0:1013288b8e43 53 #define LCD3 lcd.locate(0,22); // LCD line 3.
gatedClock 7:562f136c7681 54
gatedClock 0:1013288b8e43 55 #define METROMAX 800 // max. beats per minute.
gatedClock 0:1013288b8e43 56 #define METROMIN 8 // min. beats per minute.
gatedClock 19:0db1451d19ef 57 #define METROTIME 60.0 // metronome on-time in seconds.
gatedClock 0:1013288b8e43 58 #define UDSAMPLERATE 0.1 // how often to sample U/D joystick.
gatedClock 0:1013288b8e43 59 #define LCDSAMPLERATE 0.1 // how often to redraw the LCD.
gatedClock 0:1013288b8e43 60 #define PULSELENGTH 0.0625 // how long the LED-on-time is.
gatedClock 10:4f2fa66cc430 61 #define DEBOUNCE 0.16 // debounce pause duration in S.
gatedClock 0:1013288b8e43 62 //--global_definitions--------------------------//------------------------------
gatedClock 0:1013288b8e43 63 //--global_variables----------------------------//------------------------------
gatedClock 0:1013288b8e43 64 float fMetroDelay; // time between ticks, in seconds.
gatedClock 0:1013288b8e43 65 float fMetroDuty; // duration of metro high, in seconds.
gatedClock 0:1013288b8e43 66 int dMetroBPM; // master parameter.
gatedClock 12:fab8e658ae76 67 char cMetronomeOn; // 1 = allow blink.
gatedClock 0:1013288b8e43 68 //--global_instances----------------------------//------------------------------
gatedClock 18:f19721f1069e 69 C12832_LCD lcd; // LCD object.
gatedClock 0:1013288b8e43 70
gatedClock 18:f19721f1069e 71 InterruptIn iJoyStickUp (p15); // joystick up rising edge.
gatedClock 18:f19721f1069e 72 InterruptIn iJoyStickDown (p12); // joystick down rising edge.
gatedClock 18:f19721f1069e 73 InterruptIn iJoyStickLeft (p13); // joystick left rising edge.
gatedClock 18:f19721f1069e 74 InterruptIn iJoyStickRight (p16); // joystick right rising edge.
gatedClock 18:f19721f1069e 75 InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed.
gatedClock 0:1013288b8e43 76
gatedClock 0:1013288b8e43 77 DigitalOut led3(LED1); // leftmost LED.
gatedClock 0:1013288b8e43 78
gatedClock 0:1013288b8e43 79 Ticker tickerMetronome; // blinking LED.
gatedClock 0:1013288b8e43 80 Ticker tickerLCD; // display ticker.
gatedClock 0:1013288b8e43 81 Timeout timeoutDutyCycle; // LED duty cycle delay.
gatedClock 12:fab8e658ae76 82 Timeout timeoutMetronome;
gatedClock 0:1013288b8e43 83 //-------prototypes-----------------------------//------------------------------
gatedClock 0:1013288b8e43 84 void initialization(); // initialize settings.
gatedClock 0:1013288b8e43 85 void lcd_display(); // display on LCD.
gatedClock 18:f19721f1069e 86 void interrupt_service_M(); // metronome tick.
gatedClock 0:1013288b8e43 87 void led3_off(); // attachable LED control.
gatedClock 0:1013288b8e43 88 void led3_on(); // attachable LED control.
gatedClock 18:f19721f1069e 89 void ISR_up(); // stop metronome.
gatedClock 18:f19721f1069e 90 void ISR_down(); // start metronome.
gatedClock 18:f19721f1069e 91 void ISR_right_rising(); // decrease BPM.
gatedClock 18:f19721f1069e 92 void ISR_right_falling(); // bounce protection.
gatedClock 18:f19721f1069e 93 void ISR_left_rising(); // increase BPM.
gatedClock 18:f19721f1069e 94 void ISR_left_falling(); // bounce protection.
gatedClock 18:f19721f1069e 95 void ISR_center(); // set to 60BPM.
gatedClock 18:f19721f1069e 96 void turn_off_metronome(); // turn off blinker.
gatedClock 0:1013288b8e43 97 //==============================================//==============================
gatedClock 0:1013288b8e43 98 int main(void)
gatedClock 0:1013288b8e43 99 {
gatedClock 18:f19721f1069e 100 iJoyStickUp.rise (&ISR_up); // metronome stop.
gatedClock 18:f19721f1069e 101 iJoyStickDown.rise (&ISR_down); // metronome start.
gatedClock 11:e764ed79553a 102
gatedClock 18:f19721f1069e 103 iJoyStickLeft.rise (&ISR_left_rising); // increase BPM.
gatedClock 18:f19721f1069e 104 iJoyStickLeft.fall (&ISR_left_falling); // anti-bounce.
gatedClock 10:4f2fa66cc430 105
gatedClock 18:f19721f1069e 106 iJoyStickRight.rise(&ISR_right_rising); // decrease BPM.
gatedClock 18:f19721f1069e 107 iJoyStickRight.fall(&ISR_right_falling); // anti-bounce.
gatedClock 10:4f2fa66cc430 108
gatedClock 18:f19721f1069e 109 iJoyStickCenter.rise(&ISR_center); // 60BPM.
gatedClock 6:bb5f989e3425 110
gatedClock 18:f19721f1069e 111 initialization(); // initialize variables.
gatedClock 6:bb5f989e3425 112
gatedClock 18:f19721f1069e 113 // metronome ticker.
gatedClock 15:7c0a94d2a439 114 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 6:bb5f989e3425 115
gatedClock 15:7c0a94d2a439 116 // LCD ticker.
gatedClock 15:7c0a94d2a439 117 tickerLCD.attach(&lcd_display,LCDSAMPLERATE);
gatedClock 6:bb5f989e3425 118
gatedClock 22:5d9bcc7f4440 119 #ifdef NOETHER
gatedClock 22:5d9bcc7f4440 120 // Normal mbed power level for this setup is around 690mW
gatedClock 22:5d9bcc7f4440 121 // assuming 5V used on Vin pin
gatedClock 22:5d9bcc7f4440 122 // If you don't need networking...
gatedClock 22:5d9bcc7f4440 123 // Power down Ethernet interface - saves around 175mW
gatedClock 22:5d9bcc7f4440 124 // Also need to unplug network cable - just a cable sucks power
gatedClock 22:5d9bcc7f4440 125 PHY_PowerDown();
gatedClock 22:5d9bcc7f4440 126 #endif
gatedClock 22:5d9bcc7f4440 127
gatedClock 18:f19721f1069e 128 while(1) // all timer/interrupt driven.
gatedClock 6:bb5f989e3425 129 {
gatedClock 6:bb5f989e3425 130 wait(10.0);
gatedClock 6:bb5f989e3425 131 }
gatedClock 0:1013288b8e43 132 }
gatedClock 0:1013288b8e43 133 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 134 void initialization(void) // program initializations.
gatedClock 0:1013288b8e43 135 {
gatedClock 0:1013288b8e43 136 dMetroBPM = 60; // initialize to 60BPM.
gatedClock 0:1013288b8e43 137 fMetroDelay = 60.0 / (float) (dMetroBPM);
gatedClock 0:1013288b8e43 138 fMetroDuty = PULSELENGTH; // initialize LED on-duration.
gatedClock 17:cd6c76be8046 139 cMetronomeOn = 0;
gatedClock 0:1013288b8e43 140 }
gatedClock 0:1013288b8e43 141 /*----------------------------------------------//----------------------------*/
gatedClock 11:e764ed79553a 142 void ISR_left_rising(void) // increase BPM.
gatedClock 1:5f41e2df0e85 143 {
gatedClock 18:f19721f1069e 144 __disable_irq(); // anti-bounce.
gatedClock 11:e764ed79553a 145
gatedClock 11:e764ed79553a 146 dMetroBPM++; // increase BPM.
gatedClock 11:e764ed79553a 147
gatedClock 12:fab8e658ae76 148 // saturate metronome BPM.
gatedClock 12:fab8e658ae76 149 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 12:fab8e658ae76 150 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 20:64f1671c57ec 151 fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
gatedClock 12:fab8e658ae76 152
gatedClock 11:e764ed79553a 153 wait(DEBOUNCE); // debounce time.
gatedClock 11:e764ed79553a 154
gatedClock 18:f19721f1069e 155 __enable_irq(); // safe by now.
gatedClock 1:5f41e2df0e85 156 }
gatedClock 1:5f41e2df0e85 157 /*----------------------------------------------//----------------------------*/
gatedClock 11:e764ed79553a 158 void ISR_left_falling(void) // ignore rising after falling edge.
gatedClock 11:e764ed79553a 159 {
gatedClock 18:f19721f1069e 160 __disable_irq(); // anti-bounce.
gatedClock 11:e764ed79553a 161
gatedClock 11:e764ed79553a 162 wait(DEBOUNCE); // debounce time.
gatedClock 11:e764ed79553a 163
gatedClock 18:f19721f1069e 164 __enable_irq(); // safe by now.
gatedClock 11:e764ed79553a 165 }
gatedClock 11:e764ed79553a 166 /*----------------------------------------------//----------------------------*/
gatedClock 10:4f2fa66cc430 167 void ISR_right_rising(void) // decrease BPM.
gatedClock 4:67d958cc00e8 168 {
gatedClock 18:f19721f1069e 169 __disable_irq(); // anti-bounce.
gatedClock 10:4f2fa66cc430 170
gatedClock 10:4f2fa66cc430 171 dMetroBPM--; // decrease BPM.
gatedClock 4:67d958cc00e8 172
gatedClock 12:fab8e658ae76 173 // saturate metronome BPM.
gatedClock 12:fab8e658ae76 174 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 12:fab8e658ae76 175 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 20:64f1671c57ec 176 fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
gatedClock 12:fab8e658ae76 177
gatedClock 10:4f2fa66cc430 178 wait(DEBOUNCE); // debounce time.
gatedClock 9:ea97a69b9b93 179
gatedClock 18:f19721f1069e 180 __enable_irq(); // safe by now.
gatedClock 10:4f2fa66cc430 181 }
gatedClock 10:4f2fa66cc430 182 /*----------------------------------------------//----------------------------*/
gatedClock 10:4f2fa66cc430 183 void ISR_right_falling(void) // ignore rising after falling edge.
gatedClock 10:4f2fa66cc430 184 {
gatedClock 18:f19721f1069e 185 __disable_irq(); // anti-bounce.
gatedClock 10:4f2fa66cc430 186
gatedClock 10:4f2fa66cc430 187 wait(DEBOUNCE); // debounce time.
gatedClock 10:4f2fa66cc430 188
gatedClock 18:f19721f1069e 189 __enable_irq(); // safe by now.
gatedClock 4:67d958cc00e8 190 }
gatedClock 4:67d958cc00e8 191 /*----------------------------------------------//----------------------------*/
gatedClock 18:f19721f1069e 192 void ISR_up(void) // turn off metronome.
gatedClock 1:5f41e2df0e85 193 {
gatedClock 17:cd6c76be8046 194 cMetronomeOn = 0;
gatedClock 1:5f41e2df0e85 195 }
gatedClock 1:5f41e2df0e85 196 /*----------------------------------------------//----------------------------*/
gatedClock 18:f19721f1069e 197 void ISR_down(void) // metronome on with timeout.
gatedClock 1:5f41e2df0e85 198 {
gatedClock 12:fab8e658ae76 199 cMetronomeOn = 1;
gatedClock 12:fab8e658ae76 200 timeoutMetronome.detach();
gatedClock 17:cd6c76be8046 201 timeoutMetronome.attach(&turn_off_metronome,METROTIME);
gatedClock 1:5f41e2df0e85 202 }
gatedClock 1:5f41e2df0e85 203 /*----------------------------------------------//----------------------------*/
gatedClock 3:8ff0c9c853be 204 void ISR_center(void) // set BPM = 60.
gatedClock 1:5f41e2df0e85 205 {
gatedClock 8:95f4f470ae28 206 dMetroBPM = 60;
gatedClock 20:64f1671c57ec 207 fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
gatedClock 19:0db1451d19ef 208 tickerMetronome.detach(); // change BPM immediately.
gatedClock 19:0db1451d19ef 209 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 1:5f41e2df0e85 210 }
gatedClock 1:5f41e2df0e85 211 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 212 void lcd_display(void) // display metronome info.
gatedClock 0:1013288b8e43 213 {
gatedClock 0:1013288b8e43 214 lcd.cls(); // clear display.
gatedClock 0:1013288b8e43 215
gatedClock 0:1013288b8e43 216 LCD1; // line 1.
gatedClock 17:cd6c76be8046 217
gatedClock 16:d7bbd641929c 218 if (cMetronomeOn)
gatedClock 16:d7bbd641929c 219 lcd.printf(" metronome ON");
gatedClock 16:d7bbd641929c 220 else
gatedClock 16:d7bbd641929c 221 lcd.printf(" metronome OFF");
gatedClock 0:1013288b8e43 222
gatedClock 0:1013288b8e43 223 LCD2; // line 2.
gatedClock 0:1013288b8e43 224
gatedClock 0:1013288b8e43 225 if (dMetroBPM == METROMIN) // BPM, with saturation notification.
gatedClock 0:1013288b8e43 226 lcd.printf(" %5.2d BPM minimum",dMetroBPM);
gatedClock 0:1013288b8e43 227 else
gatedClock 0:1013288b8e43 228 if (dMetroBPM == METROMAX)
gatedClock 0:1013288b8e43 229 lcd.printf(" %5.2d BPM maximum",dMetroBPM);
gatedClock 0:1013288b8e43 230 else
gatedClock 0:1013288b8e43 231 lcd.printf(" %5.2d BPM",dMetroBPM);
gatedClock 0:1013288b8e43 232
gatedClock 0:1013288b8e43 233 LCD3; // line 3.
gatedClock 0:1013288b8e43 234
gatedClock 8:95f4f470ae28 235 lcd.printf(" RTOS HW 4");
gatedClock 0:1013288b8e43 236 }
gatedClock 0:1013288b8e43 237 /*----------------------------------------------//----------------------------*/
gatedClock 17:cd6c76be8046 238 // this metronome tick ISR will self-adjust to the current user-selected
gatedClock 17:cd6c76be8046 239 // metronome rate. that has to be done here, and at the start of the function,
gatedClock 17:cd6c76be8046 240 // in order to maintain a constant phase and to prevent a beat-skip.
gatedClock 17:cd6c76be8046 241
gatedClock 17:cd6c76be8046 242 void interrupt_service_M() // metronome tick.
gatedClock 17:cd6c76be8046 243 {
gatedClock 17:cd6c76be8046 244 if (cMetronomeOn)
gatedClock 17:cd6c76be8046 245 {
gatedClock 18:f19721f1069e 246 tickerMetronome.detach(); // only one attachment.
gatedClock 17:cd6c76be8046 247 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 17:cd6c76be8046 248 led3_on();
gatedClock 17:cd6c76be8046 249 timeoutDutyCycle.attach(&led3_off,fMetroDuty);
gatedClock 17:cd6c76be8046 250 } else led3_off();
gatedClock 17:cd6c76be8046 251 }
gatedClock 17:cd6c76be8046 252 /*----------------------------------------------//----------------------------*/
gatedClock 18:f19721f1069e 253 void turn_off_metronome(void) // turn off metronome.
gatedClock 17:cd6c76be8046 254 {
gatedClock 17:cd6c76be8046 255 cMetronomeOn = 0;
gatedClock 17:cd6c76be8046 256 }
gatedClock 17:cd6c76be8046 257 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 258 void led3_off(void) {led3 = 0;} // turn off the LED.
gatedClock 0:1013288b8e43 259 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 260 void led3_on( void) {led3 = 1;} // turn on the led.
gatedClock 15:7c0a94d2a439 261 /*----------------------------------------------//----------------------------*/