homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Thu Sep 12 22:04:23 2013 +0000
Revision:
117:b9e18163c282
Parent:
116:15084f54baf9
Child:
118:d0e057d79acc
toward magnetron/carousel.

Who changed what in which revision?

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