homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Thu Sep 12 20:22:43 2013 +0000
Revision:
100:fd7006aa3d05
Parent:
99:a3e8344024c0
Child:
101:33dc62b6b728
there's a couple of seconds between done and idle states.  why?

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