Forum >LCD12864 menu structure
ArduinoGeneral

LCD12864 menu structure

userHead Steve50j 2016-03-03 10:33:22 2558 Views2 Replies
Hi

I just received my new LCD12864 arduino shield and I want to use it to make a menu structure. After trying different code examples I cant seem to get the menu to scroll down.

I tested the joystick and it is working. I am not a strong coder so if someone can tell me where my problem is I would reaaaally appreciate it.

see code below

Thanks


Code: Select all
#include "U8glib.h"

// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The complete list of supported devices is here: http://code.google.com/p/u8glib/wiki/device
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);


#define KEY_NONE 0
#define KEY_PREV 1
#define KEY_NEXT 2
#define KEY_SELECT 3
#define KEY_BACK 4

/* DOGS102 shield configuration values
uint8_t uiKeyPrev = 2;
uint8_t uiKeyNext = 4;
uint8_t uiKeySelect = 5;
uint8_t uiKeyBack = 3;
*/
/* DOGM128-Shield configuration values
// DOGXL60-Shield configuration values
uint8_t uiKeyPrev = 7;
uint8_t uiKeyNext = 3;
uint8_t uiKeySelect = 2;
uint8_t uiKeyBack = 8;
*/
uint8_t uiKeyCodeFirst = KEY_NONE;
uint8_t uiKeyCodeSecond = KEY_NONE;
uint8_t uiKeyCode = KEY_NONE;

int adc_key_in;
int key=-1;
int oldkey=-1;


/*Convert ADC Value to key number

4
|
0--1--3
|
2

*/

int get_key(unsigned int input)
{
if(input<10) return 4;
else if (input<200) return 0;
else if (input<400) return 2;
else if (input<600) return 1;
else if (input<800) return 3;
else return -1;

}


void uiStep(void) {
Serial.println("uiStep");
adc_key_in = analogRead(0);
key = get_key(adc_key_in);
if(key!= oldkey)
{
delay(50);
adc_key_in = analogRead(0);
key = get_key(adc_key_in);
if(key!= oldkey)
{
oldkey = key;
if(key >=0){
uiKeyCodeFirst = KEY_BACK;
}
else if (key==0)
uiKeyCodeFirst = KEY_SELECT;
else if (key==1)
uiKeyCodeFirst = KEY_NEXT;
else if (key==2)
uiKeyCodeFirst = KEY_PREV;
else if (key==4)
uiKeyCodeFirst = KEY_NONE;

uiKeyCode = uiKeyCodeFirst;
}
}
delay(100);
}




#define MENU_ITEMS 4
char *menu_strings[MENU_ITEMS] = { "First Line", "Second Item", "3333333", "abcdefg" };

uint8_t menu_current = 0;
uint8_t menu_redraw_required = 0;
uint8_t last_key_code = KEY_NONE;


void drawMenu(void) {
uint8_t i, h;
u8g_uint_t w, d;

u8g.setFont(u8g_font_6x13);
u8g.setFontRefHeightText();
u8g.setFontPosTop();

h = u8g.getFontAscent()-u8g.getFontDescent();
w = u8g.getWidth();
for( i = 0; i < MENU_ITEMS; i++ ) {
d = (w-u8g.getStrWidth(menu_strings[i]))/2;
u8g.setDefaultForegroundColor();
if ( i == menu_current ) {
u8g.drawBox(0, i*h+1, w, h);
u8g.setDefaultBackgroundColor();
}
u8g.drawStr(d, i*h, menu_strings[i]);
}
}

void updateMenu(void)
{
Serial.println("update menu");
switch (uiKeyCode){
case KEY_NEXT:
menu_current++;
if (menu_current >= MENU_ITEMS) menu_current = 0;
menu_redraw_required = 1;
break;
case KEY_PREV:
if (menu_current == 0) menu_current - MENU_ITEMS;
menu_current--;
menu_redraw_required = 1;
break;
}
uiKeyCode = KEY_NONE;

}


void setup() {
// rotate screen, if required
// u8g.setRot180();

Serial.begin(9600);
uiStep(); // setup key detection and debounce algorithm
u8g.setContrast(0);
menu_redraw_required = 1; // force initial redraw
}

void loop() {

Serial.println("loop");
uiStep(); // check for key press
updateMenu();

if ( menu_redraw_required != 0 ) {
u8g.firstPage();
do {
drawMenu();
} while( u8g.nextPage() );
menu_redraw_required = 0;
}

}
2016-03-04 21:07:05 Thanks for the reply

I will go check out that link.
userHeadPic Steve50j
2016-03-03 21:57:11 If you haven't already, check out this resource:
https://code.google.com/archive/p/u8gli ... etPrintPos
I'm not so sure about scrolling down, but I know you can make multiple "pages" of text by scrolling left and right, details are in there
We will investigate further in the mean time...
userHeadPic Maht