SimpleRotary

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