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