![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Updated following fixing of Joystick 'Bounce'. Also insertion of a Starting Temp to be compared against following user selection.
Dependencies: mbed N5110v02 TMP102 JoystickIoT
main.cpp@10:357b3329620f, 2022-01-19 (annotated)
- Committer:
- legstar85
- Date:
- Wed Jan 19 07:37:55 2022 +0000
- Revision:
- 10:357b3329620f
- Parent:
- 9:d71b92c916f8
- Child:
- 11:a6eec09b546e
Up-Rev of code, small changes including general tidy of code and adding of missing comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
legstar85 | 4:0090145b3286 | 1 | /* |
legstar85 | 2:e0091b5311f1 | 2 | * @ File main.cpp |
legstar85 | 4:0090145b3286 | 3 | * @ Author - David Leaming - 25574043 |
legstar85 | 9:d71b92c916f8 | 4 | * @ Date - January 2022 |
legstar85 | 2:e0091b5311f1 | 5 | * Acknowledgements |
legstar85 | 2:e0091b5311f1 | 6 | * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 |
legstar85 | 2:e0091b5311f1 | 7 | * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries |
eencae | 0:21a200b880d7 | 8 | */ |
eencae | 0:21a200b880d7 | 9 | |
legstar85 | 2:e0091b5311f1 | 10 | #include "mbed.h" // include the library header, ensure the library has been imported into the project |
eencae | 1:dd5fb735acf1 | 11 | #include "TMP102.h" |
legstar85 | 2:e0091b5311f1 | 12 | #include "N5110.h" |
legstar85 | 2:e0091b5311f1 | 13 | #include "Joystick.h" |
legstar85 | 2:e0091b5311f1 | 14 | #include "Bitmap.h" |
eencae | 0:21a200b880d7 | 15 | |
legstar85 | 2:e0091b5311f1 | 16 | TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object |
legstar85 | 2:e0091b5311f1 | 17 | |
legstar85 | 2:e0091b5311f1 | 18 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
legstar85 | 2:e0091b5311f1 | 19 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting |
legstar85 | 2:e0091b5311f1 | 20 | |
legstar85 | 3:80c1eba78f9b | 21 | // y x button |
legstar85 | 3:80c1eba78f9b | 22 | Joystick joystick(PTB10,PTB11,PTC16); // Define Joystick Object |
eencae | 0:21a200b880d7 | 23 | |
legstar85 | 2:e0091b5311f1 | 24 | Serial pc(USBTX,USBRX); // UART connection for PC |
legstar85 | 2:e0091b5311f1 | 25 | |
legstar85 | 5:eebd36a47049 | 26 | Ticker ticker_menu; // Create Menu ticker object |
legstar85 | 5:eebd36a47049 | 27 | |
legstar85 | 2:e0091b5311f1 | 28 | DigitalOut r_led(LED_RED); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 29 | DigitalOut g_led(LED_GREEN); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 30 | DigitalOut b_led(LED_BLUE); // K64F on-board LEDs |
eencae | 0:21a200b880d7 | 31 | |
legstar85 | 8:07323fcab6d1 | 32 | PwmOut LED01 (PTA1); // PCB Surface Mounted LED's - LED1 |
legstar85 | 8:07323fcab6d1 | 33 | PwmOut LED02 (PTA2); // PCB Surface Mounted LED's - LED2 |
legstar85 | 8:07323fcab6d1 | 34 | PwmOut LED03 (PTC2); // PCB Surface Mounted LED's - LED3 |
legstar85 | 8:07323fcab6d1 | 35 | PwmOut LED04 (PTC3); // PCB Surface Mounted LED's - LED4 |
legstar85 | 8:07323fcab6d1 | 36 | PwmOut LED05 (PTC4); // PCB Surface Mounted LED's - LED5 |
legstar85 | 8:07323fcab6d1 | 37 | PwmOut LED06 (PTD3); // PCB Surface Mounted LED's - LED6 |
legstar85 | 8:07323fcab6d1 | 38 | |
legstar85 | 8:07323fcab6d1 | 39 | PwmOut Buzzer (PTC10); // PCB Surface Mounted Piezo Buzzer |
legstar85 | 3:80c1eba78f9b | 40 | |
legstar85 | 2:e0091b5311f1 | 41 | InterruptIn sw2(SW2); // K64F on-board switches |
legstar85 | 2:e0091b5311f1 | 42 | InterruptIn sw3(SW3); // K64F on-board switches |
legstar85 | 3:80c1eba78f9b | 43 | InterruptIn ButtonA (PTB9); // PCB Button - A |
legstar85 | 3:80c1eba78f9b | 44 | InterruptIn ButtonB (PTD0); // PCB Button - B |
legstar85 | 7:d77e95505e43 | 45 | InterruptIn ButtonBack (PTB10); // PCB Button - Back |
legstar85 | 3:80c1eba78f9b | 46 | |
legstar85 | 5:eebd36a47049 | 47 | 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 | 5:eebd36a47049 | 48 | 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 | 7:d77e95505e43 | 49 | 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 | 5:eebd36a47049 | 50 | 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 | 5:eebd36a47049 | 51 | 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 | 52 | volatile int option = 0; // Menu option selection based on joystick direction |
legstar85 | 2:e0091b5311f1 | 53 | |
legstar85 | 2:e0091b5311f1 | 54 | void error(); // error function hangs flashing an LED |
legstar85 | 2:e0091b5311f1 | 55 | void init_serial(); // setup serial port |
legstar85 | 2:e0091b5311f1 | 56 | void init_K64F(); // set-up the on-board LEDs and switches |
legstar85 | 3:80c1eba78f9b | 57 | void init_PCB(); // set-up the PCB LEDs and buttons |
legstar85 | 9:d71b92c916f8 | 58 | void ButtonA_isr(); // |
legstar85 | 9:d71b92c916f8 | 59 | void ButtonB_isr(); // |
legstar85 | 9:d71b92c916f8 | 60 | void ButtonBack_isr(); // |
legstar85 | 9:d71b92c916f8 | 61 | void sw2_isr(); // |
legstar85 | 9:d71b92c916f8 | 62 | void menu_timer_isr(); // |
legstar85 | 6:ebf4784ce92b | 63 | void OnStartup(); // |
legstar85 | 6:ebf4784ce92b | 64 | void Run(); // |
legstar85 | 6:ebf4784ce92b | 65 | void ConstantMonitoring(); // |
eencae | 0:21a200b880d7 | 66 | |
eencae | 0:21a200b880d7 | 67 | int main() |
eencae | 0:21a200b880d7 | 68 | { |
legstar85 | 2:e0091b5311f1 | 69 | init_K64F(); // initialise the board |
legstar85 | 2:e0091b5311f1 | 70 | init_serial(); // initialise the serial port |
legstar85 | 3:80c1eba78f9b | 71 | init_PCB(); // initialise the PCB |
legstar85 | 3:80c1eba78f9b | 72 | |
legstar85 | 2:e0091b5311f1 | 73 | tmp102.init(); // call the sensor init method using dot syntax |
legstar85 | 2:e0091b5311f1 | 74 | lcd.init(); // initialise display |
legstar85 | 3:80c1eba78f9b | 75 | joystick.init(); // initialise joystick |
legstar85 | 3:80c1eba78f9b | 76 | |
legstar85 | 5:eebd36a47049 | 77 | ticker_menu.attach(&menu_timer_isr,0.2); // Attach ticker for the Joystick |
legstar85 | 5:eebd36a47049 | 78 | |
legstar85 | 3:80c1eba78f9b | 79 | 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 | 80 | 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 | 81 | ButtonB.rise(&ButtonB_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for |
legstar85 | 2:e0091b5311f1 | 82 | |
legstar85 | 2:e0091b5311f1 | 83 | lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0 |
eencae | 0:21a200b880d7 | 84 | |
legstar85 | 9:d71b92c916f8 | 85 | OnStartup(); // Call intro screen |
legstar85 | 9:d71b92c916f8 | 86 | Run(); // Call main-menu and functions |
legstar85 | 5:eebd36a47049 | 87 | } |
eencae | 0:21a200b880d7 | 88 | |
eencae | 0:21a200b880d7 | 89 | void init_serial() { |
legstar85 | 2:e0091b5311f1 | 90 | pc.baud(115200); // set to highest baud - ensure terminal software matches |
eencae | 0:21a200b880d7 | 91 | } |
eencae | 0:21a200b880d7 | 92 | |
eencae | 0:21a200b880d7 | 93 | void init_K64F() |
eencae | 0:21a200b880d7 | 94 | { |
legstar85 | 2:e0091b5311f1 | 95 | r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 96 | g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 97 | b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
eencae | 0:21a200b880d7 | 98 | |
legstar85 | 2:e0091b5311f1 | 99 | sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down |
legstar85 | 2:e0091b5311f1 | 100 | sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn |
eencae | 0:21a200b880d7 | 101 | } |
legstar85 | 3:80c1eba78f9b | 102 | |
legstar85 | 3:80c1eba78f9b | 103 | void init_PCB () |
legstar85 | 3:80c1eba78f9b | 104 | { |
legstar85 | 3:80c1eba78f9b | 105 | LED01 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 106 | LED02 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 107 | LED03 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 108 | LED04 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 109 | LED05 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 110 | LED06 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 9:d71b92c916f8 | 111 | |
legstar85 | 9:d71b92c916f8 | 112 | Buzzer = 0; // Ensure Piezo Buzzer is off |
legstar85 | 3:80c1eba78f9b | 113 | |
legstar85 | 3:80c1eba78f9b | 114 | 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 | 115 | ButtonB.mode(PullDown); // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed |
legstar85 | 3:80c1eba78f9b | 116 | } |
legstar85 | 3:80c1eba78f9b | 117 | |
legstar85 | 3:80c1eba78f9b | 118 | void ButtonA_isr() // ButtonA event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 119 | { |
legstar85 | 3:80c1eba78f9b | 120 | g_ButtonA_flag = 1; // set flag in ISR |
legstar85 | 3:80c1eba78f9b | 121 | } |
legstar85 | 3:80c1eba78f9b | 122 | |
legstar85 | 3:80c1eba78f9b | 123 | void ButtonB_isr() // ButtonB event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 124 | { |
legstar85 | 3:80c1eba78f9b | 125 | g_ButtonB_flag = 1; // set flag in ISR |
legstar85 | 3:80c1eba78f9b | 126 | } |
legstar85 | 3:80c1eba78f9b | 127 | |
legstar85 | 7:d77e95505e43 | 128 | void ButtonBack_isr() // ButtonB event-triggered interrupt |
legstar85 | 7:d77e95505e43 | 129 | { |
legstar85 | 7:d77e95505e43 | 130 | g_ButtonBack_flag = 1; // set flag in ISR |
legstar85 | 7:d77e95505e43 | 131 | } |
legstar85 | 7:d77e95505e43 | 132 | |
legstar85 | 3:80c1eba78f9b | 133 | void sw2_isr() // SW2 event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 134 | { |
legstar85 | 3:80c1eba78f9b | 135 | g_sw2_flag = 1; // set flag in ISR |
legstar85 | 5:eebd36a47049 | 136 | } |
legstar85 | 5:eebd36a47049 | 137 | |
legstar85 | 5:eebd36a47049 | 138 | void menu_timer_isr() |
legstar85 | 5:eebd36a47049 | 139 | { |
legstar85 | 6:ebf4784ce92b | 140 | g_menu_timer_flag = 1; // set flag in ISR |
legstar85 | 6:ebf4784ce92b | 141 | } |
legstar85 | 6:ebf4784ce92b | 142 | |
legstar85 | 9:d71b92c916f8 | 143 | void OnStartup() // Run some start up display |
legstar85 | 6:ebf4784ce92b | 144 | { |
legstar85 | 9:d71b92c916f8 | 145 | Buzzer.period(1.0/659.0); // Welcome sounds from Piezo |
legstar85 | 9:d71b92c916f8 | 146 | Buzzer = 0.5; // |
legstar85 | 9:d71b92c916f8 | 147 | wait(0.5); // |
legstar85 | 9:d71b92c916f8 | 148 | Buzzer.period(1.0/494.0); // |
legstar85 | 9:d71b92c916f8 | 149 | Buzzer = 0.5; // |
legstar85 | 9:d71b92c916f8 | 150 | wait(0.5); // |
legstar85 | 9:d71b92c916f8 | 151 | Buzzer.period(1.0/554.0); // |
legstar85 | 9:d71b92c916f8 | 152 | Buzzer = 0.5; // |
legstar85 | 9:d71b92c916f8 | 153 | wait(0.5); // |
legstar85 | 9:d71b92c916f8 | 154 | Buzzer = 0; // Turn off welcome sounds |
legstar85 | 9:d71b92c916f8 | 155 | lcd.clear(); // Clear buffer at start of every loop |
legstar85 | 9:d71b92c916f8 | 156 | 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 | 157 | lcd.printString(" Smart Cold",0,1); // Just a welcome message before auto moving to main menu |
legstar85 | 7:d77e95505e43 | 158 | lcd.printString(" Storage",0,2); // |
legstar85 | 7:d77e95505e43 | 159 | lcd.printString(" Monitoring",0,3); // |
legstar85 | 10:357b3329620f | 160 | lcd.printString("V10 - Jan 2022",0,4); // |
legstar85 | 7:d77e95505e43 | 161 | lcd.printString("--------------",0,5); // |
legstar85 | 9:d71b92c916f8 | 162 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 9:d71b92c916f8 | 163 | wait(5.0); // Leave welcome screen on for designated amount of time |
legstar85 | 9:d71b92c916f8 | 164 | lcd.clear(); // Clear buffer at start of every loop |
legstar85 | 9:d71b92c916f8 | 165 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 8:07323fcab6d1 | 166 | lcd.printString("--------------",0,0); // |
legstar85 | 8:07323fcab6d1 | 167 | lcd.printString(" Use Joystick",0,1); // Instruction for use of menu |
legstar85 | 8:07323fcab6d1 | 168 | lcd.printString(" To Navigate",0,2); // |
legstar85 | 8:07323fcab6d1 | 169 | lcd.printString("",0,3); // Blank Line |
legstar85 | 8:07323fcab6d1 | 170 | lcd.printString(" A = Select",0,4); // |
legstar85 | 8:07323fcab6d1 | 171 | lcd.printString("--------------",0,5); // |
legstar85 | 9:d71b92c916f8 | 172 | lcd.refresh(); // Need to refresh display after setting pixels or writing strings |
legstar85 | 7:d77e95505e43 | 173 | wait(5.0); // |
legstar85 | 9:d71b92c916f8 | 174 | init_PCB(); // Ran again to ensure all LED's etc are turned off |
legstar85 | 5:eebd36a47049 | 175 | } |
legstar85 | 5:eebd36a47049 | 176 | |
legstar85 | 9:d71b92c916f8 | 177 | enum EMenuState // An enum controlling the current state of the display. |
legstar85 | 6:ebf4784ce92b | 178 | { |
legstar85 | 9:d71b92c916f8 | 179 | MENUSTATE_Main, // Defining each menu state to be called upon later |
legstar85 | 9:d71b92c916f8 | 180 | MENUSTATE_Monitor, // Defining each menu state to be called upon later |
legstar85 | 9:d71b92c916f8 | 181 | MENUSTATE_OneOff, // Defining each menu state to be called upon later |
legstar85 | 9:d71b92c916f8 | 182 | MENUSTATE_About, // Defining each menu state to be called upon later |
legstar85 | 9:d71b92c916f8 | 183 | MENUSTATE_Author, // Defining each menu state to be called upon later |
legstar85 | 6:ebf4784ce92b | 184 | |
legstar85 | 6:ebf4784ce92b | 185 | MENUSTATTE_Num, // This is a special enum value that just gives is a way to get the number of elements in the enum. |
legstar85 | 6:ebf4784ce92b | 186 | }; |
legstar85 | 6:ebf4784ce92b | 187 | |
legstar85 | 6:ebf4784ce92b | 188 | void Run() |
legstar85 | 5:eebd36a47049 | 189 | { |
legstar85 | 9:d71b92c916f8 | 190 | int MenuState; // |
legstar85 | 9:d71b92c916f8 | 191 | int SelectedItem = 0; // |
legstar85 | 9:d71b92c916f8 | 192 | int NumMenuItems = 1; // |
legstar85 | 6:ebf4784ce92b | 193 | |
legstar85 | 7:d77e95505e43 | 194 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
legstar85 | 7:d77e95505e43 | 195 | |
legstar85 | 6:ebf4784ce92b | 196 | while(1){ |
legstar85 | 9:d71b92c916f8 | 197 | if (g_menu_timer_flag){ // |
legstar85 | 9:d71b92c916f8 | 198 | g_menu_timer_flag = 0; // |
legstar85 | 5:eebd36a47049 | 199 | |
legstar85 | 6:ebf4784ce92b | 200 | bool bAButtonWasPressed = g_ButtonA_flag; // Get the value of the input flags and reset them |
legstar85 | 9:d71b92c916f8 | 201 | bool bBButtonWasPressed = g_ButtonB_flag; // Get the value of the input flags and reset them |
legstar85 | 9:d71b92c916f8 | 202 | g_ButtonA_flag = 0; // |
legstar85 | 9:d71b92c916f8 | 203 | g_ButtonB_flag = 0; // |
legstar85 | 6:ebf4784ce92b | 204 | |
legstar85 | 5:eebd36a47049 | 205 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 6:ebf4784ce92b | 206 | |
legstar85 | 6:ebf4784ce92b | 207 | int NewMenuState = MENUSTATTE_Num; // The new menu we want to transition to, if any. |
legstar85 | 6:ebf4784ce92b | 208 | |
legstar85 | 6:ebf4784ce92b | 209 | switch(MenuState) // Update and Draw whichever menu we're on. |
legstar85 | 6:ebf4784ce92b | 210 | { |
legstar85 | 9:d71b92c916f8 | 211 | case MENUSTATE_Main: // |
legstar85 | 6:ebf4784ce92b | 212 | { |
legstar85 | 6:ebf4784ce92b | 213 | |
legstar85 | 7:d77e95505e43 | 214 | NumMenuItems = 4; // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 215 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 6:ebf4784ce92b | 216 | { |
legstar85 | 6:ebf4784ce92b | 217 | |
legstar85 | 6:ebf4784ce92b | 218 | SelectedItem = 0; // Something has gone wrong, reset selected item. |
legstar85 | 6:ebf4784ce92b | 219 | } |
legstar85 | 9:d71b92c916f8 | 220 | lcd.printString("---- MENU ----",0,0); // Menu title and selectable options |
legstar85 | 9:d71b92c916f8 | 221 | lcd.printString("M1 - Monitor",0,1); // Printed to the LCD screen |
legstar85 | 9:d71b92c916f8 | 222 | lcd.printString("M2 - One-off",0,2); // |
legstar85 | 9:d71b92c916f8 | 223 | lcd.printString("About",0,3); // |
legstar85 | 9:d71b92c916f8 | 224 | lcd.printString("Author",0,4); // |
legstar85 | 6:ebf4784ce92b | 225 | |
legstar85 | 6:ebf4784ce92b | 226 | if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen. |
legstar85 | 6:ebf4784ce92b | 227 | { |
legstar85 | 8:07323fcab6d1 | 228 | if(SelectedItem == 0) // If 0 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 229 | { // Actually 0 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 230 | NewMenuState = MENUSTATE_Monitor; // |
legstar85 | 6:ebf4784ce92b | 231 | } |
legstar85 | 8:07323fcab6d1 | 232 | else if(SelectedItem == 1) // If 1 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 233 | { // Actually 1 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 234 | NewMenuState = MENUSTATE_OneOff; // |
legstar85 | 6:ebf4784ce92b | 235 | } |
legstar85 | 8:07323fcab6d1 | 236 | else if(SelectedItem == 2) // If 2 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 237 | { // Actually 2 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 238 | NewMenuState = MENUSTATE_About; // |
legstar85 | 6:ebf4784ce92b | 239 | } |
legstar85 | 8:07323fcab6d1 | 240 | else if(SelectedItem == 3) // If 3 line is selected, move to detailed menu |
legstar85 | 8:07323fcab6d1 | 241 | { // Actually 3 line + 1, see circle draw and selection below |
legstar85 | 9:d71b92c916f8 | 242 | NewMenuState = MENUSTATE_Author; // |
legstar85 | 7:d77e95505e43 | 243 | } |
legstar85 | 6:ebf4784ce92b | 244 | } |
legstar85 | 6:ebf4784ce92b | 245 | } |
legstar85 | 9:d71b92c916f8 | 246 | break; // |
legstar85 | 9:d71b92c916f8 | 247 | case MENUSTATE_Monitor: // Call constant measurement menu following top menu selection |
legstar85 | 6:ebf4784ce92b | 248 | { |
legstar85 | 6:ebf4784ce92b | 249 | |
legstar85 | 8:07323fcab6d1 | 250 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 6:ebf4784ce92b | 251 | |
legstar85 | 9:d71b92c916f8 | 252 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 6:ebf4784ce92b | 253 | { |
legstar85 | 6:ebf4784ce92b | 254 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 6:ebf4784ce92b | 255 | } |
legstar85 | 6:ebf4784ce92b | 256 | |
legstar85 | 7:d77e95505e43 | 257 | float T = tmp102.get_temperature(); // read temperature and print to lcd |
legstar85 | 9:d71b92c916f8 | 258 | pc.printf("T = %f K\n",T); // Print to serial - allows testing without device attached |
legstar85 | 7:d77e95505e43 | 259 | 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 | 260 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
legstar85 | 9:d71b92c916f8 | 261 | lcd.printString("-- Constant --",0,0); // |
legstar85 | 9:d71b92c916f8 | 262 | lcd.printString("- Monitoring -",0,1); // |
legstar85 | 7:d77e95505e43 | 263 | lcd.printString(buffer,0,3); // display on screen |
legstar85 | 9:d71b92c916f8 | 264 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 7:d77e95505e43 | 265 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 9:d71b92c916f8 | 266 | wait(0.5); // |
legstar85 | 8:07323fcab6d1 | 267 | |
legstar85 | 8:07323fcab6d1 | 268 | float high_temp = tmp102.get_temperature(); // read temperature |
legstar85 | 9:d71b92c916f8 | 269 | if (high_temp >= 25) { // High temp alarm condition - in real world would be lot lower!! |
legstar85 | 9:d71b92c916f8 | 270 | LED01 = 0; // Light LED01 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 9:d71b92c916f8 | 271 | LED02 = 0; // Light LED02 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 9:d71b92c916f8 | 272 | LED03 = 0; // Light LED03 if temperature is over specified - Simulated starting of cold blowers |
legstar85 | 8:07323fcab6d1 | 273 | } |
legstar85 | 8:07323fcab6d1 | 274 | else { |
legstar85 | 9:d71b92c916f8 | 275 | LED01 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 9:d71b92c916f8 | 276 | LED02 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 9:d71b92c916f8 | 277 | LED03 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers |
legstar85 | 9:d71b92c916f8 | 278 | Buzzer = 0; // |
legstar85 | 9:d71b92c916f8 | 279 | } |
legstar85 | 9:d71b92c916f8 | 280 | |
legstar85 | 9:d71b92c916f8 | 281 | float low_temp = tmp102.get_temperature(); // Read temperature |
legstar85 | 9:d71b92c916f8 | 282 | if (low_temp <= 22) { // Low temp alarm condition - in real world would be lot lower!! |
legstar85 | 9:d71b92c916f8 | 283 | LED04 = 0; // Light LED04 if temperature is over specified - Simulated starting of heaters |
legstar85 | 9:d71b92c916f8 | 284 | LED05 = 0; // Light LED05 if temperature is over specified - Simulated starting of heaters |
legstar85 | 9:d71b92c916f8 | 285 | LED06 = 0; // Light LED06 if temperature is over specified - Simulated starting of heaters |
legstar85 | 9:d71b92c916f8 | 286 | } |
legstar85 | 9:d71b92c916f8 | 287 | else { |
legstar85 | 9:d71b92c916f8 | 288 | LED04 = 1; // LED04 off if temperature is above specified - Simulated stopping of heaters |
legstar85 | 9:d71b92c916f8 | 289 | LED05 = 1; // LED05 off if temperature is above specified - Simulated stopping of heaters |
legstar85 | 9:d71b92c916f8 | 290 | LED06 = 1; // LED06 off if temperature is above specified - Simulated stopping of heaters |
legstar85 | 8:07323fcab6d1 | 291 | } |
legstar85 | 7:d77e95505e43 | 292 | |
legstar85 | 9:d71b92c916f8 | 293 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 6:ebf4784ce92b | 294 | { |
legstar85 | 9:d71b92c916f8 | 295 | if(SelectedItem == 0) // |
legstar85 | 6:ebf4784ce92b | 296 | { |
legstar85 | 9:d71b92c916f8 | 297 | NewMenuState = MENUSTATE_Main; // Transition back to the main menu |
legstar85 | 9:d71b92c916f8 | 298 | LED01 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 9:d71b92c916f8 | 299 | LED02 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 9:d71b92c916f8 | 300 | LED03 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 9:d71b92c916f8 | 301 | LED04 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 9:d71b92c916f8 | 302 | LED05 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 9:d71b92c916f8 | 303 | LED06 = 1; // Turn off LED upon transition back to main menu |
legstar85 | 6:ebf4784ce92b | 304 | } |
legstar85 | 6:ebf4784ce92b | 305 | } |
legstar85 | 6:ebf4784ce92b | 306 | } |
legstar85 | 6:ebf4784ce92b | 307 | break; |
legstar85 | 9:d71b92c916f8 | 308 | case MENUSTATE_OneOff: // Call a one off measuremnt menu following top menu selection |
legstar85 | 6:ebf4784ce92b | 309 | { |
legstar85 | 6:ebf4784ce92b | 310 | |
legstar85 | 8:07323fcab6d1 | 311 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 312 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 6:ebf4784ce92b | 313 | { |
legstar85 | 7:d77e95505e43 | 314 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 6:ebf4784ce92b | 315 | } |
legstar85 | 6:ebf4784ce92b | 316 | |
legstar85 | 8:07323fcab6d1 | 317 | float T = tmp102.get_temperature(); // read temperature and print to lcd |
legstar85 | 9:d71b92c916f8 | 318 | pc.printf("T = %f K\n",T); // Print to serial - allows testing without device attached |
legstar85 | 8:07323fcab6d1 | 319 | 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 | 8:07323fcab6d1 | 320 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
legstar85 | 9:d71b92c916f8 | 321 | lcd.printString("-- One-Off --",0,0); // |
legstar85 | 9:d71b92c916f8 | 322 | lcd.printString("-- Measure --",0,1); // |
legstar85 | 8:07323fcab6d1 | 323 | lcd.printString(buffer,0,3); // display on screen |
legstar85 | 9:d71b92c916f8 | 324 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 6:ebf4784ce92b | 325 | |
legstar85 | 9:d71b92c916f8 | 326 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 6:ebf4784ce92b | 327 | { |
legstar85 | 9:d71b92c916f8 | 328 | if(SelectedItem == 0) // |
legstar85 | 6:ebf4784ce92b | 329 | { |
legstar85 | 8:07323fcab6d1 | 330 | NewMenuState = MENUSTATE_Main; // Take us back to top menu |
legstar85 | 6:ebf4784ce92b | 331 | } |
legstar85 | 6:ebf4784ce92b | 332 | } |
legstar85 | 6:ebf4784ce92b | 333 | |
legstar85 | 6:ebf4784ce92b | 334 | } |
legstar85 | 6:ebf4784ce92b | 335 | break; |
legstar85 | 9:d71b92c916f8 | 336 | case MENUSTATE_About: // Call About menu following top menu selection |
legstar85 | 6:ebf4784ce92b | 337 | { |
legstar85 | 6:ebf4784ce92b | 338 | |
legstar85 | 8:07323fcab6d1 | 339 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 340 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 6:ebf4784ce92b | 341 | { |
legstar85 | 6:ebf4784ce92b | 342 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 6:ebf4784ce92b | 343 | } |
legstar85 | 6:ebf4784ce92b | 344 | |
legstar85 | 9:d71b92c916f8 | 345 | lcd.printString("--- About ---",0,0); // |
legstar85 | 9:d71b92c916f8 | 346 | lcd.printString("ELE3006M - IoT",0,1); // |
legstar85 | 9:d71b92c916f8 | 347 | lcd.printString(" Project",0,2); // |
legstar85 | 9:d71b92c916f8 | 348 | lcd.printString("Uni of Lincoln",0,3); // |
legstar85 | 9:d71b92c916f8 | 349 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 9:d71b92c916f8 | 350 | lcd.refresh(); // |
legstar85 | 6:ebf4784ce92b | 351 | |
legstar85 | 9:d71b92c916f8 | 352 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 6:ebf4784ce92b | 353 | { |
legstar85 | 9:d71b92c916f8 | 354 | if(SelectedItem == 0) // |
legstar85 | 6:ebf4784ce92b | 355 | { |
legstar85 | 9:d71b92c916f8 | 356 | NewMenuState = MENUSTATE_Main; // Transition back to Main Menu |
legstar85 | 6:ebf4784ce92b | 357 | } |
legstar85 | 6:ebf4784ce92b | 358 | } |
legstar85 | 6:ebf4784ce92b | 359 | } |
legstar85 | 6:ebf4784ce92b | 360 | break; |
legstar85 | 9:d71b92c916f8 | 361 | case MENUSTATE_Author: // Call Author menu following top menu selection |
legstar85 | 7:d77e95505e43 | 362 | { |
legstar85 | 7:d77e95505e43 | 363 | |
legstar85 | 8:07323fcab6d1 | 364 | NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc. |
legstar85 | 9:d71b92c916f8 | 365 | if(SelectedItem >= NumMenuItems) // |
legstar85 | 7:d77e95505e43 | 366 | { |
legstar85 | 7:d77e95505e43 | 367 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 7:d77e95505e43 | 368 | } |
legstar85 | 7:d77e95505e43 | 369 | |
legstar85 | 9:d71b92c916f8 | 370 | lcd.printString("--- Author ---",0,0); // |
legstar85 | 9:d71b92c916f8 | 371 | lcd.printString("David Leaming ",0,1); // |
legstar85 | 9:d71b92c916f8 | 372 | lcd.printString(" 25574043 ",0,2); // |
legstar85 | 9:d71b92c916f8 | 373 | lcd.printString(" VolkerRail",0,3); // |
legstar85 | 9:d71b92c916f8 | 374 | lcd.printString(" 'A' to Menu",0,5); // |
legstar85 | 7:d77e95505e43 | 375 | |
legstar85 | 9:d71b92c916f8 | 376 | if(bAButtonWasPressed) // Check if button was pressed |
legstar85 | 7:d77e95505e43 | 377 | { |
legstar85 | 9:d71b92c916f8 | 378 | if(SelectedItem == 0) // |
legstar85 | 7:d77e95505e43 | 379 | { |
legstar85 | 8:07323fcab6d1 | 380 | NewMenuState = MENUSTATE_Main; // Take us back to top menu |
legstar85 | 7:d77e95505e43 | 381 | } |
legstar85 | 7:d77e95505e43 | 382 | } |
legstar85 | 7:d77e95505e43 | 383 | } |
legstar85 | 7:d77e95505e43 | 384 | break; |
legstar85 | 7:d77e95505e43 | 385 | |
legstar85 | 6:ebf4784ce92b | 386 | default: |
legstar85 | 6:ebf4784ce92b | 387 | { |
legstar85 | 6:ebf4784ce92b | 388 | NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. |
legstar85 | 6:ebf4784ce92b | 389 | } |
legstar85 | 6:ebf4784ce92b | 390 | }; |
legstar85 | 5:eebd36a47049 | 391 | |
legstar85 | 6:ebf4784ce92b | 392 | if(NewMenuState != MENUSTATTE_Num) // If we have requested a new menu state. |
legstar85 | 6:ebf4784ce92b | 393 | { |
legstar85 | 8:07323fcab6d1 | 394 | printf("Transitioning to MenuState: %i\n", NewMenuState); // Observe on serial port - ensure transition to correct screen |
legstar85 | 6:ebf4784ce92b | 395 | |
legstar85 | 6:ebf4784ce92b | 396 | MenuState = NewMenuState; // We want to transition the menu to a new state. |
legstar85 | 6:ebf4784ce92b | 397 | |
legstar85 | 6:ebf4784ce92b | 398 | // Do any bookkeeping needed when moving to new state. |
legstar85 | 6:ebf4784ce92b | 399 | SelectedItem = 0; // Reset the selected item. |
legstar85 | 6:ebf4784ce92b | 400 | |
legstar85 | 6:ebf4784ce92b | 401 | lcd.clear(); // Clear the display for one frame on state transition. |
legstar85 | 6:ebf4784ce92b | 402 | } // Could do something else like invert colour etc here. |
legstar85 | 6:ebf4784ce92b | 403 | else |
legstar85 | 6:ebf4784ce92b | 404 | { |
legstar85 | 6:ebf4784ce92b | 405 | |
legstar85 | 7:d77e95505e43 | 406 | // If we have not selected to move to a new menu. |
legstar85 | 7:d77e95505e43 | 407 | unsigned int SelectionMarkerRadius = 4 ; // Draw a marker circle at end of line to show which is the currently selected item. |
legstar85 | 7:d77e95505e43 | 408 | unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius); |
legstar85 | 8:07323fcab6d1 | 409 | unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1); // +1 because of the menu title being on first row |
legstar85 | 9:d71b92c916f8 | 410 | lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK); // Fill the circle black so it can be seen easily |
legstar85 | 6:ebf4784ce92b | 411 | |
legstar85 | 6:ebf4784ce92b | 412 | // Handle Joystick Input |
legstar85 | 6:ebf4784ce92b | 413 | switch (joystick.get_direction()) { // Call to check direction joystick is pointing |
legstar85 | 9:d71b92c916f8 | 414 | printf("Direction = %i\n"); // Print direction to serial |
legstar85 | 6:ebf4784ce92b | 415 | case N: // |
legstar85 | 6:ebf4784ce92b | 416 | SelectedItem--; // |
legstar85 | 6:ebf4784ce92b | 417 | break; // |
legstar85 | 6:ebf4784ce92b | 418 | case S: // |
legstar85 | 6:ebf4784ce92b | 419 | SelectedItem++; // |
legstar85 | 6:ebf4784ce92b | 420 | break; // |
legstar85 | 6:ebf4784ce92b | 421 | } |
legstar85 | 6:ebf4784ce92b | 422 | |
legstar85 | 9:d71b92c916f8 | 423 | if(SelectedItem < 0) // Wrap the selection around to the start/end of the menu if it goes below 0 or above NumMenuItems. |
legstar85 | 6:ebf4784ce92b | 424 | { |
legstar85 | 9:d71b92c916f8 | 425 | SelectedItem = NumMenuItems - 1; // |
legstar85 | 6:ebf4784ce92b | 426 | } |
legstar85 | 9:d71b92c916f8 | 427 | else if(SelectedItem >= NumMenuItems) // |
legstar85 | 6:ebf4784ce92b | 428 | { |
legstar85 | 9:d71b92c916f8 | 429 | SelectedItem = 0; // |
legstar85 | 6:ebf4784ce92b | 430 | } |
legstar85 | 5:eebd36a47049 | 431 | } |
legstar85 | 5:eebd36a47049 | 432 | |
legstar85 | 6:ebf4784ce92b | 433 | lcd.refresh(); //Finally update the display. |
legstar85 | 6:ebf4784ce92b | 434 | } |
legstar85 | 6:ebf4784ce92b | 435 | } |
legstar85 | 5:eebd36a47049 | 436 | } |
legstar85 | 6:ebf4784ce92b | 437 | |
legstar85 | 5:eebd36a47049 | 438 |