homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Wed Sep 11 18:00:47 2013 +0000
Revision:
67:1d9c85a4c3c1
Parent:
66:4a0006fa5cc1
Child:
68:cbebcfc948aa
improved anti-blink code for LCD.

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 0:fcca4db7b32a 7 -----includes-----------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 8 #include "mbed.h" // mbed class.
gatedClock 10:2b0a9fc39109 9 #include "rtos.h" // rtos class.
gatedClock 39:4e7e4d935a87 10 #include "LM75B.h" // thermometer class.
gatedClock 10:2b0a9fc39109 11 #include "C12832_lcd.h" // LCD class.
gatedClock 0:fcca4db7b32a 12 //---defines------------------------------------//------------------------------
gatedClock 9:cfdb9aa5857c 13 #define LCD1 lcd.locate(0, 0); // LCD line 1.
gatedClock 9:cfdb9aa5857c 14 #define LCD2 lcd.locate(0,11); // LCD line 2.
gatedClock 9:cfdb9aa5857c 15 #define LCD3 lcd.locate(0,22); // LCD line 3.
gatedClock 9:cfdb9aa5857c 16
gatedClock 10:2b0a9fc39109 17
gatedClock 66:4a0006fa5cc1 18 #define DEBOUNCEmS 16 // debounce wait in mS.
gatedClock 19:92b11e30aaaf 19 #define uS_TIMEOUT 100 // Timer uS timeout.
gatedClock 19:92b11e30aaaf 20 #define LBSIG 1 // left button signal code.
gatedClock 33:34c1bef3c4ff 21 #define PIPEuS 1000 // pipeline clock period.
gatedClock 19:92b11e30aaaf 22 #define SLOWCLOCKuS 500000 // slow-clock period.
gatedClock 39:4e7e4d935a87 23 #define TEMPCLOCKS 1 // temperature-measuring period in S.
gatedClock 26:bff592483cb1 24 #define PIPEDATASIZE 8 // dimension of tPipeData.
gatedClock 44:d16e813e61ef 25 #define THREAD_1_WAITmS 400 // thread 1 wait in mS.
gatedClock 67:1d9c85a4c3c1 26 #define THREAD_2_WAITmS 20 // LCD thread wait.
gatedClock 67:1d9c85a4c3c1 27 #define THREAD_3_WAITmS 80 // thread 3 wait in mS.
gatedClock 66:4a0006fa5cc1 28 #define THREAD_4_WAITmS 1 // thread 4 wait in mS.
gatedClock 57:0432c68ad232 29 #define THREAD_5_WAITmS 400 // thread 5 wait in mS.
gatedClock 44:d16e813e61ef 30 #define HB_MODULO 1024 // heartbeat modulo divisor.
gatedClock 50:2928c3cbdcc3 31
gatedClock 54:b0e7352d2516 32 #define MSG_INC_TIME 0x01
gatedClock 54:b0e7352d2516 33 #define MSG_DEC_TIME 0x02
gatedClock 58:ec630b6dd9b1 34 #define MSG_START 0x04
gatedClock 58:ec630b6dd9b1 35 #define MSG_STOP 0x08
gatedClock 58:ec630b6dd9b1 36 #define MSG_OPEN 0x10
gatedClock 58:ec630b6dd9b1 37 #define MSG_CLOSED 0x20
gatedClock 57:0432c68ad232 38
gatedClock 57:0432c68ad232 39 #define FSM_IDLE 0x00 // cook-state state-machine.
gatedClock 57:0432c68ad232 40 #define FSM_COOK 0x01 // cook-state state-machine.
gatedClock 57:0432c68ad232 41 #define FSM_PAUSE 0x02 // cook-state state-machine.
gatedClock 57:0432c68ad232 42 #define FSM_DONE 0x04 // cook-state state-machine.
gatedClock 65:e39360da5929 43
gatedClock 65:e39360da5929 44 #define DEBUG1 // debug preprocessor control.
gatedClock 0:fcca4db7b32a 45 //--global_definitions--------------------------//------------------------------
gatedClock 26:bff592483cb1 46 struct tButtons // button ISR updates.
gatedClock 26:bff592483cb1 47 {
gatedClock 26:bff592483cb1 48 char cLeftButton; // cooktime +60S.
gatedClock 26:bff592483cb1 49 char cRightButton; // cooktime -60S.
gatedClock 26:bff592483cb1 50 char cTopButton; // start cook.
gatedClock 26:bff592483cb1 51 char cBottomButton; // stop cook.
gatedClock 58:ec630b6dd9b1 52 char cCenterButton; // center button pressed.
gatedClock 53:8c2baf5623c8 53 char cDoorOpen; // door open.
gatedClock 26:bff592483cb1 54 };
gatedClock 34:b449d2a7c786 55
gatedClock 61:8026a9fc0cf1 56 Queue<int, 1> queueModTotalTime; // message to modify total time.
gatedClock 61:8026a9fc0cf1 57 Queue<int, 1> queueUpdateFSM; // message to inform FSM.
gatedClock 61:8026a9fc0cf1 58 Queue<int, 1> queueUpdateRemainingTime; // message to update remaining time.
gatedClock 65:e39360da5929 59 Queue<int, 1> queueSetRemainingTime; // tell countdown it's start time.
gatedClock 0:fcca4db7b32a 60 //--global_variables----------------------------//------------------------------
gatedClock 26:bff592483cb1 61 char gcSignalWaitEnable; // 1 to wait on a signal.
gatedClock 26:bff592483cb1 62 char gcSlowClock; // slow-clock signal.
gatedClock 33:34c1bef3c4ff 63 char gcInitializePipeline; // 1 to initialize pipeline state.
gatedClock 39:4e7e4d935a87 64 float gfCelsius; // from thermometer.
gatedClock 42:266d5bbbfd19 65 float gfLCDcelsius; // feed into LCD.
gatedClock 42:266d5bbbfd19 66 int gdLCDtotalCookTimeSec; // feed into LCD.
gatedClock 42:266d5bbbfd19 67 int gdCookTimeRemainingSec; // feed into LCD.
gatedClock 26:bff592483cb1 68 tButtons gtButtons; // ISR button updates.
gatedClock 0:fcca4db7b32a 69 //--global_instances----------------------------//------------------------------
gatedClock 55:17f3354da63a 70 Serial pc(USBTX, USBRX); // PuTTY terminal communication.
gatedClock 39:4e7e4d935a87 71 LM75B temperature(p28,p27); // on-board thermometer.
gatedClock 9:cfdb9aa5857c 72 C12832_LCD lcd; // LCD object.
gatedClock 53:8c2baf5623c8 73 DigitalOut led0(LED4); // magnetron.
gatedClock 39:4e7e4d935a87 74 DigitalOut led1(LED3);
gatedClock 39:4e7e4d935a87 75 DigitalOut led2(LED2);
gatedClock 39:4e7e4d935a87 76 DigitalOut led3(LED1);
gatedClock 0:fcca4db7b32a 77
gatedClock 0:fcca4db7b32a 78 InterruptIn iJoyStickUp (p15); // joystick up rising edge.
gatedClock 0:fcca4db7b32a 79 InterruptIn iJoyStickDown (p12); // joystick down rising edge.
gatedClock 0:fcca4db7b32a 80 InterruptIn iJoyStickLeft (p13); // joystick left rising edge.
gatedClock 0:fcca4db7b32a 81 InterruptIn iJoyStickRight (p16); // joystick right rising edge.
gatedClock 0:fcca4db7b32a 82 InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed.
gatedClock 66:4a0006fa5cc1 83
gatedClock 66:4a0006fa5cc1 84 Timer debounceTimer; // button debounce timer.
gatedClock 0:fcca4db7b32a 85
gatedClock 50:2928c3cbdcc3 86 Ticker tickerButtonStateManager; // manage the button states.
gatedClock 19:92b11e30aaaf 87 Ticker tickerSlowClock; // generate a ~1Hz clock.
gatedClock 64:255295f1d782 88 Ticker tickerCookCountdown; // remaining cook time.
gatedClock 63:63f362bcc2ac 89
gatedClock 63:63f362bcc2ac 90 // Timer timerFSMdone; // duration of FSM 'done' state.
gatedClock 40:7afff79f0d8b 91
gatedClock 0:fcca4db7b32a 92 //-------prototypes-----------------------------//------------------------------
gatedClock 49:56f790977983 93
gatedClock 19:92b11e30aaaf 94 void slowClock(); // 1Hz or thereabouts.
gatedClock 49:56f790977983 95
gatedClock 9:cfdb9aa5857c 96 void initialization(); // initialize settings.
gatedClock 13:21f27ba467c2 97
gatedClock 13:21f27ba467c2 98 void ISRleftButtonRising(); // cook-time increase.
gatedClock 13:21f27ba467c2 99 void ISRleftButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 100 void ISRrightButtonRising(); // cook-time decrease.
gatedClock 13:21f27ba467c2 101 void ISRrightButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 102 void ISRtopButtonRising(); // cook start.
gatedClock 13:21f27ba467c2 103 void ISRtopButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 104 void ISRbottomButtonRising(); // cook stop.
gatedClock 13:21f27ba467c2 105 void ISRbottomButtonFalling(); // button-release debounce.
gatedClock 13:21f27ba467c2 106 void ISRcenterButtonRising(); // door state toggle.
gatedClock 13:21f27ba467c2 107 void ISRcenterButtonFalling(); // button-release debounce.
gatedClock 16:db7f0b3b2605 108 void disableSignalWaiting(); // break from signal waiting.
gatedClock 39:4e7e4d935a87 109
gatedClock 54:b0e7352d2516 110 void threadButtonStateManager(void const *args);
gatedClock 50:2928c3cbdcc3 111 void threadTotalTimeControl(void const *args);
gatedClock 57:0432c68ad232 112 void threadCookStateFSM(void const *args);
gatedClock 64:255295f1d782 113 void cookRemainingTime(); // remaining time countdown.
gatedClock 50:2928c3cbdcc3 114
gatedClock 40:7afff79f0d8b 115 void temperatureThread(void const *args); // temperature measurement.
gatedClock 42:266d5bbbfd19 116 void LCDthread (void const *args); // LCD display thread.
gatedClock 0:fcca4db7b32a 117 //==============================================//==============================
gatedClock 0:fcca4db7b32a 118 int main(void)
gatedClock 0:fcca4db7b32a 119 {
gatedClock 16:db7f0b3b2605 120 char cLeftButtonState; // 1 means button was pressed.
gatedClock 16:db7f0b3b2605 121
gatedClock 16:db7f0b3b2605 122
gatedClock 16:db7f0b3b2605 123
gatedClock 14:d3bb343cd5b2 124 iJoyStickLeft.rise (&ISRleftButtonRising);
gatedClock 14:d3bb343cd5b2 125 iJoyStickLeft.fall (&ISRleftButtonFalling);
gatedClock 9:cfdb9aa5857c 126
gatedClock 14:d3bb343cd5b2 127 iJoyStickRight.rise(&ISRrightButtonRising);
gatedClock 14:d3bb343cd5b2 128 iJoyStickRight.fall(&ISRrightButtonFalling);
gatedClock 14:d3bb343cd5b2 129
gatedClock 14:d3bb343cd5b2 130 iJoyStickUp.rise (&ISRtopButtonRising);
gatedClock 14:d3bb343cd5b2 131 iJoyStickUp.fall (&ISRtopButtonFalling);
gatedClock 9:cfdb9aa5857c 132
gatedClock 14:d3bb343cd5b2 133 iJoyStickDown.rise (&ISRbottomButtonRising);
gatedClock 14:d3bb343cd5b2 134 iJoyStickDown.fall (&ISRbottomButtonFalling);
gatedClock 7:9fbd1d540863 135
gatedClock 14:d3bb343cd5b2 136 iJoyStickCenter.rise(&ISRcenterButtonRising);
gatedClock 14:d3bb343cd5b2 137 iJoyStickCenter.fall(&ISRcenterButtonFalling);
gatedClock 9:cfdb9aa5857c 138
gatedClock 66:4a0006fa5cc1 139 debounceTimer.start(); // kick-off debounce timer.
gatedClock 66:4a0006fa5cc1 140
gatedClock 33:34c1bef3c4ff 141 gcInitializePipeline = 1; // tell pipeline to initialize.
gatedClock 16:db7f0b3b2605 142
gatedClock 9:cfdb9aa5857c 143 initialization(); // initialize variables.
gatedClock 21:b794d189c36b 144 gcSlowClock = 0;
gatedClock 44:d16e813e61ef 145 led1 = 0;
gatedClock 53:8c2baf5623c8 146 gtButtons.cDoorOpen = 0; // initialize with door closed.
gatedClock 63:63f362bcc2ac 147
gatedClock 63:63f362bcc2ac 148 // timerFSMdone.start(); // start 'done' timer.
gatedClock 49:56f790977983 149
gatedClock 39:4e7e4d935a87 150 tickerSlowClock.attach_us(&slowClock ,SLOWCLOCKuS);
gatedClock 64:255295f1d782 151
gatedClock 64:255295f1d782 152 // count-down by one second.
gatedClock 65:e39360da5929 153 tickerCookCountdown.attach(&cookRemainingTime,1); // causing trouble.
gatedClock 54:b0e7352d2516 154
gatedClock 40:7afff79f0d8b 155
gatedClock 66:4a0006fa5cc1 156 Thread thread_1(temperatureThread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 66:4a0006fa5cc1 157 Thread thread_2(LCDthread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 66:4a0006fa5cc1 158 Thread thread_3(threadTotalTimeControl ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 66:4a0006fa5cc1 159 Thread thread_4(threadButtonStateManager,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); // was osPriorityIdle
gatedClock 66:4a0006fa5cc1 160 Thread thread_5(threadCookStateFSM ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL);
gatedClock 65:e39360da5929 161
gatedClock 66:4a0006fa5cc1 162 //Thread thread_4(threadButtonStateManager);
gatedClock 52:8ba6a0c91a89 163
gatedClock 40:7afff79f0d8b 164
gatedClock 59:5e45b5e4a874 165 // the message-receiving threads need 'else' for message-receive-timeout.
gatedClock 9:cfdb9aa5857c 166
gatedClock 14:d3bb343cd5b2 167 while(1)
gatedClock 0:fcca4db7b32a 168 {
gatedClock 44:d16e813e61ef 169 Thread::wait(10000);
gatedClock 16:db7f0b3b2605 170
gatedClock 16:db7f0b3b2605 171
gatedClock 16:db7f0b3b2605 172
gatedClock 14:d3bb343cd5b2 173
gatedClock 0:fcca4db7b32a 174 }
gatedClock 19:92b11e30aaaf 175 }
gatedClock 19:92b11e30aaaf 176 /*----------------------------------------------//----------------------------*/
gatedClock 59:5e45b5e4a874 177
gatedClock 59:5e45b5e4a874 178 // this serves as the bottom-half for all of the button-press IRSs.
gatedClock 59:5e45b5e4a874 179 // it sends button-status messages to other threads, and it also
gatedClock 59:5e45b5e4a874 180 // clears the button-state globals to prepare them for the next
gatedClock 59:5e45b5e4a874 181 // button press.
gatedClock 59:5e45b5e4a874 182
gatedClock 59:5e45b5e4a874 183
gatedClock 54:b0e7352d2516 184 void threadButtonStateManager(void const *args)
gatedClock 50:2928c3cbdcc3 185 {
gatedClock 56:18cff6eb91db 186 int dMessage; // message.
gatedClock 50:2928c3cbdcc3 187
gatedClock 54:b0e7352d2516 188 while(1) // thread loop.
gatedClock 67:1d9c85a4c3c1 189 {
gatedClock 54:b0e7352d2516 190 //--- // TOTAL TIME CONTROL.
gatedClock 56:18cff6eb91db 191
gatedClock 56:18cff6eb91db 192 // encoded integers will be sent,
gatedClock 56:18cff6eb91db 193 // not pointers.
gatedClock 50:2928c3cbdcc3 194
gatedClock 50:2928c3cbdcc3 195 if (gtButtons.cLeftButton) // total time increment button.
gatedClock 50:2928c3cbdcc3 196 {
gatedClock 54:b0e7352d2516 197 dMessage = MSG_INC_TIME; // set message.
gatedClock 56:18cff6eb91db 198 queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
gatedClock 50:2928c3cbdcc3 199 gtButtons.cLeftButton = 0; // clear the button state.
gatedClock 67:1d9c85a4c3c1 200 // pc.printf("\n\r time increment button.");
gatedClock 50:2928c3cbdcc3 201 }
gatedClock 67:1d9c85a4c3c1 202
gatedClock 59:5e45b5e4a874 203 if (gtButtons.cRightButton) // total time decrement button.
gatedClock 50:2928c3cbdcc3 204 {
gatedClock 54:b0e7352d2516 205 dMessage = MSG_DEC_TIME; // set message.
gatedClock 56:18cff6eb91db 206 queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
gatedClock 50:2928c3cbdcc3 207 gtButtons.cRightButton = 0; // clear the button state.
gatedClock 67:1d9c85a4c3c1 208 // pc.printf("\n\r time decrement button.");
gatedClock 50:2928c3cbdcc3 209 }
gatedClock 67:1d9c85a4c3c1 210
gatedClock 59:5e45b5e4a874 211 //--- // COOK-STATE FSM.
gatedClock 59:5e45b5e4a874 212
gatedClock 59:5e45b5e4a874 213 if (gtButtons.cTopButton) // start-cook button.
gatedClock 58:ec630b6dd9b1 214 {
gatedClock 59:5e45b5e4a874 215 dMessage = MSG_START; // set message.
gatedClock 59:5e45b5e4a874 216 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 217 gtButtons.cTopButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 218 }
gatedClock 67:1d9c85a4c3c1 219
gatedClock 59:5e45b5e4a874 220 if (gtButtons.cBottomButton) // stop-cook button.
gatedClock 58:ec630b6dd9b1 221 {
gatedClock 59:5e45b5e4a874 222 dMessage = MSG_STOP; // set message.
gatedClock 59:5e45b5e4a874 223 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 224 gtButtons.cBottomButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 225 }
gatedClock 67:1d9c85a4c3c1 226
gatedClock 59:5e45b5e4a874 227 if (gtButtons.cCenterButton) // door-state-toggle.
gatedClock 58:ec630b6dd9b1 228 {
gatedClock 59:5e45b5e4a874 229 dMessage = gtButtons.cDoorOpen; // determined in ISR.
gatedClock 59:5e45b5e4a874 230 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 231 gtButtons.cCenterButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 232 }
gatedClock 59:5e45b5e4a874 233 //---
gatedClock 58:ec630b6dd9b1 234
gatedClock 54:b0e7352d2516 235 Thread::wait(THREAD_4_WAITmS); // multitasking.
gatedClock 54:b0e7352d2516 236 } // thread loop.
gatedClock 54:b0e7352d2516 237 } // threadButtonStateManager.
gatedClock 50:2928c3cbdcc3 238 /*----------------------------------------------//----------------------------*/
gatedClock 51:e5ec74c49b01 239 // the incoming messages are mutually-exclusive.
gatedClock 51:e5ec74c49b01 240 // total time controller.
gatedClock 51:e5ec74c49b01 241 void threadTotalTimeControl(void const *args)
gatedClock 50:2928c3cbdcc3 242 {
gatedClock 51:e5ec74c49b01 243 static int dTotalTime = 0; // total time variable.
gatedClock 60:e10bf95bbc96 244 int dMessage; // message.
gatedClock 51:e5ec74c49b01 245 osEvent queueEvent; // queue event.
gatedClock 50:2928c3cbdcc3 246
gatedClock 50:2928c3cbdcc3 247 while(1) // thread loop.
gatedClock 50:2928c3cbdcc3 248 {
gatedClock 50:2928c3cbdcc3 249
gatedClock 55:17f3354da63a 250 queueEvent = queueModTotalTime.get(1); // get message.
gatedClock 51:e5ec74c49b01 251 if (queueEvent.status == osEventMessage)
gatedClock 51:e5ec74c49b01 252 {
gatedClock 56:18cff6eb91db 253 dMessage = (int) queueEvent.value.p; // interpret as integer, not pointer.
gatedClock 54:b0e7352d2516 254
gatedClock 54:b0e7352d2516 255 // increment total time.
gatedClock 56:18cff6eb91db 256 if (dMessage == MSG_INC_TIME) dTotalTime += 60;
gatedClock 54:b0e7352d2516 257
gatedClock 54:b0e7352d2516 258 // decrement total time.
gatedClock 56:18cff6eb91db 259 if (dMessage == MSG_DEC_TIME) dTotalTime -= 60;
gatedClock 65:e39360da5929 260
gatedClock 65:e39360da5929 261 // saturations.
gatedClock 65:e39360da5929 262 if (dTotalTime > 180) dTotalTime = 180;
gatedClock 65:e39360da5929 263 if (dTotalTime < 0) dTotalTime = 0;
gatedClock 65:e39360da5929 264
gatedClock 65:e39360da5929 265 queueSetRemainingTime.put((int *) dTotalTime);
gatedClock 65:e39360da5929 266
gatedClock 65:e39360da5929 267
gatedClock 51:e5ec74c49b01 268 }
gatedClock 54:b0e7352d2516 269
gatedClock 65:e39360da5929 270
gatedClock 51:e5ec74c49b01 271
gatedClock 51:e5ec74c49b01 272 gdLCDtotalCookTimeSec = dTotalTime; // transmit to LCD.
gatedClock 61:8026a9fc0cf1 273
gatedClock 61:8026a9fc0cf1 274 // pretend it's a pointer.
gatedClock 61:8026a9fc0cf1 275 // queueUpdateTotalTime.put((int *) dTotalTime);
gatedClock 61:8026a9fc0cf1 276
gatedClock 61:8026a9fc0cf1 277
gatedClock 50:2928c3cbdcc3 278
gatedClock 50:2928c3cbdcc3 279 Thread::wait(THREAD_3_WAITmS); // multitasking.
gatedClock 50:2928c3cbdcc3 280 } // thread loop.
gatedClock 50:2928c3cbdcc3 281 } // threadTotalTimeControl.
gatedClock 33:34c1bef3c4ff 282 /*----------------------------------------------//----------------------------*/
gatedClock 60:e10bf95bbc96 283 /*
gatedClock 60:e10bf95bbc96 284 #define FSM_IDLE 0x00 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 285 #define FSM_COOK 0x01 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 286 #define FSM_PAUSE 0x02 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 287 #define FSM_DONE 0x04 // cook-state state-machine.
gatedClock 62:48e7c196e2a5 288
gatedClock 62:48e7c196e2a5 289 #define MSG_START 0x04
gatedClock 62:48e7c196e2a5 290 #define MSG_STOP 0x08
gatedClock 62:48e7c196e2a5 291 #define MSG_OPEN 0x10
gatedClock 62:48e7c196e2a5 292 #define MSG_CLOSED 0x20
gatedClock 60:e10bf95bbc96 293 */
gatedClock 57:0432c68ad232 294 void threadCookStateFSM(void const *args) // cook-cycle FSM.
gatedClock 57:0432c68ad232 295 {
gatedClock 60:e10bf95bbc96 296 static int dFSMstate = FSM_IDLE; // state of this FSM.
gatedClock 61:8026a9fc0cf1 297 static int dButtonState; // received button state.
gatedClock 61:8026a9fc0cf1 298 static int dRemainingTime; // received remaining time.
gatedClock 62:48e7c196e2a5 299
gatedClock 62:48e7c196e2a5 300 static int dButtonStart = 0;
gatedClock 62:48e7c196e2a5 301 static int dButtonStop = 0;
gatedClock 62:48e7c196e2a5 302 static int dDoorOpen = 0;
gatedClock 62:48e7c196e2a5 303 static int dTimeRemaining = 0;
gatedClock 62:48e7c196e2a5 304
gatedClock 63:63f362bcc2ac 305 osEvent queueEvent; // queue event.
gatedClock 63:63f362bcc2ac 306
gatedClock 57:0432c68ad232 307 while(1) // thread loop.
gatedClock 57:0432c68ad232 308 {
gatedClock 62:48e7c196e2a5 309
gatedClock 63:63f362bcc2ac 310 if (dFSMstate == FSM_IDLE) // idle state.
gatedClock 62:48e7c196e2a5 311 {
gatedClock 62:48e7c196e2a5 312 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 313 }
gatedClock 62:48e7c196e2a5 314 else
gatedClock 63:63f362bcc2ac 315 if (dFSMstate == FSM_COOK) // cook state.
gatedClock 62:48e7c196e2a5 316 {
gatedClock 62:48e7c196e2a5 317 if (dDoorOpen & dTimeRemaining) dFSMstate = FSM_PAUSE;
gatedClock 62:48e7c196e2a5 318 }
gatedClock 62:48e7c196e2a5 319 else
gatedClock 63:63f362bcc2ac 320 if (dFSMstate == FSM_PAUSE) // pause state.
gatedClock 62:48e7c196e2a5 321 {
gatedClock 62:48e7c196e2a5 322 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_DONE;
gatedClock 62:48e7c196e2a5 323 if (dButtonStop) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 324 }
gatedClock 62:48e7c196e2a5 325 else
gatedClock 62:48e7c196e2a5 326 if (dFSMstate == FSM_DONE) // incomplete.
gatedClock 62:48e7c196e2a5 327 {
gatedClock 63:63f362bcc2ac 328 // timerFSMdone.reset(); // reset the timer.
gatedClock 63:63f362bcc2ac 329 dFSMstate = FSM_IDLE; // end of beep.
gatedClock 62:48e7c196e2a5 330 }
gatedClock 62:48e7c196e2a5 331
gatedClock 62:48e7c196e2a5 332
gatedClock 63:63f362bcc2ac 333
gatedClock 60:e10bf95bbc96 334 queueEvent = queueUpdateFSM.get(1); // get message.
gatedClock 60:e10bf95bbc96 335 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 60:e10bf95bbc96 336 {
gatedClock 61:8026a9fc0cf1 337 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 338 dButtonState = (int) queueEvent.value.p;
gatedClock 63:63f362bcc2ac 339
gatedClock 63:63f362bcc2ac 340 if (dButtonState == MSG_START)
gatedClock 63:63f362bcc2ac 341 {
gatedClock 63:63f362bcc2ac 342 dButtonStart = 1;
gatedClock 63:63f362bcc2ac 343 dButtonStop = 0;
gatedClock 63:63f362bcc2ac 344 }
gatedClock 63:63f362bcc2ac 345 if (dButtonState == MSG_STOP)
gatedClock 63:63f362bcc2ac 346 {
gatedClock 63:63f362bcc2ac 347 dButtonStart = 0;
gatedClock 63:63f362bcc2ac 348 dButtonStop = 1;
gatedClock 63:63f362bcc2ac 349 }
gatedClock 63:63f362bcc2ac 350
gatedClock 63:63f362bcc2ac 351 if (dButtonState == MSG_OPEN)
gatedClock 63:63f362bcc2ac 352 {
gatedClock 63:63f362bcc2ac 353 dDoorOpen = 1;
gatedClock 63:63f362bcc2ac 354 }
gatedClock 63:63f362bcc2ac 355
gatedClock 63:63f362bcc2ac 356 if (dButtonState == MSG_CLOSED)
gatedClock 63:63f362bcc2ac 357 {
gatedClock 63:63f362bcc2ac 358 dDoorOpen = 0;
gatedClock 63:63f362bcc2ac 359 }
gatedClock 63:63f362bcc2ac 360
gatedClock 61:8026a9fc0cf1 361 }
gatedClock 61:8026a9fc0cf1 362 // get message.
gatedClock 61:8026a9fc0cf1 363 queueEvent = queueUpdateRemainingTime.get(1);
gatedClock 61:8026a9fc0cf1 364 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 61:8026a9fc0cf1 365 {
gatedClock 61:8026a9fc0cf1 366 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 367 dRemainingTime = (int) queueEvent.value.p;
gatedClock 61:8026a9fc0cf1 368 }
gatedClock 60:e10bf95bbc96 369
gatedClock 60:e10bf95bbc96 370
gatedClock 61:8026a9fc0cf1 371
gatedClock 57:0432c68ad232 372
gatedClock 57:0432c68ad232 373
gatedClock 57:0432c68ad232 374
gatedClock 57:0432c68ad232 375
gatedClock 57:0432c68ad232 376
gatedClock 57:0432c68ad232 377 Thread::wait(THREAD_5_WAITmS); // multitasking.
gatedClock 57:0432c68ad232 378 } // thread loop.
gatedClock 57:0432c68ad232 379 } // threadCookStateFSM.
gatedClock 57:0432c68ad232 380 /*----------------------------------------------//----------------------------*/
gatedClock 19:92b11e30aaaf 381 void slowClock(void) // 1Hz or thereabouts.
gatedClock 19:92b11e30aaaf 382 {
gatedClock 24:d39516e077ea 383 gcSlowClock = !gcSlowClock; // toggle clock.
gatedClock 19:92b11e30aaaf 384 }
gatedClock 19:92b11e30aaaf 385 /*----------------------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 386 void initialization(void) // program initializations.
gatedClock 0:fcca4db7b32a 387 {
gatedClock 16:db7f0b3b2605 388 gcSignalWaitEnable = 1;
gatedClock 0:fcca4db7b32a 389 }
gatedClock 0:fcca4db7b32a 390 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 391 void ISRleftButtonRising(void) // cooktime plus 60s.
gatedClock 1:9188d4668a88 392 {
gatedClock 66:4a0006fa5cc1 393 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 394 gtButtons.cLeftButton = 1; // detect left button.
gatedClock 9:cfdb9aa5857c 395
gatedClock 66:4a0006fa5cc1 396 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 397 }
gatedClock 1:9188d4668a88 398 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 399 void ISRleftButtonFalling(void) // button-release debounce.
gatedClock 1:9188d4668a88 400 {
gatedClock 66:4a0006fa5cc1 401 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 402 }
gatedClock 2:665ffa57031f 403 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 404 void ISRrightButtonRising(void) // cooktime -60s.
gatedClock 12:e40272e1fd8f 405 {
gatedClock 66:4a0006fa5cc1 406 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 407 gtButtons.cRightButton = 1; // detect right button.
gatedClock 12:e40272e1fd8f 408
gatedClock 66:4a0006fa5cc1 409 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 410 }
gatedClock 12:e40272e1fd8f 411 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 412 void ISRrightButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 413 {
gatedClock 66:4a0006fa5cc1 414 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 415 }
gatedClock 12:e40272e1fd8f 416 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 417 void ISRtopButtonRising(void) // cook start.
gatedClock 12:e40272e1fd8f 418 {
gatedClock 66:4a0006fa5cc1 419 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 420 gtButtons.cTopButton = 1; // detect top button.
gatedClock 12:e40272e1fd8f 421
gatedClock 66:4a0006fa5cc1 422 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 423 }
gatedClock 12:e40272e1fd8f 424 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 425 void ISRtopButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 426 {
gatedClock 66:4a0006fa5cc1 427 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 428 }
gatedClock 12:e40272e1fd8f 429 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 430 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 431 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 432 // upon off-button press, in the interrupt that it generates.
gatedClock 53:8c2baf5623c8 433
gatedClock 26:bff592483cb1 434 void ISRbottomButtonRising(void) // cook stop.
gatedClock 12:e40272e1fd8f 435 {
gatedClock 66:4a0006fa5cc1 436 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 437 {
gatedClock 66:4a0006fa5cc1 438 led0 = 0; // magnetron off.
gatedClock 66:4a0006fa5cc1 439 gtButtons.cBottomButton = 1; // detect bottom button.
gatedClock 66:4a0006fa5cc1 440 }
gatedClock 66:4a0006fa5cc1 441 debounceTimer.reset(); // begin debounce period.
gatedClock 66:4a0006fa5cc1 442 }
gatedClock 12:e40272e1fd8f 443 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 444 void ISRbottomButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 445 {
gatedClock 66:4a0006fa5cc1 446 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 447 }
gatedClock 12:e40272e1fd8f 448 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 449 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 450 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 451 // upon detection of an open door.
gatedClock 53:8c2baf5623c8 452
gatedClock 26:bff592483cb1 453 void ISRcenterButtonRising(void) // toggle door state.
gatedClock 12:e40272e1fd8f 454 {
gatedClock 66:4a0006fa5cc1 455 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 456 {
gatedClock 66:4a0006fa5cc1 457 if (gtButtons.cDoorOpen == MSG_OPEN) // calculate door state.
gatedClock 66:4a0006fa5cc1 458 gtButtons.cDoorOpen = MSG_CLOSED;
gatedClock 66:4a0006fa5cc1 459 else
gatedClock 66:4a0006fa5cc1 460 gtButtons.cDoorOpen = MSG_OPEN;
gatedClock 53:8c2baf5623c8 461
gatedClock 59:5e45b5e4a874 462 // magnetron off.
gatedClock 66:4a0006fa5cc1 463 if (gtButtons.cDoorOpen == MSG_OPEN) led0 = 0;
gatedClock 58:ec630b6dd9b1 464
gatedClock 66:4a0006fa5cc1 465 gtButtons.cCenterButton = 1;
gatedClock 66:4a0006fa5cc1 466 }
gatedClock 66:4a0006fa5cc1 467 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 468 }
gatedClock 12:e40272e1fd8f 469 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 470 void ISRcenterButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 471 {
gatedClock 66:4a0006fa5cc1 472 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 473 }
gatedClock 12:e40272e1fd8f 474 /*----------------------------------------------//----------------------------*/
gatedClock 40:7afff79f0d8b 475 void temperatureThread(void const *args) // temperature measurement.
gatedClock 39:4e7e4d935a87 476 {
gatedClock 45:e3207684e841 477 while(1) // thread loop.
gatedClock 45:e3207684e841 478 {
gatedClock 45:e3207684e841 479 gfCelsius = temperature.read(); // physical measurement.
gatedClock 40:7afff79f0d8b 480
gatedClock 45:e3207684e841 481 Thread::wait(THREAD_1_WAITmS); // multitasking.
gatedClock 45:e3207684e841 482 } // thread loop.
gatedClock 45:e3207684e841 483 } // temperatureThread.
gatedClock 42:266d5bbbfd19 484 /*----------------------------------------------//----------------------------*/
gatedClock 42:266d5bbbfd19 485 void LCDthread(void const *args) // LCD display thread.
gatedClock 42:266d5bbbfd19 486 {
gatedClock 67:1d9c85a4c3c1 487
gatedClock 67:1d9c85a4c3c1 488 static int dLCDtotalCookTimeSec = 0; // sample current values.
gatedClock 67:1d9c85a4c3c1 489 static int dCookTimeRemainingSec = 0;
gatedClock 67:1d9c85a4c3c1 490 static float fLCDcelsius = 0.0;
gatedClock 67:1d9c85a4c3c1 491
gatedClock 67:1d9c85a4c3c1 492 // remember previous values.
gatedClock 67:1d9c85a4c3c1 493 static int dLCDtotalCookTimeSecLast = 0;
gatedClock 67:1d9c85a4c3c1 494 static int dCookTimeRemainingSecLast = 0;
gatedClock 67:1d9c85a4c3c1 495 static float fLCDcelsiusLast = 0.0;
gatedClock 67:1d9c85a4c3c1 496
gatedClock 44:d16e813e61ef 497 while(1) // thread loop.
gatedClock 44:d16e813e61ef 498 {
gatedClock 67:1d9c85a4c3c1 499 // don't allow the values to
gatedClock 67:1d9c85a4c3c1 500 // change in the middle of the
gatedClock 67:1d9c85a4c3c1 501 // below else the anti-blink
gatedClock 67:1d9c85a4c3c1 502 // code won't work.
gatedClock 67:1d9c85a4c3c1 503 dLCDtotalCookTimeSec = gdLCDtotalCookTimeSec;
gatedClock 67:1d9c85a4c3c1 504 dCookTimeRemainingSec = gdCookTimeRemainingSec;
gatedClock 67:1d9c85a4c3c1 505 fLCDcelsius = gfLCDcelsius;
gatedClock 67:1d9c85a4c3c1 506
gatedClock 67:1d9c85a4c3c1 507
gatedClock 67:1d9c85a4c3c1 508 // clear display only when
gatedClock 67:1d9c85a4c3c1 509 // necessary, in order to avoid
gatedClock 67:1d9c85a4c3c1 510 // 'blinkieness'.
gatedClock 67:1d9c85a4c3c1 511 if (dLCDtotalCookTimeSec != dLCDtotalCookTimeSecLast ||
gatedClock 67:1d9c85a4c3c1 512 dCookTimeRemainingSec != dCookTimeRemainingSecLast ||
gatedClock 67:1d9c85a4c3c1 513 fLCDcelsius != fLCDcelsiusLast)
gatedClock 67:1d9c85a4c3c1 514 {
gatedClock 67:1d9c85a4c3c1 515 lcd.cls();
gatedClock 67:1d9c85a4c3c1 516 pc.printf("\n\r cleared LCD.");
gatedClock 67:1d9c85a4c3c1 517 }
gatedClock 42:266d5bbbfd19 518
gatedClock 44:d16e813e61ef 519 LCD1; // line 1.
gatedClock 67:1d9c85a4c3c1 520 lcd.printf(" total cook time: %d",dLCDtotalCookTimeSec);
gatedClock 42:266d5bbbfd19 521
gatedClock 44:d16e813e61ef 522 LCD2; // line 2.
gatedClock 67:1d9c85a4c3c1 523 lcd.printf(" remaing cook time: %d",dCookTimeRemainingSec);
gatedClock 42:266d5bbbfd19 524
gatedClock 44:d16e813e61ef 525 LCD3; // line 3.
gatedClock 67:1d9c85a4c3c1 526 lcd.printf(" temperature : %5.3f",fLCDcelsius);
gatedClock 67:1d9c85a4c3c1 527
gatedClock 67:1d9c85a4c3c1 528 // pipeline variables.
gatedClock 67:1d9c85a4c3c1 529 dLCDtotalCookTimeSecLast = dLCDtotalCookTimeSec;
gatedClock 67:1d9c85a4c3c1 530 dCookTimeRemainingSecLast = dCookTimeRemainingSec;
gatedClock 67:1d9c85a4c3c1 531 fLCDcelsiusLast = fLCDcelsius;
gatedClock 42:266d5bbbfd19 532
gatedClock 44:d16e813e61ef 533 Thread::wait(THREAD_2_WAITmS); // multitasking.
gatedClock 44:d16e813e61ef 534 } // thread loop.
gatedClock 44:d16e813e61ef 535 } // LCDthread.
gatedClock 42:266d5bbbfd19 536 /*----------------------------------------------//----------------------------*/
gatedClock 64:255295f1d782 537 void cookRemainingTime(void) // cook-cycle countdown.
gatedClock 64:255295f1d782 538 {
gatedClock 64:255295f1d782 539
gatedClock 65:e39360da5929 540 static int dRemainingTime = 0; // remaining time.
gatedClock 65:e39360da5929 541 osEvent queueEvent; // queue event.
gatedClock 66:4a0006fa5cc1 542
gatedClock 65:e39360da5929 543 dRemainingTime--;
gatedClock 66:4a0006fa5cc1 544
gatedClock 65:e39360da5929 545 if (dRemainingTime < 0) dRemainingTime = 0;
gatedClock 66:4a0006fa5cc1 546
gatedClock 65:e39360da5929 547
gatedClock 65:e39360da5929 548 // pc.printf("\n\r dRemainingTime = %d",dRemainingTime);
gatedClock 65:e39360da5929 549
gatedClock 65:e39360da5929 550 // get message.
gatedClock 65:e39360da5929 551 queueEvent = queueSetRemainingTime.get(1);
gatedClock 65:e39360da5929 552 if (queueEvent.status == osEventMessage) // update state variable.
gatedClock 65:e39360da5929 553 {
gatedClock 65:e39360da5929 554 // interpret as integer, not pointer.
gatedClock 65:e39360da5929 555 dRemainingTime = (int) queueEvent.value.p;
gatedClock 65:e39360da5929 556 }
gatedClock 64:255295f1d782 557 }
gatedClock 64:255295f1d782 558 /*----------------------------------------------//----------------------------*/