Mike Moore
/
RTOS_HW_04
RTOS homework 4
main.cpp@8:95f4f470ae28, 2013-08-18 (annotated)
- Committer:
- gatedClock
- Date:
- Sun Aug 18 16:42:56 2013 +0000
- Revision:
- 8:95f4f470ae28
- Parent:
- 7:562f136c7681
- Child:
- 9:ea97a69b9b93
increase/decrease BPM starting to work, but bouncy.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:1013288b8e43 | 46 | //--global_definitions--------------------------//------------------------------ |
gatedClock | 0:1013288b8e43 | 47 | //--global_variables----------------------------//------------------------------ |
gatedClock | 0:1013288b8e43 | 48 | float fMetroDelay; // time between ticks, in seconds. |
gatedClock | 0:1013288b8e43 | 49 | float fMetroDuty; // duration of metro high, in seconds. |
gatedClock | 0:1013288b8e43 | 50 | int dMetroBPM; // master parameter. |
gatedClock | 0:1013288b8e43 | 51 | long lUpDownHowMany; // count how long up/down joystick pressed. |
gatedClock | 0:1013288b8e43 | 52 | //--global_instances----------------------------//------------------------------ |
gatedClock | 0:1013288b8e43 | 53 | C12832_LCD lcd; // LCD object. |
gatedClock | 0:1013288b8e43 | 54 | |
gatedClock | 7:562f136c7681 | 55 | InterruptIn iJoyStickUp (p15); // joystick up rising edge. |
gatedClock | 7:562f136c7681 | 56 | InterruptIn iJoyStickDown (p12); // joystick down rising edge. |
gatedClock | 7:562f136c7681 | 57 | InterruptIn iJoyStickLeft (p13); // joystick left rising edge. |
gatedClock | 7:562f136c7681 | 58 | InterruptIn iJoyStickRight (p16); // joystick right rising edge. |
gatedClock | 7:562f136c7681 | 59 | InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed. |
gatedClock | 3:8ff0c9c853be | 60 | |
gatedClock | 8:95f4f470ae28 | 61 | DigitalIn dJoyStickUp (p15); // joystick up sample. |
gatedClock | 8:95f4f470ae28 | 62 | DigitalIn dJoyStickDown (p12); // joystick down sample. |
gatedClock | 8:95f4f470ae28 | 63 | DigitalIn dJoyStickLeft (p13); // joystick left sample. |
gatedClock | 8:95f4f470ae28 | 64 | DigitalIn dJoyStickRight (p16); // joystick right sample. |
gatedClock | 8:95f4f470ae28 | 65 | DigitalIn dJoyStickCenter(p14); // joystick center sample. |
gatedClock | 0:1013288b8e43 | 66 | |
gatedClock | 0:1013288b8e43 | 67 | DigitalOut led3(LED1); // leftmost LED. |
gatedClock | 0:1013288b8e43 | 68 | |
gatedClock | 0:1013288b8e43 | 69 | Ticker tickerMetronome; // blinking LED. |
gatedClock | 0:1013288b8e43 | 70 | Ticker tickerJoystickUD; // joystick up/down sample. |
gatedClock | 0:1013288b8e43 | 71 | Ticker tickerLCD; // display ticker. |
gatedClock | 0:1013288b8e43 | 72 | Timeout timeoutDutyCycle; // LED duty cycle delay. |
gatedClock | 0:1013288b8e43 | 73 | //-------prototypes-----------------------------//------------------------------ |
gatedClock | 0:1013288b8e43 | 74 | void initialization(); // initialize settings. |
gatedClock | 0:1013288b8e43 | 75 | void lcd_display(); // display on LCD. |
gatedClock | 0:1013288b8e43 | 76 | void interrupt_service_M(); // metronome tick. |
gatedClock | 0:1013288b8e43 | 77 | void interrupt_service_UD(); // joystick up/down sample. |
gatedClock | 0:1013288b8e43 | 78 | void led3_off(); // attachable LED control. |
gatedClock | 0:1013288b8e43 | 79 | void led3_on(); // attachable LED control. |
gatedClock | 1:5f41e2df0e85 | 80 | void ISR_up(); |
gatedClock | 1:5f41e2df0e85 | 81 | void ISR_down(); |
gatedClock | 1:5f41e2df0e85 | 82 | void ISR_right(); |
gatedClock | 1:5f41e2df0e85 | 83 | void ISR_left(); |
gatedClock | 1:5f41e2df0e85 | 84 | void ISR_center(); |
gatedClock | 0:1013288b8e43 | 85 | //==============================================//============================== |
gatedClock | 0:1013288b8e43 | 86 | int main(void) |
gatedClock | 0:1013288b8e43 | 87 | { |
gatedClock | 3:8ff0c9c853be | 88 | iJoyStickUp.rise(&ISR_up); |
gatedClock | 5:faa6d8a04501 | 89 | iJoyStickDown.rise(&ISR_down); |
gatedClock | 5:faa6d8a04501 | 90 | iJoyStickLeft.rise(&ISR_left); |
gatedClock | 5:faa6d8a04501 | 91 | iJoyStickRight.rise(&ISR_right); |
gatedClock | 5:faa6d8a04501 | 92 | iJoyStickCenter.rise(&ISR_center); |
gatedClock | 6:bb5f989e3425 | 93 | |
gatedClock | 6:bb5f989e3425 | 94 | initialization(); |
gatedClock | 6:bb5f989e3425 | 95 | |
gatedClock | 6:bb5f989e3425 | 96 | // metronome ticker. |
gatedClock | 6:bb5f989e3425 | 97 | tickerMetronome.attach(&interrupt_service_M,fMetroDelay); |
gatedClock | 6:bb5f989e3425 | 98 | |
gatedClock | 6:bb5f989e3425 | 99 | // LCD ticker. |
gatedClock | 6:bb5f989e3425 | 100 | tickerLCD.attach(&lcd_display,LCDSAMPLERATE); |
gatedClock | 6:bb5f989e3425 | 101 | |
gatedClock | 6:bb5f989e3425 | 102 | while(1) |
gatedClock | 6:bb5f989e3425 | 103 | { |
gatedClock | 6:bb5f989e3425 | 104 | |
gatedClock | 6:bb5f989e3425 | 105 | wait(10.0); |
gatedClock | 6:bb5f989e3425 | 106 | } |
gatedClock | 2:1a3acbc4d799 | 107 | |
gatedClock | 0:1013288b8e43 | 108 | } |
gatedClock | 0:1013288b8e43 | 109 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 110 | void initialization(void) // program initializations. |
gatedClock | 0:1013288b8e43 | 111 | { |
gatedClock | 0:1013288b8e43 | 112 | dMetroBPM = 60; // initialize to 60BPM. |
gatedClock | 0:1013288b8e43 | 113 | fMetroDelay = 60.0 / (float) (dMetroBPM); |
gatedClock | 0:1013288b8e43 | 114 | fMetroDuty = PULSELENGTH; // initialize LED on-duration. |
gatedClock | 0:1013288b8e43 | 115 | lUpDownHowMany = 0; |
gatedClock | 0:1013288b8e43 | 116 | } |
gatedClock | 0:1013288b8e43 | 117 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 8:95f4f470ae28 | 118 | void ISR_left(void) // increase BPM. |
gatedClock | 1:5f41e2df0e85 | 119 | { |
gatedClock | 8:95f4f470ae28 | 120 | dMetroBPM++; |
gatedClock | 4:67d958cc00e8 | 121 | |
gatedClock | 4:67d958cc00e8 | 122 | // saturate metronome BPM. |
gatedClock | 4:67d958cc00e8 | 123 | if (dMetroBPM > METROMAX) dMetroBPM = METROMAX; |
gatedClock | 4:67d958cc00e8 | 124 | if (dMetroBPM < METROMIN) dMetroBPM = METROMIN; |
gatedClock | 1:5f41e2df0e85 | 125 | } |
gatedClock | 1:5f41e2df0e85 | 126 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 4:67d958cc00e8 | 127 | void ISR_right(void) // decrease beat rate. |
gatedClock | 4:67d958cc00e8 | 128 | { |
gatedClock | 8:95f4f470ae28 | 129 | dMetroBPM--; |
gatedClock | 4:67d958cc00e8 | 130 | |
gatedClock | 8:95f4f470ae28 | 131 | // saturate metronome BPM. |
gatedClock | 4:67d958cc00e8 | 132 | if (dMetroBPM > METROMAX) dMetroBPM = METROMAX; |
gatedClock | 4:67d958cc00e8 | 133 | if (dMetroBPM < METROMIN) dMetroBPM = METROMIN; |
gatedClock | 4:67d958cc00e8 | 134 | } |
gatedClock | 4:67d958cc00e8 | 135 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 4:67d958cc00e8 | 136 | void ISR_up(void) |
gatedClock | 1:5f41e2df0e85 | 137 | { |
gatedClock | 1:5f41e2df0e85 | 138 | } |
gatedClock | 1:5f41e2df0e85 | 139 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 4:67d958cc00e8 | 140 | void ISR_down(void) |
gatedClock | 1:5f41e2df0e85 | 141 | { |
gatedClock | 1:5f41e2df0e85 | 142 | } |
gatedClock | 1:5f41e2df0e85 | 143 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 3:8ff0c9c853be | 144 | void ISR_center(void) // set BPM = 60. |
gatedClock | 1:5f41e2df0e85 | 145 | { |
gatedClock | 8:95f4f470ae28 | 146 | dMetroBPM = 60; |
gatedClock | 4:67d958cc00e8 | 147 | |
gatedClock | 1:5f41e2df0e85 | 148 | } |
gatedClock | 1:5f41e2df0e85 | 149 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 150 | void lcd_display(void) // display metronome info. |
gatedClock | 0:1013288b8e43 | 151 | { |
gatedClock | 0:1013288b8e43 | 152 | lcd.cls(); // clear display. |
gatedClock | 0:1013288b8e43 | 153 | |
gatedClock | 0:1013288b8e43 | 154 | LCD1; // line 1. |
gatedClock | 0:1013288b8e43 | 155 | lcd.printf(" METRONOME"); |
gatedClock | 0:1013288b8e43 | 156 | |
gatedClock | 0:1013288b8e43 | 157 | LCD2; // line 2. |
gatedClock | 0:1013288b8e43 | 158 | |
gatedClock | 0:1013288b8e43 | 159 | if (dMetroBPM == METROMIN) // BPM, with saturation notification. |
gatedClock | 0:1013288b8e43 | 160 | lcd.printf(" %5.2d BPM minimum",dMetroBPM); |
gatedClock | 0:1013288b8e43 | 161 | else |
gatedClock | 0:1013288b8e43 | 162 | if (dMetroBPM == METROMAX) |
gatedClock | 0:1013288b8e43 | 163 | lcd.printf(" %5.2d BPM maximum",dMetroBPM); |
gatedClock | 0:1013288b8e43 | 164 | else |
gatedClock | 0:1013288b8e43 | 165 | lcd.printf(" %5.2d BPM",dMetroBPM); |
gatedClock | 0:1013288b8e43 | 166 | |
gatedClock | 0:1013288b8e43 | 167 | LCD3; // line 3. |
gatedClock | 0:1013288b8e43 | 168 | |
gatedClock | 8:95f4f470ae28 | 169 | lcd.printf(" RTOS HW 4"); |
gatedClock | 0:1013288b8e43 | 170 | } |
gatedClock | 0:1013288b8e43 | 171 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 172 | // this metronome tick ISR will self-adjust to the current user-selected |
gatedClock | 0:1013288b8e43 | 173 | // metronome rate. that has to be done here, and at the start of the function, |
gatedClock | 0:1013288b8e43 | 174 | // in order to maintain a constant phase and to prevent a beat-skip. |
gatedClock | 0:1013288b8e43 | 175 | |
gatedClock | 0:1013288b8e43 | 176 | void interrupt_service_M() // metronome tick. |
gatedClock | 0:1013288b8e43 | 177 | { |
gatedClock | 0:1013288b8e43 | 178 | tickerMetronome.detach(); // only one attachment. |
gatedClock | 0:1013288b8e43 | 179 | tickerMetronome.attach(&interrupt_service_M,fMetroDelay); |
gatedClock | 0:1013288b8e43 | 180 | led3 = 1; |
gatedClock | 0:1013288b8e43 | 181 | timeoutDutyCycle.attach(&led3_off,fMetroDuty); |
gatedClock | 0:1013288b8e43 | 182 | } |
gatedClock | 0:1013288b8e43 | 183 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 184 | // this routine measures the number of seconds for which the joystick is |
gatedClock | 0:1013288b8e43 | 185 | // in the up or down position. for three ranges of time, three different |
gatedClock | 0:1013288b8e43 | 186 | // BPM rates-of-change are used. This means that as the user controls the |
gatedClock | 0:1013288b8e43 | 187 | // metronome rate using the joystick, at first it will change slowly, then |
gatedClock | 0:1013288b8e43 | 188 | // it will change at a moderate speed, then it will change quickly. |
gatedClock | 0:1013288b8e43 | 189 | // additionally, pressing the center joystick button will bring the metronome |
gatedClock | 0:1013288b8e43 | 190 | // back to 60BPM immediately, breaking BPM phase continuity. |
gatedClock | 0:1013288b8e43 | 191 | void interrupt_service_UD(void) // joystick up/down sample |
gatedClock | 0:1013288b8e43 | 192 | { |
gatedClock | 0:1013288b8e43 | 193 | int dPressedSeconds; // how many seconds joystick pressed. |
gatedClock | 0:1013288b8e43 | 194 | int dMultiCount; // slow count rate. |
gatedClock | 0:1013288b8e43 | 195 | char cDiscontinuity; // 1 = break phase & change BPM now. |
gatedClock | 0:1013288b8e43 | 196 | |
gatedClock | 0:1013288b8e43 | 197 | cDiscontinuity = 0; // don't break phase. |
gatedClock | 0:1013288b8e43 | 198 | |
gatedClock | 0:1013288b8e43 | 199 | // calculate slow rate period. |
gatedClock | 0:1013288b8e43 | 200 | dMultiCount = (int) ((float) (1.0 / ((float) UDSAMPLERATE))); |
gatedClock | 0:1013288b8e43 | 201 | |
gatedClock | 3:8ff0c9c853be | 202 | if (dJoyStickUp) // joystick up. |
gatedClock | 0:1013288b8e43 | 203 | { |
gatedClock | 0:1013288b8e43 | 204 | // rate-range calculations. |
gatedClock | 0:1013288b8e43 | 205 | dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE); |
gatedClock | 0:1013288b8e43 | 206 | if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM ++;} |
gatedClock | 0:1013288b8e43 | 207 | else |
gatedClock | 0:1013288b8e43 | 208 | if (dPressedSeconds < 10) dMetroBPM++; |
gatedClock | 0:1013288b8e43 | 209 | else dMetroBPM += 5; |
gatedClock | 0:1013288b8e43 | 210 | lUpDownHowMany++; // joystick holddown time. |
gatedClock | 0:1013288b8e43 | 211 | } |
gatedClock | 0:1013288b8e43 | 212 | else |
gatedClock | 3:8ff0c9c853be | 213 | if (dJoyStickDown) // joystick down. |
gatedClock | 0:1013288b8e43 | 214 | { |
gatedClock | 0:1013288b8e43 | 215 | // rate-range calculations. |
gatedClock | 0:1013288b8e43 | 216 | dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE); |
gatedClock | 0:1013288b8e43 | 217 | if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM --;} |
gatedClock | 0:1013288b8e43 | 218 | else |
gatedClock | 0:1013288b8e43 | 219 | if (dPressedSeconds < 10) dMetroBPM--; |
gatedClock | 0:1013288b8e43 | 220 | else dMetroBPM -= 5; |
gatedClock | 0:1013288b8e43 | 221 | lUpDownHowMany++; // joystick holddown time. |
gatedClock | 0:1013288b8e43 | 222 | } |
gatedClock | 0:1013288b8e43 | 223 | else lUpDownHowMany = 0; // clear when not up or down. |
gatedClock | 0:1013288b8e43 | 224 | |
gatedClock | 3:8ff0c9c853be | 225 | if (dJoyStickCenter) |
gatedClock | 0:1013288b8e43 | 226 | { |
gatedClock | 0:1013288b8e43 | 227 | dMetroBPM = 60; // center-button -> 60BPM. |
gatedClock | 0:1013288b8e43 | 228 | cDiscontinuity = 1; // pending phase-break. |
gatedClock | 0:1013288b8e43 | 229 | } |
gatedClock | 0:1013288b8e43 | 230 | // saturate metronome BPM. |
gatedClock | 0:1013288b8e43 | 231 | if (dMetroBPM > METROMAX) dMetroBPM = METROMAX; |
gatedClock | 0:1013288b8e43 | 232 | if (dMetroBPM < METROMIN) dMetroBPM = METROMIN; |
gatedClock | 0:1013288b8e43 | 233 | |
gatedClock | 0:1013288b8e43 | 234 | fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time. |
gatedClock | 0:1013288b8e43 | 235 | |
gatedClock | 0:1013288b8e43 | 236 | if (cDiscontinuity) // implement 60BPS now. |
gatedClock | 0:1013288b8e43 | 237 | { |
gatedClock | 0:1013288b8e43 | 238 | tickerMetronome.detach(); // only one attachment. |
gatedClock | 0:1013288b8e43 | 239 | tickerMetronome.attach(&interrupt_service_M,fMetroDelay); |
gatedClock | 0:1013288b8e43 | 240 | } |
gatedClock | 0:1013288b8e43 | 241 | |
gatedClock | 0:1013288b8e43 | 242 | } |
gatedClock | 0:1013288b8e43 | 243 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 244 | void led3_off(void) {led3 = 0;} // turn off the LED. |
gatedClock | 0:1013288b8e43 | 245 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:1013288b8e43 | 246 | void led3_on( void) {led3 = 1;} // turn on the led. |
gatedClock | 0:1013288b8e43 | 247 | /*----------------------------------------------//----------------------------*/ |