homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Thu Sep 12 19:48:16 2013 +0000
Revision:
97:ee840478d6b3
Parent:
96:c8b0f29d9758
Child:
98:156298be5ba2
prevent total time change while countdown.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gatedClock 0:fcca4db7b32a 1 /*----------------------------------------------//------------------------------
gatedClock 0:fcca4db7b32a 2 student : m-moore
gatedClock 0:fcca4db7b32a 3 class : rtos
gatedClock 10:2b0a9fc39109 4 directory : RTOS_HW_07
gatedClock 0:fcca4db7b32a 5 file : main.cpp
gatedClock 0:fcca4db7b32a 6 ----description---------------------------------//------------------------------
gatedClock 70:7c0743c28b11 7 gotchyas
gatedClock 70:7c0743c28b11 8 1. using pc.printf inside a ticker routine will freeze the routine.
gatedClock 71:4a5f256ecf7c 9 2. using queues (get, put) will not work within a ticker routine.
gatedClock 76:74c454c9d75b 10 3. Ticker has a bug. http://mbed.org/questions/1563/Mbed-Tickerfunction-hangs-system-from-re/
gatedClock 80:e3e1a2161435 11
gatedClock 80:e3e1a2161435 12 improvements
gatedClock 80:e3e1a2161435 13 countdown timer needs to be sync'd to start button press.
gatedClock 87:147e2b08fae6 14 this could not be done, use granularity setting instead.
gatedClock 80:e3e1a2161435 15
gatedClock 80:e3e1a2161435 16
gatedClock 0:fcca4db7b32a 17 -----includes-----------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 18 #include "mbed.h" // mbed class.
gatedClock 10:2b0a9fc39109 19 #include "rtos.h" // rtos class.
gatedClock 39:4e7e4d935a87 20 #include "LM75B.h" // thermometer class.
gatedClock 10:2b0a9fc39109 21 #include "C12832_lcd.h" // LCD class.
gatedClock 0:fcca4db7b32a 22 //---defines------------------------------------//------------------------------
gatedClock 9:cfdb9aa5857c 23 #define LCD1 lcd.locate(0, 0); // LCD line 1.
gatedClock 9:cfdb9aa5857c 24 #define LCD2 lcd.locate(0,11); // LCD line 2.
gatedClock 9:cfdb9aa5857c 25 #define LCD3 lcd.locate(0,22); // LCD line 3.
gatedClock 9:cfdb9aa5857c 26
gatedClock 10:2b0a9fc39109 27
gatedClock 66:4a0006fa5cc1 28 #define DEBOUNCEmS 16 // debounce wait in mS.
gatedClock 19:92b11e30aaaf 29 #define uS_TIMEOUT 100 // Timer uS timeout.
gatedClock 19:92b11e30aaaf 30 #define LBSIG 1 // left button signal code.
gatedClock 33:34c1bef3c4ff 31 #define PIPEuS 1000 // pipeline clock period.
gatedClock 19:92b11e30aaaf 32 #define SLOWCLOCKuS 500000 // slow-clock period.
gatedClock 39:4e7e4d935a87 33 #define TEMPCLOCKS 1 // temperature-measuring period in S.
gatedClock 26:bff592483cb1 34 #define PIPEDATASIZE 8 // dimension of tPipeData.
gatedClock 44:d16e813e61ef 35 #define THREAD_1_WAITmS 400 // thread 1 wait in mS.
gatedClock 91:b54500888b67 36 #define THREAD_2_WAITmS 1 // LCD thread wait.
gatedClock 67:1d9c85a4c3c1 37 #define THREAD_3_WAITmS 80 // thread 3 wait in mS.
gatedClock 66:4a0006fa5cc1 38 #define THREAD_4_WAITmS 1 // thread 4 wait in mS.
gatedClock 78:7df160e0db7b 39 #define THREAD_5_WAITmS 1 // FSM thread wait.
gatedClock 44:d16e813e61ef 40 #define HB_MODULO 1024 // heartbeat modulo divisor.
gatedClock 50:2928c3cbdcc3 41
gatedClock 54:b0e7352d2516 42 #define MSG_INC_TIME 0x01
gatedClock 54:b0e7352d2516 43 #define MSG_DEC_TIME 0x02
gatedClock 58:ec630b6dd9b1 44 #define MSG_START 0x04
gatedClock 58:ec630b6dd9b1 45 #define MSG_STOP 0x08
gatedClock 58:ec630b6dd9b1 46 #define MSG_OPEN 0x10
gatedClock 58:ec630b6dd9b1 47 #define MSG_CLOSED 0x20
gatedClock 57:0432c68ad232 48
gatedClock 78:7df160e0db7b 49 #define FSM_IDLE 0x01 // cook-state state-machine.
gatedClock 78:7df160e0db7b 50 #define FSM_COOK 0x02 // cook-state state-machine.
gatedClock 78:7df160e0db7b 51 #define FSM_PAUSE 0x04 // cook-state state-machine.
gatedClock 78:7df160e0db7b 52 #define FSM_CONTINUE 0x08 // cook-state state-machine.
gatedClock 78:7df160e0db7b 53 #define FSM_DONE 0x10 // cook-state state-machine.
gatedClock 65:e39360da5929 54
gatedClock 71:4a5f256ecf7c 55 #define RT_PRELOAD 0x01 // remaining-time preload.
gatedClock 71:4a5f256ecf7c 56 #define RT_DECREMENT 0x02 // remaining-time decrement.
gatedClock 71:4a5f256ecf7c 57 #define RT_PAUSE 0x03 // remaining-time don't change.
gatedClock 71:4a5f256ecf7c 58 #define RT_CLEAR 0x04 // remaining-time set to zero.
gatedClock 71:4a5f256ecf7c 59
gatedClock 97:ee840478d6b3 60 #define TEMP_READ 0x01 // temperature synthesizer control.
gatedClock 97:ee840478d6b3 61 #define TEMP_CALC 0x02 // temperature synthesizer control.
gatedClock 97:ee840478d6b3 62 #define TEMP_FREEZE 0x03 // temperature synthesizer control.
gatedClock 94:551b1df4b10c 63
gatedClock 86:388c2b4b7cf5 64 #define GRANULARITY 0x400 // 1-second countdown ticker granularity.
gatedClock 88:0b1b812945eb 65 #define MAXSECONDS 180 // maximum microwave-on time.
gatedClock 89:290c96cd027f 66 #define BEEPTIME 3 // beep duration in S.
gatedClock 94:551b1df4b10c 67 #define BEEPFREQ 300 // beep frequency in Hz.
gatedClock 86:388c2b4b7cf5 68
gatedClock 65:e39360da5929 69 #define DEBUG1 // debug preprocessor control.
gatedClock 0:fcca4db7b32a 70 //--global_definitions--------------------------//------------------------------
gatedClock 26:bff592483cb1 71 struct tButtons // button ISR updates.
gatedClock 26:bff592483cb1 72 {
gatedClock 26:bff592483cb1 73 char cLeftButton; // cooktime +60S.
gatedClock 26:bff592483cb1 74 char cRightButton; // cooktime -60S.
gatedClock 26:bff592483cb1 75 char cTopButton; // start cook.
gatedClock 26:bff592483cb1 76 char cBottomButton; // stop cook.
gatedClock 58:ec630b6dd9b1 77 char cCenterButton; // center button pressed.
gatedClock 53:8c2baf5623c8 78 char cDoorOpen; // door open.
gatedClock 26:bff592483cb1 79 };
gatedClock 34:b449d2a7c786 80
gatedClock 71:4a5f256ecf7c 81 struct tRemainingTime // remaining time related.
gatedClock 71:4a5f256ecf7c 82 {
gatedClock 71:4a5f256ecf7c 83 char cControl; // countdown control.
gatedClock 71:4a5f256ecf7c 84 int dTotalTime; // initialize to this.
gatedClock 71:4a5f256ecf7c 85 int dRemainingTime; // the countdown value.
gatedClock 71:4a5f256ecf7c 86 };
gatedClock 71:4a5f256ecf7c 87
gatedClock 80:e3e1a2161435 88 struct tLCD // LCD related.
gatedClock 80:e3e1a2161435 89 {
gatedClock 80:e3e1a2161435 90 int dTotalCookTime; // display time in seconds.
gatedClock 80:e3e1a2161435 91 int dRemainingTime; // display time in seconds.
gatedClock 80:e3e1a2161435 92 float fCelsius; // temperature in celsius.
gatedClock 80:e3e1a2161435 93 };
gatedClock 80:e3e1a2161435 94
gatedClock 80:e3e1a2161435 95
gatedClock 80:e3e1a2161435 96
gatedClock 61:8026a9fc0cf1 97 Queue<int, 1> queueModTotalTime; // message to modify total time.
gatedClock 61:8026a9fc0cf1 98 Queue<int, 1> queueUpdateFSM; // message to inform FSM.
gatedClock 61:8026a9fc0cf1 99 Queue<int, 1> queueUpdateRemainingTime; // message to update remaining time.
gatedClock 71:4a5f256ecf7c 100 Queue<int, 1> queueSetRemainingTime; // tell countdown it's start time.
gatedClock 69:55b836e8ced7 101 Queue<int, 1> queueFSMnewState; // latest FSM state.
gatedClock 92:be8d69aba1fc 102 Queue<int, 1> queueSpeakerControl; // speaker control queue.
gatedClock 94:551b1df4b10c 103 Queue<int, 1> queueTemperatureControl; // control the temperature synthesizer.
gatedClock 97:ee840478d6b3 104 Queue<int, 1> queueSuppressTimeAdjust; // total time control.
gatedClock 0:fcca4db7b32a 105 //--global_variables----------------------------//------------------------------
gatedClock 26:bff592483cb1 106 char gcSignalWaitEnable; // 1 to wait on a signal.
gatedClock 26:bff592483cb1 107 char gcSlowClock; // slow-clock signal.
gatedClock 33:34c1bef3c4ff 108 char gcInitializePipeline; // 1 to initialize pipeline state.
gatedClock 39:4e7e4d935a87 109 float gfCelsius; // from thermometer.
gatedClock 84:fee8fb80d190 110
gatedClock 26:bff592483cb1 111 tButtons gtButtons; // ISR button updates.
gatedClock 70:7c0743c28b11 112
gatedClock 71:4a5f256ecf7c 113 tRemainingTime giRemainingTime; // structure instance.
gatedClock 80:e3e1a2161435 114 tLCD giLCD; // structure instance.
gatedClock 71:4a5f256ecf7c 115
gatedClock 70:7c0743c28b11 116 int gdDiagTotalTime;
gatedClock 0:fcca4db7b32a 117 //--global_instances----------------------------//------------------------------
gatedClock 55:17f3354da63a 118 Serial pc(USBTX, USBRX); // PuTTY terminal communication.
gatedClock 39:4e7e4d935a87 119 LM75B temperature(p28,p27); // on-board thermometer.
gatedClock 9:cfdb9aa5857c 120 C12832_LCD lcd; // LCD object.
gatedClock 53:8c2baf5623c8 121 DigitalOut led0(LED4); // magnetron.
gatedClock 39:4e7e4d935a87 122 DigitalOut led1(LED3);
gatedClock 39:4e7e4d935a87 123 DigitalOut led2(LED2);
gatedClock 39:4e7e4d935a87 124 DigitalOut led3(LED1);
gatedClock 91:b54500888b67 125 DigitalOut speaker(p26); // speaker device.
gatedClock 0:fcca4db7b32a 126
gatedClock 0:fcca4db7b32a 127 InterruptIn iJoyStickUp (p15); // joystick up rising edge.
gatedClock 0:fcca4db7b32a 128 InterruptIn iJoyStickDown (p12); // joystick down rising edge.
gatedClock 0:fcca4db7b32a 129 InterruptIn iJoyStickLeft (p13); // joystick left rising edge.
gatedClock 0:fcca4db7b32a 130 InterruptIn iJoyStickRight (p16); // joystick right rising edge.
gatedClock 0:fcca4db7b32a 131 InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed.
gatedClock 66:4a0006fa5cc1 132
gatedClock 66:4a0006fa5cc1 133 Timer debounceTimer; // button debounce timer.
gatedClock 89:290c96cd027f 134 Timer beepTimer; // beep-duration timer.
gatedClock 94:551b1df4b10c 135 Timer temperatureTimer; // how often to raise temperature.
gatedClock 0:fcca4db7b32a 136
gatedClock 50:2928c3cbdcc3 137 Ticker tickerButtonStateManager; // manage the button states.
gatedClock 19:92b11e30aaaf 138 Ticker tickerSlowClock; // generate a ~1Hz clock.
gatedClock 77:73e4fd83642f 139 Ticker tickerCookCountdown; // remaining cook time.
gatedClock 63:63f362bcc2ac 140
gatedClock 63:63f362bcc2ac 141 // Timer timerFSMdone; // duration of FSM 'done' state.
gatedClock 40:7afff79f0d8b 142
gatedClock 0:fcca4db7b32a 143 //-------prototypes-----------------------------//------------------------------
gatedClock 49:56f790977983 144
gatedClock 19:92b11e30aaaf 145 void slowClock(); // 1Hz or thereabouts.
gatedClock 49:56f790977983 146
gatedClock 9:cfdb9aa5857c 147 void initialization(); // initialize settings.
gatedClock 13:21f27ba467c2 148
gatedClock 13:21f27ba467c2 149 void ISRleftButtonRising(); // cook-time increase.
gatedClock 13:21f27ba467c2 150 void ISRleftButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 151 void ISRrightButtonRising(); // cook-time decrease.
gatedClock 13:21f27ba467c2 152 void ISRrightButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 153 void ISRtopButtonRising(); // cook start.
gatedClock 13:21f27ba467c2 154 void ISRtopButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 155 void ISRbottomButtonRising(); // cook stop.
gatedClock 13:21f27ba467c2 156 void ISRbottomButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 157 void ISRcenterButtonRising(); // door state toggle.
gatedClock 13:21f27ba467c2 158 void ISRcenterButtonFalling(); // button-release debounce.
gatedClock 16:db7f0b3b2605 159 void disableSignalWaiting(); // break from signal waiting.
gatedClock 39:4e7e4d935a87 160
gatedClock 54:b0e7352d2516 161 void threadButtonStateManager(void const *args);
gatedClock 50:2928c3cbdcc3 162 void threadTotalTimeControl(void const *args);
gatedClock 57:0432c68ad232 163 void threadCookStateFSM(void const *args);
gatedClock 71:4a5f256ecf7c 164 void tickCookRemainingTime(); // remaining time countdown.
gatedClock 50:2928c3cbdcc3 165
gatedClock 40:7afff79f0d8b 166 void temperatureThread(void const *args); // temperature measurement.
gatedClock 42:266d5bbbfd19 167 void LCDthread (void const *args); // LCD display thread.
gatedClock 92:be8d69aba1fc 168 void speakerThread(void const *args); // speaker thread.
gatedClock 0:fcca4db7b32a 169 //==============================================//==============================
gatedClock 0:fcca4db7b32a 170 int main(void)
gatedClock 0:fcca4db7b32a 171 {
gatedClock 16:db7f0b3b2605 172 char cLeftButtonState; // 1 means button was pressed.
gatedClock 16:db7f0b3b2605 173
gatedClock 16:db7f0b3b2605 174
gatedClock 16:db7f0b3b2605 175
gatedClock 14:d3bb343cd5b2 176 iJoyStickLeft.rise (&ISRleftButtonRising);
gatedClock 14:d3bb343cd5b2 177 iJoyStickLeft.fall (&ISRleftButtonFalling);
gatedClock 9:cfdb9aa5857c 178
gatedClock 14:d3bb343cd5b2 179 iJoyStickRight.rise(&ISRrightButtonRising);
gatedClock 14:d3bb343cd5b2 180 iJoyStickRight.fall(&ISRrightButtonFalling);
gatedClock 14:d3bb343cd5b2 181
gatedClock 14:d3bb343cd5b2 182 iJoyStickUp.rise (&ISRtopButtonRising);
gatedClock 14:d3bb343cd5b2 183 iJoyStickUp.fall (&ISRtopButtonFalling);
gatedClock 9:cfdb9aa5857c 184
gatedClock 14:d3bb343cd5b2 185 iJoyStickDown.rise (&ISRbottomButtonRising);
gatedClock 14:d3bb343cd5b2 186 iJoyStickDown.fall (&ISRbottomButtonFalling);
gatedClock 7:9fbd1d540863 187
gatedClock 14:d3bb343cd5b2 188 iJoyStickCenter.rise(&ISRcenterButtonRising);
gatedClock 14:d3bb343cd5b2 189 iJoyStickCenter.fall(&ISRcenterButtonFalling);
gatedClock 9:cfdb9aa5857c 190
gatedClock 66:4a0006fa5cc1 191 debounceTimer.start(); // kick-off debounce timer.
gatedClock 66:4a0006fa5cc1 192
gatedClock 33:34c1bef3c4ff 193 gcInitializePipeline = 1; // tell pipeline to initialize.
gatedClock 16:db7f0b3b2605 194
gatedClock 9:cfdb9aa5857c 195 initialization(); // initialize variables.
gatedClock 21:b794d189c36b 196 gcSlowClock = 0;
gatedClock 44:d16e813e61ef 197 led1 = 0;
gatedClock 53:8c2baf5623c8 198 gtButtons.cDoorOpen = 0; // initialize with door closed.
gatedClock 63:63f362bcc2ac 199
gatedClock 63:63f362bcc2ac 200 // timerFSMdone.start(); // start 'done' timer.
gatedClock 49:56f790977983 201
gatedClock 86:388c2b4b7cf5 202 // tickerSlowClock.attach_us(&slowClock ,SLOWCLOCKuS);
gatedClock 64:255295f1d782 203
gatedClock 75:c2894d531f42 204
gatedClock 86:388c2b4b7cf5 205 tickerCookCountdown.attach_us(&tickCookRemainingTime,1000000/GRANULARITY);
gatedClock 54:b0e7352d2516 206
gatedClock 40:7afff79f0d8b 207
gatedClock 70:7c0743c28b11 208 Thread thread_1(temperatureThread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 70:7c0743c28b11 209 Thread thread_2(LCDthread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 70:7c0743c28b11 210 Thread thread_3(threadTotalTimeControl ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 70:7c0743c28b11 211 Thread thread_4(threadButtonStateManager,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); // was osPriorityIdle
gatedClock 70:7c0743c28b11 212 Thread thread_5(threadCookStateFSM ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 70:7c0743c28b11 213
gatedClock 66:4a0006fa5cc1 214 //Thread thread_4(threadButtonStateManager);
gatedClock 52:8ba6a0c91a89 215
gatedClock 40:7afff79f0d8b 216
gatedClock 59:5e45b5e4a874 217 // the message-receiving threads need 'else' for message-receive-timeout.
gatedClock 91:b54500888b67 218
gatedClock 93:0e93e4a23f2b 219 // int dSeconds;
gatedClock 93:0e93e4a23f2b 220 // dSeconds = 8;
gatedClock 92:be8d69aba1fc 221
gatedClock 91:b54500888b67 222
gatedClock 93:0e93e4a23f2b 223 // speakerThread((void *) &dSeconds);
gatedClock 9:cfdb9aa5857c 224
gatedClock 14:d3bb343cd5b2 225 while(1)
gatedClock 0:fcca4db7b32a 226 {
gatedClock 75:c2894d531f42 227 // pc.printf("\n\r gdDiagTotalTime %d",gdDiagTotalTime);
gatedClock 70:7c0743c28b11 228
gatedClock 70:7c0743c28b11 229 Thread::wait(1000);
gatedClock 16:db7f0b3b2605 230
gatedClock 16:db7f0b3b2605 231
gatedClock 16:db7f0b3b2605 232
gatedClock 14:d3bb343cd5b2 233
gatedClock 0:fcca4db7b32a 234 }
gatedClock 19:92b11e30aaaf 235 }
gatedClock 19:92b11e30aaaf 236 /*----------------------------------------------//----------------------------*/
gatedClock 59:5e45b5e4a874 237
gatedClock 59:5e45b5e4a874 238 // this serves as the bottom-half for all of the button-press IRSs.
gatedClock 59:5e45b5e4a874 239 // it sends button-status messages to other threads, and it also
gatedClock 59:5e45b5e4a874 240 // clears the button-state globals to prepare them for the next
gatedClock 59:5e45b5e4a874 241 // button press.
gatedClock 59:5e45b5e4a874 242
gatedClock 59:5e45b5e4a874 243
gatedClock 54:b0e7352d2516 244 void threadButtonStateManager(void const *args)
gatedClock 50:2928c3cbdcc3 245 {
gatedClock 56:18cff6eb91db 246 int dMessage; // message.
gatedClock 50:2928c3cbdcc3 247
gatedClock 54:b0e7352d2516 248 while(1) // thread loop.
gatedClock 67:1d9c85a4c3c1 249 {
gatedClock 54:b0e7352d2516 250 //--- // TOTAL TIME CONTROL.
gatedClock 56:18cff6eb91db 251
gatedClock 56:18cff6eb91db 252 // encoded integers will be sent,
gatedClock 56:18cff6eb91db 253 // not pointers.
gatedClock 50:2928c3cbdcc3 254
gatedClock 50:2928c3cbdcc3 255 if (gtButtons.cLeftButton) // total time increment button.
gatedClock 50:2928c3cbdcc3 256 {
gatedClock 54:b0e7352d2516 257 dMessage = MSG_INC_TIME; // set message.
gatedClock 69:55b836e8ced7 258 queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer.
gatedClock 50:2928c3cbdcc3 259 gtButtons.cLeftButton = 0; // clear the button state.
gatedClock 50:2928c3cbdcc3 260 }
gatedClock 67:1d9c85a4c3c1 261
gatedClock 59:5e45b5e4a874 262 if (gtButtons.cRightButton) // total time decrement button.
gatedClock 50:2928c3cbdcc3 263 {
gatedClock 54:b0e7352d2516 264 dMessage = MSG_DEC_TIME; // set message.
gatedClock 69:55b836e8ced7 265 queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer.
gatedClock 50:2928c3cbdcc3 266 gtButtons.cRightButton = 0; // clear the button state.
gatedClock 50:2928c3cbdcc3 267 }
gatedClock 67:1d9c85a4c3c1 268
gatedClock 59:5e45b5e4a874 269 //--- // COOK-STATE FSM.
gatedClock 59:5e45b5e4a874 270
gatedClock 59:5e45b5e4a874 271 if (gtButtons.cTopButton) // start-cook button.
gatedClock 58:ec630b6dd9b1 272 {
gatedClock 59:5e45b5e4a874 273 dMessage = MSG_START; // set message.
gatedClock 69:55b836e8ced7 274 queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 275 gtButtons.cTopButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 276 }
gatedClock 67:1d9c85a4c3c1 277
gatedClock 59:5e45b5e4a874 278 if (gtButtons.cBottomButton) // stop-cook button.
gatedClock 58:ec630b6dd9b1 279 {
gatedClock 59:5e45b5e4a874 280 dMessage = MSG_STOP; // set message.
gatedClock 69:55b836e8ced7 281 queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 282 gtButtons.cBottomButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 283 }
gatedClock 67:1d9c85a4c3c1 284
gatedClock 59:5e45b5e4a874 285 if (gtButtons.cCenterButton) // door-state-toggle.
gatedClock 58:ec630b6dd9b1 286 {
gatedClock 59:5e45b5e4a874 287 dMessage = gtButtons.cDoorOpen; // determined in ISR.
gatedClock 69:55b836e8ced7 288 queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 289 gtButtons.cCenterButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 290 }
gatedClock 59:5e45b5e4a874 291 //---
gatedClock 58:ec630b6dd9b1 292
gatedClock 54:b0e7352d2516 293 Thread::wait(THREAD_4_WAITmS); // multitasking.
gatedClock 54:b0e7352d2516 294 } // thread loop.
gatedClock 54:b0e7352d2516 295 } // threadButtonStateManager.
gatedClock 50:2928c3cbdcc3 296 /*----------------------------------------------//----------------------------*/
gatedClock 51:e5ec74c49b01 297 // the incoming messages are mutually-exclusive.
gatedClock 51:e5ec74c49b01 298 // total time controller.
gatedClock 51:e5ec74c49b01 299 void threadTotalTimeControl(void const *args)
gatedClock 50:2928c3cbdcc3 300 {
gatedClock 51:e5ec74c49b01 301 static int dTotalTime = 0; // total time variable.
gatedClock 60:e10bf95bbc96 302 int dMessage; // message.
gatedClock 97:ee840478d6b3 303 int dSuppress; // 1 to suppress operation.
gatedClock 51:e5ec74c49b01 304 osEvent queueEvent; // queue event.
gatedClock 70:7c0743c28b11 305 int dRC;
gatedClock 50:2928c3cbdcc3 306
gatedClock 97:ee840478d6b3 307 dSuppress = 0; // initialize.
gatedClock 97:ee840478d6b3 308
gatedClock 50:2928c3cbdcc3 309 while(1) // thread loop.
gatedClock 50:2928c3cbdcc3 310 {
gatedClock 50:2928c3cbdcc3 311
gatedClock 97:ee840478d6b3 312
gatedClock 97:ee840478d6b3 313 // suppression mail from FSM.
gatedClock 97:ee840478d6b3 314 queueEvent = queueSuppressTimeAdjust.get(1);
gatedClock 97:ee840478d6b3 315 if (queueEvent.status == osEventMessage)
gatedClock 97:ee840478d6b3 316 {
gatedClock 97:ee840478d6b3 317 dSuppress = (int) queueEvent.value.p;
gatedClock 97:ee840478d6b3 318 }
gatedClock 97:ee840478d6b3 319
gatedClock 97:ee840478d6b3 320
gatedClock 97:ee840478d6b3 321
gatedClock 97:ee840478d6b3 322
gatedClock 55:17f3354da63a 323 queueEvent = queueModTotalTime.get(1); // get message.
gatedClock 51:e5ec74c49b01 324 if (queueEvent.status == osEventMessage)
gatedClock 51:e5ec74c49b01 325 {
gatedClock 56:18cff6eb91db 326 dMessage = (int) queueEvent.value.p; // interpret as integer, not pointer.
gatedClock 54:b0e7352d2516 327
gatedClock 97:ee840478d6b3 328 if (!dSuppress) // increment total time.
gatedClock 56:18cff6eb91db 329 if (dMessage == MSG_INC_TIME) dTotalTime += 60;
gatedClock 54:b0e7352d2516 330
gatedClock 97:ee840478d6b3 331 if (!dSuppress) // decrement total time.
gatedClock 56:18cff6eb91db 332 if (dMessage == MSG_DEC_TIME) dTotalTime -= 60;
gatedClock 65:e39360da5929 333
gatedClock 65:e39360da5929 334 // saturations.
gatedClock 65:e39360da5929 335 if (dTotalTime > 180) dTotalTime = 180;
gatedClock 65:e39360da5929 336 if (dTotalTime < 0) dTotalTime = 0;
gatedClock 65:e39360da5929 337
gatedClock 70:7c0743c28b11 338 dRC = queueSetRemainingTime.put((int *) dTotalTime,1);
gatedClock 75:c2894d531f42 339 giRemainingTime.dTotalTime = dTotalTime;
gatedClock 51:e5ec74c49b01 340 }
gatedClock 97:ee840478d6b3 341
gatedClock 81:12bc26973cb8 342
gatedClock 50:2928c3cbdcc3 343 Thread::wait(THREAD_3_WAITmS); // multitasking.
gatedClock 50:2928c3cbdcc3 344 } // thread loop.
gatedClock 50:2928c3cbdcc3 345 } // threadTotalTimeControl.
gatedClock 33:34c1bef3c4ff 346 /*----------------------------------------------//----------------------------*/
gatedClock 81:12bc26973cb8 347
gatedClock 57:0432c68ad232 348 void threadCookStateFSM(void const *args) // cook-cycle FSM.
gatedClock 57:0432c68ad232 349 {
gatedClock 83:2e01f2a799ba 350 int dFSMstate = FSM_IDLE; // state of this FSM.
gatedClock 83:2e01f2a799ba 351 int dFSMstateLast = FSM_IDLE; // previous FSM state.
gatedClock 83:2e01f2a799ba 352 int dButtonState; // received button state.
gatedClock 83:2e01f2a799ba 353 int dRemainingTime = 0; // received remaining time.
gatedClock 62:48e7c196e2a5 354
gatedClock 83:2e01f2a799ba 355 int dButtonStart = 0;
gatedClock 83:2e01f2a799ba 356 int dButtonStop = 0;
gatedClock 83:2e01f2a799ba 357 int dDoorOpen = 0;
gatedClock 93:0e93e4a23f2b 358 int dBeepSeconds = 0;
gatedClock 81:12bc26973cb8 359
gatedClock 81:12bc26973cb8 360 osEvent queueEvent; // from button state manager.
gatedClock 89:290c96cd027f 361 beepTimer.start(); // run the beep timer.
gatedClock 75:c2894d531f42 362
gatedClock 93:0e93e4a23f2b 363 dBeepSeconds = BEEPTIME;
gatedClock 63:63f362bcc2ac 364
gatedClock 94:551b1df4b10c 365 // queueModTotalTime.put((int *) dMessage,1);// pre
gatedClock 94:551b1df4b10c 366
gatedClock 94:551b1df4b10c 367
gatedClock 94:551b1df4b10c 368 queueTemperatureControl.put((int *) TEMP_READ,1);
gatedClock 94:551b1df4b10c 369
gatedClock 57:0432c68ad232 370 while(1) // thread loop.
gatedClock 57:0432c68ad232 371 {
gatedClock 81:12bc26973cb8 372
gatedClock 74:4debb8f2e21d 373 switch (dFSMstate) // cook-mode state machine.
gatedClock 62:48e7c196e2a5 374 {
gatedClock 82:73fa3fe8a217 375 //---
gatedClock 73:44739860198b 376 case FSM_IDLE : // IDLE.
gatedClock 73:44739860198b 377 {
gatedClock 89:290c96cd027f 378 led0 = 0;
gatedClock 75:c2894d531f42 379 if (dFSMstate != dFSMstateLast) // if just entered state.
gatedClock 94:551b1df4b10c 380 {
gatedClock 94:551b1df4b10c 381 queueTemperatureControl.put((int *) TEMP_READ,1);
gatedClock 97:ee840478d6b3 382 queueSuppressTimeAdjust.put((int*) 0,1);
gatedClock 75:c2894d531f42 383 }
gatedClock 80:e3e1a2161435 384
gatedClock 80:e3e1a2161435 385 giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
gatedClock 80:e3e1a2161435 386 giLCD.dRemainingTime = 0; // suppress remaining time display.
gatedClock 94:551b1df4b10c 387 // giLCD.fCelsius = 0;
gatedClock 80:e3e1a2161435 388
gatedClock 78:7df160e0db7b 389 giRemainingTime.cControl = RT_PRELOAD;
gatedClock 76:74c454c9d75b 390 dFSMstateLast = dFSMstate; // determine next state.
gatedClock 75:c2894d531f42 391 if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_COOK;
gatedClock 73:44739860198b 392 break;
gatedClock 73:44739860198b 393 }
gatedClock 82:73fa3fe8a217 394 //---
gatedClock 73:44739860198b 395 case FSM_COOK : // COOK.
gatedClock 73:44739860198b 396 {
gatedClock 75:c2894d531f42 397 if (dFSMstate != dFSMstateLast) // if just entered state.
gatedClock 75:c2894d531f42 398 {
gatedClock 75:c2894d531f42 399 giRemainingTime.cControl = RT_DECREMENT;
gatedClock 94:551b1df4b10c 400 queueTemperatureControl.put((int *) TEMP_CALC,1);
gatedClock 97:ee840478d6b3 401 queueSuppressTimeAdjust.put((int*) 1,1);
gatedClock 75:c2894d531f42 402 }
gatedClock 80:e3e1a2161435 403
gatedClock 80:e3e1a2161435 404 giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
gatedClock 80:e3e1a2161435 405 giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
gatedClock 94:551b1df4b10c 406 // giLCD.fCelsius = 0;
gatedClock 80:e3e1a2161435 407
gatedClock 80:e3e1a2161435 408
gatedClock 76:74c454c9d75b 409
gatedClock 76:74c454c9d75b 410 dFSMstateLast = dFSMstate; // determine next state.
gatedClock 78:7df160e0db7b 411 if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE;
gatedClock 78:7df160e0db7b 412 else
gatedClock 76:74c454c9d75b 413 if (dButtonStop) dFSMstate = FSM_IDLE;
gatedClock 78:7df160e0db7b 414 else
gatedClock 78:7df160e0db7b 415 if (dRemainingTime <= 0) dFSMstate = FSM_DONE;
gatedClock 73:44739860198b 416 break;
gatedClock 73:44739860198b 417 }
gatedClock 82:73fa3fe8a217 418 //---
gatedClock 73:44739860198b 419 case FSM_PAUSE : // PAUSE.
gatedClock 73:44739860198b 420 {
gatedClock 75:c2894d531f42 421
gatedClock 75:c2894d531f42 422 if (dFSMstate != dFSMstateLast) // if just entered state.
gatedClock 75:c2894d531f42 423 {
gatedClock 97:ee840478d6b3 424 queueTemperatureControl.put((int *) TEMP_FREEZE,1);
gatedClock 75:c2894d531f42 425 }
gatedClock 80:e3e1a2161435 426
gatedClock 80:e3e1a2161435 427
gatedClock 80:e3e1a2161435 428 giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
gatedClock 80:e3e1a2161435 429 giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
gatedClock 94:551b1df4b10c 430 // giLCD.fCelsius = 0;
gatedClock 80:e3e1a2161435 431
gatedClock 80:e3e1a2161435 432
gatedClock 75:c2894d531f42 433 giRemainingTime.cControl = RT_PAUSE;
gatedClock 75:c2894d531f42 434 // determine next state.
gatedClock 75:c2894d531f42 435 dFSMstateLast = dFSMstate;
gatedClock 78:7df160e0db7b 436 if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_CONTINUE;
gatedClock 75:c2894d531f42 437 else
gatedClock 78:7df160e0db7b 438 if (dButtonStop) dFSMstate = FSM_IDLE;
gatedClock 73:44739860198b 439 break;
gatedClock 73:44739860198b 440 }
gatedClock 78:7df160e0db7b 441
gatedClock 82:73fa3fe8a217 442 //---
gatedClock 79:4286319e48b4 443 case FSM_CONTINUE : // CONTINUE.
gatedClock 78:7df160e0db7b 444 {
gatedClock 78:7df160e0db7b 445
gatedClock 78:7df160e0db7b 446 if (dFSMstate != dFSMstateLast) // if just entered state.
gatedClock 78:7df160e0db7b 447 {
gatedClock 97:ee840478d6b3 448 queueTemperatureControl.put((int *) TEMP_CALC,1);
gatedClock 78:7df160e0db7b 449 }
gatedClock 80:e3e1a2161435 450
gatedClock 80:e3e1a2161435 451
gatedClock 80:e3e1a2161435 452 giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
gatedClock 80:e3e1a2161435 453 giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
gatedClock 94:551b1df4b10c 454 // giLCD.fCelsius = 0;
gatedClock 80:e3e1a2161435 455
gatedClock 80:e3e1a2161435 456
gatedClock 78:7df160e0db7b 457 giRemainingTime.cControl = RT_DECREMENT;
gatedClock 78:7df160e0db7b 458 // determine next state.
gatedClock 78:7df160e0db7b 459 dFSMstateLast = dFSMstate;
gatedClock 78:7df160e0db7b 460 if (dRemainingTime <= 0) dFSMstate = FSM_DONE;
gatedClock 78:7df160e0db7b 461 else
gatedClock 78:7df160e0db7b 462 if (dButtonStop) dFSMstate = FSM_IDLE;
gatedClock 79:4286319e48b4 463 else
gatedClock 79:4286319e48b4 464 if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE;
gatedClock 78:7df160e0db7b 465 break;
gatedClock 78:7df160e0db7b 466 }
gatedClock 78:7df160e0db7b 467
gatedClock 78:7df160e0db7b 468
gatedClock 82:73fa3fe8a217 469 //---
gatedClock 73:44739860198b 470 case FSM_DONE : // DONE.
gatedClock 73:44739860198b 471 {
gatedClock 89:290c96cd027f 472 led0 = 1;
gatedClock 75:c2894d531f42 473 if (dFSMstate != dFSMstateLast) // if just entered state.
gatedClock 75:c2894d531f42 474 {
gatedClock 93:0e93e4a23f2b 475
gatedClock 97:ee840478d6b3 476 queueTemperatureControl.put((int *) TEMP_FREEZE,1);
gatedClock 93:0e93e4a23f2b 477 Thread thread_beep(&speakerThread,(void *) &dBeepSeconds);
gatedClock 93:0e93e4a23f2b 478
gatedClock 93:0e93e4a23f2b 479
gatedClock 89:290c96cd027f 480 beepTimer.reset(); // clear the beep timer.
gatedClock 75:c2894d531f42 481 }
gatedClock 80:e3e1a2161435 482
gatedClock 80:e3e1a2161435 483
gatedClock 80:e3e1a2161435 484 giLCD.dTotalCookTime = giRemainingTime.dTotalTime;
gatedClock 80:e3e1a2161435 485 giLCD.dRemainingTime = giRemainingTime.dRemainingTime;
gatedClock 94:551b1df4b10c 486 // giLCD.fCelsius = 0;
gatedClock 80:e3e1a2161435 487
gatedClock 80:e3e1a2161435 488
gatedClock 80:e3e1a2161435 489
gatedClock 75:c2894d531f42 490 giRemainingTime.cControl = RT_CLEAR;
gatedClock 75:c2894d531f42 491 // determine next state.
gatedClock 75:c2894d531f42 492 dFSMstateLast = dFSMstate;
gatedClock 89:290c96cd027f 493 if (beepTimer.read() >= BEEPTIME) dFSMstate = FSM_IDLE;
gatedClock 89:290c96cd027f 494 else dFSMstate = FSM_DONE;
gatedClock 73:44739860198b 495 break;
gatedClock 73:44739860198b 496 }
gatedClock 82:73fa3fe8a217 497 //---
gatedClock 73:44739860198b 498 default : {dFSMstate = FSM_IDLE; break;}
gatedClock 73:44739860198b 499
gatedClock 62:48e7c196e2a5 500 }
gatedClock 73:44739860198b 501
gatedClock 63:63f362bcc2ac 502
gatedClock 75:c2894d531f42 503
gatedClock 75:c2894d531f42 504 queueEvent = queueUpdateFSM.get(1); // threadButtonStateManager
gatedClock 60:e10bf95bbc96 505 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 60:e10bf95bbc96 506 {
gatedClock 61:8026a9fc0cf1 507 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 508 dButtonState = (int) queueEvent.value.p;
gatedClock 63:63f362bcc2ac 509
gatedClock 75:c2894d531f42 510
gatedClock 69:55b836e8ced7 511
gatedClock 63:63f362bcc2ac 512 if (dButtonState == MSG_START)
gatedClock 63:63f362bcc2ac 513 {
gatedClock 63:63f362bcc2ac 514 dButtonStart = 1;
gatedClock 63:63f362bcc2ac 515 dButtonStop = 0;
gatedClock 79:4286319e48b4 516
gatedClock 79:4286319e48b4 517 // if the door is open, ignore
gatedClock 79:4286319e48b4 518 // and cancel 'start' state.
gatedClock 79:4286319e48b4 519 if (dDoorOpen) dButtonStart = 0;
gatedClock 63:63f362bcc2ac 520 }
gatedClock 63:63f362bcc2ac 521 if (dButtonState == MSG_STOP)
gatedClock 63:63f362bcc2ac 522 {
gatedClock 63:63f362bcc2ac 523 dButtonStart = 0;
gatedClock 63:63f362bcc2ac 524 dButtonStop = 1;
gatedClock 63:63f362bcc2ac 525 }
gatedClock 63:63f362bcc2ac 526
gatedClock 63:63f362bcc2ac 527 if (dButtonState == MSG_OPEN)
gatedClock 63:63f362bcc2ac 528 {
gatedClock 79:4286319e48b4 529 // if the door opens, clear current 'start' state.
gatedClock 79:4286319e48b4 530 dDoorOpen = 1;
gatedClock 79:4286319e48b4 531 dButtonStart = 0;
gatedClock 63:63f362bcc2ac 532 }
gatedClock 63:63f362bcc2ac 533
gatedClock 63:63f362bcc2ac 534 if (dButtonState == MSG_CLOSED)
gatedClock 63:63f362bcc2ac 535 {
gatedClock 63:63f362bcc2ac 536 dDoorOpen = 0;
gatedClock 63:63f362bcc2ac 537 }
gatedClock 63:63f362bcc2ac 538
gatedClock 61:8026a9fc0cf1 539 }
gatedClock 90:d9bb516141f1 540 else // no queue update available.
gatedClock 90:d9bb516141f1 541 {
gatedClock 90:d9bb516141f1 542 dButtonStart = 0;
gatedClock 90:d9bb516141f1 543 dButtonStop = 0;
gatedClock 90:d9bb516141f1 544 // dDoorOpen = 0;
gatedClock 90:d9bb516141f1 545
gatedClock 90:d9bb516141f1 546
gatedClock 90:d9bb516141f1 547 }
gatedClock 75:c2894d531f42 548
gatedClock 75:c2894d531f42 549 // fetch from global scope.
gatedClock 75:c2894d531f42 550 dRemainingTime = giRemainingTime.dRemainingTime;
gatedClock 69:55b836e8ced7 551
gatedClock 75:c2894d531f42 552 // pipeline variable.
gatedClock 57:0432c68ad232 553 Thread::wait(THREAD_5_WAITmS); // multitasking.
gatedClock 57:0432c68ad232 554 } // thread loop.
gatedClock 57:0432c68ad232 555 } // threadCookStateFSM.
gatedClock 57:0432c68ad232 556 /*----------------------------------------------//----------------------------*/
gatedClock 19:92b11e30aaaf 557 void slowClock(void) // 1Hz or thereabouts.
gatedClock 19:92b11e30aaaf 558 {
gatedClock 24:d39516e077ea 559 gcSlowClock = !gcSlowClock; // toggle clock.
gatedClock 19:92b11e30aaaf 560 }
gatedClock 19:92b11e30aaaf 561 /*----------------------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 562 void initialization(void) // program initializations.
gatedClock 0:fcca4db7b32a 563 {
gatedClock 16:db7f0b3b2605 564 gcSignalWaitEnable = 1;
gatedClock 0:fcca4db7b32a 565 }
gatedClock 0:fcca4db7b32a 566 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 567 void ISRleftButtonRising(void) // cooktime plus 60s.
gatedClock 1:9188d4668a88 568 {
gatedClock 66:4a0006fa5cc1 569 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 570 gtButtons.cLeftButton = 1; // detect left button.
gatedClock 9:cfdb9aa5857c 571
gatedClock 66:4a0006fa5cc1 572 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 573 }
gatedClock 1:9188d4668a88 574 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 575 void ISRleftButtonFalling(void) // button-release debounce.
gatedClock 1:9188d4668a88 576 {
gatedClock 66:4a0006fa5cc1 577 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 578 }
gatedClock 2:665ffa57031f 579 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 580 void ISRrightButtonRising(void) // cooktime -60s.
gatedClock 12:e40272e1fd8f 581 {
gatedClock 66:4a0006fa5cc1 582 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 583 gtButtons.cRightButton = 1; // detect right button.
gatedClock 12:e40272e1fd8f 584
gatedClock 66:4a0006fa5cc1 585 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 586 }
gatedClock 12:e40272e1fd8f 587 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 588 void ISRrightButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 589 {
gatedClock 66:4a0006fa5cc1 590 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 591 }
gatedClock 12:e40272e1fd8f 592 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 593 void ISRtopButtonRising(void) // cook start.
gatedClock 12:e40272e1fd8f 594 {
gatedClock 66:4a0006fa5cc1 595 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 596 gtButtons.cTopButton = 1; // detect top button.
gatedClock 12:e40272e1fd8f 597
gatedClock 66:4a0006fa5cc1 598 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 599 }
gatedClock 12:e40272e1fd8f 600 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 601 void ISRtopButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 602 {
gatedClock 66:4a0006fa5cc1 603 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 604 }
gatedClock 12:e40272e1fd8f 605 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 606 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 607 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 608 // upon off-button press, in the interrupt that it generates.
gatedClock 53:8c2baf5623c8 609
gatedClock 26:bff592483cb1 610 void ISRbottomButtonRising(void) // cook stop.
gatedClock 12:e40272e1fd8f 611 {
gatedClock 66:4a0006fa5cc1 612 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 613 {
gatedClock 66:4a0006fa5cc1 614 led0 = 0; // magnetron off.
gatedClock 66:4a0006fa5cc1 615 gtButtons.cBottomButton = 1; // detect bottom button.
gatedClock 66:4a0006fa5cc1 616 }
gatedClock 66:4a0006fa5cc1 617 debounceTimer.reset(); // begin debounce period.
gatedClock 66:4a0006fa5cc1 618 }
gatedClock 12:e40272e1fd8f 619 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 620 void ISRbottomButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 621 {
gatedClock 66:4a0006fa5cc1 622 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 623 }
gatedClock 12:e40272e1fd8f 624 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 625 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 626 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 627 // upon detection of an open door.
gatedClock 53:8c2baf5623c8 628
gatedClock 26:bff592483cb1 629 void ISRcenterButtonRising(void) // toggle door state.
gatedClock 12:e40272e1fd8f 630 {
gatedClock 66:4a0006fa5cc1 631 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 632 {
gatedClock 66:4a0006fa5cc1 633 if (gtButtons.cDoorOpen == MSG_OPEN) // calculate door state.
gatedClock 66:4a0006fa5cc1 634 gtButtons.cDoorOpen = MSG_CLOSED;
gatedClock 66:4a0006fa5cc1 635 else
gatedClock 66:4a0006fa5cc1 636 gtButtons.cDoorOpen = MSG_OPEN;
gatedClock 53:8c2baf5623c8 637
gatedClock 59:5e45b5e4a874 638 // magnetron off.
gatedClock 66:4a0006fa5cc1 639 if (gtButtons.cDoorOpen == MSG_OPEN) led0 = 0;
gatedClock 58:ec630b6dd9b1 640
gatedClock 66:4a0006fa5cc1 641 gtButtons.cCenterButton = 1;
gatedClock 66:4a0006fa5cc1 642 }
gatedClock 66:4a0006fa5cc1 643 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 644 }
gatedClock 12:e40272e1fd8f 645 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 646 void ISRcenterButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 647 {
gatedClock 66:4a0006fa5cc1 648 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 649 }
gatedClock 94:551b1df4b10c 650 /*----------------------------------------------//----------------------------*/ //====================================================
gatedClock 40:7afff79f0d8b 651 void temperatureThread(void const *args) // temperature measurement.
gatedClock 39:4e7e4d935a87 652 {
gatedClock 94:551b1df4b10c 653 int dTempControl = 0; // temperature control.
gatedClock 95:7e391a5951d1 654 int dCurrentTimerMs;
gatedClock 95:7e391a5951d1 655 int dPreviousTimerMs;
gatedClock 96:c8b0f29d9758 656 float fLastMeasuredTemperature;
gatedClock 94:551b1df4b10c 657 osEvent queueEvent; // from button state manager.
gatedClock 94:551b1df4b10c 658
gatedClock 95:7e391a5951d1 659 dCurrentTimerMs = 0;
gatedClock 95:7e391a5951d1 660 dPreviousTimerMs = 0;
gatedClock 96:c8b0f29d9758 661 fLastMeasuredTemperature = 0.0;
gatedClock 95:7e391a5951d1 662
gatedClock 97:ee840478d6b3 663 // temperatureTimer.start();
gatedClock 94:551b1df4b10c 664
gatedClock 45:e3207684e841 665 while(1) // thread loop.
gatedClock 45:e3207684e841 666 {
gatedClock 94:551b1df4b10c 667 // obtain temperature command.
gatedClock 94:551b1df4b10c 668 queueEvent = queueTemperatureControl.get(1);
gatedClock 94:551b1df4b10c 669 if (queueEvent.status == osEventMessage)
gatedClock 94:551b1df4b10c 670 {
gatedClock 94:551b1df4b10c 671 dTempControl = (int) queueEvent.value.p;
gatedClock 94:551b1df4b10c 672 }
gatedClock 94:551b1df4b10c 673
gatedClock 94:551b1df4b10c 674 if (dTempControl == TEMP_READ)
gatedClock 95:7e391a5951d1 675 {
gatedClock 95:7e391a5951d1 676 gfCelsius = temperature.read(); // physical measurement.
gatedClock 96:c8b0f29d9758 677 fLastMeasuredTemperature = gfCelsius;
gatedClock 97:ee840478d6b3 678 // temperatureTimer.reset(); // reset timer while we're not using it.
gatedClock 95:7e391a5951d1 679 }
gatedClock 94:551b1df4b10c 680
gatedClock 94:551b1df4b10c 681 if (dTempControl == TEMP_CALC)
gatedClock 96:c8b0f29d9758 682
gatedClock 97:ee840478d6b3 683
gatedClock 97:ee840478d6b3 684
gatedClock 97:ee840478d6b3 685
gatedClock 97:ee840478d6b3 686 gfCelsius = ((float) (giRemainingTime.dTotalTime - giRemainingTime.dRemainingTime) / (float) 6.0) + fLastMeasuredTemperature;
gatedClock 97:ee840478d6b3 687
gatedClock 97:ee840478d6b3 688
gatedClock 97:ee840478d6b3 689
gatedClock 97:ee840478d6b3 690
gatedClock 97:ee840478d6b3 691 // gfCelsius = fLastMeasuredTemperature + (temperatureTimer.read_ms() / 6000.0);
gatedClock 95:7e391a5951d1 692
gatedClock 94:551b1df4b10c 693
gatedClock 94:551b1df4b10c 694
gatedClock 94:551b1df4b10c 695
gatedClock 94:551b1df4b10c 696 giLCD.fCelsius = gfCelsius;
gatedClock 94:551b1df4b10c 697
gatedClock 40:7afff79f0d8b 698
gatedClock 45:e3207684e841 699 Thread::wait(THREAD_1_WAITmS); // multitasking.
gatedClock 45:e3207684e841 700 } // thread loop.
gatedClock 45:e3207684e841 701 } // temperatureThread.
gatedClock 42:266d5bbbfd19 702 /*----------------------------------------------//----------------------------*/
gatedClock 42:266d5bbbfd19 703 void LCDthread(void const *args) // LCD display thread.
gatedClock 42:266d5bbbfd19 704 {
gatedClock 67:1d9c85a4c3c1 705
gatedClock 67:1d9c85a4c3c1 706 static int dLCDtotalCookTimeSec = 0; // sample current values.
gatedClock 67:1d9c85a4c3c1 707 static int dCookTimeRemainingSec = 0;
gatedClock 67:1d9c85a4c3c1 708 static float fLCDcelsius = 0.0;
gatedClock 67:1d9c85a4c3c1 709
gatedClock 67:1d9c85a4c3c1 710 // remember previous values.
gatedClock 67:1d9c85a4c3c1 711 static int dLCDtotalCookTimeSecLast = 0;
gatedClock 67:1d9c85a4c3c1 712 static int dCookTimeRemainingSecLast = 0;
gatedClock 67:1d9c85a4c3c1 713 static float fLCDcelsiusLast = 0.0;
gatedClock 67:1d9c85a4c3c1 714
gatedClock 44:d16e813e61ef 715 while(1) // thread loop.
gatedClock 44:d16e813e61ef 716 {
gatedClock 67:1d9c85a4c3c1 717 // don't allow the values to
gatedClock 67:1d9c85a4c3c1 718 // change in the middle of the
gatedClock 67:1d9c85a4c3c1 719 // below else the anti-blink
gatedClock 67:1d9c85a4c3c1 720 // code won't work.
gatedClock 80:e3e1a2161435 721 dLCDtotalCookTimeSec = giLCD.dTotalCookTime;
gatedClock 80:e3e1a2161435 722 dCookTimeRemainingSec = giLCD.dRemainingTime;
gatedClock 80:e3e1a2161435 723 fLCDcelsius = giLCD.fCelsius;
gatedClock 80:e3e1a2161435 724
gatedClock 80:e3e1a2161435 725
gatedClock 67:1d9c85a4c3c1 726
gatedClock 67:1d9c85a4c3c1 727
gatedClock 67:1d9c85a4c3c1 728 // clear display only when
gatedClock 67:1d9c85a4c3c1 729 // necessary, in order to avoid
gatedClock 67:1d9c85a4c3c1 730 // 'blinkieness'.
gatedClock 67:1d9c85a4c3c1 731 if (dLCDtotalCookTimeSec != dLCDtotalCookTimeSecLast ||
gatedClock 67:1d9c85a4c3c1 732 dCookTimeRemainingSec != dCookTimeRemainingSecLast ||
gatedClock 67:1d9c85a4c3c1 733 fLCDcelsius != fLCDcelsiusLast)
gatedClock 67:1d9c85a4c3c1 734 lcd.cls();
gatedClock 42:266d5bbbfd19 735
gatedClock 44:d16e813e61ef 736 LCD1; // line 1.
gatedClock 67:1d9c85a4c3c1 737 lcd.printf(" total cook time: %d",dLCDtotalCookTimeSec);
gatedClock 42:266d5bbbfd19 738
gatedClock 44:d16e813e61ef 739 LCD2; // line 2.
gatedClock 67:1d9c85a4c3c1 740 lcd.printf(" remaing cook time: %d",dCookTimeRemainingSec);
gatedClock 42:266d5bbbfd19 741
gatedClock 44:d16e813e61ef 742 LCD3; // line 3.
gatedClock 67:1d9c85a4c3c1 743 lcd.printf(" temperature : %5.3f",fLCDcelsius);
gatedClock 67:1d9c85a4c3c1 744
gatedClock 67:1d9c85a4c3c1 745 // pipeline variables.
gatedClock 67:1d9c85a4c3c1 746 dLCDtotalCookTimeSecLast = dLCDtotalCookTimeSec;
gatedClock 67:1d9c85a4c3c1 747 dCookTimeRemainingSecLast = dCookTimeRemainingSec;
gatedClock 67:1d9c85a4c3c1 748 fLCDcelsiusLast = fLCDcelsius;
gatedClock 42:266d5bbbfd19 749
gatedClock 44:d16e813e61ef 750 Thread::wait(THREAD_2_WAITmS); // multitasking.
gatedClock 44:d16e813e61ef 751 } // thread loop.
gatedClock 44:d16e813e61ef 752 } // LCDthread.
gatedClock 42:266d5bbbfd19 753 /*----------------------------------------------//----------------------------*/
gatedClock 71:4a5f256ecf7c 754
gatedClock 88:0b1b812945eb 755 // cook remaining time countdown counter.
gatedClock 88:0b1b812945eb 756 // possibly due to a bug in Ticker
gatedClock 88:0b1b812945eb 757 // http://mbed.org/questions/1563/Mbed-Tickerfunction-hangs-system-from-re/
gatedClock 88:0b1b812945eb 758 // I've been unable to detach/attach this routine in order to reset its phase
gatedClock 88:0b1b812945eb 759 // when I tried it at a 1s resolution. In order to provide the human perception
gatedClock 88:0b1b812945eb 760 // of an immediate restart, I've increased the ticker frequency by the factor
gatedClock 88:0b1b812945eb 761 // 'GRANULARITY' and likewise divide that factor out when this routine
gatedClock 88:0b1b812945eb 762 // promotes the remaining time to the global variable.
gatedClock 88:0b1b812945eb 763
gatedClock 71:4a5f256ecf7c 764 void tickCookRemainingTime(void) // cook-cycle countdown.
gatedClock 71:4a5f256ecf7c 765 {
gatedClock 71:4a5f256ecf7c 766 static int dRemainingTime = 0; // remaining time in seconds.
gatedClock 88:0b1b812945eb 767 int dMaximum; // MAXSECONDS * GRANULARITY.
gatedClock 88:0b1b812945eb 768
gatedClock 88:0b1b812945eb 769 dMaximum = MAXSECONDS * GRANULARITY; // precalculate.
gatedClock 75:c2894d531f42 770
gatedClock 75:c2894d531f42 771 switch (giRemainingTime.cControl) // control processing.
gatedClock 71:4a5f256ecf7c 772 {
gatedClock 88:0b1b812945eb 773
gatedClock 72:b4d0c0aa3c26 774 case RT_PRELOAD : // preload with total time.
gatedClock 71:4a5f256ecf7c 775 {
gatedClock 88:0b1b812945eb 776 // the 'GRANULARITY - 1' factor
gatedClock 88:0b1b812945eb 777 // compensates for integer division
gatedClock 88:0b1b812945eb 778 // dropping the right-of-decimal result,
gatedClock 88:0b1b812945eb 779 // that occuring at the bottom of this
gatedClock 88:0b1b812945eb 780 // routine.
gatedClock 88:0b1b812945eb 781 dRemainingTime = (giRemainingTime.dTotalTime * GRANULARITY) + (GRANULARITY - 1);
gatedClock 71:4a5f256ecf7c 782 break;
gatedClock 71:4a5f256ecf7c 783 }
gatedClock 88:0b1b812945eb 784
gatedClock 72:b4d0c0aa3c26 785 case RT_DECREMENT : // count-down.
gatedClock 71:4a5f256ecf7c 786 {
gatedClock 72:b4d0c0aa3c26 787 dRemainingTime--;
gatedClock 71:4a5f256ecf7c 788 break;
gatedClock 71:4a5f256ecf7c 789 }
gatedClock 71:4a5f256ecf7c 790
gatedClock 72:b4d0c0aa3c26 791 case RT_PAUSE : // suspend countdown.
gatedClock 69:55b836e8ced7 792 {
gatedClock 72:b4d0c0aa3c26 793 dRemainingTime = dRemainingTime;
gatedClock 71:4a5f256ecf7c 794 break;
gatedClock 71:4a5f256ecf7c 795 }
gatedClock 71:4a5f256ecf7c 796
gatedClock 72:b4d0c0aa3c26 797 case RT_CLEAR : // clear countdown.
gatedClock 71:4a5f256ecf7c 798 {
gatedClock 76:74c454c9d75b 799 dRemainingTime = 0;
gatedClock 76:74c454c9d75b 800 led3 = 1;
gatedClock 71:4a5f256ecf7c 801 break;
gatedClock 71:4a5f256ecf7c 802 }
gatedClock 69:55b836e8ced7 803
gatedClock 72:b4d0c0aa3c26 804 default : // saturate, just in case.
gatedClock 72:b4d0c0aa3c26 805 {
gatedClock 72:b4d0c0aa3c26 806 }
gatedClock 75:c2894d531f42 807 } // control processing.
gatedClock 71:4a5f256ecf7c 808
gatedClock 88:0b1b812945eb 809 // saturate value.
gatedClock 88:0b1b812945eb 810 if (dRemainingTime > dMaximum) dRemainingTime = dMaximum;
gatedClock 88:0b1b812945eb 811 if (dRemainingTime < 0) dRemainingTime = 0;
gatedClock 88:0b1b812945eb 812
gatedClock 75:c2894d531f42 813 // promote to global scope.
gatedClock 86:388c2b4b7cf5 814 giRemainingTime.dRemainingTime = dRemainingTime/GRANULARITY;
gatedClock 70:7c0743c28b11 815
gatedClock 70:7c0743c28b11 816 } // cookRemainingTime.
gatedClock 69:55b836e8ced7 817 /*----------------------------------------------//----------------------------*/
gatedClock 92:be8d69aba1fc 818
gatedClock 92:be8d69aba1fc 819 void speakerThread(void const * pSecondsBeep)
gatedClock 92:be8d69aba1fc 820 {
gatedClock 92:be8d69aba1fc 821 osEvent queueEvent; // from button state manager.
gatedClock 92:be8d69aba1fc 822 int dLoop;
gatedClock 92:be8d69aba1fc 823 int * pSeconds;
gatedClock 92:be8d69aba1fc 824 int dSeconds;
gatedClock 92:be8d69aba1fc 825 float fHalfSecond; // cycles per half-second.
gatedClock 92:be8d69aba1fc 826
gatedClock 92:be8d69aba1fc 827
gatedClock 92:be8d69aba1fc 828 pSeconds = (int *) pSecondsBeep;
gatedClock 92:be8d69aba1fc 829 dSeconds = *pSeconds;
gatedClock 92:be8d69aba1fc 830 // dSeconds = 2;
gatedClock 92:be8d69aba1fc 831
gatedClock 92:be8d69aba1fc 832 fHalfSecond = 0.5 / (float) BEEPFREQ;
gatedClock 92:be8d69aba1fc 833
gatedClock 92:be8d69aba1fc 834 for (dLoop = 0; dLoop < (dSeconds * BEEPFREQ); dLoop++)
gatedClock 92:be8d69aba1fc 835 {
gatedClock 92:be8d69aba1fc 836 speaker = 1;
gatedClock 92:be8d69aba1fc 837 wait(fHalfSecond);
gatedClock 92:be8d69aba1fc 838 speaker = 0;
gatedClock 92:be8d69aba1fc 839 wait(fHalfSecond);
gatedClock 92:be8d69aba1fc 840 }
gatedClock 92:be8d69aba1fc 841
gatedClock 92:be8d69aba1fc 842
gatedClock 92:be8d69aba1fc 843 }
gatedClock 92:be8d69aba1fc 844 /*----------------------------------------------//----------------------------*/
gatedClock 69:55b836e8ced7 845
gatedClock 69:55b836e8ced7 846
gatedClock 69:55b836e8ced7 847
gatedClock 69:55b836e8ced7 848