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