Skip to content

Commit

Permalink
Revert "Replace RotaryNavConfig with RotaryInputAdapter" (#205)
Browse files Browse the repository at this point in the history
Revert "Replace `RotaryNavConfig` with `RotaryInputAdapter` (#204)"

This reverts commit 78b85eb.
  • Loading branch information
forntoh authored Sep 17, 2024
1 parent 78b85eb commit bd65b59
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 82 deletions.
18 changes: 13 additions & 5 deletions examples/InputRotary/InputRotary.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <ItemSubMenu.h>
#include <LcdMenu.h>
#include <SimpleRotary.h>
#include <input/RotaryInputAdapter.h>
#include <interface/LiquidCrystalI2CAdapter.h>
#include <utils/RotaryNavConfig.h>

#define LCD_ROWS 2
#define LCD_COLS 16
Expand All @@ -31,18 +31,26 @@ SUB_MENU(
ITEM_INPUT_CHARSET("User", charset, inputCallback),
ITEM_COMMAND("Clear", clearInput));

SimpleRotary encoder(2, 3, 4);

LiquidCrystalI2CAdapter lcdAdapter(0x27, LCD_COLS, LCD_ROWS);
LcdMenu menu(lcdAdapter);
RotaryInputAdapter rotaryInput(&menu, &encoder);

SimpleRotary encoder(2, 3, 4);

RotaryNavConfig menuConfig = {
.encoder = &encoder,
.menu = &menu,
.longPressDuration = 1000,
};

void setup() {
Serial.begin(9600);
menu.initialize(mainMenu);
}

void loop() { rotaryInput.observe(); }
void loop() {
// Call the handleRotaryMenu function, passing the menuConfig instance
processWithRotaryEncoder(&menuConfig);
}

// Define the callbacks
void inputCallback(char* value) {
Expand Down
18 changes: 13 additions & 5 deletions examples/SimpleRotary/SimpleRotary.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <ItemToggle.h>
#include <LcdMenu.h>
#include <SimpleRotary.h>
#include <input/RotaryInputAdapter.h>
#include <interface/LiquidCrystalI2CAdapter.h>
#include <utils/RotaryNavConfig.h>

Expand Down Expand Up @@ -33,18 +32,27 @@ MAIN_MENU(
ITEM_TOGGLE("Backlight", toggleBacklight),
ITEM_BASIC("Blink random"));

SimpleRotary encoder(2, 3, 4);

LiquidCrystalI2CAdapter lcdAdapter(0x27, LCD_COLS, LCD_ROWS);
LcdMenu menu(lcdAdapter);
RotaryInputAdapter rotaryInput(&menu, &encoder);

SimpleRotary encoder(2, 3, 4);

RotaryNavConfig menuConfig = {
.encoder = &encoder,
.menu = &menu,
.longPressDuration = 1000,
.doublePressThreshold = 500,
};

void setup() {
Serial.begin(9600);
menu.initialize(mainMenu);
}

void loop() { rotaryInput.observe(); }
void loop() {
// Call the handleRotaryMenu function, passing the menuConfig instance
processWithRotaryEncoder(&menuConfig);
}

// Define the callbacks
void toggleBacklight(uint16_t isOn) {
Expand Down
72 changes: 0 additions & 72 deletions src/input/RotaryInputAdapter.h

This file was deleted.

65 changes: 65 additions & 0 deletions src/utils/RotaryNavConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <LcdMenu.h>
#include <SimpleRotary.h>

/**
* @brief Configuration for rotary encoder navigation in the LCD menu.
*
* @param encoder Pointer to the rotary encoder instance
* @param menu Pointer to the LCD menu instance
* @param doublePressThreshold Duration (ms) to consider a double press
* @param longPressDuration Duration (ms) to consider a long press
* @param lastPressTime The last time the button was pressed
* @param pendingEnter Flag to indicate if an enter action is pending
*/
struct RotaryNavConfig {
SimpleRotary* encoder;
LcdMenu* menu;
uint16_t longPressDuration;
uint16_t doublePressThreshold;
unsigned long lastPressTime;
bool pendingEnter;
};

/**
* @brief Handles rotary encoder navigation in the LCD menu.
*
* @param config Pointer to the RotaryNavConfig struct
*/
void processWithRotaryEncoder(RotaryNavConfig* config) {
// Handle rotary encoder rotation
uint8_t rotation = config->encoder->rotate();
if (rotation == 1) {
config->menu->process(DOWN); // Call DOWN action
} else if (rotation == 2) {
config->menu->process(UP); // Call UP action
}

// Handle button press (short, long, and double press)
uint8_t pressType = config->encoder->pushType(config->longPressDuration);
unsigned long currentTime = millis();

if (pressType == 1) {
if (config->pendingEnter) {
if (config->doublePressThreshold > 0 &&
currentTime - config->lastPressTime < config->doublePressThreshold) {
config->menu->process(BACKSPACE); // Call BACKSPACE action (double press)
config->pendingEnter = false;
}
} else {
config->pendingEnter = true;
config->lastPressTime = currentTime;
}
} else if (pressType == 2) {
config->menu->process(BACK); // Call BACK action (long press)
config->pendingEnter = false;
}

// Check if the doublePressThreshold has elapsed for pending enter action
if ((!config->menu->lcd.getEditModeEnabled() && config->pendingEnter) ||
(config->pendingEnter && (currentTime - config->lastPressTime >= config->doublePressThreshold))) {
config->menu->process(ENTER); // Call ENTER action (short press)
config->pendingEnter = false;
}
}

0 comments on commit bd65b59

Please sign in to comment.