Author Topic: Menu iterating problem. Best way to code this  (Read 3438 times)

0 Members and 1 Guest are viewing this topic.

Offline conducteurTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: be
Menu iterating problem. Best way to code this
« on: February 22, 2015, 05:31:29 pm »
I have a problem, i think it's quite simple to solve, but don't know how to do it.

1) On a 16*2 lcd, i want to implement a menu (think i have found a way to do that, that's not my problem)
2)In every menu-item, i want to display up to four words. Each word represents an option in the menu. When selecting an option, an action should happen (go to next menu item, or do some i/o stuff)
3)Before each option, there's a space, and i've created a special character (=an arrow >).
4)at startup, the arrow should point to the first option. The first option starts always on cursor position (0,1), so i've space in the upper left corner for the arrow.
5) When you press the menu-option-button, the arrow should point to the next option on the first line. (don't know where this is, depends on the length of each option-word). If there are 3 or 4 options on the display, the arrow should move further when the button is pressed again
6)When the select button is pressed, the subsequent action should be performed

My question: What's the most efficiënt way to move the pointer around on the screen? Don't have an OOP language it's for Just Another Language. I've made a method/function that erases the arrow and places it on another position. (i.e movecursortonewposition(new collumn position, new row position).

I need something like a for loop, but only loop through it once when the button is pressed, and that for max 4 times? How to know where to put the pointer for option nr 2 and 4 wich are on the right side? The position is depends on the length of the words for options 1 and 3 which are one the left on the display?
 

Offline Dinsdale

  • Regular Contributor
  • *
  • Posts: 77
  • Country: us
    • pretzelogic
Re: Menu iterating problem. Best way to code this
« Reply #1 on: February 22, 2015, 06:09:51 pm »
With 16 characters on a line, you don't have much room for menu items. To simplify greatly, I would make all menu item names the same length (include spaces for shorter names). Now you know the address of every menu item (and I think it will look better).
You might also consider just placing the cursor on the 1st character of the "active" item instead of using an arrow. You don't have to draw/erase the arrow and commands for cursor placement are built in to the display. Also, you do not have to keep the 1st character on a line empty to accommodate your arrow.
You could use a variable to track the current menu and another variable to track the item. On a selection, combine the two variables and create a pointer to a function, possibly an index into an array of functions.
You definitely do not require OOP for this. C would be fine. I write simple menus in assembly all the time.
This can't be happening.
 

Offline conducteurTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: be
Re: Menu iterating problem. Best way to code this
« Reply #2 on: February 22, 2015, 09:04:18 pm »
Good idea with the cursor... and fixed length to...
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Menu iterating problem. Best way to code this
« Reply #3 on: February 23, 2015, 12:11:03 am »
Easiest way: redraw the entire menu, replacing the space before the selected item with your special symbol.
Code: [Select]
for (i=0; i<4;i++) {
  if (i == selected)
    lcdprint(arrow);
  else
    lcdprint(space);
  lcdprint(menu[i]);
}
Code space efficient too: your draw and your update routine are the same code.  It could be a bit slow and visually glitchy, depending on how your LCD routines work.  (it shouldn't be.  There's only 32 characters, and you don't need to clear the display before updates.)
 

Offline JSTFLK

  • Newbie
  • Posts: 3
Re: Menu iterating problem. Best way to code this
« Reply #4 on: February 27, 2015, 07:56:29 pm »
Have you looked into the tinyMenu lib that's floating around on the web?   
 
It's a bit complex for what it does, but it offers everything you've asked for.     

Here's a video I made a while ago of it in action.



Changing the dimensions of the LCD only requires tweaking a variable.

(see attached)
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11651
  • Country: my
  • reassessing directives...
Re: Menu iterating problem. Best way to code this
« Reply #5 on: February 28, 2015, 04:32:49 am »
i havent read all requirement, but menu iterating and limited screen? another way is to code in a fashion of state machine. buttons press and pointers values will act/point differently based on current state... yes some sort of menu structure will be required... if you dont want menu structure, you can embed it in the "state code" smogashbord type of code but well, can be suited for very limited resources processor. ymmv.
« Last Edit: February 28, 2015, 04:35:07 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf