homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Thu Sep 12 21:10:06 2013 +0000
Revision:
105:d08988bfb31b
Parent:
104:5e49dbe5038a
Child:
106:5137c38345d3
cleanup.

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