ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SingleButtonMenuHandler.h
Go to the documentation of this file.
1/**
2 * @file SingleButtonMenuHandler.h
3 * @brief Declares the MenuHandler template class for managing single-button menu interactions.
4 *
5 * This file defines the MenuHandler template class, which provides functionality for handling
6 * single-button menu navigation and persistent settings storage using EEPROM. It also includes
7 * an IntervalTimer for managing menu change timing.
8 *
9 * @date 22/12/2024
10 * @author Coela Can't
11 */
12
13#pragma once
14
15#include <Arduino.h> // Include for Arduino compatibility.
16#include <EEPROM.h> // Include for EEPROM functionality.
17#include <IntervalTimer.h> // Include for IntervalTimer support.
18
19/**
20 * @class MenuHandler
21 * @brief Manages single-button menu navigation and settings storage.
22 *
23 * The MenuHandler class provides functionality for navigating through menus and managing settings
24 * using a single button. It supports persistent storage through EEPROM and leverages an IntervalTimer
25 * for handling button hold and state transitions.
26 *
27 * @tparam menuCount The number of menus to manage.
28 */
29template <uint8_t menuCount>
30class MenuHandler {
31private:
32 static IntervalTimer menuChangeTimer; ///< Timer for handling menu change intervals.
33 static long previousMillisHold; ///< Tracks the last hold event time in milliseconds.
34 static uint16_t holdingTime; ///< Time threshold for detecting holding behavior in milliseconds.
35 static uint8_t currentMenu; ///< Index of the currently active menu.
36 static uint8_t currentValue[menuCount]; ///< Array of current values for each menu.
37 static uint8_t maxValue[menuCount]; ///< Array of maximum values for each menu.
38 static uint8_t pin; ///< The pin number associated with the button.
39 static bool holdingState; ///< Indicates whether the button is in a holding state.
40 static bool previousState; ///< Tracks the previous state of the button.
41
42 /**
43 * @brief Updates the button state and menu navigation.
44 */
45 static void UpdateState();
46
47 /**
48 * @brief Reads a value from EEPROM at the specified index.
49 *
50 * @param index The EEPROM address to read from.
51 * @return The value read from EEPROM.
52 */
53 static uint8_t ReadEEPROM(uint16_t index);
54
55 /**
56 * @brief Writes a value to EEPROM at the specified index.
57 *
58 * @param index The EEPROM address to write to.
59 * @param value The value to write to EEPROM.
60 */
61 static void WriteEEPROM(uint16_t index, uint8_t value);
62
63public:
64 /**
65 * @brief Begins the menu handling process, setting up necessary states.
66 */
67 static void Begin();
68
69 /**
70 * @brief Initializes the MenuHandler with specified parameters.
71 *
72 * @param pin The pin number associated with the button.
73 * @param holdingTime The time threshold for detecting holding behavior in milliseconds.
74 * @return True if initialization was successful, false otherwise.
75 */
76 static bool Initialize(uint8_t pin, uint16_t holdingTime);
77
78 /**
79 * @brief Sets a default value for a specific menu.
80 *
81 * @param menu The menu index to set the default value for.
82 * @param value The default value to set.
83 */
84 static void SetDefaultValue(uint16_t menu, uint8_t value);
85
86 /**
87 * @brief Marks the menu system as initialized.
88 */
89 static void SetInitialized();
90
91 /**
92 * @brief Sets the maximum value for a specific menu.
93 *
94 * @param menu The menu index to set the maximum value for.
95 * @param maxValue The maximum value to set.
96 */
97 static void SetMenuMax(uint8_t menu, uint8_t maxValue);
98
99 /**
100 * @brief Retrieves the current value of a specific menu.
101 *
102 * @param menu The menu index to retrieve the value from.
103 * @return The current value of the menu.
104 */
105 static uint8_t GetMenuValue(uint8_t menu);
106
107 /**
108 * @brief Retrieves the index of the currently active menu.
109 *
110 * @return The index of the active menu.
111 */
112 static uint8_t GetCurrentMenu();
113};
114
115#include "SingleButtonMenuHandler.tpp" // Include the template implementation.
Manages multiple menus and settings using Adafruit NeoTrellis.
static bool Initialize(uint8_t pin, uint16_t holdingTime)
Initializes the MenuHandler with specified parameters.
static void UpdateState()
Updates the button state and menu navigation.
static void WriteEEPROM(uint16_t index, uint8_t value)
Writes a value to EEPROM at the specified index.
static void SetInitialized()
Marks the menu system as initialized.
static void SetMenuMax(uint8_t menu, uint8_t maxValue)
Sets the maximum value for a specific menu.
static long previousMillisHold
Tracks the last hold event time in milliseconds.
static uint8_t currentMenu
Index of the currently active menu.
static uint8_t GetMenuValue(uint8_t menu)
Retrieves the current value of a specific menu.
static uint8_t maxValue[menuCount]
Array of maximum values for each menu.
static uint8_t pin
The pin number associated with the button.
static void Begin()
Begins the menu handling process, setting up necessary states.
static IntervalTimer menuChangeTimer
Timer for handling menu change intervals.
static bool previousState
Tracks the previous state of the button.
static uint8_t GetCurrentMenu()
Retrieves the index of the currently active menu.
static uint16_t holdingTime
Time threshold for detecting holding behavior in milliseconds.
static bool holdingState
Indicates whether the button is in a holding state.
static void SetDefaultValue(uint16_t menu, uint8_t value)
Sets a default value for a specific menu.
static uint8_t currentValue[menuCount]
Array of current values for each menu.
static uint8_t ReadEEPROM(uint16_t index)
Reads a value from EEPROM at the specified index.