feat(menu): add menu class

This commit is contained in:
2026-04-28 19:54:32 +02:00
parent 2d2051e6c2
commit f2fbb9f205
2 changed files with 56 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#define MENU_ITEM_COUNT 4
#include <Arduino.h>
#include "joystick.hpp"
typedef struct {
String name;
void (*action)();
} Item;
class Menu {
public:
Menu(Item* items);
void updateCurrentItem(JoystickDirection& direction);
void execute();
Item& getItemAt(size_t index);
size_t getCurrentItemIndex() const;
private:
Item items[MENU_ITEM_COUNT];
int currentItem;
};
+34
View File
@@ -0,0 +1,34 @@
#include "menu.hpp"
Menu::Menu(Item* items) : currentItem(0) {
for (int i = 0; i < MENU_ITEM_COUNT; i++) {
this->items[i] = items[i];
}
}
void Menu::updateCurrentItem(JoystickDirection &direction) {
switch (direction) {
case JoystickDirection::UP:
currentItem = (currentItem - 1 + MENU_ITEM_COUNT) % MENU_ITEM_COUNT;
break;
case JoystickDirection::DOWN:
currentItem = (currentItem + 1) % MENU_ITEM_COUNT;
break;
default:
break;
}
}
void Menu::execute() {
if (items[currentItem].action) {
items[currentItem].action();
}
}
Item& Menu::getItemAt(size_t index) {
return items[index];
}
size_t Menu::getCurrentItemIndex() const {
return currentItem;
}