homework 7
Dependencies: mbed-rtos mbed C12832_lcd LM75B
main.cpp@85:dfc5add157e7, 2013-09-12 (annotated)
- Committer:
- gatedClock
- Date:
- Thu Sep 12 04:55:14 2013 +0000
- Revision:
- 85:dfc5add157e7
- Parent:
- 84:fee8fb80d190
having trouble attaching the ticker within the fsm thread. may need to back away from this rev.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gatedClock | 0:fcca4db7b32a | 1 | /*----------------------------------------------//------------------------------ |
gatedClock | 0:fcca4db7b32a | 2 | student : m-moore |
gatedClock | 0:fcca4db7b32a | 3 | class : rtos |
gatedClock | 10:2b0a9fc39109 | 4 | directory : RTOS_HW_07 |
gatedClock | 0:fcca4db7b32a | 5 | file : main.cpp |
gatedClock | 0:fcca4db7b32a | 6 | ----description---------------------------------//------------------------------ |
gatedClock | 70:7c0743c28b11 | 7 | gotchyas |
gatedClock | 70:7c0743c28b11 | 8 | 1. using pc.printf inside a ticker routine will freeze the routine. |
gatedClock | 71:4a5f256ecf7c | 9 | 2. using queues (get, put) will not work within a ticker routine. |
gatedClock | 76:74c454c9d75b | 10 | 3. Ticker has a bug. http://mbed.org/questions/1563/Mbed-Tickerfunction-hangs-system-from-re/ |
gatedClock | 80:e3e1a2161435 | 11 | |
gatedClock | 80:e3e1a2161435 | 12 | improvements |
gatedClock | 80:e3e1a2161435 | 13 | countdown timer needs to be sync'd to start button press. |
gatedClock | 80:e3e1a2161435 | 14 | |
gatedClock | 80:e3e1a2161435 | 15 | |
gatedClock | 0:fcca4db7b32a | 16 | -----includes-----------------------------------//----------------------------*/ |
gatedClock | 0:fcca4db7b32a | 17 | #include "mbed.h" // mbed class. |
gatedClock | 10:2b0a9fc39109 | 18 | #include "rtos.h" // rtos class. |
gatedClock | 39:4e7e4d935a87 | 19 | #include "LM75B.h" // thermometer class. |
gatedClock | 10:2b0a9fc39109 | 20 | #include "C12832_lcd.h" // LCD class. |
gatedClock | 0:fcca4db7b32a | 21 | //---defines------------------------------------//------------------------------ |
gatedClock | 9:cfdb9aa5857c | 22 | #define LCD1 lcd.locate(0, 0); // LCD line 1. |
gatedClock | 9:cfdb9aa5857c | 23 | #define LCD2 lcd.locate(0,11); // LCD line 2. |
gatedClock | 9:cfdb9aa5857c | 24 | #define LCD3 lcd.locate(0,22); // LCD line 3. |
gatedClock | 9:cfdb9aa5857c | 25 | |
gatedClock | 10:2b0a9fc39109 | 26 | |
gatedClock | 66:4a0006fa5cc1 | 27 | #define DEBOUNCEmS 16 // debounce wait in mS. |
gatedClock | 19:92b11e30aaaf | 28 | #define uS_TIMEOUT 100 // Timer uS timeout. |
gatedClock | 19:92b11e30aaaf | 29 | #define LBSIG 1 // left button signal code. |
gatedClock | 33:34c1bef3c4ff | 30 | #define PIPEuS 1000 // pipeline clock period. |
gatedClock | 19:92b11e30aaaf | 31 | #define SLOWCLOCKuS 500000 // slow-clock period. |
gatedClock | 39:4e7e4d935a87 | 32 | #define TEMPCLOCKS 1 // temperature-measuring period in S. |
gatedClock | 26:bff592483cb1 | 33 | #define PIPEDATASIZE 8 // dimension of tPipeData. |
gatedClock | 44:d16e813e61ef | 34 | #define THREAD_1_WAITmS 400 // thread 1 wait in mS. |
gatedClock | 67:1d9c85a4c3c1 | 35 | #define THREAD_2_WAITmS 20 // LCD thread wait. |
gatedClock | 67:1d9c85a4c3c1 | 36 | #define THREAD_3_WAITmS 80 // thread 3 wait in mS. |
gatedClock | 66:4a0006fa5cc1 | 37 | #define THREAD_4_WAITmS 1 // thread 4 wait in mS. |
gatedClock | 78:7df160e0db7b | 38 | #define THREAD_5_WAITmS 1 // FSM thread wait. |
gatedClock | 44:d16e813e61ef | 39 | #define HB_MODULO 1024 // heartbeat modulo divisor. |
gatedClock | 50:2928c3cbdcc3 | 40 | |
gatedClock | 54:b0e7352d2516 | 41 | #define MSG_INC_TIME 0x01 |
gatedClock | 54:b0e7352d2516 | 42 | #define MSG_DEC_TIME 0x02 |
gatedClock | 58:ec630b6dd9b1 | 43 | #define MSG_START 0x04 |
gatedClock | 58:ec630b6dd9b1 | 44 | #define MSG_STOP 0x08 |
gatedClock | 58:ec630b6dd9b1 | 45 | #define MSG_OPEN 0x10 |
gatedClock | 58:ec630b6dd9b1 | 46 | #define MSG_CLOSED 0x20 |
gatedClock | 57:0432c68ad232 | 47 | |
gatedClock | 78:7df160e0db7b | 48 | #define FSM_IDLE 0x01 // cook-state state-machine. |
gatedClock | 78:7df160e0db7b | 49 | #define FSM_COOK 0x02 // cook-state state-machine. |
gatedClock | 78:7df160e0db7b | 50 | #define FSM_PAUSE 0x04 // cook-state state-machine. |
gatedClock | 78:7df160e0db7b | 51 | #define FSM_CONTINUE 0x08 // cook-state state-machine. |
gatedClock | 78:7df160e0db7b | 52 | #define FSM_DONE 0x10 // cook-state state-machine. |
gatedClock | 65:e39360da5929 | 53 | |
gatedClock | 71:4a5f256ecf7c | 54 | #define RT_PRELOAD 0x01 // remaining-time preload. |
gatedClock | 71:4a5f256ecf7c | 55 | #define RT_DECREMENT 0x02 // remaining-time decrement. |
gatedClock | 71:4a5f256ecf7c | 56 | #define RT_PAUSE 0x03 // remaining-time don't change. |
gatedClock | 71:4a5f256ecf7c | 57 | #define RT_CLEAR 0x04 // remaining-time set to zero. |
gatedClock | 71:4a5f256ecf7c | 58 | |
gatedClock | 65:e39360da5929 | 59 | #define DEBUG1 // debug preprocessor control. |
gatedClock | 0:fcca4db7b32a | 60 | //--global_definitions--------------------------//------------------------------ |
gatedClock | 26:bff592483cb1 | 61 | struct tButtons // button ISR updates. |
gatedClock | 26:bff592483cb1 | 62 | { |
gatedClock | 26:bff592483cb1 | 63 | char cLeftButton; // cooktime +60S. |
gatedClock | 26:bff592483cb1 | 64 | char cRightButton; // cooktime -60S. |
gatedClock | 26:bff592483cb1 | 65 | char cTopButton; // start cook. |
gatedClock | 26:bff592483cb1 | 66 | char cBottomButton; // stop cook. |
gatedClock | 58:ec630b6dd9b1 | 67 | char cCenterButton; // center button pressed. |
gatedClock | 53:8c2baf5623c8 | 68 | char cDoorOpen; // door open. |
gatedClock | 26:bff592483cb1 | 69 | }; |
gatedClock | 34:b449d2a7c786 | 70 | |
gatedClock | 71:4a5f256ecf7c | 71 | struct tRemainingTime // remaining time related. |
gatedClock | 71:4a5f256ecf7c | 72 | { |
gatedClock | 71:4a5f256ecf7c | 73 | char cControl; // countdown control. |
gatedClock | 71:4a5f256ecf7c | 74 | int dTotalTime; // initialize to this. |
gatedClock | 71:4a5f256ecf7c | 75 | int dRemainingTime; // the countdown value. |
gatedClock | 71:4a5f256ecf7c | 76 | }; |
gatedClock | 71:4a5f256ecf7c | 77 | |
gatedClock | 80:e3e1a2161435 | 78 | struct tLCD // LCD related. |
gatedClock | 80:e3e1a2161435 | 79 | { |
gatedClock | 80:e3e1a2161435 | 80 | int dTotalCookTime; // display time in seconds. |
gatedClock | 80:e3e1a2161435 | 81 | int dRemainingTime; // display time in seconds. |
gatedClock | 80:e3e1a2161435 | 82 | float fCelsius; // temperature in celsius. |
gatedClock | 80:e3e1a2161435 | 83 | }; |
gatedClock | 80:e3e1a2161435 | 84 | |
gatedClock | 80:e3e1a2161435 | 85 | |
gatedClock | 80:e3e1a2161435 | 86 | |
gatedClock | 61:8026a9fc0cf1 | 87 | Queue<int, 1> queueModTotalTime; // message to modify total time. |
gatedClock | 61:8026a9fc0cf1 | 88 | Queue<int, 1> queueUpdateFSM; // message to inform FSM. |
gatedClock | 61:8026a9fc0cf1 | 89 | Queue<int, 1> queueUpdateRemainingTime; // message to update remaining time. |
gatedClock | 71:4a5f256ecf7c | 90 | Queue<int, 1> queueSetRemainingTime; // tell countdown it's start time. |
gatedClock | 69:55b836e8ced7 | 91 | Queue<int, 1> queueFSMnewState; // latest FSM state. |
gatedClock | 0:fcca4db7b32a | 92 | //--global_variables----------------------------//------------------------------ |
gatedClock | 26:bff592483cb1 | 93 | char gcSignalWaitEnable; // 1 to wait on a signal. |
gatedClock | 26:bff592483cb1 | 94 | char gcSlowClock; // slow-clock signal. |
gatedClock | 33:34c1bef3c4ff | 95 | char gcInitializePipeline; // 1 to initialize pipeline state. |
gatedClock | 39:4e7e4d935a87 | 96 | float gfCelsius; // from thermometer. |
gatedClock | 84:fee8fb80d190 | 97 | |
gatedClock | 26:bff592483cb1 | 98 | tButtons gtButtons; // ISR button updates. |
gatedClock | 70:7c0743c28b11 | 99 | |
gatedClock | 71:4a5f256ecf7c | 100 | tRemainingTime giRemainingTime; // structure instance. |
gatedClock | 80:e3e1a2161435 | 101 | tLCD giLCD; // structure instance. |
gatedClock | 71:4a5f256ecf7c | 102 | |
gatedClock | 70:7c0743c28b11 | 103 | int gdDiagTotalTime; |
gatedClock | 0:fcca4db7b32a | 104 | //--global_instances----------------------------//------------------------------ |
gatedClock | 55:17f3354da63a | 105 | Serial pc(USBTX, USBRX); // PuTTY terminal communication. |
gatedClock | 39:4e7e4d935a87 | 106 | LM75B temperature(p28,p27); // on-board thermometer. |
gatedClock | 9:cfdb9aa5857c | 107 | C12832_LCD lcd; // LCD object. |
gatedClock | 53:8c2baf5623c8 | 108 | DigitalOut led0(LED4); // magnetron. |
gatedClock | 39:4e7e4d935a87 | 109 | DigitalOut led1(LED3); |
gatedClock | 39:4e7e4d935a87 | 110 | DigitalOut led2(LED2); |
gatedClock | 39:4e7e4d935a87 | 111 | DigitalOut led3(LED1); |
gatedClock | 0:fcca4db7b32a | 112 | |
gatedClock | 0:fcca4db7b32a | 113 | InterruptIn iJoyStickUp (p15); // joystick up rising edge. |
gatedClock | 0:fcca4db7b32a | 114 | InterruptIn iJoyStickDown (p12); // joystick down rising edge. |
gatedClock | 0:fcca4db7b32a | 115 | InterruptIn iJoyStickLeft (p13); // joystick left rising edge. |
gatedClock | 0:fcca4db7b32a | 116 | InterruptIn iJoyStickRight (p16); // joystick right rising edge. |
gatedClock | 0:fcca4db7b32a | 117 | InterruptIn iJoyStickCenter(p14); // 1 if joystick middle pressed. |
gatedClock | 66:4a0006fa5cc1 | 118 | |
gatedClock | 66:4a0006fa5cc1 | 119 | Timer debounceTimer; // button debounce timer. |
gatedClock | 0:fcca4db7b32a | 120 | |
gatedClock | 50:2928c3cbdcc3 | 121 | Ticker tickerButtonStateManager; // manage the button states. |
gatedClock | 19:92b11e30aaaf | 122 | Ticker tickerSlowClock; // generate a ~1Hz clock. |
gatedClock | 77:73e4fd83642f | 123 | Ticker tickerCookCountdown; // remaining cook time. |
gatedClock | 63:63f362bcc2ac | 124 | |
gatedClock | 63:63f362bcc2ac | 125 | // Timer timerFSMdone; // duration of FSM 'done' state. |
gatedClock | 40:7afff79f0d8b | 126 | |
gatedClock | 0:fcca4db7b32a | 127 | //-------prototypes-----------------------------//------------------------------ |
gatedClock | 49:56f790977983 | 128 | |
gatedClock | 19:92b11e30aaaf | 129 | void slowClock(); // 1Hz or thereabouts. |
gatedClock | 49:56f790977983 | 130 | |
gatedClock | 9:cfdb9aa5857c | 131 | void initialization(); // initialize settings. |
gatedClock | 13:21f27ba467c2 | 132 | |
gatedClock | 13:21f27ba467c2 | 133 | void ISRleftButtonRising(); // cook-time increase. |
gatedClock | 13:21f27ba467c2 | 134 | void ISRleftButtonFalling(); // button-release debounce. |
gatedClock | 13:21f27ba467c2 | 135 | void ISRrightButtonRising(); // cook-time decrease. |
gatedClock | 13:21f27ba467c2 | 136 | void ISRrightButtonFalling(); // button-release debounce. |
gatedClock | 13:21f27ba467c2 | 137 | void ISRtopButtonRising(); // cook start. |
gatedClock | 13:21f27ba467c2 | 138 | void ISRtopButtonFalling(); // button-release debounce. |
gatedClock | 13:21f27ba467c2 | 139 | void ISRbottomButtonRising(); // cook stop. |
gatedClock | 13:21f27ba467c2 | 140 | void ISRbottomButtonFalling(); // button-release debounce. |
gatedClock | 13:21f27ba467c2 | 141 | void ISRcenterButtonRising(); // door state toggle. |
gatedClock | 13:21f27ba467c2 | 142 | void ISRcenterButtonFalling(); // button-release debounce. |
gatedClock | 16:db7f0b3b2605 | 143 | void disableSignalWaiting(); // break from signal waiting. |
gatedClock | 39:4e7e4d935a87 | 144 | |
gatedClock | 54:b0e7352d2516 | 145 | void threadButtonStateManager(void const *args); |
gatedClock | 50:2928c3cbdcc3 | 146 | void threadTotalTimeControl(void const *args); |
gatedClock | 57:0432c68ad232 | 147 | void threadCookStateFSM(void const *args); |
gatedClock | 71:4a5f256ecf7c | 148 | void tickCookRemainingTime(); // remaining time countdown. |
gatedClock | 50:2928c3cbdcc3 | 149 | |
gatedClock | 40:7afff79f0d8b | 150 | void temperatureThread(void const *args); // temperature measurement. |
gatedClock | 42:266d5bbbfd19 | 151 | void LCDthread (void const *args); // LCD display thread. |
gatedClock | 0:fcca4db7b32a | 152 | //==============================================//============================== |
gatedClock | 0:fcca4db7b32a | 153 | int main(void) |
gatedClock | 0:fcca4db7b32a | 154 | { |
gatedClock | 16:db7f0b3b2605 | 155 | char cLeftButtonState; // 1 means button was pressed. |
gatedClock | 16:db7f0b3b2605 | 156 | |
gatedClock | 16:db7f0b3b2605 | 157 | |
gatedClock | 16:db7f0b3b2605 | 158 | |
gatedClock | 14:d3bb343cd5b2 | 159 | iJoyStickLeft.rise (&ISRleftButtonRising); |
gatedClock | 14:d3bb343cd5b2 | 160 | iJoyStickLeft.fall (&ISRleftButtonFalling); |
gatedClock | 9:cfdb9aa5857c | 161 | |
gatedClock | 14:d3bb343cd5b2 | 162 | iJoyStickRight.rise(&ISRrightButtonRising); |
gatedClock | 14:d3bb343cd5b2 | 163 | iJoyStickRight.fall(&ISRrightButtonFalling); |
gatedClock | 14:d3bb343cd5b2 | 164 | |
gatedClock | 14:d3bb343cd5b2 | 165 | iJoyStickUp.rise (&ISRtopButtonRising); |
gatedClock | 14:d3bb343cd5b2 | 166 | iJoyStickUp.fall (&ISRtopButtonFalling); |
gatedClock | 9:cfdb9aa5857c | 167 | |
gatedClock | 14:d3bb343cd5b2 | 168 | iJoyStickDown.rise (&ISRbottomButtonRising); |
gatedClock | 14:d3bb343cd5b2 | 169 | iJoyStickDown.fall (&ISRbottomButtonFalling); |
gatedClock | 7:9fbd1d540863 | 170 | |
gatedClock | 14:d3bb343cd5b2 | 171 | iJoyStickCenter.rise(&ISRcenterButtonRising); |
gatedClock | 14:d3bb343cd5b2 | 172 | iJoyStickCenter.fall(&ISRcenterButtonFalling); |
gatedClock | 9:cfdb9aa5857c | 173 | |
gatedClock | 66:4a0006fa5cc1 | 174 | debounceTimer.start(); // kick-off debounce timer. |
gatedClock | 66:4a0006fa5cc1 | 175 | |
gatedClock | 33:34c1bef3c4ff | 176 | gcInitializePipeline = 1; // tell pipeline to initialize. |
gatedClock | 16:db7f0b3b2605 | 177 | |
gatedClock | 9:cfdb9aa5857c | 178 | initialization(); // initialize variables. |
gatedClock | 21:b794d189c36b | 179 | gcSlowClock = 0; |
gatedClock | 44:d16e813e61ef | 180 | led1 = 0; |
gatedClock | 53:8c2baf5623c8 | 181 | gtButtons.cDoorOpen = 0; // initialize with door closed. |
gatedClock | 63:63f362bcc2ac | 182 | |
gatedClock | 63:63f362bcc2ac | 183 | // timerFSMdone.start(); // start 'done' timer. |
gatedClock | 49:56f790977983 | 184 | |
gatedClock | 70:7c0743c28b11 | 185 | tickerSlowClock.attach_us(&slowClock ,SLOWCLOCKuS); |
gatedClock | 64:255295f1d782 | 186 | |
gatedClock | 75:c2894d531f42 | 187 | |
gatedClock | 85:dfc5add157e7 | 188 | // tickerCookCountdown.attach(&tickCookRemainingTime,1); |
gatedClock | 54:b0e7352d2516 | 189 | |
gatedClock | 40:7afff79f0d8b | 190 | |
gatedClock | 70:7c0743c28b11 | 191 | Thread thread_1(temperatureThread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); |
gatedClock | 70:7c0743c28b11 | 192 | Thread thread_2(LCDthread ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); |
gatedClock | 70:7c0743c28b11 | 193 | Thread thread_3(threadTotalTimeControl ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); |
gatedClock | 70:7c0743c28b11 | 194 | Thread thread_4(threadButtonStateManager,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); // was osPriorityIdle |
gatedClock | 70:7c0743c28b11 | 195 | Thread thread_5(threadCookStateFSM ,NULL,osPriorityIdle,DEFAULT_STACK_SIZE,NULL); |
gatedClock | 70:7c0743c28b11 | 196 | |
gatedClock | 66:4a0006fa5cc1 | 197 | //Thread thread_4(threadButtonStateManager); |
gatedClock | 52:8ba6a0c91a89 | 198 | |
gatedClock | 40:7afff79f0d8b | 199 | |
gatedClock | 59:5e45b5e4a874 | 200 | // the message-receiving threads need 'else' for message-receive-timeout. |
gatedClock | 9:cfdb9aa5857c | 201 | |
gatedClock | 14:d3bb343cd5b2 | 202 | while(1) |
gatedClock | 0:fcca4db7b32a | 203 | { |
gatedClock | 75:c2894d531f42 | 204 | // pc.printf("\n\r gdDiagTotalTime %d",gdDiagTotalTime); |
gatedClock | 70:7c0743c28b11 | 205 | |
gatedClock | 70:7c0743c28b11 | 206 | Thread::wait(1000); |
gatedClock | 16:db7f0b3b2605 | 207 | |
gatedClock | 16:db7f0b3b2605 | 208 | |
gatedClock | 16:db7f0b3b2605 | 209 | |
gatedClock | 14:d3bb343cd5b2 | 210 | |
gatedClock | 0:fcca4db7b32a | 211 | } |
gatedClock | 19:92b11e30aaaf | 212 | } |
gatedClock | 19:92b11e30aaaf | 213 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 59:5e45b5e4a874 | 214 | |
gatedClock | 59:5e45b5e4a874 | 215 | // this serves as the bottom-half for all of the button-press IRSs. |
gatedClock | 59:5e45b5e4a874 | 216 | // it sends button-status messages to other threads, and it also |
gatedClock | 59:5e45b5e4a874 | 217 | // clears the button-state globals to prepare them for the next |
gatedClock | 59:5e45b5e4a874 | 218 | // button press. |
gatedClock | 59:5e45b5e4a874 | 219 | |
gatedClock | 59:5e45b5e4a874 | 220 | |
gatedClock | 54:b0e7352d2516 | 221 | void threadButtonStateManager(void const *args) |
gatedClock | 50:2928c3cbdcc3 | 222 | { |
gatedClock | 56:18cff6eb91db | 223 | int dMessage; // message. |
gatedClock | 50:2928c3cbdcc3 | 224 | |
gatedClock | 54:b0e7352d2516 | 225 | while(1) // thread loop. |
gatedClock | 67:1d9c85a4c3c1 | 226 | { |
gatedClock | 54:b0e7352d2516 | 227 | //--- // TOTAL TIME CONTROL. |
gatedClock | 56:18cff6eb91db | 228 | |
gatedClock | 56:18cff6eb91db | 229 | // encoded integers will be sent, |
gatedClock | 56:18cff6eb91db | 230 | // not pointers. |
gatedClock | 50:2928c3cbdcc3 | 231 | |
gatedClock | 50:2928c3cbdcc3 | 232 | if (gtButtons.cLeftButton) // total time increment button. |
gatedClock | 50:2928c3cbdcc3 | 233 | { |
gatedClock | 54:b0e7352d2516 | 234 | dMessage = MSG_INC_TIME; // set message. |
gatedClock | 69:55b836e8ced7 | 235 | queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer. |
gatedClock | 50:2928c3cbdcc3 | 236 | gtButtons.cLeftButton = 0; // clear the button state. |
gatedClock | 50:2928c3cbdcc3 | 237 | } |
gatedClock | 67:1d9c85a4c3c1 | 238 | |
gatedClock | 59:5e45b5e4a874 | 239 | if (gtButtons.cRightButton) // total time decrement button. |
gatedClock | 50:2928c3cbdcc3 | 240 | { |
gatedClock | 54:b0e7352d2516 | 241 | dMessage = MSG_DEC_TIME; // set message. |
gatedClock | 69:55b836e8ced7 | 242 | queueModTotalTime.put((int *) dMessage,1);// pretend it's a pointer. |
gatedClock | 50:2928c3cbdcc3 | 243 | gtButtons.cRightButton = 0; // clear the button state. |
gatedClock | 50:2928c3cbdcc3 | 244 | } |
gatedClock | 67:1d9c85a4c3c1 | 245 | |
gatedClock | 59:5e45b5e4a874 | 246 | //--- // COOK-STATE FSM. |
gatedClock | 59:5e45b5e4a874 | 247 | |
gatedClock | 59:5e45b5e4a874 | 248 | if (gtButtons.cTopButton) // start-cook button. |
gatedClock | 58:ec630b6dd9b1 | 249 | { |
gatedClock | 59:5e45b5e4a874 | 250 | dMessage = MSG_START; // set message. |
gatedClock | 69:55b836e8ced7 | 251 | queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer. |
gatedClock | 59:5e45b5e4a874 | 252 | gtButtons.cTopButton = 0; // clear the button state. |
gatedClock | 58:ec630b6dd9b1 | 253 | } |
gatedClock | 67:1d9c85a4c3c1 | 254 | |
gatedClock | 59:5e45b5e4a874 | 255 | if (gtButtons.cBottomButton) // stop-cook button. |
gatedClock | 58:ec630b6dd9b1 | 256 | { |
gatedClock | 59:5e45b5e4a874 | 257 | dMessage = MSG_STOP; // set message. |
gatedClock | 69:55b836e8ced7 | 258 | queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer. |
gatedClock | 59:5e45b5e4a874 | 259 | gtButtons.cBottomButton = 0; // clear the button state. |
gatedClock | 58:ec630b6dd9b1 | 260 | } |
gatedClock | 67:1d9c85a4c3c1 | 261 | |
gatedClock | 59:5e45b5e4a874 | 262 | if (gtButtons.cCenterButton) // door-state-toggle. |
gatedClock | 58:ec630b6dd9b1 | 263 | { |
gatedClock | 59:5e45b5e4a874 | 264 | dMessage = gtButtons.cDoorOpen; // determined in ISR. |
gatedClock | 69:55b836e8ced7 | 265 | queueUpdateFSM.put((int *) dMessage,1); // pretend it's a pointer. |
gatedClock | 59:5e45b5e4a874 | 266 | gtButtons.cCenterButton = 0; // clear the button state. |
gatedClock | 58:ec630b6dd9b1 | 267 | } |
gatedClock | 59:5e45b5e4a874 | 268 | //--- |
gatedClock | 58:ec630b6dd9b1 | 269 | |
gatedClock | 54:b0e7352d2516 | 270 | Thread::wait(THREAD_4_WAITmS); // multitasking. |
gatedClock | 54:b0e7352d2516 | 271 | } // thread loop. |
gatedClock | 54:b0e7352d2516 | 272 | } // threadButtonStateManager. |
gatedClock | 50:2928c3cbdcc3 | 273 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 51:e5ec74c49b01 | 274 | // the incoming messages are mutually-exclusive. |
gatedClock | 51:e5ec74c49b01 | 275 | // total time controller. |
gatedClock | 51:e5ec74c49b01 | 276 | void threadTotalTimeControl(void const *args) |
gatedClock | 50:2928c3cbdcc3 | 277 | { |
gatedClock | 51:e5ec74c49b01 | 278 | static int dTotalTime = 0; // total time variable. |
gatedClock | 60:e10bf95bbc96 | 279 | int dMessage; // message. |
gatedClock | 51:e5ec74c49b01 | 280 | osEvent queueEvent; // queue event. |
gatedClock | 70:7c0743c28b11 | 281 | int dRC; |
gatedClock | 50:2928c3cbdcc3 | 282 | |
gatedClock | 50:2928c3cbdcc3 | 283 | while(1) // thread loop. |
gatedClock | 50:2928c3cbdcc3 | 284 | { |
gatedClock | 50:2928c3cbdcc3 | 285 | |
gatedClock | 55:17f3354da63a | 286 | queueEvent = queueModTotalTime.get(1); // get message. |
gatedClock | 51:e5ec74c49b01 | 287 | if (queueEvent.status == osEventMessage) |
gatedClock | 51:e5ec74c49b01 | 288 | { |
gatedClock | 56:18cff6eb91db | 289 | dMessage = (int) queueEvent.value.p; // interpret as integer, not pointer. |
gatedClock | 54:b0e7352d2516 | 290 | |
gatedClock | 54:b0e7352d2516 | 291 | // increment total time. |
gatedClock | 56:18cff6eb91db | 292 | if (dMessage == MSG_INC_TIME) dTotalTime += 60; |
gatedClock | 54:b0e7352d2516 | 293 | |
gatedClock | 54:b0e7352d2516 | 294 | // decrement total time. |
gatedClock | 56:18cff6eb91db | 295 | if (dMessage == MSG_DEC_TIME) dTotalTime -= 60; |
gatedClock | 65:e39360da5929 | 296 | |
gatedClock | 65:e39360da5929 | 297 | // saturations. |
gatedClock | 65:e39360da5929 | 298 | if (dTotalTime > 180) dTotalTime = 180; |
gatedClock | 65:e39360da5929 | 299 | if (dTotalTime < 0) dTotalTime = 0; |
gatedClock | 65:e39360da5929 | 300 | |
gatedClock | 70:7c0743c28b11 | 301 | dRC = queueSetRemainingTime.put((int *) dTotalTime,1); |
gatedClock | 75:c2894d531f42 | 302 | giRemainingTime.dTotalTime = dTotalTime; |
gatedClock | 51:e5ec74c49b01 | 303 | } |
gatedClock | 81:12bc26973cb8 | 304 | |
gatedClock | 50:2928c3cbdcc3 | 305 | Thread::wait(THREAD_3_WAITmS); // multitasking. |
gatedClock | 50:2928c3cbdcc3 | 306 | } // thread loop. |
gatedClock | 50:2928c3cbdcc3 | 307 | } // threadTotalTimeControl. |
gatedClock | 33:34c1bef3c4ff | 308 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 81:12bc26973cb8 | 309 | |
gatedClock | 57:0432c68ad232 | 310 | void threadCookStateFSM(void const *args) // cook-cycle FSM. |
gatedClock | 57:0432c68ad232 | 311 | { |
gatedClock | 83:2e01f2a799ba | 312 | int dFSMstate = FSM_IDLE; // state of this FSM. |
gatedClock | 83:2e01f2a799ba | 313 | int dFSMstateLast = FSM_IDLE; // previous FSM state. |
gatedClock | 83:2e01f2a799ba | 314 | int dButtonState; // received button state. |
gatedClock | 83:2e01f2a799ba | 315 | int dRemainingTime = 0; // received remaining time. |
gatedClock | 62:48e7c196e2a5 | 316 | |
gatedClock | 83:2e01f2a799ba | 317 | int dButtonStart = 0; |
gatedClock | 83:2e01f2a799ba | 318 | int dButtonStop = 0; |
gatedClock | 83:2e01f2a799ba | 319 | int dDoorOpen = 0; |
gatedClock | 81:12bc26973cb8 | 320 | |
gatedClock | 81:12bc26973cb8 | 321 | osEvent queueEvent; // from button state manager. |
gatedClock | 75:c2894d531f42 | 322 | |
gatedClock | 62:48e7c196e2a5 | 323 | |
gatedClock | 63:63f362bcc2ac | 324 | |
gatedClock | 57:0432c68ad232 | 325 | while(1) // thread loop. |
gatedClock | 57:0432c68ad232 | 326 | { |
gatedClock | 85:dfc5add157e7 | 327 | |
gatedClock | 85:dfc5add157e7 | 328 | tickerCookCountdown.attach(&tickCookRemainingTime,1); |
gatedClock | 81:12bc26973cb8 | 329 | |
gatedClock | 74:4debb8f2e21d | 330 | switch (dFSMstate) // cook-mode state machine. |
gatedClock | 62:48e7c196e2a5 | 331 | { |
gatedClock | 82:73fa3fe8a217 | 332 | //--- |
gatedClock | 73:44739860198b | 333 | case FSM_IDLE : // IDLE. |
gatedClock | 73:44739860198b | 334 | { |
gatedClock | 75:c2894d531f42 | 335 | if (dFSMstate != dFSMstateLast) // if just entered state. |
gatedClock | 80:e3e1a2161435 | 336 | { |
gatedClock | 75:c2894d531f42 | 337 | } |
gatedClock | 80:e3e1a2161435 | 338 | |
gatedClock | 80:e3e1a2161435 | 339 | giLCD.dTotalCookTime = giRemainingTime.dTotalTime; |
gatedClock | 80:e3e1a2161435 | 340 | giLCD.dRemainingTime = 0; // suppress remaining time display. |
gatedClock | 80:e3e1a2161435 | 341 | giLCD.fCelsius = 0; |
gatedClock | 80:e3e1a2161435 | 342 | |
gatedClock | 78:7df160e0db7b | 343 | giRemainingTime.cControl = RT_PRELOAD; |
gatedClock | 76:74c454c9d75b | 344 | dFSMstateLast = dFSMstate; // determine next state. |
gatedClock | 75:c2894d531f42 | 345 | if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_COOK; |
gatedClock | 73:44739860198b | 346 | break; |
gatedClock | 73:44739860198b | 347 | } |
gatedClock | 82:73fa3fe8a217 | 348 | //--- |
gatedClock | 73:44739860198b | 349 | case FSM_COOK : // COOK. |
gatedClock | 73:44739860198b | 350 | { |
gatedClock | 75:c2894d531f42 | 351 | if (dFSMstate != dFSMstateLast) // if just entered state. |
gatedClock | 75:c2894d531f42 | 352 | { |
gatedClock | 75:c2894d531f42 | 353 | giRemainingTime.cControl = RT_DECREMENT; |
gatedClock | 85:dfc5add157e7 | 354 | tickerCookCountdown.detach(); |
gatedClock | 85:dfc5add157e7 | 355 | tickerCookCountdown.attach(&tickCookRemainingTime,1); |
gatedClock | 75:c2894d531f42 | 356 | } |
gatedClock | 80:e3e1a2161435 | 357 | |
gatedClock | 80:e3e1a2161435 | 358 | giLCD.dTotalCookTime = giRemainingTime.dTotalTime; |
gatedClock | 80:e3e1a2161435 | 359 | giLCD.dRemainingTime = giRemainingTime.dRemainingTime; |
gatedClock | 80:e3e1a2161435 | 360 | giLCD.fCelsius = 0; |
gatedClock | 80:e3e1a2161435 | 361 | |
gatedClock | 80:e3e1a2161435 | 362 | |
gatedClock | 76:74c454c9d75b | 363 | |
gatedClock | 76:74c454c9d75b | 364 | dFSMstateLast = dFSMstate; // determine next state. |
gatedClock | 78:7df160e0db7b | 365 | if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE; |
gatedClock | 78:7df160e0db7b | 366 | else |
gatedClock | 76:74c454c9d75b | 367 | if (dButtonStop) dFSMstate = FSM_IDLE; |
gatedClock | 78:7df160e0db7b | 368 | else |
gatedClock | 78:7df160e0db7b | 369 | if (dRemainingTime <= 0) dFSMstate = FSM_DONE; |
gatedClock | 73:44739860198b | 370 | break; |
gatedClock | 73:44739860198b | 371 | } |
gatedClock | 82:73fa3fe8a217 | 372 | //--- |
gatedClock | 73:44739860198b | 373 | case FSM_PAUSE : // PAUSE. |
gatedClock | 73:44739860198b | 374 | { |
gatedClock | 75:c2894d531f42 | 375 | |
gatedClock | 75:c2894d531f42 | 376 | if (dFSMstate != dFSMstateLast) // if just entered state. |
gatedClock | 75:c2894d531f42 | 377 | { |
gatedClock | 75:c2894d531f42 | 378 | } |
gatedClock | 80:e3e1a2161435 | 379 | |
gatedClock | 80:e3e1a2161435 | 380 | |
gatedClock | 80:e3e1a2161435 | 381 | giLCD.dTotalCookTime = giRemainingTime.dTotalTime; |
gatedClock | 80:e3e1a2161435 | 382 | giLCD.dRemainingTime = giRemainingTime.dRemainingTime; |
gatedClock | 80:e3e1a2161435 | 383 | giLCD.fCelsius = 0; |
gatedClock | 80:e3e1a2161435 | 384 | |
gatedClock | 80:e3e1a2161435 | 385 | |
gatedClock | 75:c2894d531f42 | 386 | giRemainingTime.cControl = RT_PAUSE; |
gatedClock | 75:c2894d531f42 | 387 | // determine next state. |
gatedClock | 75:c2894d531f42 | 388 | dFSMstateLast = dFSMstate; |
gatedClock | 78:7df160e0db7b | 389 | if ((dButtonStart == 1) && (dDoorOpen == 0) && (giRemainingTime.dTotalTime > 0)) dFSMstate = FSM_CONTINUE; |
gatedClock | 75:c2894d531f42 | 390 | else |
gatedClock | 78:7df160e0db7b | 391 | if (dButtonStop) dFSMstate = FSM_IDLE; |
gatedClock | 73:44739860198b | 392 | break; |
gatedClock | 73:44739860198b | 393 | } |
gatedClock | 78:7df160e0db7b | 394 | |
gatedClock | 82:73fa3fe8a217 | 395 | //--- |
gatedClock | 79:4286319e48b4 | 396 | case FSM_CONTINUE : // CONTINUE. |
gatedClock | 78:7df160e0db7b | 397 | { |
gatedClock | 78:7df160e0db7b | 398 | |
gatedClock | 78:7df160e0db7b | 399 | if (dFSMstate != dFSMstateLast) // if just entered state. |
gatedClock | 78:7df160e0db7b | 400 | { |
gatedClock | 78:7df160e0db7b | 401 | } |
gatedClock | 80:e3e1a2161435 | 402 | |
gatedClock | 80:e3e1a2161435 | 403 | |
gatedClock | 80:e3e1a2161435 | 404 | giLCD.dTotalCookTime = giRemainingTime.dTotalTime; |
gatedClock | 80:e3e1a2161435 | 405 | giLCD.dRemainingTime = giRemainingTime.dRemainingTime; |
gatedClock | 80:e3e1a2161435 | 406 | giLCD.fCelsius = 0; |
gatedClock | 80:e3e1a2161435 | 407 | |
gatedClock | 80:e3e1a2161435 | 408 | |
gatedClock | 78:7df160e0db7b | 409 | giRemainingTime.cControl = RT_DECREMENT; |
gatedClock | 78:7df160e0db7b | 410 | // determine next state. |
gatedClock | 78:7df160e0db7b | 411 | dFSMstateLast = dFSMstate; |
gatedClock | 78:7df160e0db7b | 412 | if (dRemainingTime <= 0) dFSMstate = FSM_DONE; |
gatedClock | 78:7df160e0db7b | 413 | else |
gatedClock | 78:7df160e0db7b | 414 | if (dButtonStop) dFSMstate = FSM_IDLE; |
gatedClock | 79:4286319e48b4 | 415 | else |
gatedClock | 79:4286319e48b4 | 416 | if ((dDoorOpen == 1) && (dRemainingTime > 0)) dFSMstate = FSM_PAUSE; |
gatedClock | 78:7df160e0db7b | 417 | break; |
gatedClock | 78:7df160e0db7b | 418 | } |
gatedClock | 78:7df160e0db7b | 419 | |
gatedClock | 78:7df160e0db7b | 420 | |
gatedClock | 82:73fa3fe8a217 | 421 | //--- |
gatedClock | 73:44739860198b | 422 | case FSM_DONE : // DONE. |
gatedClock | 73:44739860198b | 423 | { |
gatedClock | 75:c2894d531f42 | 424 | |
gatedClock | 75:c2894d531f42 | 425 | if (dFSMstate != dFSMstateLast) // if just entered state. |
gatedClock | 75:c2894d531f42 | 426 | { |
gatedClock | 75:c2894d531f42 | 427 | } |
gatedClock | 80:e3e1a2161435 | 428 | |
gatedClock | 80:e3e1a2161435 | 429 | |
gatedClock | 80:e3e1a2161435 | 430 | giLCD.dTotalCookTime = giRemainingTime.dTotalTime; |
gatedClock | 80:e3e1a2161435 | 431 | giLCD.dRemainingTime = giRemainingTime.dRemainingTime; |
gatedClock | 80:e3e1a2161435 | 432 | giLCD.fCelsius = 0; |
gatedClock | 80:e3e1a2161435 | 433 | |
gatedClock | 80:e3e1a2161435 | 434 | |
gatedClock | 80:e3e1a2161435 | 435 | |
gatedClock | 75:c2894d531f42 | 436 | giRemainingTime.cControl = RT_CLEAR; |
gatedClock | 75:c2894d531f42 | 437 | // determine next state. |
gatedClock | 75:c2894d531f42 | 438 | dFSMstateLast = dFSMstate; |
gatedClock | 74:4debb8f2e21d | 439 | dFSMstate = FSM_IDLE; |
gatedClock | 73:44739860198b | 440 | break; |
gatedClock | 73:44739860198b | 441 | } |
gatedClock | 82:73fa3fe8a217 | 442 | //--- |
gatedClock | 73:44739860198b | 443 | default : {dFSMstate = FSM_IDLE; break;} |
gatedClock | 73:44739860198b | 444 | |
gatedClock | 62:48e7c196e2a5 | 445 | } |
gatedClock | 73:44739860198b | 446 | |
gatedClock | 63:63f362bcc2ac | 447 | |
gatedClock | 75:c2894d531f42 | 448 | |
gatedClock | 75:c2894d531f42 | 449 | queueEvent = queueUpdateFSM.get(1); // threadButtonStateManager |
gatedClock | 60:e10bf95bbc96 | 450 | if (queueEvent.status == osEventMessage)// update state variable. |
gatedClock | 60:e10bf95bbc96 | 451 | { |
gatedClock | 61:8026a9fc0cf1 | 452 | // interpret as integer, not pointer. |
gatedClock | 61:8026a9fc0cf1 | 453 | dButtonState = (int) queueEvent.value.p; |
gatedClock | 63:63f362bcc2ac | 454 | |
gatedClock | 75:c2894d531f42 | 455 | |
gatedClock | 69:55b836e8ced7 | 456 | |
gatedClock | 63:63f362bcc2ac | 457 | if (dButtonState == MSG_START) |
gatedClock | 63:63f362bcc2ac | 458 | { |
gatedClock | 63:63f362bcc2ac | 459 | dButtonStart = 1; |
gatedClock | 63:63f362bcc2ac | 460 | dButtonStop = 0; |
gatedClock | 79:4286319e48b4 | 461 | |
gatedClock | 79:4286319e48b4 | 462 | // if the door is open, ignore |
gatedClock | 79:4286319e48b4 | 463 | // and cancel 'start' state. |
gatedClock | 79:4286319e48b4 | 464 | if (dDoorOpen) dButtonStart = 0; |
gatedClock | 63:63f362bcc2ac | 465 | } |
gatedClock | 63:63f362bcc2ac | 466 | if (dButtonState == MSG_STOP) |
gatedClock | 63:63f362bcc2ac | 467 | { |
gatedClock | 63:63f362bcc2ac | 468 | dButtonStart = 0; |
gatedClock | 63:63f362bcc2ac | 469 | dButtonStop = 1; |
gatedClock | 63:63f362bcc2ac | 470 | } |
gatedClock | 63:63f362bcc2ac | 471 | |
gatedClock | 63:63f362bcc2ac | 472 | if (dButtonState == MSG_OPEN) |
gatedClock | 63:63f362bcc2ac | 473 | { |
gatedClock | 79:4286319e48b4 | 474 | // if the door opens, clear current 'start' state. |
gatedClock | 79:4286319e48b4 | 475 | dDoorOpen = 1; |
gatedClock | 79:4286319e48b4 | 476 | dButtonStart = 0; |
gatedClock | 63:63f362bcc2ac | 477 | } |
gatedClock | 63:63f362bcc2ac | 478 | |
gatedClock | 63:63f362bcc2ac | 479 | if (dButtonState == MSG_CLOSED) |
gatedClock | 63:63f362bcc2ac | 480 | { |
gatedClock | 63:63f362bcc2ac | 481 | dDoorOpen = 0; |
gatedClock | 63:63f362bcc2ac | 482 | } |
gatedClock | 63:63f362bcc2ac | 483 | |
gatedClock | 61:8026a9fc0cf1 | 484 | } |
gatedClock | 75:c2894d531f42 | 485 | |
gatedClock | 75:c2894d531f42 | 486 | // fetch from global scope. |
gatedClock | 75:c2894d531f42 | 487 | dRemainingTime = giRemainingTime.dRemainingTime; |
gatedClock | 69:55b836e8ced7 | 488 | |
gatedClock | 75:c2894d531f42 | 489 | // pipeline variable. |
gatedClock | 57:0432c68ad232 | 490 | Thread::wait(THREAD_5_WAITmS); // multitasking. |
gatedClock | 57:0432c68ad232 | 491 | } // thread loop. |
gatedClock | 57:0432c68ad232 | 492 | } // threadCookStateFSM. |
gatedClock | 57:0432c68ad232 | 493 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 19:92b11e30aaaf | 494 | void slowClock(void) // 1Hz or thereabouts. |
gatedClock | 19:92b11e30aaaf | 495 | { |
gatedClock | 24:d39516e077ea | 496 | gcSlowClock = !gcSlowClock; // toggle clock. |
gatedClock | 19:92b11e30aaaf | 497 | } |
gatedClock | 19:92b11e30aaaf | 498 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 0:fcca4db7b32a | 499 | void initialization(void) // program initializations. |
gatedClock | 0:fcca4db7b32a | 500 | { |
gatedClock | 16:db7f0b3b2605 | 501 | gcSignalWaitEnable = 1; |
gatedClock | 0:fcca4db7b32a | 502 | } |
gatedClock | 0:fcca4db7b32a | 503 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 26:bff592483cb1 | 504 | void ISRleftButtonRising(void) // cooktime plus 60s. |
gatedClock | 1:9188d4668a88 | 505 | { |
gatedClock | 66:4a0006fa5cc1 | 506 | if (debounceTimer.read_ms() > DEBOUNCEmS) |
gatedClock | 26:bff592483cb1 | 507 | gtButtons.cLeftButton = 1; // detect left button. |
gatedClock | 9:cfdb9aa5857c | 508 | |
gatedClock | 66:4a0006fa5cc1 | 509 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 11:9cae003da12b | 510 | } |
gatedClock | 1:9188d4668a88 | 511 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 15:5eaa2ab1d00d | 512 | void ISRleftButtonFalling(void) // button-release debounce. |
gatedClock | 1:9188d4668a88 | 513 | { |
gatedClock | 66:4a0006fa5cc1 | 514 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 11:9cae003da12b | 515 | } |
gatedClock | 2:665ffa57031f | 516 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 26:bff592483cb1 | 517 | void ISRrightButtonRising(void) // cooktime -60s. |
gatedClock | 12:e40272e1fd8f | 518 | { |
gatedClock | 66:4a0006fa5cc1 | 519 | if (debounceTimer.read_ms() > DEBOUNCEmS) |
gatedClock | 26:bff592483cb1 | 520 | gtButtons.cRightButton = 1; // detect right button. |
gatedClock | 12:e40272e1fd8f | 521 | |
gatedClock | 66:4a0006fa5cc1 | 522 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 523 | } |
gatedClock | 12:e40272e1fd8f | 524 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 15:5eaa2ab1d00d | 525 | void ISRrightButtonFalling(void) // button-release debounce. |
gatedClock | 12:e40272e1fd8f | 526 | { |
gatedClock | 66:4a0006fa5cc1 | 527 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 528 | } |
gatedClock | 12:e40272e1fd8f | 529 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 26:bff592483cb1 | 530 | void ISRtopButtonRising(void) // cook start. |
gatedClock | 12:e40272e1fd8f | 531 | { |
gatedClock | 66:4a0006fa5cc1 | 532 | if (debounceTimer.read_ms() > DEBOUNCEmS) |
gatedClock | 26:bff592483cb1 | 533 | gtButtons.cTopButton = 1; // detect top button. |
gatedClock | 12:e40272e1fd8f | 534 | |
gatedClock | 66:4a0006fa5cc1 | 535 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 536 | } |
gatedClock | 12:e40272e1fd8f | 537 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 15:5eaa2ab1d00d | 538 | void ISRtopButtonFalling(void) // button-release debounce. |
gatedClock | 12:e40272e1fd8f | 539 | { |
gatedClock | 66:4a0006fa5cc1 | 540 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 541 | } |
gatedClock | 12:e40272e1fd8f | 542 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 53:8c2baf5623c8 | 543 | // front-end control of magnetron off. |
gatedClock | 53:8c2baf5623c8 | 544 | // due to physical danger, the magnetron is turned off immediately |
gatedClock | 53:8c2baf5623c8 | 545 | // upon off-button press, in the interrupt that it generates. |
gatedClock | 53:8c2baf5623c8 | 546 | |
gatedClock | 26:bff592483cb1 | 547 | void ISRbottomButtonRising(void) // cook stop. |
gatedClock | 12:e40272e1fd8f | 548 | { |
gatedClock | 66:4a0006fa5cc1 | 549 | if (debounceTimer.read_ms() > DEBOUNCEmS) |
gatedClock | 66:4a0006fa5cc1 | 550 | { |
gatedClock | 66:4a0006fa5cc1 | 551 | led0 = 0; // magnetron off. |
gatedClock | 66:4a0006fa5cc1 | 552 | gtButtons.cBottomButton = 1; // detect bottom button. |
gatedClock | 66:4a0006fa5cc1 | 553 | } |
gatedClock | 66:4a0006fa5cc1 | 554 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 66:4a0006fa5cc1 | 555 | } |
gatedClock | 12:e40272e1fd8f | 556 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 15:5eaa2ab1d00d | 557 | void ISRbottomButtonFalling(void) // button-release debounce. |
gatedClock | 12:e40272e1fd8f | 558 | { |
gatedClock | 66:4a0006fa5cc1 | 559 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 560 | } |
gatedClock | 12:e40272e1fd8f | 561 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 53:8c2baf5623c8 | 562 | // front-end control of magnetron off. |
gatedClock | 53:8c2baf5623c8 | 563 | // due to physical danger, the magnetron is turned off immediately |
gatedClock | 53:8c2baf5623c8 | 564 | // upon detection of an open door. |
gatedClock | 53:8c2baf5623c8 | 565 | |
gatedClock | 26:bff592483cb1 | 566 | void ISRcenterButtonRising(void) // toggle door state. |
gatedClock | 12:e40272e1fd8f | 567 | { |
gatedClock | 66:4a0006fa5cc1 | 568 | if (debounceTimer.read_ms() > DEBOUNCEmS) |
gatedClock | 66:4a0006fa5cc1 | 569 | { |
gatedClock | 66:4a0006fa5cc1 | 570 | if (gtButtons.cDoorOpen == MSG_OPEN) // calculate door state. |
gatedClock | 66:4a0006fa5cc1 | 571 | gtButtons.cDoorOpen = MSG_CLOSED; |
gatedClock | 66:4a0006fa5cc1 | 572 | else |
gatedClock | 66:4a0006fa5cc1 | 573 | gtButtons.cDoorOpen = MSG_OPEN; |
gatedClock | 53:8c2baf5623c8 | 574 | |
gatedClock | 59:5e45b5e4a874 | 575 | // magnetron off. |
gatedClock | 66:4a0006fa5cc1 | 576 | if (gtButtons.cDoorOpen == MSG_OPEN) led0 = 0; |
gatedClock | 58:ec630b6dd9b1 | 577 | |
gatedClock | 66:4a0006fa5cc1 | 578 | gtButtons.cCenterButton = 1; |
gatedClock | 66:4a0006fa5cc1 | 579 | } |
gatedClock | 66:4a0006fa5cc1 | 580 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 581 | } |
gatedClock | 12:e40272e1fd8f | 582 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 15:5eaa2ab1d00d | 583 | void ISRcenterButtonFalling(void) // button-release debounce. |
gatedClock | 12:e40272e1fd8f | 584 | { |
gatedClock | 66:4a0006fa5cc1 | 585 | debounceTimer.reset(); // begin debounce period. |
gatedClock | 12:e40272e1fd8f | 586 | } |
gatedClock | 12:e40272e1fd8f | 587 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 40:7afff79f0d8b | 588 | void temperatureThread(void const *args) // temperature measurement. |
gatedClock | 39:4e7e4d935a87 | 589 | { |
gatedClock | 45:e3207684e841 | 590 | while(1) // thread loop. |
gatedClock | 45:e3207684e841 | 591 | { |
gatedClock | 45:e3207684e841 | 592 | gfCelsius = temperature.read(); // physical measurement. |
gatedClock | 40:7afff79f0d8b | 593 | |
gatedClock | 45:e3207684e841 | 594 | Thread::wait(THREAD_1_WAITmS); // multitasking. |
gatedClock | 45:e3207684e841 | 595 | } // thread loop. |
gatedClock | 45:e3207684e841 | 596 | } // temperatureThread. |
gatedClock | 42:266d5bbbfd19 | 597 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 42:266d5bbbfd19 | 598 | void LCDthread(void const *args) // LCD display thread. |
gatedClock | 42:266d5bbbfd19 | 599 | { |
gatedClock | 67:1d9c85a4c3c1 | 600 | |
gatedClock | 67:1d9c85a4c3c1 | 601 | static int dLCDtotalCookTimeSec = 0; // sample current values. |
gatedClock | 67:1d9c85a4c3c1 | 602 | static int dCookTimeRemainingSec = 0; |
gatedClock | 67:1d9c85a4c3c1 | 603 | static float fLCDcelsius = 0.0; |
gatedClock | 67:1d9c85a4c3c1 | 604 | |
gatedClock | 67:1d9c85a4c3c1 | 605 | // remember previous values. |
gatedClock | 67:1d9c85a4c3c1 | 606 | static int dLCDtotalCookTimeSecLast = 0; |
gatedClock | 67:1d9c85a4c3c1 | 607 | static int dCookTimeRemainingSecLast = 0; |
gatedClock | 67:1d9c85a4c3c1 | 608 | static float fLCDcelsiusLast = 0.0; |
gatedClock | 67:1d9c85a4c3c1 | 609 | |
gatedClock | 44:d16e813e61ef | 610 | while(1) // thread loop. |
gatedClock | 44:d16e813e61ef | 611 | { |
gatedClock | 67:1d9c85a4c3c1 | 612 | // don't allow the values to |
gatedClock | 67:1d9c85a4c3c1 | 613 | // change in the middle of the |
gatedClock | 67:1d9c85a4c3c1 | 614 | // below else the anti-blink |
gatedClock | 67:1d9c85a4c3c1 | 615 | // code won't work. |
gatedClock | 80:e3e1a2161435 | 616 | dLCDtotalCookTimeSec = giLCD.dTotalCookTime; |
gatedClock | 80:e3e1a2161435 | 617 | dCookTimeRemainingSec = giLCD.dRemainingTime; |
gatedClock | 80:e3e1a2161435 | 618 | fLCDcelsius = giLCD.fCelsius; |
gatedClock | 80:e3e1a2161435 | 619 | |
gatedClock | 80:e3e1a2161435 | 620 | |
gatedClock | 67:1d9c85a4c3c1 | 621 | |
gatedClock | 67:1d9c85a4c3c1 | 622 | |
gatedClock | 67:1d9c85a4c3c1 | 623 | // clear display only when |
gatedClock | 67:1d9c85a4c3c1 | 624 | // necessary, in order to avoid |
gatedClock | 67:1d9c85a4c3c1 | 625 | // 'blinkieness'. |
gatedClock | 67:1d9c85a4c3c1 | 626 | if (dLCDtotalCookTimeSec != dLCDtotalCookTimeSecLast || |
gatedClock | 67:1d9c85a4c3c1 | 627 | dCookTimeRemainingSec != dCookTimeRemainingSecLast || |
gatedClock | 67:1d9c85a4c3c1 | 628 | fLCDcelsius != fLCDcelsiusLast) |
gatedClock | 67:1d9c85a4c3c1 | 629 | lcd.cls(); |
gatedClock | 42:266d5bbbfd19 | 630 | |
gatedClock | 44:d16e813e61ef | 631 | LCD1; // line 1. |
gatedClock | 67:1d9c85a4c3c1 | 632 | lcd.printf(" total cook time: %d",dLCDtotalCookTimeSec); |
gatedClock | 42:266d5bbbfd19 | 633 | |
gatedClock | 44:d16e813e61ef | 634 | LCD2; // line 2. |
gatedClock | 67:1d9c85a4c3c1 | 635 | lcd.printf(" remaing cook time: %d",dCookTimeRemainingSec); |
gatedClock | 42:266d5bbbfd19 | 636 | |
gatedClock | 44:d16e813e61ef | 637 | LCD3; // line 3. |
gatedClock | 67:1d9c85a4c3c1 | 638 | lcd.printf(" temperature : %5.3f",fLCDcelsius); |
gatedClock | 67:1d9c85a4c3c1 | 639 | |
gatedClock | 67:1d9c85a4c3c1 | 640 | // pipeline variables. |
gatedClock | 67:1d9c85a4c3c1 | 641 | dLCDtotalCookTimeSecLast = dLCDtotalCookTimeSec; |
gatedClock | 67:1d9c85a4c3c1 | 642 | dCookTimeRemainingSecLast = dCookTimeRemainingSec; |
gatedClock | 67:1d9c85a4c3c1 | 643 | fLCDcelsiusLast = fLCDcelsius; |
gatedClock | 42:266d5bbbfd19 | 644 | |
gatedClock | 44:d16e813e61ef | 645 | Thread::wait(THREAD_2_WAITmS); // multitasking. |
gatedClock | 44:d16e813e61ef | 646 | } // thread loop. |
gatedClock | 44:d16e813e61ef | 647 | } // LCDthread. |
gatedClock | 42:266d5bbbfd19 | 648 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 71:4a5f256ecf7c | 649 | |
gatedClock | 71:4a5f256ecf7c | 650 | /* |
gatedClock | 71:4a5f256ecf7c | 651 | struct tRemainingTime // remaining time related. |
gatedClock | 64:255295f1d782 | 652 | { |
gatedClock | 71:4a5f256ecf7c | 653 | char cControl; // countdown control. |
gatedClock | 71:4a5f256ecf7c | 654 | int dTotalTime; // initialize to this. |
gatedClock | 71:4a5f256ecf7c | 655 | int dRemainingTime; // the countdown value. |
gatedClock | 71:4a5f256ecf7c | 656 | } |
gatedClock | 71:4a5f256ecf7c | 657 | |
gatedClock | 71:4a5f256ecf7c | 658 | giRemainingTime. |
gatedClock | 66:4a0006fa5cc1 | 659 | |
gatedClock | 71:4a5f256ecf7c | 660 | #define RT_PRELOAD 0x01 // remaining-time preload. |
gatedClock | 71:4a5f256ecf7c | 661 | #define RT_DECREMENT 0x02 // remaining-time decrement. |
gatedClock | 71:4a5f256ecf7c | 662 | #define RT_PAUSE 0x03 // remaining-time don't change. |
gatedClock | 71:4a5f256ecf7c | 663 | #define RT_CLEAR 0x04 // remaining-time set to zero. |
gatedClock | 71:4a5f256ecf7c | 664 | |
gatedClock | 71:4a5f256ecf7c | 665 | */ |
gatedClock | 71:4a5f256ecf7c | 666 | |
gatedClock | 71:4a5f256ecf7c | 667 | |
gatedClock | 71:4a5f256ecf7c | 668 | |
gatedClock | 71:4a5f256ecf7c | 669 | |
gatedClock | 71:4a5f256ecf7c | 670 | |
gatedClock | 71:4a5f256ecf7c | 671 | |
gatedClock | 71:4a5f256ecf7c | 672 | void tickCookRemainingTime(void) // cook-cycle countdown. |
gatedClock | 71:4a5f256ecf7c | 673 | { |
gatedClock | 71:4a5f256ecf7c | 674 | static int dRemainingTime = 0; // remaining time in seconds. |
gatedClock | 70:7c0743c28b11 | 675 | |
gatedClock | 76:74c454c9d75b | 676 | |
gatedClock | 75:c2894d531f42 | 677 | |
gatedClock | 75:c2894d531f42 | 678 | switch (giRemainingTime.cControl) // control processing. |
gatedClock | 71:4a5f256ecf7c | 679 | { |
gatedClock | 72:b4d0c0aa3c26 | 680 | case RT_PRELOAD : // preload with total time. |
gatedClock | 71:4a5f256ecf7c | 681 | { |
gatedClock | 72:b4d0c0aa3c26 | 682 | dRemainingTime = giRemainingTime.dTotalTime; |
gatedClock | 72:b4d0c0aa3c26 | 683 | if (dRemainingTime > 180) dRemainingTime = 180; |
gatedClock | 72:b4d0c0aa3c26 | 684 | if (dRemainingTime < 0) dRemainingTime = 0; |
gatedClock | 71:4a5f256ecf7c | 685 | break; |
gatedClock | 71:4a5f256ecf7c | 686 | } |
gatedClock | 72:b4d0c0aa3c26 | 687 | case RT_DECREMENT : // count-down. |
gatedClock | 71:4a5f256ecf7c | 688 | { |
gatedClock | 72:b4d0c0aa3c26 | 689 | dRemainingTime--; |
gatedClock | 72:b4d0c0aa3c26 | 690 | if (dRemainingTime > 180) dRemainingTime = 180; |
gatedClock | 72:b4d0c0aa3c26 | 691 | if (dRemainingTime < 0) dRemainingTime = 0; |
gatedClock | 71:4a5f256ecf7c | 692 | break; |
gatedClock | 71:4a5f256ecf7c | 693 | } |
gatedClock | 71:4a5f256ecf7c | 694 | |
gatedClock | 72:b4d0c0aa3c26 | 695 | case RT_PAUSE : // suspend countdown. |
gatedClock | 69:55b836e8ced7 | 696 | { |
gatedClock | 72:b4d0c0aa3c26 | 697 | dRemainingTime = dRemainingTime; |
gatedClock | 72:b4d0c0aa3c26 | 698 | if (dRemainingTime > 180) dRemainingTime = 180; |
gatedClock | 72:b4d0c0aa3c26 | 699 | if (dRemainingTime < 0) dRemainingTime = 0; |
gatedClock | 71:4a5f256ecf7c | 700 | break; |
gatedClock | 71:4a5f256ecf7c | 701 | } |
gatedClock | 71:4a5f256ecf7c | 702 | |
gatedClock | 72:b4d0c0aa3c26 | 703 | case RT_CLEAR : // clear countdown. |
gatedClock | 71:4a5f256ecf7c | 704 | { |
gatedClock | 76:74c454c9d75b | 705 | dRemainingTime = 0; |
gatedClock | 76:74c454c9d75b | 706 | led3 = 1; |
gatedClock | 71:4a5f256ecf7c | 707 | break; |
gatedClock | 71:4a5f256ecf7c | 708 | } |
gatedClock | 69:55b836e8ced7 | 709 | |
gatedClock | 72:b4d0c0aa3c26 | 710 | default : // saturate, just in case. |
gatedClock | 72:b4d0c0aa3c26 | 711 | { |
gatedClock | 72:b4d0c0aa3c26 | 712 | if (dRemainingTime > 180) dRemainingTime = 180; |
gatedClock | 72:b4d0c0aa3c26 | 713 | if (dRemainingTime < 0) dRemainingTime = 0; |
gatedClock | 72:b4d0c0aa3c26 | 714 | } |
gatedClock | 75:c2894d531f42 | 715 | } // control processing. |
gatedClock | 71:4a5f256ecf7c | 716 | |
gatedClock | 75:c2894d531f42 | 717 | // promote to global scope. |
gatedClock | 75:c2894d531f42 | 718 | giRemainingTime.dRemainingTime = dRemainingTime; |
gatedClock | 70:7c0743c28b11 | 719 | |
gatedClock | 70:7c0743c28b11 | 720 | } // cookRemainingTime. |
gatedClock | 69:55b836e8ced7 | 721 | /*----------------------------------------------//----------------------------*/ |
gatedClock | 69:55b836e8ced7 | 722 | |
gatedClock | 69:55b836e8ced7 | 723 | |
gatedClock | 69:55b836e8ced7 | 724 | |
gatedClock | 69:55b836e8ced7 | 725 | |
gatedClock | 69:55b836e8ced7 | 726 | |
gatedClock | 69:55b836e8ced7 | 727 | |
gatedClock | 69:55b836e8ced7 | 728 | |
gatedClock | 69:55b836e8ced7 | 729 | |
gatedClock | 69:55b836e8ced7 | 730 | |
gatedClock | 69:55b836e8ced7 | 731 | |
gatedClock | 69:55b836e8ced7 | 732 |