Menu Problem with reprap128x64

Dear all,

I am lost and trying to understand my code.

Behind is what i would like to do with my screen REPRAP 128x64:

And here is my code:

#include "U8glib.h"

U8GLIB_ST7920_128X64_1X u8g(23, 17, 16);

#define KEY_NONE 0
#define KEY_PREV 31
#define KEY_NEXT 33
#define KEY_SELECT 35
#define KEY_BACK 29

uint8_t uiKeyPrev = 33;
uint8_t uiKeyNext = 31;
uint8_t uiKeySelect = 35;
uint8_t uiKeyBack = 8;

uint8_t uiKeyCodeFirst = KEY_NONE;
uint8_t uiKeyCodeSecond = KEY_NONE;
uint8_t uiKeyCode = KEY_NONE;

void uiSetup(void) {
// configure input keys

pinMode(uiKeyPrev, INPUT_PULLUP);
pinMode(uiKeyNext, INPUT_PULLUP);
pinMode(uiKeySelect, INPUT_PULLUP);
pinMode(uiKeyBack, INPUT_PULLUP);
}

void uiStep(void) {
uiKeyCodeSecond = uiKeyCodeFirst;
if ( digitalRead(uiKeyPrev) == LOW )
uiKeyCodeFirst = KEY_PREV;
else if ( digitalRead(uiKeyNext) == LOW )
uiKeyCodeFirst = KEY_NEXT;
else if ( digitalRead(uiKeySelect) == LOW )
uiKeyCodeFirst = KEY_SELECT;
else if ( digitalRead(uiKeyBack) == LOW )
uiKeyCodeFirst = KEY_BACK;
else
uiKeyCodeFirst = KEY_NONE;

if ( uiKeyCodeSecond == uiKeyCodeFirst )
uiKeyCode = uiKeyCodeFirst;
else
uiKeyCode = KEY_NONE;
}

#define MENU_ITEMS 3
const char *menu_strings[MENU_ITEMS] = { "Position du robot", "Nombre de cycles", "Lancement Essai"};
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, ih+1, w, h);
u8g.setDefaultBackgroundColor();
}
u8g.drawStr(d, i
h, menu_strings[i]);
}
}

void updateMenu(void) {
if ( uiKeyCode != KEY_NONE && last_key_code == uiKeyCode ) {
return;
}
last_key_code = uiKeyCode;

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;

  case KEY_SELECT:
  if ( menu_current == 0 )
    menu_current = MENU_ITEMS;
  menu_redraw_required = 1;
  break;

}
}

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

uiSetup(); // setup key detection and debounce algorithm
menu_redraw_required = 1; // force initial redraw
}

void loop() {

uiStep(); // check for key press

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

updateMenu(); // update menu bar

}

I do not understand how can i reload a new screen when i press the select button. For exemble i did this kind of code:

I would like to do something like if it is the first line, and i press select, i go to the menu "position"
i add somethinkg lika that but it does not work:

#define MENU_ITEMS2 3
const char *menu_strings2[MENU_ITEMS] = { "Position du robot", "Nombre de cycles",

case KEY_SELECT:
if ( menu_current == 0 )
menu_current = MENU_ITEMS2;
menu_redraw_required = 1;
break;

If someone can help me to undertand how to change my screen when i press the select button...
Thanks a lot! :slight_smile:

This seems backward. It uses the new key code (uiKeyCodeFirst) only if it is the same as the previous key code (uiKeyCodeSecond). Wouldn't it make more sense to ignore the new key if it's the same as the previous key?