Dynamically interacting with different arrays

Hi,

I'm looking for ways to interact with different arrays depending on user input. I have different lists of menu items that can be shown on a small LCD screen depending on where the user is navigating. So, for example, while you're in the navigation menu, you'd see only menu items related to navigation. Then, when you select another menu, the interface is refreshed and new menu items are shown.

My first thought was to create a generic function that accepts an array as an argument and that displays the chars contained in this array. But this seemed not possible since different menus might differ in the number of items they offer.

Secondly, I thought I could have a variable that holds a reference to the currently active menu, but that seemed impossible for the same reason as above. For example:

char *navigationMenuItems[] = {"Previous", "Current", "Next"};
char *settingsMenuItems[] = {"Volume", "Controls", "Brightness", ...}
currentMenu = settingsMenuItems

Lastly, I tried creating an array that contained all the other menu item arrays like so:

char *navigationMenuItems[] = {"Previous", "Current", "Next"};
char *settingsMenuItems[] = {"Volume", "Controls", "Brightness", ...}
char **allMenus[][] = {navigationMenuItems, settingsMenuItems};

But again, the compiler would not let me run this, no matter what I tried.

Any ideas here? I'm coming from a web dev perspective and this way of dealing with data is very foreign to me.

Thank you!

Two ideas:

1- Use the function but have an additional parameter where you pass the number of menu items.

2- Have a separator (for example, \0) that is recognized to end a section.

I would go with option (1) above. Declare 2D arrays of characters, with a column size that can accommodate the largest character string, say 12. Don't forget to count the zero byte terminator.

char navigationMenuItems[3][12] = {"Previous", "Current", "Next"};
char settingsMenuItems[4][12] = {"Volume", "Controls", "Brightness", "Tint"};

void print_menu(int n, char menu[][12]) {  //n=number of rows
  for (int i = 0; i < n; i++) {
    Serial.println(menu[i]);
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  print_menu(3, navigationMenuItems); //3 strings total
  Serial.println("...");
  print_menu(4, settingsMenuItems); //4 strings total
}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

Previous
Current
Next
...
Volume
Controls
Brightness
Tint

another possible solution (ran it in online C compiler since I do not have an arduino at hand! :wink: )

#include<stdio.h>


char *settingsMenuItems[4] = {"Controls","Volume", "Brightness", "Tint"}; //4 = number of menus

char *navigationMenuItems[4][3] = { {"Previous", "Current", "Next"},
                                    {"Up", "Down", "Mute"},
                                    {"Up", "Down",""},
                                    {"Increase","Decrease",""}}; //4 = number of menus, 3= MAXIMUM number of navigation items per menu
char navigationMenulength[4] = {3,3,2,2}; //this array declares actual number of navigation items per menu

void print_menu_settings(int j) {
  
  if(j<4){
    printf("Selected Menu: %s\n",settingsMenuItems[j]);
    
    printf("\nNavigation Menu Items: ");
    for(int k=0; k<navigationMenulength[j];++k){
        printf("%s\n\t\t\t\t\t   ",navigationMenuItems[j][k]);   
    }
  }

}

int main() {
    print_menu_settings(0);
    printf("\n--------------------\n");
    print_menu_settings(2);
}

Output:

Selected Menu: Controls

Navigation Menu Items: Previous
		       Current
		       Next				   
--------------------
Selected Menu: Brightness

Navigation Menu Items: Up
		       Down

hope that helps....

Hi all,

I decided to use classes instead to hold the data for the menus, this way I could couple a menu name with a callback to be executed when the user does an action.

Your replies were very insightful in any case so thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.