void drawMainMenu(uint8_t menuPosition){ char *mainMenuStrings[MAIN_MENU_ITEMS] = {"Last swim", "New Swim"}; uint8_t i, h; u8g_uint_t w, d; u8g_SetFont(&u8g, u8g_font_6x13); u8g_SetFontRefHeightText(&u8g); u8g_SetFontPosTop(&u8g); h = u8g_GetFontAscent(&u8g) - u8g_GetFontDescent(&u8g); w = u8g_GetWidth(&u8g); char vccChar[10]; my_itoa(vcc, vccChar, 10); // Doing the actual drawing u8g_FirstPage(&u8g); do{ for (i = 0; i < MAIN_MENU_ITEMS; i++){ d = (w - u8g_GetStrWidth(&u8g, mainMenuStrings[i])) / 2; u8g_SetDefaultForegroundColor(&u8g); if (i == menuPosition){ u8g_DrawBox(&u8g, 0, i*h+1, w, h); u8g_SetDefaultBackgroundColor(&u8g); } u8g_DrawStr(&u8g, d, i*h, mainMenuStrings[i]); } u8g_SetDefaultForegroundColor(&u8g); u8g_DrawFrame(&u8g, 0, 0, 128, 64); u8g_DrawStr(&u8g, 5, 50, vccChar); } while (u8g_NextPage(&u8g)); }
// Lua: u8g.setFontRefHeightText( self ) static int lu8g_setFontRefHeightText( lua_State *L ) { lu8g_userdata_t *lud; if ((lud = get_lud( L )) == NULL) return 0; u8g_SetFontRefHeightText( LU8G ); return 0; }
void menu_draw(menu_t *menu, u8g_t *u8g) { uint8_t i, h; static char ramstr[32]; menu_buttons_t *b; u8g_uint_t w, d; if(!menu) return; // Calculate text size u8g_SetFont(u8g, u8g_font_5x7); u8g_SetFontRefHeightText(u8g); u8g_SetFontPosTop(u8g); h = u8g_GetFontAscent(u8g)-u8g_GetFontDescent(u8g); w = u8g_GetWidth(u8g); for( i = 0;; i++ ) { // draw all menu items // Copy string from program memory to a work string in RAM strncpy_P(ramstr, (PGM_P)pgm_read_word(&(menu->strings[i])), sizeof(ramstr)); ramstr[31] = 0; // Zero length string marks end of string list. if(!ramstr[0]) break; // Get its length in pixels d = (w-u8g_GetStrWidth(u8g, ramstr))/2; // Set foreground color u8g_SetDefaultForegroundColor(u8g); // If item selected if ( i == menu->selected ) { // current selected menu item u8g_DrawBox(u8g, 0, i*h+1, w, h); // draw cursor bar u8g_SetDefaultBackgroundColor(u8g); } // Display the string u8g_DrawStr(u8g, d, i*h, ramstr); } // Remember menu item count menu->item_count = i; // Ensure text and background are set back to normal u8g_SetDefaultForegroundColor(u8g); // If there are soft buttons, draw them here if(menu->buttons){ for(b = menu->buttons; b; b = b->next){ strncpy_P(ramstr, b->label, sizeof(ramstr)); ramstr[31] = 0; u8g_DrawStr(u8g, b->col, b->row, ramstr); } } }
int main(void) { u8g_t u8g; u8g_Init(&u8g, &u8g_dev_sdl_1bit_h); u8g_FirstPage(&u8g); do { u8g_SetColorIndex(&u8g, 1); u8g_DrawPixel(&u8g,0,0); u8g_DrawPixel(&u8g,0,1); u8g_SetFont(&u8g, u8g_font_unifont); //u8g_SetFont(&u8g, u8g_font_10x20); //u8g_SetFont(&u8g, u8g_font_gdb17); u8g_SetFontRefHeightText(&u8g); //u8g_SetRefHeightAll(&u8g); //u8g_SetFontPosBaseline(&u8g); //u8g_SetFontPosCenter(&u8g); u8g_SetFontPosTop(&u8g); glyph(&u8g, 5,25, 'g'); u8g_SetFontPosCenter(&u8g); glyph(&u8g, 5+25,25, 'g'); u8g_SetFontPosBaseline(&u8g); glyph(&u8g, 5+2*25,25, 'g'); u8g_SetFontPosBottom(&u8g); glyph(&u8g, 5+3*25,25, 'g'); }while( u8g_NextPage(&u8g) ); while( u8g_sdl_get_key() < 0 ) ; return 0; }
void draw_menu(void) { uint8_t i, h; u8g_uint_t w, d; u8g_SetFont(&u8g, u8g_font_6x13); u8g_SetFontRefHeightText(&u8g); u8g_SetFontPosTop(&u8g); h = u8g_GetFontAscent(&u8g)-u8g_GetFontDescent(&u8g); w = u8g_GetWidth(&u8g); for( i = 0; i < MENU_ITEMS; i++ ) { d = (w-u8g_GetStrWidth(&u8g, menu_strings[i]))/2; u8g_SetDefaultForegroundColor(&u8g); if ( i == menu_current ) { u8g_DrawBox(&u8g, 0, i*h+1, w, h); u8g_SetDefaultBackgroundColor(&u8g); } u8g_DrawStr(&u8g, d, i*h, menu_strings[i]); } }