From b738f89cb873727c3f6ff099127b02d1e5c7a312 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Sun, 1 Dec 2019 17:01:33 +0100 Subject: [PATCH] add menu --- code/inc/menu.h | 16 ++++++++ code/src/main.c | 82 +++++++++++++++++++++++++++++++++------- code/src/menu.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 code/inc/menu.h create mode 100644 code/src/menu.c diff --git a/code/inc/menu.h b/code/inc/menu.h new file mode 100644 index 0000000..44b769d --- /dev/null +++ b/code/inc/menu.h @@ -0,0 +1,16 @@ +#include + +extern int8_t menu_val_timezone; +extern uint8_t menu_val_format; + +typedef enum { + OFF, + TIMEZONE, + FORMAT, + EXIT +} menu_state_t; + +void enter_menu(void); +const char* get_menu_text(void); +uint8_t get_menu_active(void); +void menu_up_down(uint8_t up_down); diff --git a/code/src/main.c b/code/src/main.c index 302cd2c..feec8fa 100644 --- a/code/src/main.c +++ b/code/src/main.c @@ -7,6 +7,7 @@ #include "uart.h" #include "i2cmaster.h" #include "DS3231.h" +#include "menu.h" #define RX_INPUT_BUFFER_SIZE 92 @@ -27,6 +28,48 @@ const unsigned char number_Font[10] = { 0b11111110, //8 0b01111110 //9 }; +const unsigned char character_Font[26] = { + 0b10111110, //A + 0b11110100, //b + 0b11100000, //c + 0b11100110, //d + 0b11111000, //e + 0b10111000, //F + 0b11011100, //G + 0b10110100, //h + 0b00000100, //i + 0b01000110, //J + 0b10111100, //K + 0b11010000, //L + 0b10011110, //M + 0b10100100, //n + 0b11100100, //o + 0b10111010, //P + 0b00111110, //q + 0b10100000, //r + 0b01111100, //S + 0b11110000, //t + 0b11000100, //u + 0b11000100, //v + 0b11010110, //W + 0b10110110, //X + 0b01110110, //Y + 0b11101010 //Z +}; +const unsigned char character_minus = 0b00100000; //zeigt '-' an + +void string_to_buffer(const char input_string[6]){ + for(uint8_t i=0; i < 6; i++){ + if(input_string[i] >= 0x41 && input_string[i] <= 0x5A) + SR_Buffer[i] = character_Font[input_string[i] - 0x41]; + else if(input_string[i] >= 0x30 && input_string[i] <= 0x39) + SR_Buffer[i] = number_Font[input_string[i] - 0x30]; + else if(input_string[i] == 0x2D) + SR_Buffer[i] = character_minus; + else + SR_Buffer[i] = 0; + } +} void clear_SR_Buffer(void) { for (uint8_t i = 0; i < 6; i++) { @@ -70,21 +113,29 @@ int8_t parse_nmea_gps(char input_string[RX_INPUT_BUFFER_SIZE], uint8_t output_bu int main(void){ setup(); + printf("Moin\n\r"); while(1){ check_dip_switches(); - uint8_t clock[3]; - cli(); - DS3231_read(clock); - sei(); - for(uint8_t i=0; i < 3; i++){ - SR_Buffer[5-(2*i)] = number_Font[clock[i] & 0x0F]; - SR_Buffer[5-(2*i)-1] = number_Font[(clock[i] & 0xF0)>>4]; + if(get_menu_active()){ + PORTC &= ~DOT; // blink dots + string_to_buffer(get_menu_text()); + _delay_ms(10); + } + else{ + uint8_t clock[3]; + cli(); + DS3231_read(clock); + sei(); + for(uint8_t i=0; i < 3; i++){ + SR_Buffer[5-(2*i)] = number_Font[clock[i] & 0x0F]; + SR_Buffer[5-(2*i)-1] = number_Font[(clock[i] & 0xF0)>>4]; + } + PORTC = (PORTC & (~DOT)) | (clock[0]%2) << 4; // blink dots + _delay_ms(100); } - PORTC = (PORTC & (~DOT)) | (clock[0]%2) << 4; // blink dots print_SR_Buffer(); - _delay_ms(100); } } @@ -176,17 +227,22 @@ void setup(){ //Encoder rotate ISR(INT0_vect) { if (PIND & ENCODER_B) { - printf("step-\n\r"); - OCR2A--; + if(get_menu_active()) + menu_up_down(1); + else + OCR2A++; } else { - OCR2A++; - printf("step+\n\r"); + if(get_menu_active()) + menu_up_down(-1); + else + OCR2A--; } } //Encoder Button ISR(INT1_vect) { printf("button\n\r"); + enter_menu(); } ISR(USART0_RX_vect) diff --git a/code/src/menu.c b/code/src/menu.c new file mode 100644 index 0000000..72b2642 --- /dev/null +++ b/code/src/menu.c @@ -0,0 +1,113 @@ +#include "menu.h" +#include +#include + +menu_state_t state = OFF; +uint8_t selected = 0; + +int8_t menu_val_timezone = -1; +uint8_t menu_val_format = 24; + +//void debug_menu(){ +// printf("state: %d\r\n", state); +// printf("selected: %d\r\n", selected); +// printf("timezone: %d\r\n", menu_val_timezone); +// printf("\r\n"); +//} + +uint8_t get_menu_active(){ + if(state != OFF) + return 1; + return 0; +} + +const char *text[] = { + "test ", + " test" +}; + +void enter_menu(){ + switch(state){ + case OFF: + state = 1; + break; + case EXIT: + state = OFF; + break; + default: + selected ^= 1; + break; + } + //debug_menu(); +} + +const char* get_menu_text(){ + char *res = " "; + + memset(res, ' ', 6); + if(selected) + switch(state){ + case TIMEZONE: + res = " "; + if(menu_val_timezone < 0) + res[3] = '-'; + else + res[3] = ' '; + res[4] = abs(menu_val_timezone / 10)+0x30; + res[5] = abs(menu_val_timezone % 10)+0x30; + break; + case FORMAT: + res[2] = (menu_val_format / 10) + 0x30; + res[3] = (menu_val_format % 10) + 0x30; + res[4] = 'H'; + break; + default: + res = "ERROR"; + break; + } + else + switch(state){ + case TIMEZONE: + res = "ZONE "; + break; + case FORMAT: + res = "FORMAT"; + break; + case EXIT: + res = "ZURUEK"; + break; + default: + res = "ERROR"; + break; + } + return res; +} + +void menu_up_down(uint8_t up_down){ + if(selected) + switch(state){ + case TIMEZONE: + menu_val_timezone += up_down; + if(menu_val_timezone < -12) + menu_val_timezone = 12; + if(menu_val_timezone > 12) + menu_val_timezone = -12; + break; + case FORMAT: + if(menu_val_format == 12) + menu_val_format = 24; + else if(menu_val_format == 24) + menu_val_format = 12; + break; + default: + break; + } + else{ + state += up_down; + if(state < 1) + state = EXIT; + if(state > 3) + state = TIMEZONE; + } + //debug_menu(); +}