![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Update to menu, selection process, insertion of Temp monitoring into correct menu
Dependencies: mbed N5110v02 TMP102 JoystickIoT
main.cpp@5:eebd36a47049, 2022-01-07 (annotated)
- Committer:
- legstar85
- Date:
- Fri Jan 07 23:14:23 2022 +0000
- Revision:
- 5:eebd36a47049
- Parent:
- 4:0090145b3286
- Child:
- 6:ebf4784ce92b
Initial modularisation of code, attempt to start building menu
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 | 2:e0091b5311f1 | 4 | * @ Date - December 2021 |
legstar85 | 2:e0091b5311f1 | 5 | * |
legstar85 | 2:e0091b5311f1 | 6 | * Acknowledgements |
legstar85 | 2:e0091b5311f1 | 7 | * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 |
legstar85 | 2:e0091b5311f1 | 8 | * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries |
legstar85 | 2:e0091b5311f1 | 9 | * |
eencae | 0:21a200b880d7 | 10 | */ |
eencae | 0:21a200b880d7 | 11 | |
legstar85 | 2:e0091b5311f1 | 12 | #include "mbed.h" // include the library header, ensure the library has been imported into the project |
eencae | 1:dd5fb735acf1 | 13 | #include "TMP102.h" |
legstar85 | 2:e0091b5311f1 | 14 | #include "N5110.h" |
legstar85 | 2:e0091b5311f1 | 15 | #include "Joystick.h" |
legstar85 | 2:e0091b5311f1 | 16 | #include "Bitmap.h" |
eencae | 0:21a200b880d7 | 17 | |
legstar85 | 2:e0091b5311f1 | 18 | TMP102 tmp102(I2C_SDA,I2C_SCL); // Create TMP102 object |
legstar85 | 2:e0091b5311f1 | 19 | |
legstar85 | 2:e0091b5311f1 | 20 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
legstar85 | 2:e0091b5311f1 | 21 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting |
legstar85 | 2:e0091b5311f1 | 22 | |
legstar85 | 3:80c1eba78f9b | 23 | // y x button |
legstar85 | 3:80c1eba78f9b | 24 | Joystick joystick(PTB10,PTB11,PTC16); // Define Joystick Object |
eencae | 0:21a200b880d7 | 25 | |
legstar85 | 2:e0091b5311f1 | 26 | Serial pc(USBTX,USBRX); // UART connection for PC |
legstar85 | 2:e0091b5311f1 | 27 | |
legstar85 | 5:eebd36a47049 | 28 | Ticker ticker_menu; // Create Menu ticker object |
legstar85 | 5:eebd36a47049 | 29 | |
legstar85 | 2:e0091b5311f1 | 30 | DigitalOut r_led(LED_RED); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 31 | DigitalOut g_led(LED_GREEN); // K64F on-board LEDs |
legstar85 | 2:e0091b5311f1 | 32 | DigitalOut b_led(LED_BLUE); // K64F on-board LEDs |
eencae | 0:21a200b880d7 | 33 | |
legstar85 | 3:80c1eba78f9b | 34 | DigitalOut LED01 (PTA1); // PCB Surface Mounted LED's - LED1 |
legstar85 | 3:80c1eba78f9b | 35 | DigitalOut LED02 (PTA2); // PCB Surface Mounted LED's - LED2 |
legstar85 | 3:80c1eba78f9b | 36 | DigitalOut LED03 (PTC2); // PCB Surface Mounted LED's - LED3 |
legstar85 | 3:80c1eba78f9b | 37 | DigitalOut LED04 (PTC3); // PCB Surface Mounted LED's - LED4 |
legstar85 | 3:80c1eba78f9b | 38 | DigitalOut LED05 (PTC4); // PCB Surface Mounted LED's - LED5 |
legstar85 | 3:80c1eba78f9b | 39 | DigitalOut LED06 (PTD3); // PCB Surface Mounted LED's - LED6 |
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 | 3:80c1eba78f9b | 45 | |
legstar85 | 5:eebd36a47049 | 46 | 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 | 47 | 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 | 5:eebd36a47049 | 48 | 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 | 49 | 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 | 50 | volatile int option = 0; // Menu option selection based on joystick direction |
legstar85 | 2:e0091b5311f1 | 51 | |
legstar85 | 2:e0091b5311f1 | 52 | void error(); // error function hangs flashing an LED |
legstar85 | 2:e0091b5311f1 | 53 | void init_serial(); // setup serial port |
legstar85 | 2:e0091b5311f1 | 54 | void init_K64F(); // set-up the on-board LEDs and switches |
legstar85 | 3:80c1eba78f9b | 55 | void init_PCB(); // set-up the PCB LEDs and buttons |
legstar85 | 5:eebd36a47049 | 56 | void ButtonA_isr(); // |
legstar85 | 5:eebd36a47049 | 57 | void ButtonB_isr(); // |
legstar85 | 5:eebd36a47049 | 58 | void sw2_isr(); // |
legstar85 | 5:eebd36a47049 | 59 | void menu_timer_isr(); // |
eencae | 0:21a200b880d7 | 60 | |
eencae | 0:21a200b880d7 | 61 | int main() |
eencae | 0:21a200b880d7 | 62 | { |
legstar85 | 2:e0091b5311f1 | 63 | init_K64F(); // initialise the board |
legstar85 | 2:e0091b5311f1 | 64 | init_serial(); // initialise the serial port |
legstar85 | 3:80c1eba78f9b | 65 | init_PCB(); // initialise the PCB |
legstar85 | 3:80c1eba78f9b | 66 | |
legstar85 | 2:e0091b5311f1 | 67 | tmp102.init(); // call the sensor init method using dot syntax |
legstar85 | 2:e0091b5311f1 | 68 | lcd.init(); // initialise display |
legstar85 | 3:80c1eba78f9b | 69 | joystick.init(); // initialise joystick |
legstar85 | 3:80c1eba78f9b | 70 | |
legstar85 | 5:eebd36a47049 | 71 | ticker_menu.attach(&menu_timer_isr,0.2); // Attach ticker for the Joystick |
legstar85 | 5:eebd36a47049 | 72 | |
legstar85 | 3:80c1eba78f9b | 73 | 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 | 74 | 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 | 75 | 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 | 76 | |
legstar85 | 2:e0091b5311f1 | 77 | lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0 |
eencae | 0:21a200b880d7 | 78 | |
legstar85 | 3:80c1eba78f9b | 79 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
legstar85 | 3:80c1eba78f9b | 80 | |
legstar85 | 3:80c1eba78f9b | 81 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 3:80c1eba78f9b | 82 | lcd.printString("Smart Cold",0,0); // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) |
legstar85 | 3:80c1eba78f9b | 83 | lcd.printString("Storage",0,1); // Just a welcome message before auto moving to main menu |
legstar85 | 3:80c1eba78f9b | 84 | lcd.printString("Monitoring",0,2); |
legstar85 | 3:80c1eba78f9b | 85 | lcd.printString("",0,2); // Blank Line |
legstar85 | 3:80c1eba78f9b | 86 | lcd.printString("V1.0 - 2021",0,4); |
legstar85 | 3:80c1eba78f9b | 87 | lcd.printString("David Leaming",0,5); |
legstar85 | 3:80c1eba78f9b | 88 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 3:80c1eba78f9b | 89 | wait(5.0); // leave welcome screen on for designated amount of time |
legstar85 | 3:80c1eba78f9b | 90 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 3:80c1eba78f9b | 91 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 3:80c1eba78f9b | 92 | lcd.printString("Use Joystick",0,0); // Instruction for use of menu |
legstar85 | 3:80c1eba78f9b | 93 | lcd.printString("To Navigate",0,1); |
legstar85 | 3:80c1eba78f9b | 94 | lcd.printString("",0,2); // Blank Line |
legstar85 | 3:80c1eba78f9b | 95 | lcd.printString("A = Select",0,3); |
legstar85 | 3:80c1eba78f9b | 96 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 3:80c1eba78f9b | 97 | wait(5.0); |
legstar85 | 3:80c1eba78f9b | 98 | |
legstar85 | 5:eebd36a47049 | 99 | MainMenu(); // |
legstar85 | 5:eebd36a47049 | 100 | |
legstar85 | 5:eebd36a47049 | 101 | } |
eencae | 0:21a200b880d7 | 102 | |
eencae | 0:21a200b880d7 | 103 | void init_serial() { |
legstar85 | 2:e0091b5311f1 | 104 | pc.baud(115200); // set to highest baud - ensure terminal software matches |
eencae | 0:21a200b880d7 | 105 | } |
eencae | 0:21a200b880d7 | 106 | |
eencae | 0:21a200b880d7 | 107 | void init_K64F() |
eencae | 0:21a200b880d7 | 108 | { |
legstar85 | 2:e0091b5311f1 | 109 | r_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 110 | g_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
legstar85 | 2:e0091b5311f1 | 111 | b_led = 1; // on-board LEDs are active-low, so set pin high to turn them off. |
eencae | 0:21a200b880d7 | 112 | |
legstar85 | 2:e0091b5311f1 | 113 | sw2.mode(PullNone); // since the on-board switches have external pull-ups, we should disable the internal pull-down |
legstar85 | 2:e0091b5311f1 | 114 | sw3.mode(PullNone); // resistors that are enabled by default using InterruptIn |
eencae | 0:21a200b880d7 | 115 | |
eencae | 0:21a200b880d7 | 116 | } |
legstar85 | 3:80c1eba78f9b | 117 | |
legstar85 | 3:80c1eba78f9b | 118 | void init_PCB () |
legstar85 | 3:80c1eba78f9b | 119 | { |
legstar85 | 3:80c1eba78f9b | 120 | LED01 = 1; // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly |
legstar85 | 3:80c1eba78f9b | 121 | LED02 = 1; |
legstar85 | 3:80c1eba78f9b | 122 | LED03 = 1; |
legstar85 | 3:80c1eba78f9b | 123 | LED04 = 1; |
legstar85 | 3:80c1eba78f9b | 124 | LED05 = 1; |
legstar85 | 3:80c1eba78f9b | 125 | LED06 = 1; |
legstar85 | 3:80c1eba78f9b | 126 | |
legstar85 | 3:80c1eba78f9b | 127 | 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 | 128 | 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 | 129 | |
legstar85 | 3:80c1eba78f9b | 130 | } |
legstar85 | 3:80c1eba78f9b | 131 | |
legstar85 | 3:80c1eba78f9b | 132 | void ButtonA_isr() // ButtonA event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 133 | { |
legstar85 | 3:80c1eba78f9b | 134 | g_ButtonA_flag = 1; // set flag in ISR |
legstar85 | 3:80c1eba78f9b | 135 | } |
legstar85 | 3:80c1eba78f9b | 136 | |
legstar85 | 3:80c1eba78f9b | 137 | void ButtonB_isr() // ButtonB event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 138 | { |
legstar85 | 3:80c1eba78f9b | 139 | g_ButtonB_flag = 1; // set flag in ISR |
legstar85 | 3:80c1eba78f9b | 140 | } |
legstar85 | 3:80c1eba78f9b | 141 | |
legstar85 | 3:80c1eba78f9b | 142 | void sw2_isr() // SW2 event-triggered interrupt |
legstar85 | 3:80c1eba78f9b | 143 | { |
legstar85 | 3:80c1eba78f9b | 144 | g_sw2_flag = 1; // set flag in ISR |
legstar85 | 5:eebd36a47049 | 145 | } |
legstar85 | 5:eebd36a47049 | 146 | |
legstar85 | 5:eebd36a47049 | 147 | void menu_timer_isr() |
legstar85 | 5:eebd36a47049 | 148 | { |
legstar85 | 5:eebd36a47049 | 149 | g_menu_timer_flag = 1; // set flag in ISR |
legstar85 | 5:eebd36a47049 | 150 | } |
legstar85 | 5:eebd36a47049 | 151 | |
legstar85 | 5:eebd36a47049 | 152 | void MainMenu() |
legstar85 | 5:eebd36a47049 | 153 | { |
legstar85 | 5:eebd36a47049 | 154 | while(1) { |
legstar85 | 5:eebd36a47049 | 155 | if (g_menu_timer_flag){ |
legstar85 | 5:eebd36a47049 | 156 | g_menu_timer_flag = 0; |
legstar85 | 5:eebd36a47049 | 157 | |
legstar85 | 5:eebd36a47049 | 158 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 5:eebd36a47049 | 159 | joystick.init(); // initialise joystick |
legstar85 | 5:eebd36a47049 | 160 | |
legstar85 | 5:eebd36a47049 | 161 | lcd.printString("---- MENU ----",0,0); // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) |
legstar85 | 5:eebd36a47049 | 162 | lcd.printString("M1 - Monitor",0,1); // |
legstar85 | 5:eebd36a47049 | 163 | lcd.printString("M2 - One-off",0,2); // |
legstar85 | 5:eebd36a47049 | 164 | lcd.printString("About",0,3); // |
legstar85 | 5:eebd36a47049 | 165 | lcd.refresh(); // |
legstar85 | 5:eebd36a47049 | 166 | |
legstar85 | 5:eebd36a47049 | 167 | switch (joystick.get_direction()) { // Call to check direction joystick is pointing |
legstar85 | 5:eebd36a47049 | 168 | printf("Direction = %i\n"); // |
legstar85 | 5:eebd36a47049 | 169 | case N: // |
legstar85 | 5:eebd36a47049 | 170 | option--; // |
legstar85 | 5:eebd36a47049 | 171 | break; // |
legstar85 | 5:eebd36a47049 | 172 | case S: // |
legstar85 | 5:eebd36a47049 | 173 | option++; // |
legstar85 | 5:eebd36a47049 | 174 | break; // |
legstar85 | 5:eebd36a47049 | 175 | } |
legstar85 | 5:eebd36a47049 | 176 | if (option < 0) { // If last/bottom menu item is selcted and user presses joystick down |
legstar85 | 5:eebd36a47049 | 177 | option = 2; // Selection circle to move to first/top menu option |
legstar85 | 5:eebd36a47049 | 178 | } |
legstar85 | 5:eebd36a47049 | 179 | if (option < 2) { // If first/top menu item is selcted and user presses joystick up |
legstar85 | 5:eebd36a47049 | 180 | option = 0; // Selection circle to move to last/bottom menu option |
legstar85 | 5:eebd36a47049 | 181 | } |
legstar85 | 5:eebd36a47049 | 182 | |
legstar85 | 5:eebd36a47049 | 183 | if (option == 1) { // |
legstar85 | 5:eebd36a47049 | 184 | lcd.drawCircle (55,20,2,0); // |
legstar85 | 5:eebd36a47049 | 185 | } |
legstar85 | 5:eebd36a47049 | 186 | if (option == 2) { // |
legstar85 | 5:eebd36a47049 | 187 | lcd.drawCircle (35,35,2,0); // |
legstar85 | 5:eebd36a47049 | 188 | } |
legstar85 | 5:eebd36a47049 | 189 | if (option == 3) { // |
legstar85 | 5:eebd36a47049 | 190 | lcd.drawCircle (20,50,2,0); // |
legstar85 | 5:eebd36a47049 | 191 | } |
legstar85 | 5:eebd36a47049 | 192 | |
legstar85 | 5:eebd36a47049 | 193 | } |
legstar85 | 5:eebd36a47049 | 194 | |
legstar85 | 5:eebd36a47049 | 195 | void ConstantMonitoring() |
legstar85 | 5:eebd36a47049 | 196 | { |
legstar85 | 5:eebd36a47049 | 197 | while (1) { |
legstar85 | 5:eebd36a47049 | 198 | |
legstar85 | 5:eebd36a47049 | 199 | lcd.normalMode(); // normal colour mode |
legstar85 | 5:eebd36a47049 | 200 | lcd.setBrightness(0.5); // put LED backlight on 50% |
legstar85 | 5:eebd36a47049 | 201 | lcd.clear(); // clear buffer at start of every loop |
legstar85 | 5:eebd36a47049 | 202 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 5:eebd36a47049 | 203 | |
legstar85 | 5:eebd36a47049 | 204 | float T = tmp102.get_temperature(); // read temperature and print to lcd |
legstar85 | 5:eebd36a47049 | 205 | 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 | 5:eebd36a47049 | 206 | if (length <= 14) // if string will fit on display (assuming printing at x=0) |
legstar85 | 5:eebd36a47049 | 207 | lcd.printString(buffer,0,2); // display on screen |
legstar85 | 5:eebd36a47049 | 208 | lcd.refresh(); // need to refresh display after setting pixels or writing strings |
legstar85 | 5:eebd36a47049 | 209 | wait(2.0); |
legstar85 | 5:eebd36a47049 | 210 | |
legstar85 | 5:eebd36a47049 | 211 | if (g_ButtonA_flag) { // check if flag i.e. interrupt has occured |
legstar85 | 5:eebd36a47049 | 212 | g_ButtonA_flag = 0; // if it has, clear the flag |
legstar85 | 5:eebd36a47049 | 213 | |
legstar85 | 5:eebd36a47049 | 214 | printf("Button A Press \n"); // send message over serial port - can observe in CoolTerm etc. |
legstar85 | 5:eebd36a47049 | 215 | LED01=0; |
legstar85 | 5:eebd36a47049 | 216 | wait(5.0); |
legstar85 | 5:eebd36a47049 | 217 | LED01=1; |
legstar85 | 5:eebd36a47049 | 218 | } |
legstar85 | 5:eebd36a47049 | 219 | |
legstar85 | 5:eebd36a47049 | 220 | if (g_ButtonB_flag) { // check if flag i.e. interrupt has occured |
legstar85 | 5:eebd36a47049 | 221 | g_ButtonB_flag = 0; // if it has, clear the flag |
legstar85 | 5:eebd36a47049 | 222 | |
legstar85 | 5:eebd36a47049 | 223 | printf("Button B Press \n"); // send message over serial port - can observe in CoolTerm etc. |
legstar85 | 5:eebd36a47049 | 224 | LED02=0; |
legstar85 | 5:eebd36a47049 | 225 | wait(5.0); |
legstar85 | 5:eebd36a47049 | 226 | LED02=1; |
legstar85 | 5:eebd36a47049 | 227 | } |
legstar85 | 5:eebd36a47049 | 228 | } |
legstar85 | 5:eebd36a47049 | 229 | } |