homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Wed Sep 11 18:07:15 2013 +0000
Revision:
68:cbebcfc948aa
Parent:
67:1d9c85a4c3c1
Child:
69:55b836e8ced7
FSM not getting out of IDLE state.

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 68:cbebcfc948aa 312 pc.printf("\n\r FSM_IDLE");
gatedClock 62:48e7c196e2a5 313 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 314 }
gatedClock 62:48e7c196e2a5 315 else
gatedClock 63:63f362bcc2ac 316 if (dFSMstate == FSM_COOK) // cook state.
gatedClock 62:48e7c196e2a5 317 {
gatedClock 68:cbebcfc948aa 318 pc.printf("\n\r FSM_COOK");
gatedClock 68:cbebcfc948aa 319 if (dDoorOpen & dTimeRemaining) dFSMstate = FSM_PAUSE;
gatedClock 62:48e7c196e2a5 320 }
gatedClock 62:48e7c196e2a5 321 else
gatedClock 63:63f362bcc2ac 322 if (dFSMstate == FSM_PAUSE) // pause state.
gatedClock 62:48e7c196e2a5 323 {
gatedClock 68:cbebcfc948aa 324 pc.printf("\n\r FSM_PAUSE");
gatedClock 62:48e7c196e2a5 325 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_DONE;
gatedClock 62:48e7c196e2a5 326 if (dButtonStop) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 327 }
gatedClock 62:48e7c196e2a5 328 else
gatedClock 62:48e7c196e2a5 329 if (dFSMstate == FSM_DONE) // incomplete.
gatedClock 62:48e7c196e2a5 330 {
gatedClock 68:cbebcfc948aa 331 pc.printf("\n\r FSM_DONE");
gatedClock 63:63f362bcc2ac 332 // timerFSMdone.reset(); // reset the timer.
gatedClock 63:63f362bcc2ac 333 dFSMstate = FSM_IDLE; // end of beep.
gatedClock 62:48e7c196e2a5 334 }
gatedClock 62:48e7c196e2a5 335
gatedClock 62:48e7c196e2a5 336
gatedClock 63:63f362bcc2ac 337
gatedClock 60:e10bf95bbc96 338 queueEvent = queueUpdateFSM.get(1); // get message.
gatedClock 60:e10bf95bbc96 339 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 60:e10bf95bbc96 340 {
gatedClock 61:8026a9fc0cf1 341 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 342 dButtonState = (int) queueEvent.value.p;
gatedClock 63:63f362bcc2ac 343
gatedClock 63:63f362bcc2ac 344 if (dButtonState == MSG_START)
gatedClock 63:63f362bcc2ac 345 {
gatedClock 63:63f362bcc2ac 346 dButtonStart = 1;
gatedClock 63:63f362bcc2ac 347 dButtonStop = 0;
gatedClock 63:63f362bcc2ac 348 }
gatedClock 63:63f362bcc2ac 349 if (dButtonState == MSG_STOP)
gatedClock 63:63f362bcc2ac 350 {
gatedClock 63:63f362bcc2ac 351 dButtonStart = 0;
gatedClock 63:63f362bcc2ac 352 dButtonStop = 1;
gatedClock 63:63f362bcc2ac 353 }
gatedClock 63:63f362bcc2ac 354
gatedClock 63:63f362bcc2ac 355 if (dButtonState == MSG_OPEN)
gatedClock 63:63f362bcc2ac 356 {
gatedClock 63:63f362bcc2ac 357 dDoorOpen = 1;
gatedClock 63:63f362bcc2ac 358 }
gatedClock 63:63f362bcc2ac 359
gatedClock 63:63f362bcc2ac 360 if (dButtonState == MSG_CLOSED)
gatedClock 63:63f362bcc2ac 361 {
gatedClock 63:63f362bcc2ac 362 dDoorOpen = 0;
gatedClock 63:63f362bcc2ac 363 }
gatedClock 63:63f362bcc2ac 364
gatedClock 61:8026a9fc0cf1 365 }
gatedClock 61:8026a9fc0cf1 366 // get message.
gatedClock 61:8026a9fc0cf1 367 queueEvent = queueUpdateRemainingTime.get(1);
gatedClock 61:8026a9fc0cf1 368 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 61:8026a9fc0cf1 369 {
gatedClock 61:8026a9fc0cf1 370 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 371 dRemainingTime = (int) queueEvent.value.p;
gatedClock 61:8026a9fc0cf1 372 }
gatedClock 60:e10bf95bbc96 373
gatedClock 60:e10bf95bbc96 374
gatedClock 61:8026a9fc0cf1 375
gatedClock 57:0432c68ad232 376
gatedClock 57:0432c68ad232 377
gatedClock 57:0432c68ad232 378
gatedClock 57:0432c68ad232 379
gatedClock 57:0432c68ad232 380
gatedClock 57:0432c68ad232 381 Thread::wait(THREAD_5_WAITmS); // multitasking.
gatedClock 57:0432c68ad232 382 } // thread loop.
gatedClock 57:0432c68ad232 383 } // threadCookStateFSM.
gatedClock 57:0432c68ad232 384 /*----------------------------------------------//----------------------------*/
gatedClock 19:92b11e30aaaf 385 void slowClock(void) // 1Hz or thereabouts.
gatedClock 19:92b11e30aaaf 386 {
gatedClock 24:d39516e077ea 387 gcSlowClock = !gcSlowClock; // toggle clock.
gatedClock 19:92b11e30aaaf 388 }
gatedClock 19:92b11e30aaaf 389 /*----------------------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 390 void initialization(void) // program initializations.
gatedClock 0:fcca4db7b32a 391 {
gatedClock 16:db7f0b3b2605 392 gcSignalWaitEnable = 1;
gatedClock 0:fcca4db7b32a 393 }
gatedClock 0:fcca4db7b32a 394 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 395 void ISRleftButtonRising(void) // cooktime plus 60s.
gatedClock 1:9188d4668a88 396 {
gatedClock 66:4a0006fa5cc1 397 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 398 gtButtons.cLeftButton = 1; // detect left button.
gatedClock 9:cfdb9aa5857c 399
gatedClock 66:4a0006fa5cc1 400 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 401 }
gatedClock 1:9188d4668a88 402 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 403 void ISRleftButtonFalling(void) // button-release debounce.
gatedClock 1:9188d4668a88 404 {
gatedClock 66:4a0006fa5cc1 405 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 406 }
gatedClock 2:665ffa57031f 407 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 408 void ISRrightButtonRising(void) // cooktime -60s.
gatedClock 12:e40272e1fd8f 409 {
gatedClock 66:4a0006fa5cc1 410 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 411 gtButtons.cRightButton = 1; // detect right button.
gatedClock 12:e40272e1fd8f 412
gatedClock 66:4a0006fa5cc1 413 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 414 }
gatedClock 12:e40272e1fd8f 415 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 416 void ISRrightButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 417 {
gatedClock 66:4a0006fa5cc1 418 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 419 }
gatedClock 12:e40272e1fd8f 420 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 421 void ISRtopButtonRising(void) // cook start.
gatedClock 12:e40272e1fd8f 422 {
gatedClock 66:4a0006fa5cc1 423 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 424 gtButtons.cTopButton = 1; // detect top button.
gatedClock 12:e40272e1fd8f 425
gatedClock 66:4a0006fa5cc1 426 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 427 }
gatedClock 12:e40272e1fd8f 428 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 429 void ISRtopButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 430 {
gatedClock 66:4a0006fa5cc1 431 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 432 }
gatedClock 12:e40272e1fd8f 433 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 434 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 435 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 436 // upon off-button press, in the interrupt that it generates.
gatedClock 53:8c2baf5623c8 437
gatedClock 26:bff592483cb1 438 void ISRbottomButtonRising(void) // cook stop.
gatedClock 12:e40272e1fd8f 439 {
gatedClock 66:4a0006fa5cc1 440 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 441 {
gatedClock 66:4a0006fa5cc1 442 led0 = 0; // magnetron off.
gatedClock 66:4a0006fa5cc1 443 gtButtons.cBottomButton = 1; // detect bottom button.
gatedClock 66:4a0006fa5cc1 444 }
gatedClock 66:4a0006fa5cc1 445 debounceTimer.reset(); // begin debounce period.
gatedClock 66:4a0006fa5cc1 446 }
gatedClock 12:e40272e1fd8f 447 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 448 void ISRbottomButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 449 {
gatedClock 66:4a0006fa5cc1 450 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 451 }
gatedClock 12:e40272e1fd8f 452 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 453 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 454 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 455 // upon detection of an open door.
gatedClock 53:8c2baf5623c8 456
gatedClock 26:bff592483cb1 457 void ISRcenterButtonRising(void) // toggle door state.
gatedClock 12:e40272e1fd8f 458 {
gatedClock 66:4a0006fa5cc1 459 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 460 {
gatedClock 66:4a0006fa5cc1 461 if (gtButtons.cDoorOpen == MSG_OPEN) // calculate door state.
gatedClock 66:4a0006fa5cc1 462 gtButtons.cDoorOpen = MSG_CLOSED;
gatedClock 66:4a0006fa5cc1 463 else
gatedClock 66:4a0006fa5cc1 464 gtButtons.cDoorOpen = MSG_OPEN;
gatedClock 53:8c2baf5623c8 465
gatedClock 59:5e45b5e4a874 466 // magnetron off.
gatedClock 66:4a0006fa5cc1 467 if (gtButtons.cDoorOpen == MSG_OPEN) led0 = 0;
gatedClock 58:ec630b6dd9b1 468
gatedClock 66:4a0006fa5cc1 469 gtButtons.cCenterButton = 1;
gatedClock 66:4a0006fa5cc1 470 }
gatedClock 66:4a0006fa5cc1 471 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 472 }
gatedClock 12:e40272e1fd8f 473 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 474 void ISRcenterButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 475 {
gatedClock 66:4a0006fa5cc1 476 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 477 }
gatedClock 12:e40272e1fd8f 478 /*----------------------------------------------//----------------------------*/
gatedClock 40:7afff79f0d8b 479 void temperatureThread(void const *args) // temperature measurement.
gatedClock 39:4e7e4d935a87 480 {
gatedClock 45:e3207684e841 481 while(1) // thread loop.
gatedClock 45:e3207684e841 482 {
gatedClock 45:e3207684e841 483 gfCelsius = temperature.read(); // physical measurement.
gatedClock 40:7afff79f0d8b 484
gatedClock 45:e3207684e841 485 Thread::wait(THREAD_1_WAITmS); // multitasking.
gatedClock 45:e3207684e841 486 } // thread loop.
gatedClock 45:e3207684e841 487 } // temperatureThread.
gatedClock 42:266d5bbbfd19 488 /*----------------------------------------------//----------------------------*/
gatedClock 42:266d5bbbfd19 489 void LCDthread(void const *args) // LCD display thread.
gatedClock 42:266d5bbbfd19 490 {
gatedClock 67:1d9c85a4c3c1 491
gatedClock 67:1d9c85a4c3c1 492 static int dLCDtotalCookTimeSec = 0; // sample current values.
gatedClock 67:1d9c85a4c3c1 493 static int dCookTimeRemainingSec = 0;
gatedClock 67:1d9c85a4c3c1 494 static float fLCDcelsius = 0.0;
gatedClock 67:1d9c85a4c3c1 495
gatedClock 67:1d9c85a4c3c1 496 // remember previous values.
gatedClock 67:1d9c85a4c3c1 497 static int dLCDtotalCookTimeSecLast = 0;
gatedClock 67:1d9c85a4c3c1 498 static int dCookTimeRemainingSecLast = 0;
gatedClock 67:1d9c85a4c3c1 499 static float fLCDcelsiusLast = 0.0;
gatedClock 67:1d9c85a4c3c1 500
gatedClock 44:d16e813e61ef 501 while(1) // thread loop.
gatedClock 44:d16e813e61ef 502 {
gatedClock 67:1d9c85a4c3c1 503 // don't allow the values to
gatedClock 67:1d9c85a4c3c1 504 // change in the middle of the
gatedClock 67:1d9c85a4c3c1 505 // below else the anti-blink
gatedClock 67:1d9c85a4c3c1 506 // code won't work.
gatedClock 67:1d9c85a4c3c1 507 dLCDtotalCookTimeSec = gdLCDtotalCookTimeSec;
gatedClock 67:1d9c85a4c3c1 508 dCookTimeRemainingSec = gdCookTimeRemainingSec;
gatedClock 67:1d9c85a4c3c1 509 fLCDcelsius = gfLCDcelsius;
gatedClock 67:1d9c85a4c3c1 510
gatedClock 67:1d9c85a4c3c1 511
gatedClock 67:1d9c85a4c3c1 512 // clear display only when
gatedClock 67:1d9c85a4c3c1 513 // necessary, in order to avoid
gatedClock 67:1d9c85a4c3c1 514 // 'blinkieness'.
gatedClock 67:1d9c85a4c3c1 515 if (dLCDtotalCookTimeSec != dLCDtotalCookTimeSecLast ||
gatedClock 67:1d9c85a4c3c1 516 dCookTimeRemainingSec != dCookTimeRemainingSecLast ||
gatedClock 67:1d9c85a4c3c1 517 fLCDcelsius != fLCDcelsiusLast)
gatedClock 67:1d9c85a4c3c1 518 lcd.cls();
gatedClock 42:266d5bbbfd19 519
gatedClock 44:d16e813e61ef 520 LCD1; // line 1.
gatedClock 67:1d9c85a4c3c1 521 lcd.printf(" total cook time: %d",dLCDtotalCookTimeSec);
gatedClock 42:266d5bbbfd19 522
gatedClock 44:d16e813e61ef 523 LCD2; // line 2.
gatedClock 67:1d9c85a4c3c1 524 lcd.printf(" remaing cook time: %d",dCookTimeRemainingSec);
gatedClock 42:266d5bbbfd19 525
gatedClock 44:d16e813e61ef 526 LCD3; // line 3.
gatedClock 67:1d9c85a4c3c1 527 lcd.printf(" temperature : %5.3f",fLCDcelsius);
gatedClock 67:1d9c85a4c3c1 528
gatedClock 67:1d9c85a4c3c1 529 // pipeline variables.
gatedClock 67:1d9c85a4c3c1 530 dLCDtotalCookTimeSecLast = dLCDtotalCookTimeSec;
gatedClock 67:1d9c85a4c3c1 531 dCookTimeRemainingSecLast = dCookTimeRemainingSec;
gatedClock 67:1d9c85a4c3c1 532 fLCDcelsiusLast = fLCDcelsius;
gatedClock 42:266d5bbbfd19 533
gatedClock 44:d16e813e61ef 534 Thread::wait(THREAD_2_WAITmS); // multitasking.
gatedClock 44:d16e813e61ef 535 } // thread loop.
gatedClock 44:d16e813e61ef 536 } // LCDthread.
gatedClock 42:266d5bbbfd19 537 /*----------------------------------------------//----------------------------*/
gatedClock 64:255295f1d782 538 void cookRemainingTime(void) // cook-cycle countdown.
gatedClock 64:255295f1d782 539 {
gatedClock 64:255295f1d782 540
gatedClock 65:e39360da5929 541 static int dRemainingTime = 0; // remaining time.
gatedClock 65:e39360da5929 542 osEvent queueEvent; // queue event.
gatedClock 66:4a0006fa5cc1 543
gatedClock 65:e39360da5929 544 dRemainingTime--;
gatedClock 66:4a0006fa5cc1 545
gatedClock 65:e39360da5929 546 if (dRemainingTime < 0) dRemainingTime = 0;
gatedClock 66:4a0006fa5cc1 547
gatedClock 65:e39360da5929 548
gatedClock 65:e39360da5929 549 // pc.printf("\n\r dRemainingTime = %d",dRemainingTime);
gatedClock 65:e39360da5929 550
gatedClock 65:e39360da5929 551 // get message.
gatedClock 65:e39360da5929 552 queueEvent = queueSetRemainingTime.get(1);
gatedClock 65:e39360da5929 553 if (queueEvent.status == osEventMessage) // update state variable.
gatedClock 65:e39360da5929 554 {
gatedClock 65:e39360da5929 555 // interpret as integer, not pointer.
gatedClock 65:e39360da5929 556 dRemainingTime = (int) queueEvent.value.p;
gatedClock 65:e39360da5929 557 }
gatedClock 64:255295f1d782 558 }
gatedClock 64:255295f1d782 559 /*----------------------------------------------//----------------------------*/