DynamicMenu

 1#include <ItemCommand.h>
 2#include <ItemSubMenu.h>
 3#include <LcdMenu.h>
 4#include <MenuScreen.h>
 5#include <display/LiquidCrystal_I2CAdapter.h>
 6#include <input/KeyboardAdapter.h>
 7#include <renderer/CharacterDisplayRenderer.h>
 8
 9#define LCD_ROWS 2
10#define LCD_COLS 16
11
12// Declare the main menu and sub-menu
13extern MenuScreen* settingsScreen;
14
15LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
16LiquidCrystal_I2CAdapter lcdAdapter(&lcd);
17CharacterDisplayRenderer renderer(&lcdAdapter, LCD_COLS, LCD_ROWS);
18LcdMenu menu(renderer);
19KeyboardAdapter keyboard(&menu, &Serial);
20
21// clang-format off
22MENU_SCREEN(mainScreen, mainItems,
23    ITEM_BASIC("Placeholder 1"),
24    ITEM_SUBMENU("Dynamic >", settingsScreen),
25    ITEM_BASIC("Placeholder 2"),
26    ITEM_BASIC("Placeholder 3"));
27
28MENU_SCREEN(settingsScreen, settingsItems,
29    ITEM_COMMAND("Add network", []() {
30        // Dynamically add items to the screen
31        settingsScreen->addItem(ITEM_BASIC("Network Added"));
32        menu.refresh();
33    }),
34    ITEM_COMMAND("Add between", []() {
35        // Dynamically add items to the screen at a specific position
36        settingsScreen->addItemAt(1, ITEM_BASIC("Network Between"));
37        menu.refresh();
38    }),
39    ITEM_COMMAND("Remove last", []() {
40        // Dynamically remove the last item from the screen
41        settingsScreen->removeLastItem();
42        menu.refresh();
43    }),
44    ITEM_COMMAND("Remove between", []() {
45        // Dynamically remove an item from the screen at a specific position
46        settingsScreen->removeItemAt(1);
47        menu.refresh();
48    }));
49// clang-format on
50
51void setup() {
52    Serial.begin(9600);
53    renderer.begin();
54    menu.setScreen(mainScreen);
55}
56
57void loop() {
58    keyboard.observe();
59}