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