Menu iterating problem. Best way to code this - Page 1
Forum section: electronics / general

author: conducteur
author_rank: Regular Contributor
timestamp: February 22, 2015, 05:31:29 pm
content: 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 again6)When the select button is pressed, the subsequent action should be performedMy 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?

author: Dinsdale
author_rank: Regular Contributor
timestamp: February 22, 2015, 06:09:51 pm
content: 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.

author: conducteur
author_rank: Regular Contributor
timestamp: February 22, 2015, 09:04:18 pm
content: Good idea with the cursor... and fixed length to...

author: westfw
author_rank: Super Contributor
timestamp: February 23, 2015, 12:11:03 am
content: Easiest way: redraw the entire menu, replacing the space before the selected item with your special symbol.Code:for (i=0; i<4;i++) {if (i == selected)lcdprint(arrow);elselcdprint(space);lcdprint(menu);}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.)

author: JSTFLK
author_rank: Newbie
timestamp: February 27, 2015, 07:56:29 pm
content: 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)

author: Mechatrommer
author_rank: Super Contributor
timestamp: February 28, 2015, 04:32:49 am
content: 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.