BlinkenArea - GitList
Repositories
Blog
Wiki
mips_sys
Code
Commits
Branches
Tags
Search
Tree:
c89d999
Branches
Tags
master
mips_sys
fw
menu.c
implement menu on LCD
Stefan Schuermans
commited
c89d999
at 2012-04-04 22:26:27
menu.c
Blame
History
Raw
#include "config.h" #include "eth.h" #include "lcd.h" #include "macros.h" #include "menu.h" #include "switches.h" /// last value of rotary knob static unsigned int menu_cur_rot_val = 0; /// current screen static int menu_cur_screen = 0; /// current content on screen static char menu_cur_content[16]; /** * @brief convert unsigned integer to decimal * @param[in] uint unsigned integer value * @param[in] cnt number of digits * @param[out] digits value as decimal digits */ void menu_uint2dec(unsigned int uint, unsigned int cnt, char digits[]) { while (cnt > 0) { --cnt; digits[cnt] = '0' + uint % 10; uint /= 10; } } /** * @brief convert unsigned integer to hexadecimal * @param[in] uint unsigned integer value * @param[in] cnt number of digits * @param[out] digits value as decimal hexadigits */ void menu_uint2hex(unsigned int uint, unsigned int cnt, char digits[]) { unsigned int val; while (cnt > 0) { --cnt; val = uint & 15; digits[cnt] = val < 10 ? '0' + val : 'A' - 10 + val; uint >>= 4; } } /** * @brief update screen content * @param[in] str new content to display */ static void menu_update_content(const char *str) { unsigned int i; // update only characters that changed (LCD is slow) for (i = 0; str[i] != 0 && i < 16; ++i) { if (menu_cur_content[i] != str[i]) { menu_cur_content[i] = str[i]; lcd_chr(1, i, str[i]); } } // clear rest of line for (; i < 16; ++i) { if (menu_cur_content[i] != ' ') { menu_cur_content[i] = ' '; lcd_chr(1, i, ' '); } } } /// start screen static void menu_screen_start(void) { menu_update_content("turn knob..."); } /// switches screen static void menu_screen_switches(void) { char buf[11]; buf[0] = switches_get_state(sw_0) ? '0' : ' '; buf[1] = switches_get_state(sw_1) ? '1' : ' '; buf[2] = switches_get_state(sw_2) ? '2' : ' '; buf[3] = switches_get_state(sw_3) ? '3' : ' '; buf[4] = switches_get_state(sw_east) ? 'E' : ' '; buf[5] = switches_get_state(sw_north) ? 'N' : ' '; buf[6] = switches_get_state(sw_south) ? 'S' : ' '; buf[7] = switches_get_state(sw_west) ? 'W' : ' '; buf[8] = switches_get_state(sw_center) ? 'C' : ' '; buf[9] = switches_get_state(sw_rot_a) ? 'a' : ' '; buf[10] = switches_get_state(sw_rot_b) ? 'b' : ' '; buf[11] = 0; menu_update_content(buf); } /// ethernet screen static void menu_screen_eth(void) { char buf[] = "RX 0000 TX 0000"; menu_uint2dec(eth_rx_get_cnt(), 4, &buf[3]); menu_uint2dec(eth_tx_get_cnt(), 4, &buf[11]); menu_update_content(buf); } /// MAC address screen static void menu_screen_mac(void) { unsigned int i; char buf[] = "000000000000"; for (i = 0; i < 6; ++i) menu_uint2hex(config_mac.mac[i], 2, &buf[i * 2]); menu_update_content(buf); } /// IP address screen static void menu_screen_ip(void) { unsigned int i; char buf[] = "000.000.000.000"; for (i = 0; i < 4; ++i) menu_uint2dec(config_ip.ip[i], 3, &buf[i * 4]); menu_update_content(buf); } /// subnet mask screen static void menu_screen_netmask(void) { unsigned int i; char buf[] = "000.000.000.000"; for (i = 0; i < 4; ++i) menu_uint2dec(config_ip.mask[i], 3, &buf[i * 4]); menu_update_content(buf); } /// gateway IP screen static void menu_screen_gateway(void) { unsigned int i; char buf[] = "000.000.000.000"; for (i = 0; i < 4; ++i) menu_uint2dec(config_ip.gw[i], 3, &buf[i * 4]); menu_update_content(buf); } /// available screens static struct menu_screen { const char *title; ///< title of screen void (*update)(void); ///< function to update screen content } menu_screens[] = { { "MIPS I", menu_screen_start }, { "switches", menu_screen_switches }, { "ethernet", menu_screen_eth }, { "MAC address", menu_screen_mac }, { "IP address", menu_screen_ip }, { "subnet mask", menu_screen_netmask }, { "gateway IP", menu_screen_gateway }, }; /// initialize void menu_init(void) { lcd_init(); menu_cur_screen = 0; lcd_str(0, menu_screens[0].title); menu_screens[0].update(); } /// task procedure - call as often as possible void menu_task(void) { // get new screen to show unsigned int new_rot_cnt = switches_get_rot_cnt(); unsigned int new_rot_val = (new_rot_cnt + 2) / 4; // 1 click is 4 steps int delta = new_rot_val - menu_cur_rot_val; menu_cur_rot_val = new_rot_val; int new_screen = menu_cur_screen + delta; if (new_screen < 0) new_screen = count(menu_screens) - (-new_screen % count(menu_screens)); new_screen = new_screen % count(menu_screens); // screen changed -> show new title if (new_screen != menu_cur_screen) { menu_cur_screen = new_screen; lcd_str(0, menu_screens[menu_cur_screen].title); } // update current screen menu_screens[menu_cur_screen].update(); }