homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

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