Menu for Display

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

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 ?

The content of linebuf will be displayed whenever you execute m2.draw(). Anything else is under your control. This means: If you update linebuf you should call m2.draw() to make your changes visiable.

Actually Menu:

Dev: 85 State: 255
back ok zero

M2tklib will display the value of the "connected" variable. Nothing else. Are you sure that there is no programming error, assigning 85 to the device value?

Oliver

Thanks for the information,
I've intercept the issues ... I've change the type of variable from uint8_t to byte ...
the logical part was ok, but probably there was a "memory overlapping" between variable ... is possible ?

Thanks I'll let you know about the next steps :slight_smile:

ps it's pleasure working with your library :slight_smile:

thanks
gnux

Ciao Oliver,
during the weekend, I've thought how to use ... so I'll try to use a mixed mode to put &null element to display for disable the menu and then enable the menu again ...

So, Id like to do is also ... so now i didn't have the exit button ... and for didn't wasting memory I was thing to this:

I want that when I'm inside my menu when I click on something the menu will go on the welcome screen ...
for do that I've thing to put a "Exit" voices into the menu is possible ? Is possible also do something without didn't a sub menu ? so for example to the voice exit when I click the select but will go directly to welcome screen ...

Thanks for the information :slight_smile:

warm regards,
gnux

Hi

This should work with the "HOME" button. This means, do not use the Exit button, but instead the HOME button (M2_KEY_HOME). Assign the "home" menu with setHome(). This also could be the &null element.

Oliver

Good Morning Oliver,
And for do that I need physically another button correct ? :slight_smile: so it's very complete your menu ! Many Congratulations ... !!! :slight_smile: So I promise to you that when I'll finish the project I'll send to you a gift :slight_smile: please send to me in private if you want without you I was completely lost ...

So the final things that I want do for the moment is that ... in attachment you can see a a combo menu ... I've take this simple example from a library ... one time that i will press ok I will store the value devices and the "Is" name (only the index) inside my array ... for example like this:

array[0][13] = idx;
array[0][0] = &u8num;

Then I would like retrieve this value inside another menù:
I would like that for example when I'm inside the menu device when I click on "Dev: 01" Will appear kitchen ...
In this way I can say what Im managing ...

Could be possible for you ? make sense ? :slight_smile:

Thanks again :slight_smile:
gnux

Schermata 2013-02-20 a 07.19.48.png

I think can I use
M2_LABELFN();
:slight_smile:

looking the tutorial seems fine :slight_smile:
correct ?

regards,
gnux

Hi

You do not need another button. Replace the one for the Exit with the Home Button: Exchange EXIT with HOME...

And yes, M2_LABELFN will do the job.
A one or two pictures of your device would be the best reward that i can imagine.

Oliver