Addition of FSM to try and set initial starting temperature for use throughout project. Additional menu option. Joystick input 'bouncing' during FSM use - to be debugged

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

Committer:
legstar85
Date:
Thu Jan 20 13:42:30 2022 +0000
Revision:
11:a6eec09b546e
Parent:
10:357b3329620f
Addition of FSM to try and set initial starting temperature for use throughout project. Additional menu option. Joystick input 'bouncing' during FSM use - to be debugged

Who changed what in which revision?

UserRevisionLine numberNew 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 11:a6eec09b546e 26 struct State { // Struct for state
legstar85 11:a6eec09b546e 27 int output; // output value
legstar85 11:a6eec09b546e 28 float time; // time in state
legstar85 11:a6eec09b546e 29 int nextState[9]; // array of next states
legstar85 11:a6eec09b546e 30 };
legstar85 11:a6eec09b546e 31
legstar85 11:a6eec09b546e 32 State fsm[11] = {
legstar85 11:a6eec09b546e 33 {15,0.5,{0,1,0,0,0,10,0,0,0}}, // Start Temp = 15 Degrees
legstar85 11:a6eec09b546e 34 {16,0.5,{1,2,1,1,1,0,1,1,1}}, // 1
legstar85 11:a6eec09b546e 35 {17,0.5,{2,3,2,2,2,1,2,2,2}}, //
legstar85 11:a6eec09b546e 36 {18,0.5,{3,4,3,3,3,2,3,3,3}}, //
legstar85 11:a6eec09b546e 37 {19,0.5,{4,5,4,4,4,3,4,4,4}}, //
legstar85 11:a6eec09b546e 38 {20,0.5,{5,6,5,5,5,4,5,5,5}}, //
legstar85 11:a6eec09b546e 39 {21,0.5,{6,7,6,6,6,5,6,6,6}}, //
legstar85 11:a6eec09b546e 40 {22,0.5,{7,8,7,7,7,6,7,7,7}}, //
legstar85 11:a6eec09b546e 41 {23,0.5,{8,9,8,8,8,7,8,8,8}}, //
legstar85 11:a6eec09b546e 42 {24,0.5,{9,10,9,9,9,8,9,9,9}}, //
legstar85 11:a6eec09b546e 43 {25,0.5,{10,1,10,10,10,9,10,10,10}} //
legstar85 11:a6eec09b546e 44 };
legstar85 11:a6eec09b546e 45
legstar85 5:eebd36a47049 46 Ticker ticker_menu; // Create Menu ticker object
legstar85 5:eebd36a47049 47
legstar85 2:e0091b5311f1 48 DigitalOut r_led(LED_RED); // K64F on-board LEDs
legstar85 2:e0091b5311f1 49 DigitalOut g_led(LED_GREEN); // K64F on-board LEDs
legstar85 2:e0091b5311f1 50 DigitalOut b_led(LED_BLUE); // K64F on-board LEDs
eencae 0:21a200b880d7 51
legstar85 8:07323fcab6d1 52 PwmOut LED01 (PTA1); // PCB Surface Mounted LED's - LED1
legstar85 8:07323fcab6d1 53 PwmOut LED02 (PTA2); // PCB Surface Mounted LED's - LED2
legstar85 8:07323fcab6d1 54 PwmOut LED03 (PTC2); // PCB Surface Mounted LED's - LED3
legstar85 8:07323fcab6d1 55 PwmOut LED04 (PTC3); // PCB Surface Mounted LED's - LED4
legstar85 8:07323fcab6d1 56 PwmOut LED05 (PTC4); // PCB Surface Mounted LED's - LED5
legstar85 8:07323fcab6d1 57 PwmOut LED06 (PTD3); // PCB Surface Mounted LED's - LED6
legstar85 8:07323fcab6d1 58
legstar85 8:07323fcab6d1 59 PwmOut Buzzer (PTC10); // PCB Surface Mounted Piezo Buzzer
legstar85 3:80c1eba78f9b 60
legstar85 2:e0091b5311f1 61 InterruptIn sw2(SW2); // K64F on-board switches
legstar85 2:e0091b5311f1 62 InterruptIn sw3(SW3); // K64F on-board switches
legstar85 3:80c1eba78f9b 63 InterruptIn ButtonA (PTB9); // PCB Button - A
legstar85 3:80c1eba78f9b 64 InterruptIn ButtonB (PTD0); // PCB Button - B
legstar85 7:d77e95505e43 65 InterruptIn ButtonBack (PTB10); // PCB Button - Back
legstar85 3:80c1eba78f9b 66
legstar85 5:eebd36a47049 67 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 68 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 69 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 70 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 71 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 72 volatile int option = 0; // Menu option selection based on joystick direction
legstar85 11:a6eec09b546e 73 volatile int g_state = 0; //
legstar85 11:a6eec09b546e 74 volatile int g_StartTemp = 0; //
legstar85 2:e0091b5311f1 75
legstar85 2:e0091b5311f1 76 void error(); // error function hangs flashing an LED
legstar85 2:e0091b5311f1 77 void init_serial(); // setup serial port
legstar85 2:e0091b5311f1 78 void init_K64F(); // set-up the on-board LEDs and switches
legstar85 3:80c1eba78f9b 79 void init_PCB(); // set-up the PCB LEDs and buttons
legstar85 9:d71b92c916f8 80 void ButtonA_isr(); //
legstar85 9:d71b92c916f8 81 void ButtonB_isr(); //
legstar85 9:d71b92c916f8 82 void ButtonBack_isr(); //
legstar85 9:d71b92c916f8 83 void sw2_isr(); //
legstar85 9:d71b92c916f8 84 void menu_timer_isr(); //
legstar85 6:ebf4784ce92b 85 void OnStartup(); //
legstar85 6:ebf4784ce92b 86 void Run(); //
legstar85 6:ebf4784ce92b 87 void ConstantMonitoring(); //
legstar85 11:a6eec09b546e 88 void StartTemp(); //
eencae 0:21a200b880d7 89
eencae 0:21a200b880d7 90 int main()
eencae 0:21a200b880d7 91 {
legstar85 2:e0091b5311f1 92 init_K64F(); // initialise the board
legstar85 2:e0091b5311f1 93 init_serial(); // initialise the serial port
legstar85 3:80c1eba78f9b 94 init_PCB(); // initialise the PCB
legstar85 3:80c1eba78f9b 95
legstar85 2:e0091b5311f1 96 tmp102.init(); // call the sensor init method using dot syntax
legstar85 2:e0091b5311f1 97 lcd.init(); // initialise display
legstar85 3:80c1eba78f9b 98 joystick.init(); // initialise joystick
legstar85 3:80c1eba78f9b 99
legstar85 5:eebd36a47049 100 ticker_menu.attach(&menu_timer_isr,0.2); // Attach ticker for the Joystick
legstar85 5:eebd36a47049 101
legstar85 3:80c1eba78f9b 102 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 103 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 104 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 105
legstar85 2:e0091b5311f1 106 lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0
eencae 0:21a200b880d7 107
legstar85 11:a6eec09b546e 108 OnStartup(); // Call intro screen
legstar85 9:d71b92c916f8 109 Run(); // Call main-menu and functions
legstar85 5:eebd36a47049 110 }
eencae 0:21a200b880d7 111
eencae 0:21a200b880d7 112 void init_serial() {
legstar85 2:e0091b5311f1 113 pc.baud(115200); // set to highest baud - ensure terminal software matches
eencae 0:21a200b880d7 114 }
eencae 0:21a200b880d7 115
eencae 0:21a200b880d7 116 void init_K64F()
eencae 0:21a200b880d7 117 {
legstar85 2:e0091b5311f1 118 r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
legstar85 2:e0091b5311f1 119 g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
legstar85 2:e0091b5311f1 120 b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off.
eencae 0:21a200b880d7 121
legstar85 2:e0091b5311f1 122 sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down
legstar85 2:e0091b5311f1 123 sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn
eencae 0:21a200b880d7 124 }
legstar85 3:80c1eba78f9b 125
legstar85 3:80c1eba78f9b 126 void init_PCB ()
legstar85 3:80c1eba78f9b 127 {
legstar85 3:80c1eba78f9b 128 LED01 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 129 LED02 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 130 LED03 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 131 LED04 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 132 LED05 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 133 LED06 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
legstar85 9:d71b92c916f8 134
legstar85 9:d71b92c916f8 135 Buzzer = 0; // Ensure Piezo Buzzer is off
legstar85 3:80c1eba78f9b 136
legstar85 3:80c1eba78f9b 137 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 138 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 139 }
legstar85 3:80c1eba78f9b 140
legstar85 3:80c1eba78f9b 141 void ButtonA_isr() // ButtonA event-triggered interrupt
legstar85 3:80c1eba78f9b 142 {
legstar85 3:80c1eba78f9b 143 g_ButtonA_flag = 1; // set flag in ISR
legstar85 3:80c1eba78f9b 144 }
legstar85 3:80c1eba78f9b 145
legstar85 3:80c1eba78f9b 146 void ButtonB_isr() // ButtonB event-triggered interrupt
legstar85 3:80c1eba78f9b 147 {
legstar85 3:80c1eba78f9b 148 g_ButtonB_flag = 1; // set flag in ISR
legstar85 3:80c1eba78f9b 149 }
legstar85 3:80c1eba78f9b 150
legstar85 7:d77e95505e43 151 void ButtonBack_isr() // ButtonB event-triggered interrupt
legstar85 7:d77e95505e43 152 {
legstar85 7:d77e95505e43 153 g_ButtonBack_flag = 1; // set flag in ISR
legstar85 7:d77e95505e43 154 }
legstar85 7:d77e95505e43 155
legstar85 3:80c1eba78f9b 156 void sw2_isr() // SW2 event-triggered interrupt
legstar85 3:80c1eba78f9b 157 {
legstar85 3:80c1eba78f9b 158 g_sw2_flag = 1; // set flag in ISR
legstar85 5:eebd36a47049 159 }
legstar85 5:eebd36a47049 160
legstar85 5:eebd36a47049 161 void menu_timer_isr()
legstar85 5:eebd36a47049 162 {
legstar85 6:ebf4784ce92b 163 g_menu_timer_flag = 1; // set flag in ISR
legstar85 6:ebf4784ce92b 164 }
legstar85 6:ebf4784ce92b 165
legstar85 9:d71b92c916f8 166 void OnStartup() // Run some start up display
legstar85 6:ebf4784ce92b 167 {
legstar85 11:a6eec09b546e 168 //Buzzer.period(1.0/659.0); // Welcome sounds from Piezo
legstar85 11:a6eec09b546e 169 //Buzzer = 0.5; //
legstar85 11:a6eec09b546e 170 // wait(0.5); //
legstar85 11:a6eec09b546e 171 //Buzzer.period(1.0/494.0); //
legstar85 11:a6eec09b546e 172 // Buzzer = 0.5; //
legstar85 11:a6eec09b546e 173 // wait(0.5); //
legstar85 11:a6eec09b546e 174 //Buzzer.period(1.0/554.0); //
legstar85 11:a6eec09b546e 175 // Buzzer = 0.5; //
legstar85 11:a6eec09b546e 176 //wait(0.5); //
legstar85 11:a6eec09b546e 177 // Buzzer = 0; // Turn off welcome sounds
legstar85 9:d71b92c916f8 178 lcd.clear(); // Clear buffer at start of every loop
legstar85 9:d71b92c916f8 179 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 180 lcd.printString(" Smart Cold",0,1); // Just a welcome message before auto moving to main menu
legstar85 7:d77e95505e43 181 lcd.printString(" Storage",0,2); //
legstar85 7:d77e95505e43 182 lcd.printString(" Monitoring",0,3); //
legstar85 11:a6eec09b546e 183 lcd.printString("V11 - Jan 2022",0,4); //
legstar85 7:d77e95505e43 184 lcd.printString("--------------",0,5); //
legstar85 9:d71b92c916f8 185 lcd.refresh(); // Need to refresh display after setting pixels or writing strings
legstar85 9:d71b92c916f8 186 wait(5.0); // Leave welcome screen on for designated amount of time
legstar85 9:d71b92c916f8 187 lcd.clear(); // Clear buffer at start of every loop
legstar85 9:d71b92c916f8 188 lcd.refresh(); // Need to refresh display after setting pixels or writing strings
legstar85 8:07323fcab6d1 189 lcd.printString("--------------",0,0); //
legstar85 8:07323fcab6d1 190 lcd.printString(" Use Joystick",0,1); // Instruction for use of menu
legstar85 8:07323fcab6d1 191 lcd.printString(" To Navigate",0,2); //
legstar85 8:07323fcab6d1 192 lcd.printString("",0,3); // Blank Line
legstar85 8:07323fcab6d1 193 lcd.printString(" A = Select",0,4); //
legstar85 8:07323fcab6d1 194 lcd.printString("--------------",0,5); //
legstar85 9:d71b92c916f8 195 lcd.refresh(); // Need to refresh display after setting pixels or writing strings
legstar85 7:d77e95505e43 196 wait(5.0); //
legstar85 9:d71b92c916f8 197 init_PCB(); // Ran again to ensure all LED's etc are turned off
legstar85 5:eebd36a47049 198 }
legstar85 5:eebd36a47049 199
legstar85 9:d71b92c916f8 200 enum EMenuState // An enum controlling the current state of the display.
legstar85 6:ebf4784ce92b 201 {
legstar85 11:a6eec09b546e 202 MENUSTATE_StartTemp, //
legstar85 9:d71b92c916f8 203 MENUSTATE_Main, // Defining each menu state to be called upon later
legstar85 9:d71b92c916f8 204 MENUSTATE_Monitor, // Defining each menu state to be called upon later
legstar85 9:d71b92c916f8 205 MENUSTATE_OneOff, // Defining each menu state to be called upon later
legstar85 9:d71b92c916f8 206 MENUSTATE_About, // Defining each menu state to be called upon later
legstar85 9:d71b92c916f8 207 MENUSTATE_Author, // Defining each menu state to be called upon later
legstar85 6:ebf4784ce92b 208
legstar85 6:ebf4784ce92b 209 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 210 };
legstar85 6:ebf4784ce92b 211
legstar85 6:ebf4784ce92b 212 void Run()
legstar85 5:eebd36a47049 213 {
legstar85 9:d71b92c916f8 214 int MenuState; //
legstar85 9:d71b92c916f8 215 int SelectedItem = 0; //
legstar85 9:d71b92c916f8 216 int NumMenuItems = 1; //
legstar85 6:ebf4784ce92b 217
legstar85 7:d77e95505e43 218 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
legstar85 7:d77e95505e43 219
legstar85 6:ebf4784ce92b 220 while(1){
legstar85 9:d71b92c916f8 221 if (g_menu_timer_flag){ //
legstar85 9:d71b92c916f8 222 g_menu_timer_flag = 0; //
legstar85 5:eebd36a47049 223
legstar85 6:ebf4784ce92b 224 bool bAButtonWasPressed = g_ButtonA_flag; // Get the value of the input flags and reset them
legstar85 9:d71b92c916f8 225 bool bBButtonWasPressed = g_ButtonB_flag; // Get the value of the input flags and reset them
legstar85 9:d71b92c916f8 226 g_ButtonA_flag = 0; //
legstar85 9:d71b92c916f8 227 g_ButtonB_flag = 0; //
legstar85 6:ebf4784ce92b 228
legstar85 5:eebd36a47049 229 lcd.clear(); // clear buffer at start of every loop
legstar85 6:ebf4784ce92b 230
legstar85 6:ebf4784ce92b 231 int NewMenuState = MENUSTATTE_Num; // The new menu we want to transition to, if any.
legstar85 11:a6eec09b546e 232
legstar85 6:ebf4784ce92b 233
legstar85 6:ebf4784ce92b 234 switch(MenuState) // Update and Draw whichever menu we're on.
legstar85 11:a6eec09b546e 235 {
legstar85 11:a6eec09b546e 236 case MENUSTATE_StartTemp: //
legstar85 11:a6eec09b546e 237 {
legstar85 11:a6eec09b546e 238
legstar85 11:a6eec09b546e 239 NumMenuItems = 1; // 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 11:a6eec09b546e 240 if(SelectedItem >= NumMenuItems) //
legstar85 11:a6eec09b546e 241 {
legstar85 11:a6eec09b546e 242
legstar85 11:a6eec09b546e 243 SelectedItem = 0; // Something has gone wrong, reset selected item.
legstar85 11:a6eec09b546e 244 }
legstar85 11:a6eec09b546e 245 Direction d = joystick.get_direction();
legstar85 11:a6eec09b546e 246
legstar85 11:a6eec09b546e 247 StartTemp();
legstar85 11:a6eec09b546e 248
legstar85 11:a6eec09b546e 249 float g_StartTemp = fsm[g_state].output; // read temperature and print to lcd
legstar85 11:a6eec09b546e 250 pc.printf("T = %f K\n",g_state); // Print to serial - allows testing without device attached
legstar85 11:a6eec09b546e 251 printf ("Joystick Direction Points = %i\n",d); //
legstar85 11:a6eec09b546e 252 printf ("State selected = %i\n", g_StartTemp); //
legstar85 11:a6eec09b546e 253 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 11:a6eec09b546e 254 if (length <= 14) // if string will fit on display (assuming printing at x=0)
legstar85 11:a6eec09b546e 255 lcd.printString("- Set Target -",0,0); //
legstar85 11:a6eec09b546e 256 lcd.printString("---- Temp ----",0,1); //
legstar85 11:a6eec09b546e 257 lcd.printString(buffer,0,3); // display on screen
legstar85 11:a6eec09b546e 258 lcd.printString("'A' to Select",0,5); //
legstar85 11:a6eec09b546e 259 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 11:a6eec09b546e 260 //
legstar85 11:a6eec09b546e 261
legstar85 11:a6eec09b546e 262 if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen.
legstar85 11:a6eec09b546e 263 {
legstar85 11:a6eec09b546e 264 if(SelectedItem == 0) // If 0 line is selected, move to detailed menu
legstar85 11:a6eec09b546e 265 { // Actually 0 line + 1, see circle draw and selection below
legstar85 11:a6eec09b546e 266 NewMenuState = MENUSTATE_Main; //
legstar85 11:a6eec09b546e 267 }
legstar85 11:a6eec09b546e 268 }
legstar85 11:a6eec09b546e 269 }
legstar85 11:a6eec09b546e 270 break; //
legstar85 9:d71b92c916f8 271 case MENUSTATE_Main: //
legstar85 6:ebf4784ce92b 272 {
legstar85 6:ebf4784ce92b 273
legstar85 7:d77e95505e43 274 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 275 if(SelectedItem >= NumMenuItems) //
legstar85 6:ebf4784ce92b 276 {
legstar85 6:ebf4784ce92b 277
legstar85 6:ebf4784ce92b 278 SelectedItem = 0; // Something has gone wrong, reset selected item.
legstar85 6:ebf4784ce92b 279 }
legstar85 9:d71b92c916f8 280 lcd.printString("---- MENU ----",0,0); // Menu title and selectable options
legstar85 9:d71b92c916f8 281 lcd.printString("M1 - Monitor",0,1); // Printed to the LCD screen
legstar85 9:d71b92c916f8 282 lcd.printString("M2 - One-off",0,2); //
legstar85 9:d71b92c916f8 283 lcd.printString("About",0,3); //
legstar85 9:d71b92c916f8 284 lcd.printString("Author",0,4); //
legstar85 6:ebf4784ce92b 285
legstar85 6:ebf4784ce92b 286 if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen.
legstar85 6:ebf4784ce92b 287 {
legstar85 8:07323fcab6d1 288 if(SelectedItem == 0) // If 0 line is selected, move to detailed menu
legstar85 8:07323fcab6d1 289 { // Actually 0 line + 1, see circle draw and selection below
legstar85 9:d71b92c916f8 290 NewMenuState = MENUSTATE_Monitor; //
legstar85 6:ebf4784ce92b 291 }
legstar85 8:07323fcab6d1 292 else if(SelectedItem == 1) // If 1 line is selected, move to detailed menu
legstar85 8:07323fcab6d1 293 { // Actually 1 line + 1, see circle draw and selection below
legstar85 9:d71b92c916f8 294 NewMenuState = MENUSTATE_OneOff; //
legstar85 6:ebf4784ce92b 295 }
legstar85 8:07323fcab6d1 296 else if(SelectedItem == 2) // If 2 line is selected, move to detailed menu
legstar85 8:07323fcab6d1 297 { // Actually 2 line + 1, see circle draw and selection below
legstar85 9:d71b92c916f8 298 NewMenuState = MENUSTATE_About; //
legstar85 6:ebf4784ce92b 299 }
legstar85 8:07323fcab6d1 300 else if(SelectedItem == 3) // If 3 line is selected, move to detailed menu
legstar85 8:07323fcab6d1 301 { // Actually 3 line + 1, see circle draw and selection below
legstar85 9:d71b92c916f8 302 NewMenuState = MENUSTATE_Author; //
legstar85 7:d77e95505e43 303 }
legstar85 6:ebf4784ce92b 304 }
legstar85 6:ebf4784ce92b 305 }
legstar85 9:d71b92c916f8 306 break; //
legstar85 9:d71b92c916f8 307 case MENUSTATE_Monitor: // Call constant measurement menu following top menu selection
legstar85 6:ebf4784ce92b 308 {
legstar85 6:ebf4784ce92b 309
legstar85 8:07323fcab6d1 310 NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc.
legstar85 6:ebf4784ce92b 311
legstar85 9:d71b92c916f8 312 if(SelectedItem >= NumMenuItems) //
legstar85 6:ebf4784ce92b 313 {
legstar85 6:ebf4784ce92b 314 NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu.
legstar85 6:ebf4784ce92b 315 }
legstar85 6:ebf4784ce92b 316
legstar85 7:d77e95505e43 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 7:d77e95505e43 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 7:d77e95505e43 320 if (length <= 14) // if string will fit on display (assuming printing at x=0)
legstar85 9:d71b92c916f8 321 lcd.printString("-- Constant --",0,0); //
legstar85 9:d71b92c916f8 322 lcd.printString("- Monitoring -",0,1); //
legstar85 7:d77e95505e43 323 lcd.printString(buffer,0,3); // display on screen
legstar85 9:d71b92c916f8 324 lcd.printString(" 'A' to Menu",0,5); //
legstar85 7:d77e95505e43 325 lcd.refresh(); // need to refresh display after setting pixels or writing strings
legstar85 9:d71b92c916f8 326 wait(0.5); //
legstar85 8:07323fcab6d1 327
legstar85 8:07323fcab6d1 328 float high_temp = tmp102.get_temperature(); // read temperature
legstar85 9:d71b92c916f8 329 if (high_temp >= 25) { // High temp alarm condition - in real world would be lot lower!!
legstar85 9:d71b92c916f8 330 LED01 = 0; // Light LED01 if temperature is over specified - Simulated starting of cold blowers
legstar85 9:d71b92c916f8 331 LED02 = 0; // Light LED02 if temperature is over specified - Simulated starting of cold blowers
legstar85 9:d71b92c916f8 332 LED03 = 0; // Light LED03 if temperature is over specified - Simulated starting of cold blowers
legstar85 11:a6eec09b546e 333 printf("WARNING - High Temp!! \n"); //
legstar85 8:07323fcab6d1 334 }
legstar85 8:07323fcab6d1 335 else {
legstar85 9:d71b92c916f8 336 LED01 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers
legstar85 9:d71b92c916f8 337 LED02 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers
legstar85 9:d71b92c916f8 338 LED03 = 1; // LED01 off if temperature is below specified - Simulated stopping of cold blowers
legstar85 9:d71b92c916f8 339 Buzzer = 0; //
legstar85 9:d71b92c916f8 340 }
legstar85 9:d71b92c916f8 341
legstar85 9:d71b92c916f8 342 float low_temp = tmp102.get_temperature(); // Read temperature
legstar85 9:d71b92c916f8 343 if (low_temp <= 22) { // Low temp alarm condition - in real world would be lot lower!!
legstar85 9:d71b92c916f8 344 LED04 = 0; // Light LED04 if temperature is over specified - Simulated starting of heaters
legstar85 9:d71b92c916f8 345 LED05 = 0; // Light LED05 if temperature is over specified - Simulated starting of heaters
legstar85 9:d71b92c916f8 346 LED06 = 0; // Light LED06 if temperature is over specified - Simulated starting of heaters
legstar85 11:a6eec09b546e 347 printf("WARNING - Low Temp!! \n"); //
legstar85 9:d71b92c916f8 348 }
legstar85 9:d71b92c916f8 349 else {
legstar85 9:d71b92c916f8 350 LED04 = 1; // LED04 off if temperature is above specified - Simulated stopping of heaters
legstar85 9:d71b92c916f8 351 LED05 = 1; // LED05 off if temperature is above specified - Simulated stopping of heaters
legstar85 9:d71b92c916f8 352 LED06 = 1; // LED06 off if temperature is above specified - Simulated stopping of heaters
legstar85 8:07323fcab6d1 353 }
legstar85 7:d77e95505e43 354
legstar85 9:d71b92c916f8 355 if(bAButtonWasPressed) // Check if button was pressed
legstar85 6:ebf4784ce92b 356 {
legstar85 9:d71b92c916f8 357 if(SelectedItem == 0) //
legstar85 6:ebf4784ce92b 358 {
legstar85 9:d71b92c916f8 359 NewMenuState = MENUSTATE_Main; // Transition back to the main menu
legstar85 9:d71b92c916f8 360 LED01 = 1; // Turn off LED upon transition back to main menu
legstar85 9:d71b92c916f8 361 LED02 = 1; // Turn off LED upon transition back to main menu
legstar85 9:d71b92c916f8 362 LED03 = 1; // Turn off LED upon transition back to main menu
legstar85 9:d71b92c916f8 363 LED04 = 1; // Turn off LED upon transition back to main menu
legstar85 9:d71b92c916f8 364 LED05 = 1; // Turn off LED upon transition back to main menu
legstar85 9:d71b92c916f8 365 LED06 = 1; // Turn off LED upon transition back to main menu
legstar85 6:ebf4784ce92b 366 }
legstar85 6:ebf4784ce92b 367 }
legstar85 6:ebf4784ce92b 368 }
legstar85 6:ebf4784ce92b 369 break;
legstar85 11:a6eec09b546e 370 case MENUSTATE_OneOff: // Call a one off measurement menu following top menu selection
legstar85 6:ebf4784ce92b 371 {
legstar85 6:ebf4784ce92b 372
legstar85 8:07323fcab6d1 373 NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc.
legstar85 9:d71b92c916f8 374 if(SelectedItem >= NumMenuItems) //
legstar85 6:ebf4784ce92b 375 {
legstar85 7:d77e95505e43 376 NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu.
legstar85 6:ebf4784ce92b 377 }
legstar85 6:ebf4784ce92b 378
legstar85 8:07323fcab6d1 379 float T = tmp102.get_temperature(); // read temperature and print to lcd
legstar85 9:d71b92c916f8 380 pc.printf("T = %f K\n",T); // Print to serial - allows testing without device attached
legstar85 8:07323fcab6d1 381 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 382 if (length <= 14) // if string will fit on display (assuming printing at x=0)
legstar85 9:d71b92c916f8 383 lcd.printString("-- One-Off --",0,0); //
legstar85 9:d71b92c916f8 384 lcd.printString("-- Measure --",0,1); //
legstar85 8:07323fcab6d1 385 lcd.printString(buffer,0,3); // display on screen
legstar85 9:d71b92c916f8 386 lcd.printString(" 'A' to Menu",0,5); //
legstar85 6:ebf4784ce92b 387
legstar85 9:d71b92c916f8 388 if(bAButtonWasPressed) // Check if button was pressed
legstar85 6:ebf4784ce92b 389 {
legstar85 9:d71b92c916f8 390 if(SelectedItem == 0) //
legstar85 6:ebf4784ce92b 391 {
legstar85 8:07323fcab6d1 392 NewMenuState = MENUSTATE_Main; // Take us back to top menu
legstar85 6:ebf4784ce92b 393 }
legstar85 6:ebf4784ce92b 394 }
legstar85 6:ebf4784ce92b 395
legstar85 6:ebf4784ce92b 396 }
legstar85 6:ebf4784ce92b 397 break;
legstar85 9:d71b92c916f8 398 case MENUSTATE_About: // Call About menu following top menu selection
legstar85 6:ebf4784ce92b 399 {
legstar85 6:ebf4784ce92b 400
legstar85 8:07323fcab6d1 401 NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc.
legstar85 9:d71b92c916f8 402 if(SelectedItem >= NumMenuItems) //
legstar85 6:ebf4784ce92b 403 {
legstar85 6:ebf4784ce92b 404 NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu.
legstar85 6:ebf4784ce92b 405 }
legstar85 6:ebf4784ce92b 406
legstar85 9:d71b92c916f8 407 lcd.printString("--- About ---",0,0); //
legstar85 9:d71b92c916f8 408 lcd.printString("ELE3006M - IoT",0,1); //
legstar85 9:d71b92c916f8 409 lcd.printString(" Project",0,2); //
legstar85 9:d71b92c916f8 410 lcd.printString("Uni of Lincoln",0,3); //
legstar85 9:d71b92c916f8 411 lcd.printString(" 'A' to Menu",0,5); //
legstar85 9:d71b92c916f8 412 lcd.refresh(); //
legstar85 6:ebf4784ce92b 413
legstar85 9:d71b92c916f8 414 if(bAButtonWasPressed) // Check if button was pressed
legstar85 6:ebf4784ce92b 415 {
legstar85 9:d71b92c916f8 416 if(SelectedItem == 0) //
legstar85 6:ebf4784ce92b 417 {
legstar85 9:d71b92c916f8 418 NewMenuState = MENUSTATE_Main; // Transition back to Main Menu
legstar85 6:ebf4784ce92b 419 }
legstar85 6:ebf4784ce92b 420 }
legstar85 6:ebf4784ce92b 421 }
legstar85 6:ebf4784ce92b 422 break;
legstar85 9:d71b92c916f8 423 case MENUSTATE_Author: // Call Author menu following top menu selection
legstar85 7:d77e95505e43 424 {
legstar85 7:d77e95505e43 425
legstar85 8:07323fcab6d1 426 NumMenuItems = 1; // Detail the number of items in Menu - need this to wrap the selection around properly etc.
legstar85 9:d71b92c916f8 427 if(SelectedItem >= NumMenuItems) //
legstar85 7:d77e95505e43 428 {
legstar85 7:d77e95505e43 429 NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu.
legstar85 7:d77e95505e43 430 }
legstar85 7:d77e95505e43 431
legstar85 9:d71b92c916f8 432 lcd.printString("--- Author ---",0,0); //
legstar85 9:d71b92c916f8 433 lcd.printString("David Leaming ",0,1); //
legstar85 9:d71b92c916f8 434 lcd.printString(" 25574043 ",0,2); //
legstar85 9:d71b92c916f8 435 lcd.printString(" VolkerRail",0,3); //
legstar85 9:d71b92c916f8 436 lcd.printString(" 'A' to Menu",0,5); //
legstar85 7:d77e95505e43 437
legstar85 9:d71b92c916f8 438 if(bAButtonWasPressed) // Check if button was pressed
legstar85 7:d77e95505e43 439 {
legstar85 9:d71b92c916f8 440 if(SelectedItem == 0) //
legstar85 7:d77e95505e43 441 {
legstar85 8:07323fcab6d1 442 NewMenuState = MENUSTATE_Main; // Take us back to top menu
legstar85 7:d77e95505e43 443 }
legstar85 7:d77e95505e43 444 }
legstar85 7:d77e95505e43 445 }
legstar85 7:d77e95505e43 446 break;
legstar85 7:d77e95505e43 447
legstar85 6:ebf4784ce92b 448 default:
legstar85 6:ebf4784ce92b 449 {
legstar85 6:ebf4784ce92b 450 NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu.
legstar85 6:ebf4784ce92b 451 }
legstar85 6:ebf4784ce92b 452 };
legstar85 5:eebd36a47049 453
legstar85 6:ebf4784ce92b 454 if(NewMenuState != MENUSTATTE_Num) // If we have requested a new menu state.
legstar85 6:ebf4784ce92b 455 {
legstar85 8:07323fcab6d1 456 printf("Transitioning to MenuState: %i\n", NewMenuState); // Observe on serial port - ensure transition to correct screen
legstar85 6:ebf4784ce92b 457
legstar85 6:ebf4784ce92b 458 MenuState = NewMenuState; // We want to transition the menu to a new state.
legstar85 6:ebf4784ce92b 459
legstar85 6:ebf4784ce92b 460 // Do any bookkeeping needed when moving to new state.
legstar85 6:ebf4784ce92b 461 SelectedItem = 0; // Reset the selected item.
legstar85 6:ebf4784ce92b 462
legstar85 6:ebf4784ce92b 463 lcd.clear(); // Clear the display for one frame on state transition.
legstar85 11:a6eec09b546e 464 }
legstar85 6:ebf4784ce92b 465 else
legstar85 6:ebf4784ce92b 466 {
legstar85 6:ebf4784ce92b 467
legstar85 11:a6eec09b546e 468
legstar85 11:a6eec09b546e 469 unsigned int SelectionMarkerRadius = 4; // If we have not selected to move to a new menu.
legstar85 11:a6eec09b546e 470 unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius); // Draw a marker circle at end of line to show which is the currently selected item.
legstar85 8:07323fcab6d1 471 unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1); // +1 because of the menu title being on first row
legstar85 9:d71b92c916f8 472 lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK); // Fill the circle black so it can be seen easily
legstar85 6:ebf4784ce92b 473
legstar85 6:ebf4784ce92b 474 // Handle Joystick Input
legstar85 6:ebf4784ce92b 475 switch (joystick.get_direction()) { // Call to check direction joystick is pointing
legstar85 9:d71b92c916f8 476 printf("Direction = %i\n"); // Print direction to serial
legstar85 6:ebf4784ce92b 477 case N: //
legstar85 6:ebf4784ce92b 478 SelectedItem--; //
legstar85 6:ebf4784ce92b 479 break; //
legstar85 6:ebf4784ce92b 480 case S: //
legstar85 6:ebf4784ce92b 481 SelectedItem++; //
legstar85 6:ebf4784ce92b 482 break; //
legstar85 6:ebf4784ce92b 483 }
legstar85 6:ebf4784ce92b 484
legstar85 9:d71b92c916f8 485 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 486 {
legstar85 9:d71b92c916f8 487 SelectedItem = NumMenuItems - 1; //
legstar85 6:ebf4784ce92b 488 }
legstar85 9:d71b92c916f8 489 else if(SelectedItem >= NumMenuItems) //
legstar85 6:ebf4784ce92b 490 {
legstar85 9:d71b92c916f8 491 SelectedItem = 0; //
legstar85 6:ebf4784ce92b 492 }
legstar85 5:eebd36a47049 493 }
legstar85 5:eebd36a47049 494
legstar85 6:ebf4784ce92b 495 lcd.refresh(); //Finally update the display.
legstar85 6:ebf4784ce92b 496 }
legstar85 6:ebf4784ce92b 497 }
legstar85 5:eebd36a47049 498 }
legstar85 5:eebd36a47049 499
legstar85 11:a6eec09b546e 500 void StartTemp() //
legstar85 11:a6eec09b546e 501 {
legstar85 11:a6eec09b546e 502 Direction d = joystick.get_direction(); //
legstar85 11:a6eec09b546e 503
legstar85 11:a6eec09b546e 504 joystick.init();
legstar85 11:a6eec09b546e 505
legstar85 11:a6eec09b546e 506 g_StartTemp = fsm[g_state].output; // set ouput depending on current state
legstar85 11:a6eec09b546e 507 wait(fsm[g_state].time); // wait in that state for desired time
legstar85 11:a6eec09b546e 508 g_state = fsm[g_state].nextState[d]; // read input and update curent state
legstar85 11:a6eec09b546e 509 }