RTOS homework 4

Dependencies:   C12832_lcd mbed

Committer:
gatedClock
Date:
Sun Aug 18 17:34:54 2013 +0000
Revision:
10:4f2fa66cc430
Parent:
9:ea97a69b9b93
Child:
11:e764ed79553a
reduce BPM debounce looks ok now.

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 0:1013288b8e43 10 1. post-initialization functionality all timer-driven.
gatedClock 0:1013288b8e43 11 2. IRSs contain no blocking functions.
gatedClock 0:1013288b8e43 12 3. LED shows the metronome beat.
gatedClock 0:1013288b8e43 13 4. metronome speed controlled with up/down joystick.
gatedClock 0:1013288b8e43 14 5. rate-of-speed-change depends on how long the up/down has been active.
gatedClock 0:1013288b8e43 15 6. Beat-Per-Minute (BPM) shown on LCD display.
gatedClock 0:1013288b8e43 16
gatedClock 0:1013288b8e43 17 controls:
gatedClock 0:1013288b8e43 18 1. joystick-up - increase metronome rate.
gatedClock 0:1013288b8e43 19 2. joystick-down - decrease metronome rate.
gatedClock 0:1013288b8e43 20 3. joystick-center - set metronome to 60BPM.
gatedClock 0:1013288b8e43 21
gatedClock 0:1013288b8e43 22 notes:
gatedClock 0:1013288b8e43 23
gatedClock 0:1013288b8e43 24 testing:
gatedClock 0:1013288b8e43 25 1. confirm ease of being able to adjust one BPM rate with joystick up/down.
gatedClock 0:1013288b8e43 26 2. confirm three joystick up/down change rates, if keeping the stick pressed.
gatedClock 0:1013288b8e43 27 3. confirm max/min BPS saturation & display.
gatedClock 0:1013288b8e43 28 4. confirm joystick up/down control works to change display & LED BPM.
gatedClock 0:1013288b8e43 29 5. confirm joystick center-press sets rate to 60BPM immediately.
gatedClock 0:1013288b8e43 30 6. confirm long-test does not result in a crash.
gatedClock 0:1013288b8e43 31 -> all items confirmed.
gatedClock 0:1013288b8e43 32
gatedClock 0:1013288b8e43 33 -----includes-----------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 34 #include "mbed.h" // mbed class.
gatedClock 0:1013288b8e43 35 #include "C12832_lcd.h" // LCD class.
gatedClock 0:1013288b8e43 36 //---defines------------------------------------//------------------------------
gatedClock 0:1013288b8e43 37 #define LCD1 lcd.locate(0, 0); // LCD line 1.
gatedClock 0:1013288b8e43 38 #define LCD2 lcd.locate(0,11); // LCD line 2.
gatedClock 0:1013288b8e43 39 #define LCD3 lcd.locate(0,22); // LCD line 3.
gatedClock 7:562f136c7681 40
gatedClock 0:1013288b8e43 41 #define METROMAX 800 // max. beats per minute.
gatedClock 0:1013288b8e43 42 #define METROMIN 8 // min. beats per minute.
gatedClock 0:1013288b8e43 43 #define UDSAMPLERATE 0.1 // how often to sample U/D joystick.
gatedClock 0:1013288b8e43 44 #define LCDSAMPLERATE 0.1 // how often to redraw the LCD.
gatedClock 0:1013288b8e43 45 #define PULSELENGTH 0.0625 // how long the LED-on-time is.
gatedClock 10:4f2fa66cc430 46 #define DEBOUNCE 0.16 // debounce pause duration in S.
gatedClock 0:1013288b8e43 47 //--global_definitions--------------------------//------------------------------
gatedClock 0:1013288b8e43 48 //--global_variables----------------------------//------------------------------
gatedClock 0:1013288b8e43 49 float fMetroDelay; // time between ticks, in seconds.
gatedClock 0:1013288b8e43 50 float fMetroDuty; // duration of metro high, in seconds.
gatedClock 0:1013288b8e43 51 int dMetroBPM; // master parameter.
gatedClock 0:1013288b8e43 52 long lUpDownHowMany; // count how long up/down joystick pressed.
gatedClock 9:ea97a69b9b93 53 char cMutex; // debounce mutex.
gatedClock 0:1013288b8e43 54 //--global_instances----------------------------//------------------------------
gatedClock 0:1013288b8e43 55 C12832_LCD lcd; // LCD object.
gatedClock 0:1013288b8e43 56
gatedClock 7:562f136c7681 57 InterruptIn iJoyStickUp (p15); // joystick up rising edge.
gatedClock 7:562f136c7681 58 InterruptIn iJoyStickDown (p12); // joystick down rising edge.
gatedClock 7:562f136c7681 59 InterruptIn iJoyStickLeft (p13); // joystick left rising edge.
gatedClock 7:562f136c7681 60 InterruptIn iJoyStickRight (p16); // joystick right rising edge.
gatedClock 7:562f136c7681 61 InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed.
gatedClock 3:8ff0c9c853be 62
gatedClock 8:95f4f470ae28 63 DigitalIn dJoyStickUp (p15); // joystick up sample.
gatedClock 8:95f4f470ae28 64 DigitalIn dJoyStickDown (p12); // joystick down sample.
gatedClock 8:95f4f470ae28 65 DigitalIn dJoyStickLeft (p13); // joystick left sample.
gatedClock 8:95f4f470ae28 66 DigitalIn dJoyStickRight (p16); // joystick right sample.
gatedClock 8:95f4f470ae28 67 DigitalIn dJoyStickCenter(p14); // joystick center sample.
gatedClock 0:1013288b8e43 68
gatedClock 0:1013288b8e43 69 DigitalOut led3(LED1); // leftmost LED.
gatedClock 0:1013288b8e43 70
gatedClock 0:1013288b8e43 71 Ticker tickerMetronome; // blinking LED.
gatedClock 0:1013288b8e43 72 Ticker tickerJoystickUD; // joystick up/down sample.
gatedClock 0:1013288b8e43 73 Ticker tickerLCD; // display ticker.
gatedClock 0:1013288b8e43 74 Timeout timeoutDutyCycle; // LED duty cycle delay.
gatedClock 0:1013288b8e43 75 //-------prototypes-----------------------------//------------------------------
gatedClock 0:1013288b8e43 76 void initialization(); // initialize settings.
gatedClock 0:1013288b8e43 77 void lcd_display(); // display on LCD.
gatedClock 0:1013288b8e43 78 void interrupt_service_M(); // metronome tick.
gatedClock 0:1013288b8e43 79 void interrupt_service_UD(); // joystick up/down sample.
gatedClock 0:1013288b8e43 80 void led3_off(); // attachable LED control.
gatedClock 0:1013288b8e43 81 void led3_on(); // attachable LED control.
gatedClock 1:5f41e2df0e85 82 void ISR_up();
gatedClock 1:5f41e2df0e85 83 void ISR_down();
gatedClock 10:4f2fa66cc430 84 void ISR_right_rising();
gatedClock 10:4f2fa66cc430 85 void ISR_right_falling();
gatedClock 1:5f41e2df0e85 86 void ISR_left();
gatedClock 1:5f41e2df0e85 87 void ISR_center();
gatedClock 0:1013288b8e43 88 //==============================================//==============================
gatedClock 0:1013288b8e43 89 int main(void)
gatedClock 0:1013288b8e43 90 {
gatedClock 3:8ff0c9c853be 91 iJoyStickUp.rise(&ISR_up);
gatedClock 5:faa6d8a04501 92 iJoyStickDown.rise(&ISR_down);
gatedClock 5:faa6d8a04501 93 iJoyStickLeft.rise(&ISR_left);
gatedClock 10:4f2fa66cc430 94
gatedClock 10:4f2fa66cc430 95 iJoyStickRight.rise(&ISR_right_rising);
gatedClock 10:4f2fa66cc430 96 iJoyStickRight.fall(&ISR_right_falling);
gatedClock 10:4f2fa66cc430 97
gatedClock 5:faa6d8a04501 98 iJoyStickCenter.rise(&ISR_center);
gatedClock 6:bb5f989e3425 99
gatedClock 6:bb5f989e3425 100 initialization();
gatedClock 6:bb5f989e3425 101
gatedClock 6:bb5f989e3425 102 // metronome ticker.
gatedClock 6:bb5f989e3425 103 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 6:bb5f989e3425 104
gatedClock 6:bb5f989e3425 105 // LCD ticker.
gatedClock 6:bb5f989e3425 106 tickerLCD.attach(&lcd_display,LCDSAMPLERATE);
gatedClock 6:bb5f989e3425 107
gatedClock 6:bb5f989e3425 108 while(1)
gatedClock 6:bb5f989e3425 109 {
gatedClock 6:bb5f989e3425 110
gatedClock 6:bb5f989e3425 111 wait(10.0);
gatedClock 6:bb5f989e3425 112 }
gatedClock 2:1a3acbc4d799 113
gatedClock 0:1013288b8e43 114 }
gatedClock 0:1013288b8e43 115 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 116 void initialization(void) // program initializations.
gatedClock 0:1013288b8e43 117 {
gatedClock 0:1013288b8e43 118 dMetroBPM = 60; // initialize to 60BPM.
gatedClock 0:1013288b8e43 119 fMetroDelay = 60.0 / (float) (dMetroBPM);
gatedClock 0:1013288b8e43 120 fMetroDuty = PULSELENGTH; // initialize LED on-duration.
gatedClock 0:1013288b8e43 121 lUpDownHowMany = 0;
gatedClock 9:ea97a69b9b93 122 cMutex = 1;
gatedClock 0:1013288b8e43 123 }
gatedClock 0:1013288b8e43 124 /*----------------------------------------------//----------------------------*/
gatedClock 8:95f4f470ae28 125 void ISR_left(void) // increase BPM.
gatedClock 1:5f41e2df0e85 126 {
gatedClock 9:ea97a69b9b93 127 if (cMutex) // grab the mutex.
gatedClock 9:ea97a69b9b93 128 {
gatedClock 9:ea97a69b9b93 129 cMutex = 0; // disqualify any bounce.
gatedClock 9:ea97a69b9b93 130
gatedClock 9:ea97a69b9b93 131 dMetroBPM++; // increase BPM.
gatedClock 4:67d958cc00e8 132
gatedClock 9:ea97a69b9b93 133 // saturate metronome BPM.
gatedClock 9:ea97a69b9b93 134 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 9:ea97a69b9b93 135 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 9:ea97a69b9b93 136
gatedClock 9:ea97a69b9b93 137 wait(0.01); // debounce time.
gatedClock 9:ea97a69b9b93 138
gatedClock 9:ea97a69b9b93 139 cMutex = 1; // allow critical section.
gatedClock 9:ea97a69b9b93 140 }
gatedClock 1:5f41e2df0e85 141 }
gatedClock 1:5f41e2df0e85 142 /*----------------------------------------------//----------------------------*/
gatedClock 10:4f2fa66cc430 143 void ISR_right_rising(void) // decrease BPM.
gatedClock 4:67d958cc00e8 144 {
gatedClock 10:4f2fa66cc430 145 __disable_irq();
gatedClock 10:4f2fa66cc430 146
gatedClock 10:4f2fa66cc430 147 dMetroBPM--; // decrease BPM.
gatedClock 4:67d958cc00e8 148
gatedClock 9:ea97a69b9b93 149 // saturate metronome BPM.
gatedClock 10:4f2fa66cc430 150 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 10:4f2fa66cc430 151 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 10:4f2fa66cc430 152
gatedClock 10:4f2fa66cc430 153 wait(DEBOUNCE); // debounce time.
gatedClock 9:ea97a69b9b93 154
gatedClock 10:4f2fa66cc430 155 __enable_irq();
gatedClock 10:4f2fa66cc430 156 }
gatedClock 10:4f2fa66cc430 157 /*----------------------------------------------//----------------------------*/
gatedClock 10:4f2fa66cc430 158 void ISR_right_falling(void) // ignore rising after falling edge.
gatedClock 10:4f2fa66cc430 159 {
gatedClock 10:4f2fa66cc430 160 __disable_irq();
gatedClock 10:4f2fa66cc430 161
gatedClock 10:4f2fa66cc430 162 wait(DEBOUNCE); // debounce time.
gatedClock 10:4f2fa66cc430 163
gatedClock 10:4f2fa66cc430 164 __enable_irq();
gatedClock 4:67d958cc00e8 165 }
gatedClock 4:67d958cc00e8 166 /*----------------------------------------------//----------------------------*/
gatedClock 4:67d958cc00e8 167 void ISR_up(void)
gatedClock 1:5f41e2df0e85 168 {
gatedClock 1:5f41e2df0e85 169 }
gatedClock 1:5f41e2df0e85 170 /*----------------------------------------------//----------------------------*/
gatedClock 4:67d958cc00e8 171 void ISR_down(void)
gatedClock 1:5f41e2df0e85 172 {
gatedClock 1:5f41e2df0e85 173 }
gatedClock 1:5f41e2df0e85 174 /*----------------------------------------------//----------------------------*/
gatedClock 3:8ff0c9c853be 175 void ISR_center(void) // set BPM = 60.
gatedClock 1:5f41e2df0e85 176 {
gatedClock 8:95f4f470ae28 177 dMetroBPM = 60;
gatedClock 4:67d958cc00e8 178
gatedClock 1:5f41e2df0e85 179 }
gatedClock 1:5f41e2df0e85 180 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 181 void lcd_display(void) // display metronome info.
gatedClock 0:1013288b8e43 182 {
gatedClock 0:1013288b8e43 183 lcd.cls(); // clear display.
gatedClock 0:1013288b8e43 184
gatedClock 0:1013288b8e43 185 LCD1; // line 1.
gatedClock 0:1013288b8e43 186 lcd.printf(" METRONOME");
gatedClock 0:1013288b8e43 187
gatedClock 0:1013288b8e43 188 LCD2; // line 2.
gatedClock 0:1013288b8e43 189
gatedClock 0:1013288b8e43 190 if (dMetroBPM == METROMIN) // BPM, with saturation notification.
gatedClock 0:1013288b8e43 191 lcd.printf(" %5.2d BPM minimum",dMetroBPM);
gatedClock 0:1013288b8e43 192 else
gatedClock 0:1013288b8e43 193 if (dMetroBPM == METROMAX)
gatedClock 0:1013288b8e43 194 lcd.printf(" %5.2d BPM maximum",dMetroBPM);
gatedClock 0:1013288b8e43 195 else
gatedClock 0:1013288b8e43 196 lcd.printf(" %5.2d BPM",dMetroBPM);
gatedClock 0:1013288b8e43 197
gatedClock 0:1013288b8e43 198 LCD3; // line 3.
gatedClock 0:1013288b8e43 199
gatedClock 8:95f4f470ae28 200 lcd.printf(" RTOS HW 4");
gatedClock 0:1013288b8e43 201 }
gatedClock 0:1013288b8e43 202 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 203 // this metronome tick ISR will self-adjust to the current user-selected
gatedClock 0:1013288b8e43 204 // metronome rate. that has to be done here, and at the start of the function,
gatedClock 0:1013288b8e43 205 // in order to maintain a constant phase and to prevent a beat-skip.
gatedClock 0:1013288b8e43 206
gatedClock 0:1013288b8e43 207 void interrupt_service_M() // metronome tick.
gatedClock 0:1013288b8e43 208 {
gatedClock 0:1013288b8e43 209 tickerMetronome.detach(); // only one attachment.
gatedClock 0:1013288b8e43 210 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 0:1013288b8e43 211 led3 = 1;
gatedClock 0:1013288b8e43 212 timeoutDutyCycle.attach(&led3_off,fMetroDuty);
gatedClock 0:1013288b8e43 213 }
gatedClock 0:1013288b8e43 214 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 215 // this routine measures the number of seconds for which the joystick is
gatedClock 0:1013288b8e43 216 // in the up or down position. for three ranges of time, three different
gatedClock 0:1013288b8e43 217 // BPM rates-of-change are used. This means that as the user controls the
gatedClock 0:1013288b8e43 218 // metronome rate using the joystick, at first it will change slowly, then
gatedClock 0:1013288b8e43 219 // it will change at a moderate speed, then it will change quickly.
gatedClock 0:1013288b8e43 220 // additionally, pressing the center joystick button will bring the metronome
gatedClock 0:1013288b8e43 221 // back to 60BPM immediately, breaking BPM phase continuity.
gatedClock 0:1013288b8e43 222 void interrupt_service_UD(void) // joystick up/down sample
gatedClock 0:1013288b8e43 223 {
gatedClock 0:1013288b8e43 224 int dPressedSeconds; // how many seconds joystick pressed.
gatedClock 0:1013288b8e43 225 int dMultiCount; // slow count rate.
gatedClock 0:1013288b8e43 226 char cDiscontinuity; // 1 = break phase & change BPM now.
gatedClock 0:1013288b8e43 227
gatedClock 0:1013288b8e43 228 cDiscontinuity = 0; // don't break phase.
gatedClock 0:1013288b8e43 229
gatedClock 0:1013288b8e43 230 // calculate slow rate period.
gatedClock 0:1013288b8e43 231 dMultiCount = (int) ((float) (1.0 / ((float) UDSAMPLERATE)));
gatedClock 0:1013288b8e43 232
gatedClock 3:8ff0c9c853be 233 if (dJoyStickUp) // joystick up.
gatedClock 0:1013288b8e43 234 {
gatedClock 0:1013288b8e43 235 // rate-range calculations.
gatedClock 0:1013288b8e43 236 dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
gatedClock 0:1013288b8e43 237 if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM ++;}
gatedClock 0:1013288b8e43 238 else
gatedClock 0:1013288b8e43 239 if (dPressedSeconds < 10) dMetroBPM++;
gatedClock 0:1013288b8e43 240 else dMetroBPM += 5;
gatedClock 0:1013288b8e43 241 lUpDownHowMany++; // joystick holddown time.
gatedClock 0:1013288b8e43 242 }
gatedClock 0:1013288b8e43 243 else
gatedClock 3:8ff0c9c853be 244 if (dJoyStickDown) // joystick down.
gatedClock 0:1013288b8e43 245 {
gatedClock 0:1013288b8e43 246 // rate-range calculations.
gatedClock 0:1013288b8e43 247 dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
gatedClock 0:1013288b8e43 248 if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM --;}
gatedClock 0:1013288b8e43 249 else
gatedClock 0:1013288b8e43 250 if (dPressedSeconds < 10) dMetroBPM--;
gatedClock 0:1013288b8e43 251 else dMetroBPM -= 5;
gatedClock 0:1013288b8e43 252 lUpDownHowMany++; // joystick holddown time.
gatedClock 0:1013288b8e43 253 }
gatedClock 0:1013288b8e43 254 else lUpDownHowMany = 0; // clear when not up or down.
gatedClock 0:1013288b8e43 255
gatedClock 3:8ff0c9c853be 256 if (dJoyStickCenter)
gatedClock 0:1013288b8e43 257 {
gatedClock 0:1013288b8e43 258 dMetroBPM = 60; // center-button -> 60BPM.
gatedClock 0:1013288b8e43 259 cDiscontinuity = 1; // pending phase-break.
gatedClock 0:1013288b8e43 260 }
gatedClock 0:1013288b8e43 261 // saturate metronome BPM.
gatedClock 0:1013288b8e43 262 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 0:1013288b8e43 263 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 0:1013288b8e43 264
gatedClock 0:1013288b8e43 265 fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
gatedClock 0:1013288b8e43 266
gatedClock 0:1013288b8e43 267 if (cDiscontinuity) // implement 60BPS now.
gatedClock 0:1013288b8e43 268 {
gatedClock 0:1013288b8e43 269 tickerMetronome.detach(); // only one attachment.
gatedClock 0:1013288b8e43 270 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 0:1013288b8e43 271 }
gatedClock 0:1013288b8e43 272
gatedClock 0:1013288b8e43 273 }
gatedClock 0:1013288b8e43 274 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 275 void led3_off(void) {led3 = 0;} // turn off the LED.
gatedClock 0:1013288b8e43 276 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 277 void led3_on( void) {led3 = 1;} // turn on the led.
gatedClock 0:1013288b8e43 278 /*----------------------------------------------//----------------------------*/