![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularisation of some code Attempts to begin building workable menu
Dependencies: mbed N5110v02 TMP102 JoystickIoT
Revision 5:eebd36a47049, committed 2022-01-07
- Comitter:
- legstar85
- Date:
- Fri Jan 07 23:14:23 2022 +0000
- Parent:
- 4:0090145b3286
- Commit message:
- Initial modularisation of code, attempt to start building menu
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Dec 21 17:22:42 2021 +0000 +++ b/main.cpp Fri Jan 07 23:14:23 2022 +0000 @@ -25,6 +25,8 @@ Serial pc(USBTX,USBRX); // UART connection for PC +Ticker ticker_menu; // Create Menu ticker object + DigitalOut r_led(LED_RED); // K64F on-board LEDs DigitalOut g_led(LED_GREEN); // K64F on-board LEDs DigitalOut b_led(LED_BLUE); // K64F on-board LEDs @@ -41,17 +43,20 @@ InterruptIn ButtonA (PTB9); // PCB Button - A InterruptIn ButtonB (PTD0); // PCB Button - B -volatile int g_ButtonA_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global -volatile int g_ButtonB_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global -volatile int g_sw2_flag=0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global +volatile int g_ButtonA_flag = 0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global +volatile int g_ButtonB_flag = 0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global +volatile int g_sw2_flag = 0; // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global +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 +volatile int option = 0; // Menu option selection based on joystick direction void error(); // error function hangs flashing an LED void init_serial(); // setup serial port void init_K64F(); // set-up the on-board LEDs and switches void init_PCB(); // set-up the PCB LEDs and buttons -void ButtonA_isr(); -void ButtonB_isr(); -void sw2_isr(); +void ButtonA_isr(); // +void ButtonB_isr(); // +void sw2_isr(); // +void menu_timer_isr(); // int main() { @@ -63,6 +68,8 @@ lcd.init(); // initialise display joystick.init(); // initialise joystick + ticker_menu.attach(&menu_timer_isr,0.2); // Attach ticker for the Joystick + 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 ButtonA.rise(&ButtonA_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for ButtonB.rise(&ButtonB_isr); // External push button, pin set to 0V by pull down command, means a rising edge is looked for @@ -86,50 +93,12 @@ lcd.printString("To Navigate",0,1); lcd.printString("",0,2); // Blank Line lcd.printString("A = Select",0,3); - lcd.printString("B = Back",0,4); lcd.refresh(); // need to refresh display after setting pixels or writing strings wait(5.0); - - MainMenu: // Initial Main Menu Creation to allow mode to be selected - - - - while (1) { - - lcd.normalMode(); // normal colour mode - lcd.setBrightness(0.5); // put LED backlight on 50% - lcd.clear(); // clear buffer at start of every loop - lcd.refresh(); // need to refresh display after setting pixels or writing strings - - float T = tmp102.get_temperature(); // read temperature and print to lcd - 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 - if (length <= 14) // if string will fit on display (assuming printing at x=0) - lcd.printString(buffer,0,2); // display on screen - lcd.refresh(); // need to refresh display after setting pixels or writing strings - wait(2.0); - - if (g_ButtonA_flag) { // check if flag i.e. interrupt has occured - g_ButtonA_flag = 0; // if it has, clear the flag - printf("Button A Press \n"); // send message over serial port - can observe in CoolTerm etc. - LED01=0; - wait(5.0); - LED01=1; - } - - if (g_ButtonB_flag) { // check if flag i.e. interrupt has occured - g_ButtonB_flag = 0; // if it has, clear the flag - - printf("Button B Press \n"); // send message over serial port - can observe in CoolTerm etc. - LED02=0; - wait(5.0); - LED02=1; - - } - - } - -} + MainMenu(); // + +} void init_serial() { pc.baud(115200); // set to highest baud - ensure terminal software matches @@ -173,4 +142,88 @@ void sw2_isr() // SW2 event-triggered interrupt { g_sw2_flag = 1; // set flag in ISR -} \ No newline at end of file +} + +void menu_timer_isr() +{ + g_menu_timer_flag = 1; // set flag in ISR +} + +void MainMenu() +{ + while(1) { + if (g_menu_timer_flag){ + g_menu_timer_flag = 0; + + lcd.clear(); // clear buffer at start of every loop + joystick.init(); // initialise joystick + + lcd.printString("---- MENU ----",0,0); // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) + lcd.printString("M1 - Monitor",0,1); // + lcd.printString("M2 - One-off",0,2); // + lcd.printString("About",0,3); // + lcd.refresh(); // + + switch (joystick.get_direction()) { // Call to check direction joystick is pointing + printf("Direction = %i\n"); // + case N: // + option--; // + break; // + case S: // + option++; // + break; // + } + if (option < 0) { // If last/bottom menu item is selcted and user presses joystick down + option = 2; // Selection circle to move to first/top menu option + } + if (option < 2) { // If first/top menu item is selcted and user presses joystick up + option = 0; // Selection circle to move to last/bottom menu option + } + + if (option == 1) { // + lcd.drawCircle (55,20,2,0); // + } + if (option == 2) { // + lcd.drawCircle (35,35,2,0); // + } + if (option == 3) { // + lcd.drawCircle (20,50,2,0); // + } + +} + +void ConstantMonitoring() +{ + while (1) { + + lcd.normalMode(); // normal colour mode + lcd.setBrightness(0.5); // put LED backlight on 50% + lcd.clear(); // clear buffer at start of every loop + lcd.refresh(); // need to refresh display after setting pixels or writing strings + + float T = tmp102.get_temperature(); // read temperature and print to lcd + 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 + if (length <= 14) // if string will fit on display (assuming printing at x=0) + lcd.printString(buffer,0,2); // display on screen + lcd.refresh(); // need to refresh display after setting pixels or writing strings + wait(2.0); + + if (g_ButtonA_flag) { // check if flag i.e. interrupt has occured + g_ButtonA_flag = 0; // if it has, clear the flag + + printf("Button A Press \n"); // send message over serial port - can observe in CoolTerm etc. + LED01=0; + wait(5.0); + LED01=1; + } + + if (g_ButtonB_flag) { // check if flag i.e. interrupt has occured + g_ButtonB_flag = 0; // if it has, clear the flag + + printf("Button B Press \n"); // send message over serial port - can observe in CoolTerm etc. + LED02=0; + wait(5.0); + LED02=1; + } + } +} \ No newline at end of file