![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Final tidy of code following installation of new sensor, more comments added prior to submission
Dependencies: mbed
main.cpp@17:793f819e1df6, 2022-01-31 (annotated)
- Committer:
- legstar85
- Date:
- Mon Jan 31 20:41:38 2022 +0000
- Revision:
- 17:793f819e1df6
- Parent:
- 16:c63a5d084db0
- Child:
- 18:fc63b51a0302
Tidy of code, comments, functions updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
legstar85 | 4:0090145b3286 | 1 | /* |
legstar85 | 13:5ad65a688f3f | 2 | * Prints a string of characters to the screen buffer, string is cut off after the 83rd pixel. |
legstar85 | 13:5ad65a688f3f | 3 | * @param x - the column number (0 to 83) |
legstar85 | 13:5ad65a688f3f | 4 | * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row |
legstar85 | 13:5ad65a688f3f | 5 | * |
legstar85 | 2:e0091b5311f1 | 6 | * @ File main.cpp |
legstar85 | 4:0090145b3286 | 7 | * @ Author - David Leaming - 25574043 |
legstar85 | 9:d71b92c916f8 | 8 | * @ Date - January 2022 |
legstar85 | 13:5ad65a688f3f | 9 | * |
legstar85 | 2:e0091b5311f1 | 10 | * Acknowledgements |
legstar85 | 14:3e9991fe64e5 | 11 | * Craig A. Evans, University of Leeds, TMP102 Library, Feb 2016 |
legstar85 | 14:3e9991fe64e5 | 12 | * Dr Edmond Nurellari, University of Lincoln, Joystick, N5110 Libraries & SD Card Libraries |
legstar85 | 13:5ad65a688f3f | 13 | * Paul Staron, Piezo Buzzer utility, April 2019 |
eencae | 0:21a200b880d7 | 14 | */ |
eencae | 0:21a200b880d7 | 15 | |
legstar85 | 16:c63a5d084db0 | 16 | #include "mbed.h" // Include the library header, ensure the library has been imported into the project |
legstar85 | 16:c63a5d084db0 | 17 | #include "TMP102.h" // |
legstar85 | 16:c63a5d084db0 | 18 | #include "N5110.h" // |
legstar85 | 16:c63a5d084db0 | 19 | #include "Joystick.h" // |
legstar85 | 16:c63a5d084db0 | 20 | #include "Bitmap.h" // |
legstar85 | 16:c63a5d084db0 | 21 | #include "SDFileSystem.h" // |
legstar85 | 16:c63a5d084db0 | 22 | // |
legstar85 | 2:e0091b5311f1 | 23 | TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object |
legstar85 | 16:c63a5d084db0 | 24 | // |
legstar85 | 16:c63a5d084db0 | 25 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED // |
legstar85 | 2:e0091b5311f1 | 26 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting |
legstar85 | 16:c63a5d084db0 | 27 | // |
legstar85 | 16:c63a5d084db0 | 28 | // y x button // |
legstar85 | 3:80c1eba78f9b | 29 | Joystick joystick(PTB10,PTB11,PTC16); // Define Joystick Object |
legstar85 | 16:c63a5d084db0 | 30 | // |
legstar85 | 14:3e9991fe64e5 | 31 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS - Connections to SD card holder on K64F (SPI interface) |
legstar85 | 16:c63a5d084db0 | 32 | // |
legstar85 | 2:e0091b5311f1 | 33 | Serial pc(USBTX,USBRX); // UART connection for PC |
legstar85 | 16:c63a5d084db0 | 34 | // |
legstar85 | 11:a6eec09b546e | 35 | struct State { // Struct for state |
legstar85 | 11:a6eec09b546e | 36 | int output; // output value |
legstar85 | 11:a6eec09b546e | 37 | float time; // time in state |
legstar85 | 11:a6eec09b546e | 38 | int nextState[9]; // array of next states |
legstar85 | 16:c63a5d084db0 | 39 | }; // |
legstar85 | 16:c63a5d084db0 | 40 | // |
legstar85 | 16:c63a5d084db0 | 41 | State fsm[11] = { // |
legstar85 | 12:5046b7515d5c | 42 | {15,0.5,{0,1,0,0,0,10,0,0,0}}, // State 0 - 15 Degrees |
legstar85 | 12:5046b7515d5c | 43 | {16,0.5,{1,2,1,1,1,0,1,1,1}}, // State 1 - 16 Degrees |
legstar85 | 12:5046b7515d5c | 44 | {17,0.5,{2,3,2,2,2,1,2,2,2}}, // State 2 - 17 Degrees |
legstar85 | 12:5046b7515d5c | 45 | {18,0.5,{3,4,3,3,3,2,3,3,3}}, // State 3 - 18 Degrees |
legstar85 | 12:5046b7515d5c | 46 | {19,0.5,{4,5,4,4,4,3,4,4,4}}, // State 4 - 19 Degrees |
legstar85 | 12:5046b7515d5c | 47 | {20,0.5,{5,6,5,5,5,4,5,5,5}}, // State 5 - 20 Degrees |
legstar85 | 12:5046b7515d5c | 48 | {21,0.5,{6,7,6,6,6,5,6,6,6}}, // State 6 - 21 Degrees |
legstar85 | 12:5046b7515d5c | 49 | {22,0.5,{7,8,7,7,7,6,7,7,7}}, // State 7 - 22 Degrees |
legstar85 | 12:5046b7515d5c | 50 | {23,0.5,{8,9,8,8,8,7,8,8,8}}, // State 8 - 23 Degrees |
legstar85 | 12:5046b7515d5c | 51 | {24,0.5,{9,10,9,9,9,8,9,9,9}}, // State 9 - 24 Degrees |
legstar85 | 13:5ad65a688f3f | 52 | {25,0.5,{10,1,10,10,10,9,10,10,10}} // State 10 - 25 Degrees |
legstar85 | 16:c63a5d084db0 | 53 | }; // |
legstar85 | 16:c63a5d084db0 | 54 | // |
legstar85 | 5:eebd36a47049 | 55 | Ticker ticker_menu; // Create Menu ticker object |
legstar85 | 16:c63a5d084db0 | 56 | // |
legstar85 | 2:e0091b5311f1 | 57 | DigitalOut r_led(LED_RED); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 58 | DigitalOut g_led(LED_GREEN); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 59 | DigitalOut b_led(LED_BLUE); // K64F on-board LEDs |
legstar85 | 16:c63a5d084db0 | 60 | // |
legstar85 | 8:07323fcab6d1 | 61 | PwmOut LED01 (PTA1); // PCB Surface Mounted LED's - LED1 |
legstar85 | 8:07323fcab6d1 | 62 | PwmOut LED02 (PTA2); // PCB Surface Mounted LED's - LED2 |
legstar85 | 8:07323fcab6d1 | 63 | PwmOut LED03 (PTC2); // PCB Surface Mounted LED's - LED3 |
legstar85 | 8:07323fcab6d1 | 64 | PwmOut LED04 (PTC3); // PCB Surface Mounted LED's - LED4 |
legstar85 | 8:07323fcab6d1 | 65 | PwmOut LED05 (PTC4); // PCB Surface Mounted LED's - LED5 |
legstar85 | 8:07323fcab6d1 | 66 | PwmOut LED06 (PTD3); // PCB Surface Mounted LED's - LED6 |
legstar85 | 16:c63a5d084db0 | 67 | // |
legstar85 | 8:07323fcab6d1 | 68 | PwmOut Buzzer (PTC10); // PCB Surface Mounted Piezo Buzzer |
legstar85 | 16:c63a5d084db0 | 69 | // |
legstar85 | 2:e0091b5311f1 | 70 | InterruptIn sw2(SW2); // K64F on-board switches |
legstar85 | 2:e0091b5311f1 | 71 | InterruptIn sw3(SW3); // K64F on-board switches |
legstar85 | 3:80c1eba78f9b | 72 | InterruptIn ButtonA (PTB9); // PCB Button - A |
legstar85 | 3:80c1eba78f9b | 73 | InterruptIn ButtonB (PTD0); // PCB Button - B |
legstar85 | 7:d77e95505e43 | 74 | InterruptIn ButtonBack (PTB10); // PCB Button - Back |
legstar85 | 16:c63a5d084db0 | 75 | // |
legstar85 | 12:5046b7515d5c | 76 | volatile int g_ButtonA_flag = 0; // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global |
legstar85 | 12:5046b7515d5c | 77 | volatile int g_ButtonB_flag = 0; // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global |
legstar85 | 12:5046b7515d5c | 78 | volatile int g_ButtonBack_flag = 0; // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global |
legstar85 | 12:5046b7515d5c | 79 | volatile int g_sw2_flag = 0; // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global |
legstar85 | 12:5046b7515d5c | 80 | volatile int g_menu_timer_flag = 0; // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global |
legstar85 | 5:eebd36a47049 | 81 | volatile int option = 0; // Menu option selection based on joystick direction |
legstar85 | 11:a6eec09b546e | 82 | volatile int g_state = 0; // |
legstar85 | 11:a6eec09b546e | 83 | volatile int g_StartTemp = 0; // |
legstar85 | 16:c63a5d084db0 | 84 | volatile float g_TempReading = 0.0f; // |
legstar85 | 16:c63a5d084db0 | 85 | // |
legstar85 | 2:e0091b5311f1 | 86 | void error(); // error function hangs flashing an LED |
legstar85 | 2:e0091b5311f1 | 87 | void init_serial(); // setup serial port |
legstar85 | 2:e0091b5311f1 | 88 | void init_K64F(); // set-up the on-board LEDs and switches |
legstar85 | 3:80c1eba78f9b | 89 | void init_PCB(); // set-up the PCB LEDs and buttons |
legstar85 | 9:d71b92c916f8 | 90 | void ButtonA_isr(); // |
legstar85 | 9:d71b92c916f8 | 91 | void ButtonB_isr(); // |
legstar85 | 9:d71b92c916f8 | 92 | void ButtonBack_isr(); // |
legstar85 | 9:d71b92c916f8 | 93 | void sw2_isr(); // |
legstar85 | 9:d71b92c916f8 | 94 | void menu_timer_isr(); // |
legstar85 | 6:ebf4784ce92b | 95 | void OnStartup(); // |
legstar85 | 6:ebf4784ce92b | 96 | void Run(); // |
legstar85 | 11:a6eec09b546e | 97 | void StartTemp(); // |
legstar85 | 14:3e9991fe64e5 | 98 | void delete_file(char filename[]); // |
legstar85 | 17:793f819e1df6 | 99 | void HighTemp(); // |
legstar85 | 17:793f819e1df6 | 100 | void SuperHighTemp(); // |
legstar85 | 17:793f819e1df6 | 101 | void UltraHighTemp(); // |
legstar85 | 17:793f819e1df6 | 102 | void LowTemp(); // |
legstar85 | 17:793f819e1df6 | 103 | void SuperLowTemp(); // |
legstar85 | 17:793f819e1df6 | 104 | void UltraLowTemp(); // |
legstar85 | 17:793f819e1df6 | 105 | void PeripheralsOffHigh(); // |
legstar85 | 17:793f819e1df6 | 106 | void PeripheralsOffLow(); // |
legstar85 | 17:793f819e1df6 | 107 | // |
legstar85 | 16:c63a5d084db0 | 108 | int main() // |
legstar85 | 16:c63a5d084db0 | 109 | { // |
legstar85 | 16:c63a5d084db0 | 110 | init_K64F(); // Initialise the board |
legstar85 | 2:e0091b5311f1 | 111 | init_serial(); // initialise the serial port |
legstar85 | 16:c63a5d084db0 | 112 | init_PCB(); // Initialise the PCB |
legstar85 | 16:c63a5d084db0 | 113 | // |
legstar85 | 16:c63a5d084db0 | 114 | tmp102.init(); // Call the sensor init method using dot syntax |
legstar85 | 16:c63a5d084db0 | 115 | lcd.init(); // Initialise display |
legstar85 | 16:c63a5d084db0 | 116 | joystick.init(); // Initialise joystick |
legstar85 | 16:c63a5d084db0 | 117 | // |
legstar85 | 5:eebd36a47049 | 118 | ticker_menu.attach(&menu_timer_isr,0.2); // Attach ticker for the Joystick |
legstar85 | 16:c63a5d084db0 | 119 | // |
legstar85 | 3:80c1eba78f9b | 120 | sw2.fall(&sw2_isr); // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default and fall to 0 V when pressed. We therefore need to look for a falling edge on the pin to fire the interrupt |
legstar85 | 3:80c1eba78f9b | 121 | ButtonA.rise(&ButtonA_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for |
legstar85 | 3:80c1eba78f9b | 122 | ButtonB.rise(&ButtonB_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for |
legstar85 | 16:c63a5d084db0 | 123 | // |
legstar85 | 2:e0091b5311f1 | 124 | lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0 |
legstar85 | 16:c63a5d084db0 | 125 | // |
legstar85 | 11:a6eec09b546e | 126 | OnStartup(); // Call intro screen |
legstar85 | 9:d71b92c916f8 | 127 | Run(); // Call main-menu and functions |
legstar85 | 16:c63a5d084db0 | 128 | } // |
legstar85 | 16:c63a5d084db0 | 129 | // |
legstar85 | 16:c63a5d084db0 | 130 | void init_serial() { // |
legstar85 | 2:e0091b5311f1 | 131 | pc.baud(115200); // set to highest baud - ensure terminal software matches |
legstar85 | 16:c63a5d084db0 | 132 | } // |
legstar85 | 16:c63a5d084db0 | 133 | // |
legstar85 | 16:c63a5d084db0 | 134 | void init_K64F() // |
legstar85 | 16:c63a5d084db0 | 135 | { // |
legstar85 | 2:e0091b5311f1 | 136 | r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 137 | g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 138 | b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 16:c63a5d084db0 | 139 | // |
legstar85 | 2:e0091b5311f1 | 140 | sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down |
legstar85 | 2:e0091b5311f1 | 141 | sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn |
legstar85 | 16:c63a5d084db0 | 142 | } // |
legstar85 | 16:c63a5d084db0 | 143 | // |
legstar85 | 16:c63a5d084db0 | 144 | void init_PCB () // |
legstar85 | 16:c63a5d084db0 | 145 | { // |
legstar85 | 3:80c1eba78f9b | 146 | LED01 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 147 | LED02 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 148 | LED03 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 149 | LED04 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 150 | LED05 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 151 | LED06 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 16:c63a5d084db0 | 152 | // |
legstar85 | 9:d71b92c916f8 | 153 | Buzzer = 0; // Ensure Piezo Buzzer is off |
legstar85 | 16:c63a5d084db0 | 154 | // |
legstar85 | 3:80c1eba78f9b | 155 | ButtonA.mode(PullDown); // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed |
legstar85 | 3:80c1eba78f9b | 156 | ButtonB.mode(PullDown); // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed |
legstar85 | 16:c63a5d084db0 | 157 | } // |
legstar85 | 16:c63a5d084db0 | 158 | // |
legstar85 | 3:80c1eba78f9b | 159 | void ButtonA_isr() // ButtonA event-triggered interrupt |
legstar85 | 16:c63a5d084db0 | 160 | { // |
legstar85 | 3:80c1eba78f9b | 161 | g_ButtonA_flag = 1; // set flag in ISR |
legstar85 | 16:c63a5d084db0 | 162 | } // |
legstar85 | 16:c63a5d084db0 | 163 | // |
legstar85 | 3:80c1eba78f9b | 164 | void ButtonB_isr() // ButtonB event-triggered interrupt |
legstar85 | 16:c63a5d084db0 | 165 | { // |
legstar85 | 3:80c1eba78f9b | 166 | g_ButtonB_flag = 1; // set flag in ISR |
legstar85 | 16:c63a5d084db0 | 167 | } // |
legstar85 | 16:c63a5d084db0 | 168 | // |
legstar85 | 7:d77e95505e43 | 169 | void ButtonBack_isr() // ButtonB event-triggered interrupt |
legstar85 | 16:c63a5d084db0 | 170 | { // |
legstar85 | 7:d77e95505e43 | 171 | g_ButtonBack_flag = 1; // set flag in ISR |
legstar85 | 16:c63a5d084db0 | 172 | } // |
legstar85 | 16:c63a5d084db0 | 173 | // |
legstar85 | 3:80c1eba78f9b | 174 | void sw2_isr() // SW2 event-triggered interrupt |
legstar85 | 16:c63a5d084db0 | 175 | { // |
legstar85 | 3:80c1eba78f9b | 176 | g_sw2_flag = 1; // set flag in ISR |
legstar85 | 16:c63a5d084db0 | 177 | } // |
legstar85 | 16:c63a5d084db0 | 178 | // |
legstar85 | 16:c63a5d084db0 | 179 | void menu_timer_isr() // |
legstar85 | 16:c63a5d084db0 | 180 | { // |
legstar85 | 6:ebf4784ce92b | 181 | g_menu_timer_flag = 1; // set flag in ISR |
legstar85 | 16:c63a5d084db0 | 182 | } // |
legstar85 | 16:c63a5d084db0 | 183 | // |
legstar85 | 9:d71b92c916f8 | 184 | void OnStartup() // Run some start up display |
legstar85 | 16:c63a5d084db0 | 185 | { // |
legstar85 | 12:5046b7515d5c | 186 | Buzzer.period(1.0/659.0); // Welcome sounds from Piezo |
legstar85 | 12:5046b7515d5c | 187 | Buzzer = 0.5; // |
legstar85 | 12:5046b7515d5c | 188 | wait(0.5); // |
legstar85 | 12:5046b7515d5c | 189 | Buzzer.period(1.0/494.0); // |
legstar85 | 12:5046b7515d5c | 190 | Buzzer = 0.5; // |
legstar85 | 12:5046b7515d5c | 191 | wait(0.5); // |
legstar85 | 12:5046b7515d5c | 192 | Buzzer.period(1.0/554.0); // |
legstar85 | 12:5046b7515d5c | 193 | Buzzer = 0.5; // |
legstar85 | 12:5046b7515d5c | 194 | wait(0.5); // |
legstar85 | 12:5046b7515d5c | 195 | Buzzer = 0; // Turn off welcome sounds |
legstar85 | 9:d71b92c916f8 | 196 | lcd.clear(); // Clear buffer at start of every loop |
legstar85 | 9:d71b92c916f8 | 197 | lcd.printString("--------------",0,0); // Can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) |
legstar85 | 7:d77e95505e43 | 198 | lcd.printString(" Smart Cold",0,1); // Just a welcome message before auto moving to main menu |
legstar85 | 7:d77e95505e43 | 199 | lcd.printString(" Storage",0,2); // |
legstar85 | 7:d77e95505e43 | 200 | lcd.printString(" Monitoring",0,3); // |
legstar85 | 17:793f819e1df6 | 201 | lcd.printString("V18 - Jan 2022",0,4); // |
legstar85 | 7:d77e95505e43 | 202 | lcd.printString("--------------",0,5); // |
legstar85 | 9:d71b92c916f8 | 203 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 9:d71b92c916f8 | 204 | wait(5.0); // Leave welcome screen on for designated amount of time |
legstar85 | 9:d71b92c916f8 | 205 | lcd.clear(); // Clear buffer at start of every loop |
legstar85 | 9:d71b92c916f8 | 206 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 8:07323fcab6d1 | 207 | lcd.printString("--------------",0,0); // |
legstar85 | 8:07323fcab6d1 | 208 | lcd.printString(" Use Joystick",0,1); // Instruction for use of menu |
legstar85 | 8:07323fcab6d1 | 209 | lcd.printString(" To Navigate",0,2); // |
legstar85 | 8:07323fcab6d1 | 210 | lcd.printString("",0,3); // Blank Line |
legstar85 | 8:07323fcab6d1 | 211 | lcd.printString(" A = Select",0,4); // |
legstar85 | 8:07323fcab6d1 | 212 | lcd.printString("--------------",0,5); // |
legstar85 | 9:d71b92c916f8 | 213 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 7:d77e95505e43 | 214 | wait(5.0); // |
legstar85 | 9:d71b92c916f8 | 215 | init_PCB(); // Ran again to ensure all LED's etc are turned off |
legstar85 | 13:5ad65a688f3f | 216 | printf("Transition to Temp Selection %i\n",StartTemp); // Observe on serial port - ensure transition to correct screen |
legstar85 | 16:c63a5d084db0 | 217 | } // |
legstar85 | 16:c63a5d084db0 | 218 | // |
legstar85 | 9:d71b92c916f8 | 219 | enum EMenuState // An enum controlling the current state of the display. |
legstar85 | 16:c63a5d084db0 | 220 | { // |
legstar85 | 16:c63a5d084db0 | 221 | MENUSTATE_StartTemp, // Defining each menu state to be called upon later (Integer Value of 0) |
legstar85 | 16:c63a5d084db0 | 222 | MENUSTATE_Main, // Defining each menu state to be called upon later (Integer Value of 1) |
legstar85 | 16:c63a5d084db0 | 223 | MENUSTATE_Monitor, // Defining each menu state to be called upon later (Integer Value of 2) |
legstar85 | 16:c63a5d084db0 | 224 | MENUSTATE_OneOff, // Defining each menu state to be called upon later (Integer Value of 3) |
legstar85 | 16:c63a5d084db0 | 225 | MENUSTATE_Results, // Defining each menu state to be called upon later (Integer Value of 4) |
legstar85 | 16:c63a5d084db0 | 226 | MENUSTATE_About, // Defining each menu state to be called upon later (Integer Value of 5) |
legstar85 | 16:c63a5d084db0 | 227 | MENUSTATE_Author, // Defining each menu state to be called upon later (Integer Value of 6) |
legstar85 | 16:c63a5d084db0 | 228 | // |
legstar85 | 16:c63a5d084db0 | 229 | MENUSTATTE_Num, // This is a special enum value that allows us to check and see how many menus we have, +1 of menu integer value |
legstar85 | 16:c63a5d084db0 | 230 | }; // |
legstar85 | 16:c63a5d084db0 | 231 | // |
legstar85 | 16:c63a5d084db0 | 232 | void Run() // |
legstar85 | 16:c63a5d084db0 | 233 | { // |
legstar85 | 15:0cd78b44ea83 | 234 | int MenuState = MENUSTATE_StartTemp; // Ensuring that the first Menu State to be brought up is the Temperature Selection Page |
legstar85 | 9:d71b92c916f8 | 235 | int SelectedItem = 0; // |
legstar85 | 9:d71b92c916f8 | 236 | int NumMenuItems = 1; // |
legstar85 | 16:c63a5d084db0 | 237 | int NumFramesInState = 0; // How many frames have we been in the current menu state. |
legstar85 | 16:c63a5d084db0 | 238 | // |
legstar85 | 15:0cd78b44ea83 | 239 | char buffer[14]; // Each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
legstar85 | 16:c63a5d084db0 | 240 | // |
legstar85 | 16:c63a5d084db0 | 241 | FILE *fp = NULL; // This is our file pointer |
legstar85 | 16:c63a5d084db0 | 242 | // |
legstar85 | 16:c63a5d084db0 | 243 | while(1){ // Project infinite while loop |
legstar85 | 9:d71b92c916f8 | 244 | if (g_menu_timer_flag){ // |
legstar85 | 9:d71b92c916f8 | 245 | g_menu_timer_flag = 0; // |
legstar85 | 16:c63a5d084db0 | 246 | // |
legstar85 | 6:ebf4784ce92b | 247 | bool bAButtonWasPressed = g_ButtonA_flag; // Get the value of the input flags and reset them |
legstar85 | 9:d71b92c916f8 | 248 | bool bBButtonWasPressed = g_ButtonB_flag; // Get the value of the input flags and reset them |
legstar85 | 9:d71b92c916f8 | 249 | g_ButtonA_flag = 0; // |
legstar85 | 9:d71b92c916f8 | 250 | g_ButtonB_flag = 0; // |
legstar85 | 16:c63a5d084db0 | 251 | // |
legstar85 | 16:c63a5d084db0 | 252 | lcd.clear(); // Clear buffer at start of every loop |
legstar85 | 16:c63a5d084db0 | 253 | // |
legstar85 | 6:ebf4784ce92b | 254 | int NewMenuState = MENUSTATTE_Num; // The new menu we want to transition to, if any. |
legstar85 | 16:c63a5d084db0 | 255 | // |
legstar85 | 6:ebf4784ce92b | 256 | switch(MenuState) // Update and Draw whichever menu we're on. |
legstar85 | 16:c63a5d084db0 | 257 | { // |
legstar85 | 12:5046b7515d5c | 258 | case MENUSTATE_StartTemp: // |
legstar85 | 16:c63a5d084db0 | 259 | { // |
legstar85 | 15:0cd78b44ea83 | 260 | NumMenuItems = 1; // Details number of items in the menu. We need this to wrap the selection around properly etc. |
legstar85 | 11:a6eec09b546e | 261 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 262 | { // |
legstar85 | 11:a6eec09b546e | 263 | SelectedItem = 0; // Something has gone wrong, reset selected item. |
legstar85 | 16:c63a5d084db0 | 264 | } // |
legstar85 | 16:c63a5d084db0 | 265 | Direction d = joystick.get_direction(); // |
legstar85 | 16:c63a5d084db0 | 266 | // |
legstar85 | 16:c63a5d084db0 | 267 | StartTemp(); // |
legstar85 | 16:c63a5d084db0 | 268 | // |
legstar85 | 16:c63a5d084db0 | 269 | float g_StartTemp = fsm[g_state].output; // Read temperature from FSM and print selection to LCD |
legstar85 | 12:5046b7515d5c | 270 | pc.printf("T = %f C\n",g_StartTemp); // Print to serial - allows testing without device attached |
legstar85 | 11:a6eec09b546e | 271 | printf ("Joystick Direction Points = %i\n",d); // |
legstar85 | 12:5046b7515d5c | 272 | printf ("State selected = %i\n", g_state); // |
legstar85 | 16:c63a5d084db0 | 273 | int length = sprintf(buffer," T = %.2f C",g_StartTemp); // Print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer |
legstar85 | 16:c63a5d084db0 | 274 | if (length <= 14){ // If string will fit on display (assuming printing at x=0) |
legstar85 | 13:5ad65a688f3f | 275 | lcd.printString("- Set Target -",0,0); // |
legstar85 | 13:5ad65a688f3f | 276 | lcd.printString("---- Temp ----",0,1); // |
legstar85 | 16:c63a5d084db0 | 277 | lcd.printString(buffer,0,3); // Display on screen |
legstar85 | 13:5ad65a688f3f | 278 | lcd.printString("'A' to Select",0,5); // |
legstar85 | 16:c63a5d084db0 | 279 | lcd.refresh(); // |
legstar85 | 13:5ad65a688f3f | 280 | } // need to refresh display after setting pixels or writing strings |
legstar85 | 16:c63a5d084db0 | 281 | // |
legstar85 | 11:a6eec09b546e | 282 | if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen. |
legstar85 | 16:c63a5d084db0 | 283 | { // |
legstar85 | 11:a6eec09b546e | 284 | if(SelectedItem == 0) // If 0 line is selected, move to detailed menu |
legstar85 | 11:a6eec09b546e | 285 | { // Actually 0 line + 1, see circle draw and selection below |
legstar85 | 11:a6eec09b546e | 286 | NewMenuState = MENUSTATE_Main; // |
legstar85 | 16:c63a5d084db0 | 287 | } // |
legstar85 | 16:c63a5d084db0 | 288 | } // |
legstar85 | 16:c63a5d084db0 | 289 | } // |
legstar85 | 11:a6eec09b546e | 290 | break; // |
legstar85 | 9:d71b92c916f8 | 291 | case MENUSTATE_Main: // |
legstar85 | 16:c63a5d084db0 | 292 | { // |
legstar85 | 15:0cd78b44ea83 | 293 | NumMenuItems = 5; // Details number of items in the menu. We need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 294 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 295 | { // |
legstar85 | 6:ebf4784ce92b | 296 | SelectedItem = 0; // Something has gone wrong, reset selected item. |
legstar85 | 16:c63a5d084db0 | 297 | } // |
legstar85 | 9:d71b92c916f8 | 298 | lcd.printString("---- MENU ----",0,0); // Menu title and selectable options |
legstar85 | 9:d71b92c916f8 | 299 | lcd.printString("M1 - Monitor",0,1); // Printed to the LCD screen |
legstar85 | 9:d71b92c916f8 | 300 | lcd.printString("M2 - One-off",0,2); // |
legstar85 | 14:3e9991fe64e5 | 301 | lcd.printString("Results",0,3); // |
legstar85 | 14:3e9991fe64e5 | 302 | lcd.printString("About",0,4); // |
legstar85 | 14:3e9991fe64e5 | 303 | lcd.printString("Author",0,5); // |
legstar85 | 16:c63a5d084db0 | 304 | // |
legstar85 | 6:ebf4784ce92b | 305 | if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen. |
legstar85 | 16:c63a5d084db0 | 306 | { // |
legstar85 | 8:07323fcab6d1 | 307 | if(SelectedItem == 0) // If 0 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 308 | { // Actually 0 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 309 | NewMenuState = MENUSTATE_Monitor; // |
legstar85 | 16:c63a5d084db0 | 310 | } // |
legstar85 | 8:07323fcab6d1 | 311 | else if(SelectedItem == 1) // If 1 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 312 | { // Actually 1 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 313 | NewMenuState = MENUSTATE_OneOff; // |
legstar85 | 16:c63a5d084db0 | 314 | } // |
legstar85 | 8:07323fcab6d1 | 315 | else if(SelectedItem == 2) // If 2 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 316 | { // Actually 2 line + 1, see circle draw and selection below |
legstar85 | 14:3e9991fe64e5 | 317 | NewMenuState = MENUSTATE_Results; // |
legstar85 | 16:c63a5d084db0 | 318 | } // |
legstar85 | 15:0cd78b44ea83 | 319 | else if(SelectedItem == 3) // If 3 line is selected, move to detailed menu |
legstar85 | 15:0cd78b44ea83 | 320 | { // Actually 3 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 321 | NewMenuState = MENUSTATE_About; // |
legstar85 | 16:c63a5d084db0 | 322 | } // |
legstar85 | 15:0cd78b44ea83 | 323 | else if(SelectedItem == 4) // If 4 line is selected, move to detailed menu |
legstar85 | 15:0cd78b44ea83 | 324 | { // Actually 4 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 325 | NewMenuState = MENUSTATE_Author; // |
legstar85 | 16:c63a5d084db0 | 326 | } // |
legstar85 | 16:c63a5d084db0 | 327 | } // |
legstar85 | 16:c63a5d084db0 | 328 | } // |
legstar85 | 9:d71b92c916f8 | 329 | break; // |
legstar85 | 9:d71b92c916f8 | 330 | case MENUSTATE_Monitor: // Call constant measurement menu following top menu selection |
legstar85 | 16:c63a5d084db0 | 331 | { // |
legstar85 | 8:07323fcab6d1 | 332 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 16:c63a5d084db0 | 333 | // |
legstar85 | 9:d71b92c916f8 | 334 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 335 | { // |
legstar85 | 6:ebf4784ce92b | 336 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 337 | } // |
legstar85 | 16:c63a5d084db0 | 338 | // |
legstar85 | 7:d77e95505e43 | 339 | float T = tmp102.get_temperature(); // read temperature and print to lcd |
legstar85 | 17:793f819e1df6 | 340 | pc.printf("T = %f C\n",T); // Print to serial - allows testing without device attached |
legstar85 | 7:d77e95505e43 | 341 | int length = sprintf(buffer," T = %.2f C",T); // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer |
legstar85 | 7:d77e95505e43 | 342 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
legstar85 | 9:d71b92c916f8 | 343 | lcd.printString("-- Constant --",0,0); // |
legstar85 | 9:d71b92c916f8 | 344 | lcd.printString("- Monitoring -",0,1); // |
legstar85 | 7:d77e95505e43 | 345 | lcd.printString(buffer,0,3); // display on screen |
legstar85 | 9:d71b92c916f8 | 346 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 7:d77e95505e43 | 347 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 9:d71b92c916f8 | 348 | wait(0.5); // |
legstar85 | 16:c63a5d084db0 | 349 | // |
legstar85 | 17:793f819e1df6 | 350 | if (T >= g_StartTemp + 2 and T < g_StartTemp + 4) { // High temp alarm condition - in real world would be lot lower!! |
legstar85 | 17:793f819e1df6 | 351 | HighTemp(); // |
legstar85 | 16:c63a5d084db0 | 352 | } // |
legstar85 | 16:c63a5d084db0 | 353 | else { // |
legstar85 | 17:793f819e1df6 | 354 | PeripheralsOffHigh(); // |
legstar85 | 17:793f819e1df6 | 355 | } // |
legstar85 | 17:793f819e1df6 | 356 | if (T >= g_StartTemp + 4 and T < g_StartTemp + 6) { // High temp alarm condition - in real world would be lot lower!! |
legstar85 | 17:793f819e1df6 | 357 | SuperHighTemp(); // |
legstar85 | 16:c63a5d084db0 | 358 | } // |
legstar85 | 17:793f819e1df6 | 359 | else { // |
legstar85 | 17:793f819e1df6 | 360 | PeripheralsOffHigh(); // |
legstar85 | 17:793f819e1df6 | 361 | } // |
legstar85 | 17:793f819e1df6 | 362 | if (T >= g_StartTemp + 6) { // |
legstar85 | 17:793f819e1df6 | 363 | UltraHighTemp(); // |
legstar85 | 17:793f819e1df6 | 364 | } // |
legstar85 | 17:793f819e1df6 | 365 | else { // |
legstar85 | 17:793f819e1df6 | 366 | PeripheralsOffHigh(); // |
legstar85 | 17:793f819e1df6 | 367 | } // |
legstar85 | 17:793f819e1df6 | 368 | if (T <= g_StartTemp - 2 and T > g_StartTemp - 4) { // Low temp alarm condition - in real world would be lot lower!! |
legstar85 | 17:793f819e1df6 | 369 | LowTemp(); // |
legstar85 | 16:c63a5d084db0 | 370 | } // |
legstar85 | 16:c63a5d084db0 | 371 | else { // |
legstar85 | 17:793f819e1df6 | 372 | PeripheralsOffLow(); // |
legstar85 | 16:c63a5d084db0 | 373 | } // |
legstar85 | 9:d71b92c916f8 | 374 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 16:c63a5d084db0 | 375 | { // |
legstar85 | 9:d71b92c916f8 | 376 | if(SelectedItem == 0) // |
legstar85 | 16:c63a5d084db0 | 377 | { // |
legstar85 | 9:d71b92c916f8 | 378 | NewMenuState = MENUSTATE_Main; // Transition back to the main menu |
legstar85 | 17:793f819e1df6 | 379 | PeripheralsOffHigh(); // |
legstar85 | 17:793f819e1df6 | 380 | PeripheralsOffLow(); // |
legstar85 | 16:c63a5d084db0 | 381 | } // |
legstar85 | 16:c63a5d084db0 | 382 | } // |
legstar85 | 16:c63a5d084db0 | 383 | } // |
legstar85 | 16:c63a5d084db0 | 384 | break; // |
legstar85 | 11:a6eec09b546e | 385 | case MENUSTATE_OneOff: // Call a one off measurement menu following top menu selection |
legstar85 | 16:c63a5d084db0 | 386 | { // |
legstar85 | 8:07323fcab6d1 | 387 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 388 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 389 | { // |
legstar85 | 7:d77e95505e43 | 390 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 391 | } // |
legstar85 | 16:c63a5d084db0 | 392 | // |
legstar85 | 16:c63a5d084db0 | 393 | if(NumFramesInState == 0) // |
legstar85 | 16:c63a5d084db0 | 394 | { // |
legstar85 | 16:c63a5d084db0 | 395 | g_TempReading = tmp102.get_temperature(); // Gather the temp readings only on the frame we enter this state and keep printing this same temp, not to refresh and read new temp. |
legstar85 | 16:c63a5d084db0 | 396 | } // |
legstar85 | 16:c63a5d084db0 | 397 | // |
legstar85 | 16:c63a5d084db0 | 398 | pc.printf("T = %f K\n",g_TempReading); // Print to serial - allows testing without device attached |
legstar85 | 16:c63a5d084db0 | 399 | int length = sprintf(buffer," T = %.2f C",g_TempReading); // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer |
legstar85 | 16:c63a5d084db0 | 400 | if (length <= 14){ // if string will fit on display (assuming printing at x=0) |
legstar85 | 16:c63a5d084db0 | 401 | lcd.printString("-- One-Off --",0,0); // |
legstar85 | 16:c63a5d084db0 | 402 | lcd.printString("-- Measure --",0,1); // |
legstar85 | 16:c63a5d084db0 | 403 | lcd.printString(buffer,0,3); // display on screen |
legstar85 | 16:c63a5d084db0 | 404 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 16:c63a5d084db0 | 405 | } // |
legstar85 | 16:c63a5d084db0 | 406 | // |
legstar85 | 9:d71b92c916f8 | 407 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 16:c63a5d084db0 | 408 | { // |
legstar85 | 9:d71b92c916f8 | 409 | if(SelectedItem == 0) // |
legstar85 | 16:c63a5d084db0 | 410 | { // |
legstar85 | 8:07323fcab6d1 | 411 | NewMenuState = MENUSTATE_Main; // Take us back to top menu |
legstar85 | 16:c63a5d084db0 | 412 | } // |
legstar85 | 16:c63a5d084db0 | 413 | } // |
legstar85 | 16:c63a5d084db0 | 414 | } // |
legstar85 | 16:c63a5d084db0 | 415 | break; // |
legstar85 | 15:0cd78b44ea83 | 416 | case MENUSTATE_Results: // Call results menu following top menu selection |
legstar85 | 16:c63a5d084db0 | 417 | { // |
legstar85 | 14:3e9991fe64e5 | 418 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 14:3e9991fe64e5 | 419 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 420 | { // |
legstar85 | 16:c63a5d084db0 | 421 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 422 | } // |
legstar85 | 16:c63a5d084db0 | 423 | // |
legstar85 | 16:c63a5d084db0 | 424 | if(NumFramesInState == 0) // |
legstar85 | 16:c63a5d084db0 | 425 | { // |
legstar85 | 16:c63a5d084db0 | 426 | if(fp != NULL){ // |
legstar85 | 16:c63a5d084db0 | 427 | printf("Error! File already open!\n"); // |
legstar85 | 16:c63a5d084db0 | 428 | fclose(fp); // |
legstar85 | 16:c63a5d084db0 | 429 | } // |
legstar85 | 16:c63a5d084db0 | 430 | // |
legstar85 | 16:c63a5d084db0 | 431 | fp = fopen("/sd/overnighttemp.csv", "w"); // Open our file handle on the first frame. |
legstar85 | 16:c63a5d084db0 | 432 | } // |
legstar85 | 16:c63a5d084db0 | 433 | // |
legstar85 | 16:c63a5d084db0 | 434 | if (fp == NULL) { // if it can't open the file then print error message |
legstar85 | 17:793f819e1df6 | 435 | printf("Error! Unable to open file! Returning to main menu.\n"); // |
legstar85 | 14:3e9991fe64e5 | 436 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 437 | } // |
legstar85 | 16:c63a5d084db0 | 438 | else // |
legstar85 | 16:c63a5d084db0 | 439 | { // |
legstar85 | 16:c63a5d084db0 | 440 | float T = tmp102.get_temperature(); // Read temperature and print to lcd |
legstar85 | 16:c63a5d084db0 | 441 | pc.printf("T = %f K\n",T); // Print to serial - allows testing without device attached |
legstar85 | 16:c63a5d084db0 | 442 | fprintf(fp, "%d,%f\n",NumFramesInState,T); // Print formatted string to file (CSV) |
legstar85 | 16:c63a5d084db0 | 443 | // |
legstar85 | 16:c63a5d084db0 | 444 | lcd.printString("-- Results --",0,0); // |
legstar85 | 16:c63a5d084db0 | 445 | lcd.printString(" Writing",0,1); // |
legstar85 | 16:c63a5d084db0 | 446 | lcd.printString(" results to ",0,2); // |
legstar85 | 16:c63a5d084db0 | 447 | lcd.printString(" file...",0,3); // |
legstar85 | 16:c63a5d084db0 | 448 | lcd.printString(" ",0,4); // |
legstar85 | 16:c63a5d084db0 | 449 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 16:c63a5d084db0 | 450 | } // |
legstar85 | 16:c63a5d084db0 | 451 | // |
legstar85 | 14:3e9991fe64e5 | 452 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 16:c63a5d084db0 | 453 | { // |
legstar85 | 14:3e9991fe64e5 | 454 | if(SelectedItem == 0) // |
legstar85 | 16:c63a5d084db0 | 455 | { // |
legstar85 | 14:3e9991fe64e5 | 456 | NewMenuState = MENUSTATE_Main; // Take us back to top menu |
legstar85 | 16:c63a5d084db0 | 457 | } // |
legstar85 | 16:c63a5d084db0 | 458 | } // |
legstar85 | 16:c63a5d084db0 | 459 | } // |
legstar85 | 16:c63a5d084db0 | 460 | break; // |
legstar85 | 9:d71b92c916f8 | 461 | case MENUSTATE_About: // Call About menu following top menu selection |
legstar85 | 16:c63a5d084db0 | 462 | { // |
legstar85 | 8:07323fcab6d1 | 463 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 464 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 465 | { // |
legstar85 | 6:ebf4784ce92b | 466 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 467 | } // |
legstar85 | 16:c63a5d084db0 | 468 | // |
legstar85 | 9:d71b92c916f8 | 469 | lcd.printString("--- About ---",0,0); // |
legstar85 | 9:d71b92c916f8 | 470 | lcd.printString("ELE3006M - IoT",0,1); // |
legstar85 | 9:d71b92c916f8 | 471 | lcd.printString(" Project",0,2); // |
legstar85 | 9:d71b92c916f8 | 472 | lcd.printString("Uni of Lincoln",0,3); // |
legstar85 | 9:d71b92c916f8 | 473 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 9:d71b92c916f8 | 474 | lcd.refresh(); // |
legstar85 | 16:c63a5d084db0 | 475 | // |
legstar85 | 9:d71b92c916f8 | 476 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 16:c63a5d084db0 | 477 | { // |
legstar85 | 9:d71b92c916f8 | 478 | if(SelectedItem == 0) // |
legstar85 | 16:c63a5d084db0 | 479 | { // |
legstar85 | 9:d71b92c916f8 | 480 | NewMenuState = MENUSTATE_Main; // Transition back to Main Menu |
legstar85 | 16:c63a5d084db0 | 481 | } // |
legstar85 | 16:c63a5d084db0 | 482 | } // |
legstar85 | 16:c63a5d084db0 | 483 | } // |
legstar85 | 16:c63a5d084db0 | 484 | break; // |
legstar85 | 9:d71b92c916f8 | 485 | case MENUSTATE_Author: // Call Author menu following top menu selection |
legstar85 | 16:c63a5d084db0 | 486 | { // |
legstar85 | 8:07323fcab6d1 | 487 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 488 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 489 | { // |
legstar85 | 7:d77e95505e43 | 490 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 491 | } // |
legstar85 | 16:c63a5d084db0 | 492 | // |
legstar85 | 9:d71b92c916f8 | 493 | lcd.printString("--- Author ---",0,0); // |
legstar85 | 9:d71b92c916f8 | 494 | lcd.printString("David Leaming ",0,1); // |
legstar85 | 9:d71b92c916f8 | 495 | lcd.printString(" 25574043 ",0,2); // |
legstar85 | 9:d71b92c916f8 | 496 | lcd.printString(" VolkerRail",0,3); // |
legstar85 | 9:d71b92c916f8 | 497 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 16:c63a5d084db0 | 498 | // |
legstar85 | 9:d71b92c916f8 | 499 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 16:c63a5d084db0 | 500 | { // |
legstar85 | 9:d71b92c916f8 | 501 | if(SelectedItem == 0) // |
legstar85 | 16:c63a5d084db0 | 502 | { // |
legstar85 | 8:07323fcab6d1 | 503 | NewMenuState = MENUSTATE_Main; // Take us back to top menu |
legstar85 | 16:c63a5d084db0 | 504 | } // |
legstar85 | 16:c63a5d084db0 | 505 | } // |
legstar85 | 16:c63a5d084db0 | 506 | } // |
legstar85 | 16:c63a5d084db0 | 507 | break; // |
legstar85 | 16:c63a5d084db0 | 508 | default: // |
legstar85 | 16:c63a5d084db0 | 509 | { // |
legstar85 | 6:ebf4784ce92b | 510 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 16:c63a5d084db0 | 511 | } // |
legstar85 | 16:c63a5d084db0 | 512 | }; // |
legstar85 | 16:c63a5d084db0 | 513 | // |
legstar85 | 6:ebf4784ce92b | 514 | if(NewMenuState != MENUSTATTE_Num) // If we have requested a new menu state. |
legstar85 | 16:c63a5d084db0 | 515 | { // |
legstar85 | 8:07323fcab6d1 | 516 | printf("Transitioning to MenuState: %i\n", NewMenuState); // Observe on serial port - ensure transition to correct screen |
legstar85 | 16:c63a5d084db0 | 517 | // |
legstar85 | 6:ebf4784ce92b | 518 | MenuState = NewMenuState; // We want to transition the menu to a new state. |
legstar85 | 16:c63a5d084db0 | 519 | // |
legstar85 | 6:ebf4784ce92b | 520 | // Do any bookkeeping needed when moving to new state. |
legstar85 | 16:c63a5d084db0 | 521 | SelectedItem = 0; // Reset the selected item. |
legstar85 | 16:c63a5d084db0 | 522 | NumFramesInState = 0; // Reset the frames in current state count. |
legstar85 | 16:c63a5d084db0 | 523 | // |
legstar85 | 16:c63a5d084db0 | 524 | if(fp){ // |
legstar85 | 16:c63a5d084db0 | 525 | fclose(fp); // Ensure we close the file if the current state had one open. |
legstar85 | 16:c63a5d084db0 | 526 | fp = NULL; // |
legstar85 | 16:c63a5d084db0 | 527 | } // |
legstar85 | 6:ebf4784ce92b | 528 | lcd.clear(); // Clear the display for one frame on state transition. |
legstar85 | 16:c63a5d084db0 | 529 | } // |
legstar85 | 16:c63a5d084db0 | 530 | else // |
legstar85 | 16:c63a5d084db0 | 531 | { // |
legstar85 | 16:c63a5d084db0 | 532 | ++NumFramesInState; // |
legstar85 | 16:c63a5d084db0 | 533 | |
legstar85 | 13:5ad65a688f3f | 534 | unsigned int SelectionMarkerRadius = 4; // If we have not selected to move to a new menu. |
legstar85 | 13:5ad65a688f3f | 535 | unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius); // Draw a marker circle at end of line to show which is the currently selected item. |
legstar85 | 13:5ad65a688f3f | 536 | unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1); // +1 because of the menu title being on first row |
legstar85 | 13:5ad65a688f3f | 537 | lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK); // Fill the circle black so it can be seen easily |
legstar85 | 6:ebf4784ce92b | 538 | |
legstar85 | 6:ebf4784ce92b | 539 | // Handle Joystick Input |
legstar85 | 16:c63a5d084db0 | 540 | Direction d = joystick.get_direction(); // |
legstar85 | 16:c63a5d084db0 | 541 | printf("Direction = %i\n", d); // |
legstar85 | 13:5ad65a688f3f | 542 | switch (joystick.get_direction()) { // Call to check direction joystick is pointing |
legstar85 | 16:c63a5d084db0 | 543 | case N: // |
legstar85 | 16:c63a5d084db0 | 544 | { // |
legstar85 | 16:c63a5d084db0 | 545 | SelectedItem--; // |
legstar85 | 16:c63a5d084db0 | 546 | printf("Selection decremented to %i\n", SelectedItem); // |
legstar85 | 16:c63a5d084db0 | 547 | } // |
legstar85 | 6:ebf4784ce92b | 548 | break; // |
legstar85 | 16:c63a5d084db0 | 549 | case S: // |
legstar85 | 16:c63a5d084db0 | 550 | { // |
legstar85 | 16:c63a5d084db0 | 551 | SelectedItem++; // |
legstar85 | 16:c63a5d084db0 | 552 | printf("Selection incremented to %i\n", SelectedItem); // |
legstar85 | 16:c63a5d084db0 | 553 | } // |
legstar85 | 6:ebf4784ce92b | 554 | break; // |
legstar85 | 16:c63a5d084db0 | 555 | } // |
legstar85 | 9:d71b92c916f8 | 556 | if(SelectedItem < 0) // Wrap the selection around to the start/end of the menu if it goes below 0 or above NumMenuItems. |
legstar85 | 16:c63a5d084db0 | 557 | { // |
legstar85 | 9:d71b92c916f8 | 558 | SelectedItem = NumMenuItems - 1; // |
legstar85 | 16:c63a5d084db0 | 559 | } // |
legstar85 | 9:d71b92c916f8 | 560 | else if(SelectedItem >= NumMenuItems) // |
legstar85 | 16:c63a5d084db0 | 561 | { // |
legstar85 | 9:d71b92c916f8 | 562 | SelectedItem = 0; // |
legstar85 | 16:c63a5d084db0 | 563 | } // |
legstar85 | 16:c63a5d084db0 | 564 | } // |
legstar85 | 15:0cd78b44ea83 | 565 | lcd.refresh(); // Finally update the display. |
legstar85 | 16:c63a5d084db0 | 566 | } // |
legstar85 | 16:c63a5d084db0 | 567 | } // |
legstar85 | 16:c63a5d084db0 | 568 | } // |
legstar85 | 16:c63a5d084db0 | 569 | // |
legstar85 | 11:a6eec09b546e | 570 | void StartTemp() // |
legstar85 | 16:c63a5d084db0 | 571 | { // |
legstar85 | 11:a6eec09b546e | 572 | Direction d = joystick.get_direction(); // |
legstar85 | 16:c63a5d084db0 | 573 | // |
legstar85 | 17:793f819e1df6 | 574 | g_StartTemp = fsm[g_state].output; // Set ouput depending on current state |
legstar85 | 17:793f819e1df6 | 575 | wait(fsm[g_state].time); // Wait in that state for desired time |
legstar85 | 17:793f819e1df6 | 576 | g_state = fsm[g_state].nextState[d]; // Read input and update curent state |
legstar85 | 17:793f819e1df6 | 577 | } // |
legstar85 | 17:793f819e1df6 | 578 | // |
legstar85 | 17:793f819e1df6 | 579 | void HighTemp() // |
legstar85 | 17:793f819e1df6 | 580 | { // |
legstar85 | 17:793f819e1df6 | 581 | LED01 = !LED01; // Flash LED01 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 582 | printf("WARNING - High Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 583 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely high |
legstar85 | 17:793f819e1df6 | 584 | Buzzer = 0.5; // |
legstar85 | 17:793f819e1df6 | 585 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 586 | Buzzer = 0; // |
legstar85 | 17:793f819e1df6 | 587 | } // |
legstar85 | 17:793f819e1df6 | 588 | // |
legstar85 | 17:793f819e1df6 | 589 | void SuperHighTemp() // |
legstar85 | 17:793f819e1df6 | 590 | { // |
legstar85 | 17:793f819e1df6 | 591 | LED01 = !LED01; // Flash LED01 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 592 | LED02 = !LED02; // Flash LED02 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 593 | printf("WARNING - Super High Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 594 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely high |
legstar85 | 17:793f819e1df6 | 595 | Buzzer = 0.5; // |
legstar85 | 17:793f819e1df6 | 596 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 597 | Buzzer = 0; // |
legstar85 | 17:793f819e1df6 | 598 | } // |
legstar85 | 17:793f819e1df6 | 599 | // |
legstar85 | 17:793f819e1df6 | 600 | void UltraHighTemp() // |
legstar85 | 17:793f819e1df6 | 601 | { // |
legstar85 | 17:793f819e1df6 | 602 | LED01 = !LED01; // Flash LED01 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 603 | LED02 = !LED02; // Flash LED02 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 604 | LED03 = !LED03; // Flash LED02 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 17:793f819e1df6 | 605 | printf("WARNING - Ultra High Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 606 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely high |
legstar85 | 17:793f819e1df6 | 607 | Buzzer = 0.5; // |
legstar85 | 17:793f819e1df6 | 608 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 609 | Buzzer = 0; // |
legstar85 | 16:c63a5d084db0 | 610 | } // |
legstar85 | 17:793f819e1df6 | 611 | // |
legstar85 | 17:793f819e1df6 | 612 | void LowTemp() // |
legstar85 | 17:793f819e1df6 | 613 | { // |
legstar85 | 17:793f819e1df6 | 614 | LED04 = !LED04; // Flash LED04 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 615 | printf("WARNING - Low Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 616 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely low |
legstar85 | 17:793f819e1df6 | 617 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 618 | Buzzer = 0; // |
legstar85 | 17:793f819e1df6 | 619 | } // |
legstar85 | 17:793f819e1df6 | 620 | // // |
legstar85 | 17:793f819e1df6 | 621 | void SuperLowTemp() // |
legstar85 | 17:793f819e1df6 | 622 | { // |
legstar85 | 17:793f819e1df6 | 623 | LED04 = !LED04; // Flash LED04 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 624 | LED05 = !LED05; // Flash LED05 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 625 | printf("WARNING - Super Low Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 626 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely low |
legstar85 | 17:793f819e1df6 | 627 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 628 | Buzzer = 0; // |
legstar85 | 17:793f819e1df6 | 629 | } // |
legstar85 | 17:793f819e1df6 | 630 | // |
legstar85 | 17:793f819e1df6 | 631 | void UltraLowTemp() // |
legstar85 | 17:793f819e1df6 | 632 | { // |
legstar85 | 17:793f819e1df6 | 633 | LED04 = !LED04; // Flash LED04 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 634 | LED05 = !LED05; // Flash LED05 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 635 | LED06 = !LED06; // Flash LED06 if temperature is over specified - Simulated starting of heaters |
legstar85 | 17:793f819e1df6 | 636 | printf("WARNING - Ultra Low Temp!! \n"); // |
legstar85 | 17:793f819e1df6 | 637 | Buzzer.period(1.0/554.0); // Warning Buzzer to extremely low |
legstar85 | 17:793f819e1df6 | 638 | wait(0.5); // |
legstar85 | 17:793f819e1df6 | 639 | Buzzer = 0; // |
legstar85 | 17:793f819e1df6 | 640 | } // |
legstar85 | 17:793f819e1df6 | 641 | // |
legstar85 | 17:793f819e1df6 | 642 | void PeripheralsOffHigh() // |
legstar85 | 17:793f819e1df6 | 643 | { // |
legstar85 | 17:793f819e1df6 | 644 | LED01 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 17:793f819e1df6 | 645 | LED02 = 1; // LED02 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 17:793f819e1df6 | 646 | LED03 = 1; // LED03 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 17:793f819e1df6 | 647 | Buzzer = 0; // Buzzer off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 17:793f819e1df6 | 648 | } // |
legstar85 | 17:793f819e1df6 | 649 | // |
legstar85 | 17:793f819e1df6 | 650 | void PeripheralsOffLow() // |
legstar85 | 17:793f819e1df6 | 651 | { // |
legstar85 | 17:793f819e1df6 | 652 | LED04 = 1; // LED04 off if temperature is below specified - Simulated stopping of heaters |
legstar85 | 17:793f819e1df6 | 653 | LED05 = 1; // LED05 off if temperature is below specified - Simulated stopping of heaters |
legstar85 | 17:793f819e1df6 | 654 | LED06 = 1; // LED06 off if temperature is below specified - Simulated stopping of heaters |
legstar85 | 17:793f819e1df6 | 655 | Buzzer = 0; // Buzzer off if temperature is below specified - Simulated stopping of heaters |
legstar85 | 17:793f819e1df6 | 656 | } // |