The ItemRange class is a wrapper around ItemWidget designed to simplify the creation of menu items that allow users to select a value within a specific range (e.g., volume, brightness, or temperature). It reduces boilerplate when an item only requires a single WidgetRange.
There are two primary ways to implement this:
ITEM_RANGE<T>(...): Creates a standalone item with a range of values.ITEM_RANGE_REF<T>(...): Creates an item that uses a reference to track and update the currently selected value, allowing the value to be updated or accessed externally.
Best Practices:
- Ensure the step size divides evenly into the range to prevent unexpected behavior at boundaries.
- Use simple numeric types like
int or float for performance-critical applications. - When using
ITEM_RANGE_REF, remember that external changes to the referenced variable will automatically be reflected in the menu display.
// Simple range usage
ITEM_RANGE<int>("Volume", 50, 5, 0, 100, [](const int value) {
Serial.println(value);
}, "%d%%")
// Range with reference usage
int selectedTemperature = 20;
ITEM_RANGE_REF<int>("Temperature", selectedTemperature, 1, 0, 40, [](const Ref<int> value) {
Serial.println(value.value);
}, "%d°C")