RTOS homework 4

Dependencies:   C12832_lcd mbed

Committer:
gatedClock
Date:
Sun Aug 18 15:13:19 2013 +0000
Revision:
1:5f41e2df0e85
Parent:
0:1013288b8e43
Child:
2:1a3acbc4d799
in progress.

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 0:1013288b8e43 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 0:1013288b8e43 55 DigitalIn joyStickUp (p15); // 1 if joystick up pressed.
gatedClock 0:1013288b8e43 56 DigitalIn joyStickDown (p12); // 1 if joystick down pressed.
gatedClock 0:1013288b8e43 57 DigitalIn joyStickCenter(p14); // 1 if joystick middle pressed.
gatedClock 0:1013288b8e43 58
gatedClock 0:1013288b8e43 59 DigitalOut led3(LED1); // leftmost LED.
gatedClock 0:1013288b8e43 60
gatedClock 0:1013288b8e43 61 Ticker tickerMetronome; // blinking LED.
gatedClock 0:1013288b8e43 62 Ticker tickerJoystickUD; // joystick up/down sample.
gatedClock 0:1013288b8e43 63 Ticker tickerLCD; // display ticker.
gatedClock 0:1013288b8e43 64 Timeout timeoutDutyCycle; // LED duty cycle delay.
gatedClock 0:1013288b8e43 65 //-------prototypes-----------------------------//------------------------------
gatedClock 0:1013288b8e43 66 void initialization(); // initialize settings.
gatedClock 0:1013288b8e43 67 void lcd_display(); // display on LCD.
gatedClock 0:1013288b8e43 68 void interrupt_service_M(); // metronome tick.
gatedClock 0:1013288b8e43 69 void interrupt_service_UD(); // joystick up/down sample.
gatedClock 0:1013288b8e43 70 void led3_off(); // attachable LED control.
gatedClock 0:1013288b8e43 71 void led3_on(); // attachable LED control.
gatedClock 1:5f41e2df0e85 72 void ISR_up();
gatedClock 1:5f41e2df0e85 73 void ISR_down();
gatedClock 1:5f41e2df0e85 74 void ISR_right();
gatedClock 1:5f41e2df0e85 75 void ISR_left();
gatedClock 1:5f41e2df0e85 76 void ISR_center();
gatedClock 0:1013288b8e43 77 //==============================================//==============================
gatedClock 0:1013288b8e43 78 int main(void)
gatedClock 0:1013288b8e43 79 {
gatedClock 0:1013288b8e43 80 initialization(); // initialize settings.
gatedClock 0:1013288b8e43 81
gatedClock 0:1013288b8e43 82 // metronome ticker.
gatedClock 0:1013288b8e43 83 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 0:1013288b8e43 84
gatedClock 0:1013288b8e43 85 // LCD ticker.
gatedClock 0:1013288b8e43 86 tickerLCD.attach(&lcd_display,LCDSAMPLERATE);
gatedClock 0:1013288b8e43 87
gatedClock 0:1013288b8e43 88 // up/down joystick sampling.
gatedClock 0:1013288b8e43 89 tickerJoystickUD.attach(&interrupt_service_UD,UDSAMPLERATE);
gatedClock 0:1013288b8e43 90
gatedClock 0:1013288b8e43 91 while (1) // main loop.
gatedClock 0:1013288b8e43 92 {
gatedClock 0:1013288b8e43 93 wait(10.0); // it's all interrupt-driven.
gatedClock 0:1013288b8e43 94 }
gatedClock 0:1013288b8e43 95 }
gatedClock 0:1013288b8e43 96 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 97 void initialization(void) // program initializations.
gatedClock 0:1013288b8e43 98 {
gatedClock 0:1013288b8e43 99 dMetroBPM = 60; // initialize to 60BPM.
gatedClock 0:1013288b8e43 100 fMetroDelay = 60.0 / (float) (dMetroBPM);
gatedClock 0:1013288b8e43 101 fMetroDuty = PULSELENGTH; // initialize LED on-duration.
gatedClock 0:1013288b8e43 102 lUpDownHowMany = 0;
gatedClock 0:1013288b8e43 103 }
gatedClock 0:1013288b8e43 104 /*----------------------------------------------//----------------------------*/
gatedClock 1:5f41e2df0e85 105 void ISR_up(void)
gatedClock 1:5f41e2df0e85 106 {
gatedClock 1:5f41e2df0e85 107 }
gatedClock 1:5f41e2df0e85 108 /*----------------------------------------------//----------------------------*/
gatedClock 1:5f41e2df0e85 109 void ISR_down(void)
gatedClock 1:5f41e2df0e85 110 {
gatedClock 1:5f41e2df0e85 111 }
gatedClock 1:5f41e2df0e85 112 /*----------------------------------------------//----------------------------*/
gatedClock 1:5f41e2df0e85 113 void ISR_left(void)
gatedClock 1:5f41e2df0e85 114 {
gatedClock 1:5f41e2df0e85 115 }
gatedClock 1:5f41e2df0e85 116 /*----------------------------------------------//----------------------------*/
gatedClock 1:5f41e2df0e85 117 void ISR_right(void)
gatedClock 1:5f41e2df0e85 118 {
gatedClock 1:5f41e2df0e85 119 }
gatedClock 1:5f41e2df0e85 120 /*----------------------------------------------//----------------------------*/
gatedClock 1:5f41e2df0e85 121 void ISR_center(void)
gatedClock 1:5f41e2df0e85 122 {
gatedClock 1:5f41e2df0e85 123 }
gatedClock 1:5f41e2df0e85 124 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 125 void lcd_display(void) // display metronome info.
gatedClock 0:1013288b8e43 126 {
gatedClock 0:1013288b8e43 127 lcd.cls(); // clear display.
gatedClock 0:1013288b8e43 128
gatedClock 0:1013288b8e43 129 LCD1; // line 1.
gatedClock 0:1013288b8e43 130 lcd.printf(" METRONOME");
gatedClock 0:1013288b8e43 131
gatedClock 0:1013288b8e43 132 LCD2; // line 2.
gatedClock 0:1013288b8e43 133
gatedClock 0:1013288b8e43 134 if (dMetroBPM == METROMIN) // BPM, with saturation notification.
gatedClock 0:1013288b8e43 135 lcd.printf(" %5.2d BPM minimum",dMetroBPM);
gatedClock 0:1013288b8e43 136 else
gatedClock 0:1013288b8e43 137 if (dMetroBPM == METROMAX)
gatedClock 0:1013288b8e43 138 lcd.printf(" %5.2d BPM maximum",dMetroBPM);
gatedClock 0:1013288b8e43 139 else
gatedClock 0:1013288b8e43 140 lcd.printf(" %5.2d BPM",dMetroBPM);
gatedClock 0:1013288b8e43 141
gatedClock 0:1013288b8e43 142 LCD3; // line 3.
gatedClock 0:1013288b8e43 143
gatedClock 0:1013288b8e43 144 lcd.printf(" RTOS HW 3 PART 1");
gatedClock 0:1013288b8e43 145 }
gatedClock 0:1013288b8e43 146 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 147 // this metronome tick ISR will self-adjust to the current user-selected
gatedClock 0:1013288b8e43 148 // metronome rate. that has to be done here, and at the start of the function,
gatedClock 0:1013288b8e43 149 // in order to maintain a constant phase and to prevent a beat-skip.
gatedClock 0:1013288b8e43 150
gatedClock 0:1013288b8e43 151 void interrupt_service_M() // metronome tick.
gatedClock 0:1013288b8e43 152 {
gatedClock 0:1013288b8e43 153 tickerMetronome.detach(); // only one attachment.
gatedClock 0:1013288b8e43 154 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 0:1013288b8e43 155 led3 = 1;
gatedClock 0:1013288b8e43 156 timeoutDutyCycle.attach(&led3_off,fMetroDuty);
gatedClock 0:1013288b8e43 157 }
gatedClock 0:1013288b8e43 158 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 159 // this routine measures the number of seconds for which the joystick is
gatedClock 0:1013288b8e43 160 // in the up or down position. for three ranges of time, three different
gatedClock 0:1013288b8e43 161 // BPM rates-of-change are used. This means that as the user controls the
gatedClock 0:1013288b8e43 162 // metronome rate using the joystick, at first it will change slowly, then
gatedClock 0:1013288b8e43 163 // it will change at a moderate speed, then it will change quickly.
gatedClock 0:1013288b8e43 164 // additionally, pressing the center joystick button will bring the metronome
gatedClock 0:1013288b8e43 165 // back to 60BPM immediately, breaking BPM phase continuity.
gatedClock 0:1013288b8e43 166 void interrupt_service_UD(void) // joystick up/down sample
gatedClock 0:1013288b8e43 167 {
gatedClock 0:1013288b8e43 168 int dPressedSeconds; // how many seconds joystick pressed.
gatedClock 0:1013288b8e43 169 int dMultiCount; // slow count rate.
gatedClock 0:1013288b8e43 170 char cDiscontinuity; // 1 = break phase & change BPM now.
gatedClock 0:1013288b8e43 171
gatedClock 0:1013288b8e43 172 cDiscontinuity = 0; // don't break phase.
gatedClock 0:1013288b8e43 173
gatedClock 0:1013288b8e43 174 // calculate slow rate period.
gatedClock 0:1013288b8e43 175 dMultiCount = (int) ((float) (1.0 / ((float) UDSAMPLERATE)));
gatedClock 0:1013288b8e43 176
gatedClock 0:1013288b8e43 177 if (joyStickUp) // joystick up.
gatedClock 0:1013288b8e43 178 {
gatedClock 0:1013288b8e43 179 // rate-range calculations.
gatedClock 0:1013288b8e43 180 dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
gatedClock 0:1013288b8e43 181 if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM ++;}
gatedClock 0:1013288b8e43 182 else
gatedClock 0:1013288b8e43 183 if (dPressedSeconds < 10) dMetroBPM++;
gatedClock 0:1013288b8e43 184 else dMetroBPM += 5;
gatedClock 0:1013288b8e43 185 lUpDownHowMany++; // joystick holddown time.
gatedClock 0:1013288b8e43 186 }
gatedClock 0:1013288b8e43 187 else
gatedClock 0:1013288b8e43 188 if (joyStickDown) // joystick down.
gatedClock 0:1013288b8e43 189 {
gatedClock 0:1013288b8e43 190 // rate-range calculations.
gatedClock 0:1013288b8e43 191 dPressedSeconds = (int) (((float) lUpDownHowMany) * UDSAMPLERATE);
gatedClock 0:1013288b8e43 192 if (dPressedSeconds < 5) {if (!(lUpDownHowMany % dMultiCount)) dMetroBPM --;}
gatedClock 0:1013288b8e43 193 else
gatedClock 0:1013288b8e43 194 if (dPressedSeconds < 10) dMetroBPM--;
gatedClock 0:1013288b8e43 195 else dMetroBPM -= 5;
gatedClock 0:1013288b8e43 196 lUpDownHowMany++; // joystick holddown time.
gatedClock 0:1013288b8e43 197 }
gatedClock 0:1013288b8e43 198 else lUpDownHowMany = 0; // clear when not up or down.
gatedClock 0:1013288b8e43 199
gatedClock 0:1013288b8e43 200 if (joyStickCenter)
gatedClock 0:1013288b8e43 201 {
gatedClock 0:1013288b8e43 202 dMetroBPM = 60; // center-button -> 60BPM.
gatedClock 0:1013288b8e43 203 cDiscontinuity = 1; // pending phase-break.
gatedClock 0:1013288b8e43 204 }
gatedClock 0:1013288b8e43 205 // saturate metronome BPM.
gatedClock 0:1013288b8e43 206 if (dMetroBPM > METROMAX) dMetroBPM = METROMAX;
gatedClock 0:1013288b8e43 207 if (dMetroBPM < METROMIN) dMetroBPM = METROMIN;
gatedClock 0:1013288b8e43 208
gatedClock 0:1013288b8e43 209 fMetroDelay = 60.0 / (float) (dMetroBPM); // calculate Ticker delay time.
gatedClock 0:1013288b8e43 210
gatedClock 0:1013288b8e43 211 if (cDiscontinuity) // implement 60BPS now.
gatedClock 0:1013288b8e43 212 {
gatedClock 0:1013288b8e43 213 tickerMetronome.detach(); // only one attachment.
gatedClock 0:1013288b8e43 214 tickerMetronome.attach(&interrupt_service_M,fMetroDelay);
gatedClock 0:1013288b8e43 215 }
gatedClock 0:1013288b8e43 216
gatedClock 0:1013288b8e43 217 }
gatedClock 0:1013288b8e43 218 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 219 void led3_off(void) {led3 = 0;} // turn off the LED.
gatedClock 0:1013288b8e43 220 /*----------------------------------------------//----------------------------*/
gatedClock 0:1013288b8e43 221 void led3_on( void) {led3 = 1;} // turn on the led.
gatedClock 0:1013288b8e43 222 /*----------------------------------------------//----------------------------*/