homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

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