homework 7

Dependencies:   mbed-rtos mbed C12832_lcd LM75B

Committer:
gatedClock
Date:
Wed Sep 11 17:28:46 2013 +0000
Revision:
66:4a0006fa5cc1
Parent:
65:e39360da5929
Child:
67:1d9c85a4c3c1
improved debounce.  fast button manager, but there's a lag elsewhere.

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 44:d16e813e61ef 26 #define THREAD_2_WAITmS 400 // thread 2 wait in mS.
gatedClock 52:8ba6a0c91a89 27 #define THREAD_3_WAITmS 400 // 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 65:e39360da5929 188
gatedClock 65:e39360da5929 189
gatedClock 65:e39360da5929 190
gatedClock 54:b0e7352d2516 191 while(1) // thread loop.
gatedClock 54:b0e7352d2516 192 {
gatedClock 66:4a0006fa5cc1 193
gatedClock 65:e39360da5929 194
gatedClock 54:b0e7352d2516 195
gatedClock 54:b0e7352d2516 196 //--- // TOTAL TIME CONTROL.
gatedClock 56:18cff6eb91db 197
gatedClock 56:18cff6eb91db 198 // encoded integers will be sent,
gatedClock 56:18cff6eb91db 199 // not pointers.
gatedClock 50:2928c3cbdcc3 200
gatedClock 50:2928c3cbdcc3 201 if (gtButtons.cLeftButton) // total time increment button.
gatedClock 50:2928c3cbdcc3 202 {
gatedClock 54:b0e7352d2516 203 dMessage = MSG_INC_TIME; // set message.
gatedClock 66:4a0006fa5cc1 204 //pc.printf("\n\r before.");
gatedClock 56:18cff6eb91db 205 queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
gatedClock 66:4a0006fa5cc1 206 //pc.printf("\n\r after.");
gatedClock 50:2928c3cbdcc3 207 gtButtons.cLeftButton = 0; // clear the button state.
gatedClock 65:e39360da5929 208 pc.printf("\n\r time increment button.");
gatedClock 50:2928c3cbdcc3 209 }
gatedClock 66:4a0006fa5cc1 210 //pc.printf("\n\r 002");
gatedClock 59:5e45b5e4a874 211 if (gtButtons.cRightButton) // total time decrement button.
gatedClock 50:2928c3cbdcc3 212 {
gatedClock 54:b0e7352d2516 213 dMessage = MSG_DEC_TIME; // set message.
gatedClock 56:18cff6eb91db 214 queueModTotalTime.put((int *) dMessage);// pretend it's a pointer.
gatedClock 50:2928c3cbdcc3 215 gtButtons.cRightButton = 0; // clear the button state.
gatedClock 65:e39360da5929 216 pc.printf("\n\r time decrement button.");
gatedClock 50:2928c3cbdcc3 217 }
gatedClock 66:4a0006fa5cc1 218 //pc.printf("\n\r 003");
gatedClock 59:5e45b5e4a874 219 //--- // COOK-STATE FSM.
gatedClock 59:5e45b5e4a874 220
gatedClock 59:5e45b5e4a874 221 if (gtButtons.cTopButton) // start-cook button.
gatedClock 58:ec630b6dd9b1 222 {
gatedClock 59:5e45b5e4a874 223 dMessage = MSG_START; // set message.
gatedClock 59:5e45b5e4a874 224 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 225 gtButtons.cTopButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 226 }
gatedClock 66:4a0006fa5cc1 227 //pc.printf("\n\r 004");
gatedClock 59:5e45b5e4a874 228 if (gtButtons.cBottomButton) // stop-cook button.
gatedClock 58:ec630b6dd9b1 229 {
gatedClock 59:5e45b5e4a874 230 dMessage = MSG_STOP; // set message.
gatedClock 59:5e45b5e4a874 231 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 232 gtButtons.cBottomButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 233 }
gatedClock 66:4a0006fa5cc1 234 //pc.printf("\n\r 005");
gatedClock 59:5e45b5e4a874 235 if (gtButtons.cCenterButton) // door-state-toggle.
gatedClock 58:ec630b6dd9b1 236 {
gatedClock 59:5e45b5e4a874 237 dMessage = gtButtons.cDoorOpen; // determined in ISR.
gatedClock 59:5e45b5e4a874 238 queueUpdateFSM.put((int *) dMessage); // pretend it's a pointer.
gatedClock 59:5e45b5e4a874 239 gtButtons.cCenterButton = 0; // clear the button state.
gatedClock 58:ec630b6dd9b1 240 }
gatedClock 66:4a0006fa5cc1 241 //pc.printf("\n\r 006");
gatedClock 58:ec630b6dd9b1 242
gatedClock 59:5e45b5e4a874 243 //---
gatedClock 58:ec630b6dd9b1 244
gatedClock 54:b0e7352d2516 245 Thread::wait(THREAD_4_WAITmS); // multitasking.
gatedClock 65:e39360da5929 246
gatedClock 66:4a0006fa5cc1 247 //pc.printf("\n\r 007");
gatedClock 54:b0e7352d2516 248 } // thread loop.
gatedClock 50:2928c3cbdcc3 249
gatedClock 54:b0e7352d2516 250 } // threadButtonStateManager.
gatedClock 50:2928c3cbdcc3 251 /*----------------------------------------------//----------------------------*/
gatedClock 51:e5ec74c49b01 252 // the incoming messages are mutually-exclusive.
gatedClock 51:e5ec74c49b01 253 // total time controller.
gatedClock 51:e5ec74c49b01 254 void threadTotalTimeControl(void const *args)
gatedClock 50:2928c3cbdcc3 255 {
gatedClock 51:e5ec74c49b01 256 static int dTotalTime = 0; // total time variable.
gatedClock 60:e10bf95bbc96 257 int dMessage; // message.
gatedClock 51:e5ec74c49b01 258 osEvent queueEvent; // queue event.
gatedClock 50:2928c3cbdcc3 259
gatedClock 50:2928c3cbdcc3 260 while(1) // thread loop.
gatedClock 50:2928c3cbdcc3 261 {
gatedClock 50:2928c3cbdcc3 262
gatedClock 55:17f3354da63a 263 queueEvent = queueModTotalTime.get(1); // get message.
gatedClock 51:e5ec74c49b01 264 if (queueEvent.status == osEventMessage)
gatedClock 51:e5ec74c49b01 265 {
gatedClock 56:18cff6eb91db 266 dMessage = (int) queueEvent.value.p; // interpret as integer, not pointer.
gatedClock 54:b0e7352d2516 267
gatedClock 54:b0e7352d2516 268 // increment total time.
gatedClock 56:18cff6eb91db 269 if (dMessage == MSG_INC_TIME) dTotalTime += 60;
gatedClock 54:b0e7352d2516 270
gatedClock 54:b0e7352d2516 271 // decrement total time.
gatedClock 56:18cff6eb91db 272 if (dMessage == MSG_DEC_TIME) dTotalTime -= 60;
gatedClock 65:e39360da5929 273
gatedClock 65:e39360da5929 274 // saturations.
gatedClock 65:e39360da5929 275 if (dTotalTime > 180) dTotalTime = 180;
gatedClock 65:e39360da5929 276 if (dTotalTime < 0) dTotalTime = 0;
gatedClock 65:e39360da5929 277
gatedClock 65:e39360da5929 278 queueSetRemainingTime.put((int *) dTotalTime);
gatedClock 65:e39360da5929 279
gatedClock 65:e39360da5929 280
gatedClock 51:e5ec74c49b01 281 }
gatedClock 54:b0e7352d2516 282
gatedClock 65:e39360da5929 283
gatedClock 51:e5ec74c49b01 284
gatedClock 51:e5ec74c49b01 285 gdLCDtotalCookTimeSec = dTotalTime; // transmit to LCD.
gatedClock 61:8026a9fc0cf1 286
gatedClock 61:8026a9fc0cf1 287 // pretend it's a pointer.
gatedClock 61:8026a9fc0cf1 288 // queueUpdateTotalTime.put((int *) dTotalTime);
gatedClock 61:8026a9fc0cf1 289
gatedClock 61:8026a9fc0cf1 290
gatedClock 50:2928c3cbdcc3 291
gatedClock 50:2928c3cbdcc3 292 Thread::wait(THREAD_3_WAITmS); // multitasking.
gatedClock 50:2928c3cbdcc3 293 } // thread loop.
gatedClock 50:2928c3cbdcc3 294 } // threadTotalTimeControl.
gatedClock 33:34c1bef3c4ff 295 /*----------------------------------------------//----------------------------*/
gatedClock 60:e10bf95bbc96 296 /*
gatedClock 60:e10bf95bbc96 297 #define FSM_IDLE 0x00 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 298 #define FSM_COOK 0x01 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 299 #define FSM_PAUSE 0x02 // cook-state state-machine.
gatedClock 60:e10bf95bbc96 300 #define FSM_DONE 0x04 // cook-state state-machine.
gatedClock 62:48e7c196e2a5 301
gatedClock 62:48e7c196e2a5 302 #define MSG_START 0x04
gatedClock 62:48e7c196e2a5 303 #define MSG_STOP 0x08
gatedClock 62:48e7c196e2a5 304 #define MSG_OPEN 0x10
gatedClock 62:48e7c196e2a5 305 #define MSG_CLOSED 0x20
gatedClock 60:e10bf95bbc96 306 */
gatedClock 57:0432c68ad232 307 void threadCookStateFSM(void const *args) // cook-cycle FSM.
gatedClock 57:0432c68ad232 308 {
gatedClock 60:e10bf95bbc96 309 static int dFSMstate = FSM_IDLE; // state of this FSM.
gatedClock 61:8026a9fc0cf1 310 static int dButtonState; // received button state.
gatedClock 61:8026a9fc0cf1 311 static int dRemainingTime; // received remaining time.
gatedClock 62:48e7c196e2a5 312
gatedClock 62:48e7c196e2a5 313 static int dButtonStart = 0;
gatedClock 62:48e7c196e2a5 314 static int dButtonStop = 0;
gatedClock 62:48e7c196e2a5 315 static int dDoorOpen = 0;
gatedClock 62:48e7c196e2a5 316 static int dTimeRemaining = 0;
gatedClock 62:48e7c196e2a5 317
gatedClock 63:63f362bcc2ac 318 osEvent queueEvent; // queue event.
gatedClock 63:63f362bcc2ac 319
gatedClock 57:0432c68ad232 320 while(1) // thread loop.
gatedClock 57:0432c68ad232 321 {
gatedClock 62:48e7c196e2a5 322
gatedClock 63:63f362bcc2ac 323 if (dFSMstate == FSM_IDLE) // idle state.
gatedClock 62:48e7c196e2a5 324 {
gatedClock 62:48e7c196e2a5 325 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 326 }
gatedClock 62:48e7c196e2a5 327 else
gatedClock 63:63f362bcc2ac 328 if (dFSMstate == FSM_COOK) // cook state.
gatedClock 62:48e7c196e2a5 329 {
gatedClock 62:48e7c196e2a5 330 if (dDoorOpen & dTimeRemaining) dFSMstate = FSM_PAUSE;
gatedClock 62:48e7c196e2a5 331 }
gatedClock 62:48e7c196e2a5 332 else
gatedClock 63:63f362bcc2ac 333 if (dFSMstate == FSM_PAUSE) // pause state.
gatedClock 62:48e7c196e2a5 334 {
gatedClock 62:48e7c196e2a5 335 if (dButtonStart & !dDoorOpen & dTimeRemaining) dFSMstate = FSM_DONE;
gatedClock 62:48e7c196e2a5 336 if (dButtonStop) dFSMstate = FSM_COOK;
gatedClock 62:48e7c196e2a5 337 }
gatedClock 62:48e7c196e2a5 338 else
gatedClock 62:48e7c196e2a5 339 if (dFSMstate == FSM_DONE) // incomplete.
gatedClock 62:48e7c196e2a5 340 {
gatedClock 63:63f362bcc2ac 341 // timerFSMdone.reset(); // reset the timer.
gatedClock 63:63f362bcc2ac 342 dFSMstate = FSM_IDLE; // end of beep.
gatedClock 62:48e7c196e2a5 343 }
gatedClock 62:48e7c196e2a5 344
gatedClock 62:48e7c196e2a5 345
gatedClock 63:63f362bcc2ac 346
gatedClock 60:e10bf95bbc96 347 queueEvent = queueUpdateFSM.get(1); // get message.
gatedClock 60:e10bf95bbc96 348 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 60:e10bf95bbc96 349 {
gatedClock 61:8026a9fc0cf1 350 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 351 dButtonState = (int) queueEvent.value.p;
gatedClock 63:63f362bcc2ac 352
gatedClock 63:63f362bcc2ac 353 if (dButtonState == MSG_START)
gatedClock 63:63f362bcc2ac 354 {
gatedClock 63:63f362bcc2ac 355 dButtonStart = 1;
gatedClock 63:63f362bcc2ac 356 dButtonStop = 0;
gatedClock 63:63f362bcc2ac 357 }
gatedClock 63:63f362bcc2ac 358 if (dButtonState == MSG_STOP)
gatedClock 63:63f362bcc2ac 359 {
gatedClock 63:63f362bcc2ac 360 dButtonStart = 0;
gatedClock 63:63f362bcc2ac 361 dButtonStop = 1;
gatedClock 63:63f362bcc2ac 362 }
gatedClock 63:63f362bcc2ac 363
gatedClock 63:63f362bcc2ac 364 if (dButtonState == MSG_OPEN)
gatedClock 63:63f362bcc2ac 365 {
gatedClock 63:63f362bcc2ac 366 dDoorOpen = 1;
gatedClock 63:63f362bcc2ac 367 }
gatedClock 63:63f362bcc2ac 368
gatedClock 63:63f362bcc2ac 369 if (dButtonState == MSG_CLOSED)
gatedClock 63:63f362bcc2ac 370 {
gatedClock 63:63f362bcc2ac 371 dDoorOpen = 0;
gatedClock 63:63f362bcc2ac 372 }
gatedClock 63:63f362bcc2ac 373
gatedClock 61:8026a9fc0cf1 374 }
gatedClock 61:8026a9fc0cf1 375 // get message.
gatedClock 61:8026a9fc0cf1 376 queueEvent = queueUpdateRemainingTime.get(1);
gatedClock 61:8026a9fc0cf1 377 if (queueEvent.status == osEventMessage)// update state variable.
gatedClock 61:8026a9fc0cf1 378 {
gatedClock 61:8026a9fc0cf1 379 // interpret as integer, not pointer.
gatedClock 61:8026a9fc0cf1 380 dRemainingTime = (int) queueEvent.value.p;
gatedClock 61:8026a9fc0cf1 381 }
gatedClock 60:e10bf95bbc96 382
gatedClock 60:e10bf95bbc96 383
gatedClock 61:8026a9fc0cf1 384
gatedClock 57:0432c68ad232 385
gatedClock 57:0432c68ad232 386
gatedClock 57:0432c68ad232 387
gatedClock 57:0432c68ad232 388
gatedClock 57:0432c68ad232 389
gatedClock 57:0432c68ad232 390 Thread::wait(THREAD_5_WAITmS); // multitasking.
gatedClock 57:0432c68ad232 391 } // thread loop.
gatedClock 57:0432c68ad232 392 } // threadCookStateFSM.
gatedClock 57:0432c68ad232 393 /*----------------------------------------------//----------------------------*/
gatedClock 19:92b11e30aaaf 394 void slowClock(void) // 1Hz or thereabouts.
gatedClock 19:92b11e30aaaf 395 {
gatedClock 24:d39516e077ea 396 gcSlowClock = !gcSlowClock; // toggle clock.
gatedClock 19:92b11e30aaaf 397 }
gatedClock 19:92b11e30aaaf 398 /*----------------------------------------------//----------------------------*/
gatedClock 0:fcca4db7b32a 399 void initialization(void) // program initializations.
gatedClock 0:fcca4db7b32a 400 {
gatedClock 16:db7f0b3b2605 401 gcSignalWaitEnable = 1;
gatedClock 0:fcca4db7b32a 402 }
gatedClock 0:fcca4db7b32a 403 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 404 void ISRleftButtonRising(void) // cooktime plus 60s.
gatedClock 1:9188d4668a88 405 {
gatedClock 66:4a0006fa5cc1 406 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 407 gtButtons.cLeftButton = 1; // detect left button.
gatedClock 9:cfdb9aa5857c 408
gatedClock 66:4a0006fa5cc1 409 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 410 }
gatedClock 1:9188d4668a88 411 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 412 void ISRleftButtonFalling(void) // button-release debounce.
gatedClock 1:9188d4668a88 413 {
gatedClock 66:4a0006fa5cc1 414 debounceTimer.reset(); // begin debounce period.
gatedClock 11:9cae003da12b 415 }
gatedClock 2:665ffa57031f 416 /*----------------------------------------------//----------------------------*/
gatedClock 26:bff592483cb1 417 void ISRrightButtonRising(void) // cooktime -60s.
gatedClock 12:e40272e1fd8f 418 {
gatedClock 66:4a0006fa5cc1 419 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 420 gtButtons.cRightButton = 1; // detect right 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 ISRrightButtonFalling(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 26:bff592483cb1 430 void ISRtopButtonRising(void) // cook start.
gatedClock 12:e40272e1fd8f 431 {
gatedClock 66:4a0006fa5cc1 432 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 26:bff592483cb1 433 gtButtons.cTopButton = 1; // detect top button.
gatedClock 12:e40272e1fd8f 434
gatedClock 66:4a0006fa5cc1 435 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 436 }
gatedClock 12:e40272e1fd8f 437 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 438 void ISRtopButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 439 {
gatedClock 66:4a0006fa5cc1 440 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 441 }
gatedClock 12:e40272e1fd8f 442 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 443 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 444 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 445 // upon off-button press, in the interrupt that it generates.
gatedClock 53:8c2baf5623c8 446
gatedClock 26:bff592483cb1 447 void ISRbottomButtonRising(void) // cook stop.
gatedClock 12:e40272e1fd8f 448 {
gatedClock 66:4a0006fa5cc1 449 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 450 {
gatedClock 66:4a0006fa5cc1 451 led0 = 0; // magnetron off.
gatedClock 66:4a0006fa5cc1 452 gtButtons.cBottomButton = 1; // detect bottom button.
gatedClock 66:4a0006fa5cc1 453 }
gatedClock 66:4a0006fa5cc1 454 debounceTimer.reset(); // begin debounce period.
gatedClock 66:4a0006fa5cc1 455 }
gatedClock 12:e40272e1fd8f 456 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 457 void ISRbottomButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 458 {
gatedClock 66:4a0006fa5cc1 459 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 460 }
gatedClock 12:e40272e1fd8f 461 /*----------------------------------------------//----------------------------*/
gatedClock 53:8c2baf5623c8 462 // front-end control of magnetron off.
gatedClock 53:8c2baf5623c8 463 // due to physical danger, the magnetron is turned off immediately
gatedClock 53:8c2baf5623c8 464 // upon detection of an open door.
gatedClock 53:8c2baf5623c8 465
gatedClock 26:bff592483cb1 466 void ISRcenterButtonRising(void) // toggle door state.
gatedClock 12:e40272e1fd8f 467 {
gatedClock 66:4a0006fa5cc1 468 if (debounceTimer.read_ms() > DEBOUNCEmS)
gatedClock 66:4a0006fa5cc1 469 {
gatedClock 66:4a0006fa5cc1 470 if (gtButtons.cDoorOpen == MSG_OPEN) // calculate door state.
gatedClock 66:4a0006fa5cc1 471 gtButtons.cDoorOpen = MSG_CLOSED;
gatedClock 66:4a0006fa5cc1 472 else
gatedClock 66:4a0006fa5cc1 473 gtButtons.cDoorOpen = MSG_OPEN;
gatedClock 53:8c2baf5623c8 474
gatedClock 59:5e45b5e4a874 475 // magnetron off.
gatedClock 66:4a0006fa5cc1 476 if (gtButtons.cDoorOpen == MSG_OPEN) led0 = 0;
gatedClock 58:ec630b6dd9b1 477
gatedClock 66:4a0006fa5cc1 478 gtButtons.cCenterButton = 1;
gatedClock 66:4a0006fa5cc1 479 }
gatedClock 66:4a0006fa5cc1 480 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 481 }
gatedClock 12:e40272e1fd8f 482 /*----------------------------------------------//----------------------------*/
gatedClock 15:5eaa2ab1d00d 483 void ISRcenterButtonFalling(void) // button-release debounce.
gatedClock 12:e40272e1fd8f 484 {
gatedClock 66:4a0006fa5cc1 485 debounceTimer.reset(); // begin debounce period.
gatedClock 12:e40272e1fd8f 486 }
gatedClock 12:e40272e1fd8f 487 /*----------------------------------------------//----------------------------*/
gatedClock 40:7afff79f0d8b 488 void temperatureThread(void const *args) // temperature measurement.
gatedClock 39:4e7e4d935a87 489 {
gatedClock 45:e3207684e841 490 while(1) // thread loop.
gatedClock 45:e3207684e841 491 {
gatedClock 45:e3207684e841 492 gfCelsius = temperature.read(); // physical measurement.
gatedClock 40:7afff79f0d8b 493
gatedClock 45:e3207684e841 494 Thread::wait(THREAD_1_WAITmS); // multitasking.
gatedClock 45:e3207684e841 495 } // thread loop.
gatedClock 45:e3207684e841 496 } // temperatureThread.
gatedClock 42:266d5bbbfd19 497 /*----------------------------------------------//----------------------------*/
gatedClock 42:266d5bbbfd19 498 void LCDthread(void const *args) // LCD display thread.
gatedClock 42:266d5bbbfd19 499 {
gatedClock 44:d16e813e61ef 500 while(1) // thread loop.
gatedClock 44:d16e813e61ef 501 {
gatedClock 44:d16e813e61ef 502 lcd.cls(); // clear the display.
gatedClock 42:266d5bbbfd19 503
gatedClock 44:d16e813e61ef 504 LCD1; // line 1.
gatedClock 44:d16e813e61ef 505 lcd.printf(" total cook time: %d",gdLCDtotalCookTimeSec);
gatedClock 42:266d5bbbfd19 506
gatedClock 44:d16e813e61ef 507 LCD2; // line 2.
gatedClock 44:d16e813e61ef 508 lcd.printf(" remaing cook time: %d",gdCookTimeRemainingSec);
gatedClock 42:266d5bbbfd19 509
gatedClock 44:d16e813e61ef 510 LCD3; // line 3.
gatedClock 44:d16e813e61ef 511 lcd.printf(" temperature : %5.3f",gfLCDcelsius);
gatedClock 42:266d5bbbfd19 512
gatedClock 44:d16e813e61ef 513 Thread::wait(THREAD_2_WAITmS); // multitasking.
gatedClock 44:d16e813e61ef 514 } // thread loop.
gatedClock 44:d16e813e61ef 515 } // LCDthread.
gatedClock 42:266d5bbbfd19 516 /*----------------------------------------------//----------------------------*/
gatedClock 64:255295f1d782 517 void cookRemainingTime(void) // cook-cycle countdown.
gatedClock 64:255295f1d782 518 {
gatedClock 64:255295f1d782 519
gatedClock 65:e39360da5929 520 static int dRemainingTime = 0; // remaining time.
gatedClock 65:e39360da5929 521 osEvent queueEvent; // queue event.
gatedClock 66:4a0006fa5cc1 522
gatedClock 65:e39360da5929 523 dRemainingTime--;
gatedClock 66:4a0006fa5cc1 524
gatedClock 65:e39360da5929 525 if (dRemainingTime < 0) dRemainingTime = 0;
gatedClock 66:4a0006fa5cc1 526
gatedClock 65:e39360da5929 527
gatedClock 65:e39360da5929 528 // pc.printf("\n\r dRemainingTime = %d",dRemainingTime);
gatedClock 65:e39360da5929 529
gatedClock 65:e39360da5929 530 // get message.
gatedClock 65:e39360da5929 531 queueEvent = queueSetRemainingTime.get(1);
gatedClock 65:e39360da5929 532 if (queueEvent.status == osEventMessage) // update state variable.
gatedClock 65:e39360da5929 533 {
gatedClock 65:e39360da5929 534 // interpret as integer, not pointer.
gatedClock 65:e39360da5929 535 dRemainingTime = (int) queueEvent.value.p;
gatedClock 65:e39360da5929 536 }
gatedClock 64:255295f1d782 537 }
gatedClock 64:255295f1d782 538 /*----------------------------------------------//----------------------------*/