SimpleRotary

 1#include <ItemList.h>
 2#include <ItemToggle.h>
 3#include <ItemWidget.h>
 4#include <LcdMenu.h>
 5#include <MenuScreen.h>
 6#include <SimpleRotary.h>
 7#include <display/LiquidCrystal_I2CAdapter.h>
 8#include <input/SimpleRotaryAdapter.h>
 9#include <renderer/CharacterDisplayRenderer.h>
10#include <widget/WidgetList.h>
11#include <widget/WidgetRange.h>
12
13#define LCD_ROWS 2
14#define LCD_COLS 16
15
16// Declare the callbacks
17void callback(int pos);
18void toggleBacklight(bool isOn);
19
20static const char* colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
21static const uint8_t COLORS_COUNT = sizeof(colors) / sizeof(colors[0]);
22
23// clang-format off
24MENU_SCREEN(mainScreen, mainItems,
25    ITEM_BASIC("Connect to WiFi"),
26    ITEM_WIDGET(
27        "Color",
28        [](const char* color) { Serial.println(color); },
29        WIDGET_LIST(colors, COLORS_COUNT, 0, "%s")),
30    ITEM_BASIC("Blink SOS"),
31    ITEM_WIDGET(
32        "Dist",
33        callback,
34        WIDGET_RANGE(0, 1, 0, 50, "%dm", 1, true)),
35    ITEM_TOGGLE("Backlight", toggleBacklight),
36    ITEM_BASIC("Blink random"));
37// clang-format on
38
39LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
40LiquidCrystal_I2CAdapter lcdAdapter(&lcd);
41CharacterDisplayRenderer renderer(&lcdAdapter, LCD_COLS, LCD_ROWS);
42LcdMenu menu(renderer);
43SimpleRotary encoder(2, 3, 4);
44SimpleRotaryAdapter rotaryInput(&menu, &encoder);
45
46void setup() {
47    Serial.begin(9600);
48    renderer.begin();
49    menu.setScreen(mainScreen);
50}
51
52void loop() { rotaryInput.observe(); }
53
54// Define the callbacks
55void toggleBacklight(bool isOn) {
56    lcdAdapter.setBacklight(isOn);
57}
58
59void callback(int pos) {
60    Serial.println(pos);
61}