ButtonWithRotaryAdapter

 1// Disable double press detection
 2#define DOUBLE_PRESS_THRESHOLD 0
 3
 4#include <Button.h>
 5#include <ItemInputCharset.h>
 6#include <ItemList.h>
 7#include <ItemToggle.h>
 8#include <LcdMenu.h>
 9#include <MenuScreen.h>
10#include <SimpleRotary.h>
11#include <display/LiquidCrystal_I2CAdapter.h>
12#include <input/ButtonAdapter.h>
13#include <input/SimpleRotaryAdapter.h>
14#include <renderer/CharacterDisplayRenderer.h>
15
16#define LCD_ROWS 2
17#define LCD_COLS 16
18
19// Declare the callbacks
20void colorsCallback(uint8_t pos);
21void toggleBacklight(bool isOn);
22void inputCallback(char* value);
23
24String colors[] = {"Red", "Green", "Blue", "Orange", "Aqua", "Yellow", "Purple", "Pink"};
25
26// clang-format off
27MENU_SCREEN(mainScreen, mainItems,
28    ITEM_INPUT_CHARSET("User", (const char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZ", inputCallback),
29    ITEM_STRING_LIST("Color", colors, 8, colorsCallback),
30    ITEM_TOGGLE("Backlight", toggleBacklight),
31    ITEM_BASIC("Placeholder 1"),
32    ITEM_BASIC("Placeholder 2"));
33// clang-format on
34
35LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
36LiquidCrystal_I2CAdapter lcdAdapter(&lcd);
37CharacterDisplayRenderer renderer(&lcdAdapter, LCD_COLS, LCD_ROWS);
38LcdMenu menu(renderer);
39SimpleRotary encoder(2, 3, 4);
40SimpleRotaryAdapter rotaryInput(&menu, &encoder);  // Rotary input adapter
41Button backspaceBtn(11);
42ButtonAdapter backspaceBtnA(&menu, &backspaceBtn, BACKSPACE);  // Push button for backspace
43
44void setup() {
45    backspaceBtn.begin();
46    renderer.begin();
47    Serial.begin(9600);
48    menu.setScreen(mainScreen);
49}
50
51void loop() {
52    rotaryInput.observe();
53    backspaceBtnA.observe();
54}
55
56// Define the callbacks
57void toggleBacklight(bool isOn) {
58    lcdAdapter.setBacklight(isOn);
59}
60
61void colorsCallback(uint8_t pos) {
62    Serial.println(colors[pos]);
63}
64
65void inputCallback(char* value) {
66    Serial.println(value);
67}