Menu for Display

Hi Oliver,
changing the pin 0 and 1 the menu it's working ok now (i will consider this for the future :slight_smile: ) ...

Very helpfull :slight_smile:

Good to read that you had some success :slight_smile:

Oliver

Thanks again :-), I'll let you know about the next steps :slight_smile:

Ciao Oliver :slight_smile: how are you,
so now that I've take more confidence with your menu I'd like to do a menu that when I turn on arduino put into display :
colums :
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

1°row| Arduino Welcome
2°row| Ver . 1.0
3°row| 06 - 02 - 2013
4°row| 10 : 23 : 55

and then just when I press the first time select button appears the other menu, then will be exit to all menu will return to this screen,

is possible from your point of view ? could you give me suggest ?

thanks for the support,
have nice day,
gnux

Sure, this is described in tutorial 6: Google Code Archive - Long-term storage for Google Code Project Hosting.

  • You start with an empty element: apply &m2_null_element as first element to M2tk. This will disable all menues.
  • draw your personal intro screen (as described above), by using the normal LiquidCrystal commands.
  • In the main "loop()" do the following:
  if ( m2.getRoot() == &m2_null_element ) {
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&el_my_own_top_menu);
  }

This will activate your menu once the user has pressed a key.

  • A soon as the user has finished working with your menu, use
         m2.setRoot(&m2_null_element);

to disable your menu again. Of course you need to redraw your own screen again.
More details are given in tutorial 6

Oliver

Ciao Oliver,
thanks for the information,
I try and however I'll let you know :slight_smile: and for example if i would like put in display time is better to use "normal LiquidCrystal commands" also ? and when i re-draw my screen again I call procedure after m2.setRoot(&m2_null_element); ? Is there a simply example that I can use for take confidence ?

so how do you will implement it ?

thanks 10000000 again :-),
gnnux

Is there a simply example that I can use for take confidence ?
Unfortunately i did not made an example for LiquidCrystal, but you can take over the GLCD tutorial 1:1.
In the "loop()" procedure, check for the &m2_null_element as root element and if this is true, than redraw your time screen
I hope all is written in tutorial 6

Oliver

Ciao Oliver,
I've just tried to do this simple try with a simple combo example ... below the code:

#include <LiquidCrystal.h>
#include "M2tk.h"
#include "utility/m2ghlc.h"

LiquidCrystal lcd(10, 9, 3, 2, 1, 0);

uint8_t uiKeySelectPin = 22;
uint8_t uiKeyNextPin = 26;

uint8_t select_color = 0;
uint8_t select_priority = 0;

void fn_ok(m2_el_fnarg_p fnarg) {
  /* accept selection */
}

void fn_cancel(m2_el_fnarg_p fnarg) {
  /* discard selection */
}

const char *fn_idx_to_color(uint8_t idx)
{
  if ( idx == 0 )
    return "red";
  else if (idx == 1 )
    return "green";
  return "blue";
}

const char *fn_idx_to_priority(uint8_t idx)
{
  switch(idx)
  {
    case 0: return "lowest";
    case 1: return "low";
    case 2: return "medium";
    case 3: return "high";
    case 4: return "highest";
  }
  return "";
}


M2_LABEL(el_label1, NULL, "Color:");
M2_COMBO(el_combo1, NULL, &select_color, 3, fn_idx_to_color);

M2_LABEL(el_label2, NULL, "Prio.: ");
M2_COMBO(el_combo2, "v1", &select_priority, 5, fn_idx_to_priority);

M2_BUTTON(el_cancel, NULL, "cancel", fn_cancel);
M2_BUTTON(el_ok, NULL, " ok ", fn_ok);

M2_LIST(list) = { 
    &el_label1, &el_combo1, 
    &el_label2, &el_combo2,  
    &el_cancel, &el_ok 
};
M2_GRIDLIST(list_element, "c2",list);
M2tk m2(&list_element, m2_es_arduino, m2_eh_2bs, m2_gh_lc);

void setup() {
  m2_SetLiquidCrystal(&lcd, 20, 4);
  m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
  m2.setPin(M2_KEY_NEXT, uiKeyNextPin);
}

void loop() {
  if ( m2.getRoot() == &m2_null_element ) {
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);
  }
}

When I push button nothing appears I've tried from serial menu also but nothing appears... so now I've a trouble ...
into the condition

m2.getRoot() == &m2_null_element

is necessary to assign "&m2_null_element" into the setup ? if yes is there particular sintax to follow ?

because looking the sketch there is no assignment for and then the condition will be never verified right ?

thanks
gnux

Hi

Instead of

M2tk m2(&list_element, m2_es_arduino, m2_eh_2bs, m2_gh_lc);

use

M2tk m2(&m2_null_element m2_es_arduino, m2_eh_2bs, m2_gh_lc);

to assign the null element at the beginning.

And then, the loop procedure MUST be expanded to this (see also the mentioned tutorial!!!)

void loop(void) {
 if ( m2.getRoot() == &m2_null_element ) {
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);
  }
  m2.checkKey();
  if ( m2.handleKey() ) {
    m2.draw();
  }
}

You must not forget to call checkKey(),

Oliver

Thanks Oliver for your precious assistance ...
this was a terrible night ... so I was wake up at 3:45 in the morning ... then I m put in front the computer for try to work ... I will go home i will try ...

Thanks 10000
Gnux

Ciao Oliver,
I've did a couple of try ...
so i don't understand why I've like into tutorial explain I've put :

M2_ROOT(el_switch_to_graphics, NULL, "Show Graphics", &m2_null_element);

and I've tried also:

if ( m2.getRoot() == &m2_null_element ) {
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);
  }
  m2.checkKey();
  if ( m2.handleKey() ) {
    m2.draw();
  }

like your suggestion seems that the condition is always verified because I've put serial debug print for see where the program pass and pass to here when I press the button:

if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);

... if put == 0 ...to here :

m2.getRoot() == &m2_null_element

the menu has been disappeared but when i check press the button the menu doesn't appers ... where could be the issues ? i very appreciate your support

thanks
gnux

hmm... i have some difficulties to understand your question. But maybe i should clarify the code a little bit with some comments:

  // check for manual written screen
  if ( m2.getRoot() == &m2_null_element ) 
  {
     // yes, it is our manual written screen
     // place your manual commands here
     lcd.print("...");
     ...
     // check if the user has pressed a key while manual screen is active
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);   // refer to a suitable menu

  }

  // do normal menu display  
  m2.checkKey();
  if ( m2.handleKey() ) {
    m2.draw();
  }

maybe you can repeat your question including a more complete code example...

Oliver

Good Morning Oliver, sorry if i didn't answer to you yestarday evening but I was completed boiled ... :*
So for example this below reported is a simple Combo Example Simple Sketch where Im trying to:

  • at the start show nothing
  • then when I press key show my combo menu

So from Theoretically stand point I've understand and I'm ok what you mean :slight_smile: at least :-), but trying to use the code below reported is not working ... So with the normal example everything is working, display, button are ok ... So maybe I did a error in some other part but I don't where to be honest because is a simple example and I've just change the condition and null element and put the condition ...

#include <LiquidCrystal.h>
#include "M2tk.h"
#include "utility/m2ghlc.h"

LiquidCrystal lcd(10, 9, 3, 2, 1, 0);

uint8_t uiKeySelectPin = 22;
uint8_t uiKeyNextPin = 26;

uint8_t select_color = 0;
uint8_t select_priority = 0;

void fn_ok(m2_el_fnarg_p fnarg) {
  /* accept selection */
}

void fn_cancel(m2_el_fnarg_p fnarg) {
  /* discard selection */
}

const char *fn_idx_to_color(uint8_t idx)
{
  if ( idx == 0 )
    return "red";
  else if (idx == 1 )
    return "green";
  return "blue";
}

const char *fn_idx_to_priority(uint8_t idx)
{
  switch(idx)
  {
    case 0: return "lowest";
    case 1: return "low";
    case 2: return "medium";
    case 3: return "high";
    case 4: return "highest";
  }
  return "";
}


M2_LABEL(el_label1, NULL, "Color:");
M2_COMBO(el_combo1, NULL, &select_color, 3, fn_idx_to_color);

M2_LABEL(el_label2, NULL, "Prio.: ");
M2_COMBO(el_combo2, "v1", &select_priority, 5, fn_idx_to_priority);

M2_BUTTON(el_cancel, NULL, "cancel", fn_cancel);
M2_BUTTON(el_ok, NULL, " ok ", fn_ok);

M2_LIST(list) = { 
    &el_label1, &el_combo1, 
    &el_label2, &el_combo2,  
    &el_cancel, &el_ok 
};
M2_GRIDLIST(list_element, "c2",list);

M2tk m2(&m2_null_element, m2_es_arduino, m2_eh_2bs, m2_gh_lc);
M2_ROOT(el_switch_to_graphics, NULL, "Show Graphics", &m2_null_element);

void setup() {
  // Serial.begin(9600);
  m2_SetLiquidCrystal(&lcd, 20, 4);
  m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
  m2.setPin(M2_KEY_NEXT, uiKeyNextPin);
}


void loop(void) {
if ( m2.getRoot() == &m2_null_element ) 
  {
     // yes, it is our manual written screen
     // place your manual commands here
     lcd.print("...");
     // check if the user has pressed a key while manual screen is active
      if ( m2.getKey() != M2_KEY_NONE )
        m2.setRoot(&list_element);   // refer to a suitable menu
  }

  // do normal menu display  
  m2.checkKey();
  if ( m2.handleKey() ) {
    m2.draw();
  }
}

So what I've note is this :

putting a simple like "debug print" for example:

void loop(void) {
    
if ( m2.getRoot() == &m2_null_element ) 
  {
     // yes, it is our manual written screen
     // place your manual commands here
    lcd.setCursor(0, 0); 
    lcd.print("Condition");
    delay(1000);
//     // check if the user has pressed a key while manual screen is active
       if ( m2.getKey() != M2_KEY_NONE  ) {
            lcd.setCursor(0, 4); 
            lcd.print("Verificata");
            lcd.setCursor(0, 4); 
            lcd.print("          ");
          m2.setRoot(&list_element);  // refer to a suitable menu
        }  
      lcd.setCursor(0, 0); 
      lcd.print("            ");
      delay(1000);
        
     m2.checkKey();   
}

}

The Condition is verified this ( m2.getRoot() == &m2_null_element ) one times ...then if enable also the second part

    m2.checkKey();
    if ( m2.handleKey() ) {
     m2.draw();
     }

execute at one times the first one part and then the if ( m2.handleKey() is verified to but for sure i didn't press but ... also because when appear a menu is stopped on the right element ...

Thanks
Gnux

wow and congratulation, you found a bug in M2tklib.
Thanks for that.

As a workaround, please extend the setup procedure to this:

void setup() {
  // Serial.begin(9600);
  m2_SetLiquidCrystal(&lcd, 20, 4);
  m2.setPin(M2_KEY_SELECT, uiKeySelectPin);
  m2.setPin(M2_KEY_NEXT, uiKeyNextPin);
  m2.getKey();   // this is new, (bugfix workaround)
}

I will create an issue for this.

Thanks, Oliver

thanks to you Oliver, i ll try and i'll let you know :slight_smile:

have nice week end
gnux

Ciao Oliver,
now it's working ... :slight_smile:

Thanks 10000
Gnux

great :slight_smile:
Oliver

Ciao Oliver :slight_smile:

so how are you :slight_smile: so I've tried your suggest and it's working :slight_smile: thanks ...

so now I would ask to you ... in my program I've stored my devices status and if are active timer ...

My devices numeration start from 0 until 31 ... do you know a simply way for display this stored value into into the menu ? below reported an example:

N° dev: 0 State: Active Timer: Inactive ... mainly I need to find a easy way to have all under control ... the best should be in a unique windows but I know that with a display 20,4 lcd is not easy ...

however thanks for the support,
have nice day,
Andrea

Hi Andrea

Glad to read that you still work with m2tklib on your menu. Hope that all required functionality is included.

My devices numeration start from 0 until 31 ... do you know a simply way for display this stored value into into the menu ?

The following examples assume, that the device is in a global variable "device"
uint8_t device;

Method 1:
Use M2_U8NUM(xyz, "r1", &device) with a read only flag if you only want to display the value.

Method 2:
char linebuf[30];
...
sprintf(linebuf, "blabla dev:%d more blabla", device);
Use M2_LABEL(xyz, NULL, linebuf)
The full line is rendered into a buffer of sufficient size. Disadvantage: "linebuf" needs to be updated when device is changed.

Method 3:
Use M2_LABELFN() similar to Method 2, but the callback procedure returns the string. See Wiki... Removes the disadvantage from above.

Method 4:
You can always do a LiquidCrystal.print directly on the screen. This is not a problem as long as your output does not overlap the menues. Usually you want this output only if a selected root element is active (similar to the problem with the initial splash screen)

Oliver

Thanks Oliver however great :slight_smile: !

so from my point of view i think that the best could be "Method 2" or "Method 3" if not understood bad the difference is that with the "Method 3" is possible run a procedure ?

with this method for example could i do a cycle for change the "linebuf" ? I will be update automatically on display ? or each time will be necessary press button for update it ?

another thing that I would like to ask you is that ... on my menu now for example when I entry into the menu for "turn on" or "turn off" off the devices I see a strange value example:

Actually Menu:

Dev: 85 State: 255
back ok zero

How should be:

Dev: 00 State: 0
back ok zero

when I press select button the value come back to 0 like should be and also the state after press select button come back to 0 like should be ...

for sure the variable are initialized to 0 before entry into the menu

Thanks 1000
Andrea