homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

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