Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/feature/move-control-to-items'…
Browse files Browse the repository at this point in the history
… into move-logic-to-items
  • Loading branch information
ShishkinDmitriy committed Sep 17, 2024
2 parents 89e98df + afeaf80 commit 6b81788
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/compile-arduino.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Compile Examples
on: [pull_request_target, pull_request]
on: [pull_request]

jobs:
compile:
Expand All @@ -21,7 +21,7 @@ jobs:
fqbn: ${{ matrix.board.fqbn }}
enable-deltas-report: true
enable-warnings-report: true
sketches-report-path: ${{ vars.SKETCHES_REPORTS_PATH }}
sketches-report-path: ${{ vars.SKETCHES_REPORTS_PATH || 'sketches-reports' }}
libraries: |
- source-path: ./
- source-url: https://github.com/johnrickman/LiquidCrystal_I2C.git
Expand All @@ -32,4 +32,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: sketches-report-${{ matrix.board.artifact-name-suffix }}
path: ${{ vars.SKETCHES_REPORTS_PATH }}
path: ${{ vars.SKETCHES_REPORTS_PATH || 'sketches-reports' }}
6 changes: 3 additions & 3 deletions .github/workflows/pr_size_check.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PR Size Checker
on:
pull_request_target:
pull_request:
branches-ignore:
- master

Expand All @@ -12,5 +12,5 @@ jobs:
- uses: budougumi0617/action-pr-size-checker@v0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
max_added_count: ${{ vars.PR_SIZE_LIMIT }}
filter_pattern: ${{ vars.PR_SIZE_IGNORE_PATTERN }}
max_added_count: ${{ vars.PR_SIZE_LIMIT || 600 }}
filter_pattern: ${{ vars.PR_SIZE_IGNORE_PATTERN || '.*.yml|.*.json|.*.md|.*.puml|Gemfile|platformio.ini|wokwi.toml|keywords.txt|test/.*.cpp' }}
4 changes: 1 addition & 3 deletions examples/InputRotary/InputRotary.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ void setup() {
menu.initialize(mainMenu);
}

void loop() {
rotaryAdapter.observe();
}
void loop() { rotaryAdapter.observe(); }

// Define the callbacks
void inputCallback(char* value) {
Expand Down
4 changes: 1 addition & 3 deletions examples/SimpleRotary/SimpleRotary.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ void setup() {
menu.initialize(mainMenu);
}

void loop() {
rotaryAdapter.observe();
}
void loop() { rotaryAdapter.observe(); }

// Define the callbacks
void toggleBacklight(uint16_t isOn) {
Expand Down
62 changes: 33 additions & 29 deletions src/input/SimpleRotaryAdapter.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
#pragma once

#include "input/InputInterface.h"
#include <LcdMenu.h>
#include "InputInterface.h"
#include <SimpleRotary.h>

// Duration (ms) to consider a long press.
#define LONG_PRESS_DURATION 1000
// Duration (ms) to consider a double press.
#define DOUBLE_PRESS_THRESHOLD 500

/**
* @class SimpleRotaryAdapter
* @brief Adapter for handling rotary input for an LCD menu.
*
* This class interfaces with a rotary encoder to manage user input
* for navigating and selecting options in an LCD menu.
*
* It processes rotary encoder rotations to navigate through menu options
* and handles button presses for various actions, including:
* - Short press for selecting an option
* - Long press for going back
* - Double press for backspacing
*
* The values for long press duration (defined as LONG_PRESS_DURATION) and
* double press threshold (defined as DOUBLE_PRESS_THRESHOLD) can be
* overwritten by defining new ones with #define.
*
* @param menu Pointer to the LcdMenu instance that this adapter will control.
* @param encoder Pointer to the SimpleRotary instance representing the rotary encoder.
*/
class SimpleRotaryAdapter : public InputInterface {
protected:
/**
* @brief Pointer to the rotary encoder instance.
*/
SimpleRotary* encoder;
/**
* @brief The last time the button was pressed.
*/
unsigned long lastPressTime;
/**
* @brief Flag to indicate if an enter action is pending.
*/
bool pendingEnter;
private:
unsigned long lastPressTime = 0; // Last time the button was pressed
bool pendingEnter = false; // Flag to indicate if an enter action is pending
SimpleRotary* encoder; // Pointer to the SimpleRotary instance

public:
SimpleRotaryAdapter(LcdMenu* menu, SimpleRotary* encoder) : InputInterface(menu), encoder(encoder) {}
/**
* @brief Handles rotary encoder navigation in the LCD menu.
*/
SimpleRotaryAdapter(LcdMenu* menu, SimpleRotary* encoder)
: InputInterface(menu), encoder(encoder) {
}

void observe() override {
// Handle rotary encoder rotation
uint8_t rotation = encoder->rotate();
Expand All @@ -44,7 +49,7 @@ class SimpleRotaryAdapter : public InputInterface {

if (pressType == 1) {
if (pendingEnter) {
if (currentTime - lastPressTime < DOUBLE_PRESS_THRESHOLD) {
if (DOUBLE_PRESS_THRESHOLD > 0 && currentTime - lastPressTime < DOUBLE_PRESS_THRESHOLD) {
menu->process(BACKSPACE); // Call BACKSPACE action (double press)
pendingEnter = false;
}
Expand All @@ -57,11 +62,10 @@ class SimpleRotaryAdapter : public InputInterface {
pendingEnter = false;
}

// Check if the DOUBLE_PRESS_THRESHOLD has elapsed for pending enter action
if ((!menu->lcd.getEditModeEnabled() && pendingEnter) ||
(pendingEnter && (currentTime - lastPressTime >= DOUBLE_PRESS_THRESHOLD))) {
// Check if the doublePressThreshold has elapsed for pending enter action
if ((!menu->lcd.getEditModeEnabled() && pendingEnter) || (pendingEnter && (currentTime - lastPressTime >= DOUBLE_PRESS_THRESHOLD))) {
menu->process(ENTER); // Call ENTER action (short press)
pendingEnter = false;
}
}
};
};
9 changes: 9 additions & 0 deletions src/utils/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const byte MENU_ITEM_PROGRESS = 10;
#define LEFT 131 // >127
#define CLEAR 132 // >127
//
// Rotary encoder configuration
//
#ifndef LONG_PRESS_DURATION
#define LONG_PRESS_DURATION 1000
#endif
#ifndef DOUBLE_PRESS_THRESHOLD
#define DOUBLE_PRESS_THRESHOLD 300
#endif
//
const byte DOWN_ARROW[8] = {
0b00100, // *
0b00100, // *
Expand Down

0 comments on commit 6b81788

Please sign in to comment.