EncoderManager

Rotary encoder input processing

#include <EncoderManager.h>

Public Member Functions

 EncoderManager ()
 
void init ()
 
void update ()
 
int getMovement ()
 
bool getClicked ()
 
bool getLongPressed ()
 
bool getUltraLongPressed ()
 
bool isButtonDown () const
 
int getPressTime () const
 
int getLastPressDuration () const
 

Constructor & Destructor Documentation

◆ EncoderManager()

CloudMouse::Hardware::EncoderManager::EncoderManager ( )

Constructor - prepares encoder manager instance Initializes encoder hardware interface but does not configure GPIO Call init() separately for actual hardware setup

Member Function Documentation

◆ getClicked()

bool CloudMouse::Hardware::EncoderManager::getClicked ( )

Check for button click event and reset flag Returns true if a short button press was detected and automatically resets flag

Returns
true if click detected (press duration < 500ms), false otherwise

Click Definition:

  • Button press followed by release within 500ms
  • Most common interaction for selection/confirmation
  • Excludes long press and ultra-long press events

Usage Examples:

◆ getLastPressDuration()

int CloudMouse::Hardware::EncoderManager::getLastPressDuration ( ) const

Get duration of last completed button press Returns the duration of the most recent press-release cycle

Returns
Duration of last press in milliseconds, 0 if no press recorded

Usage Examples:

  • Analytics: logInteraction(getLastPressDuration());
  • Adaptive UI: adjustSensitivity(getLastPressDuration());
  • Debug logging: Serial.printf("Last press: %dms", getLastPressDuration());

◆ getLongPressed()

bool CloudMouse::Hardware::EncoderManager::getLongPressed ( )

Check for long press event and reset flag Returns true if a long button press was detected and automatically resets flag

Returns
true if long press detected (1000ms <= duration < 3000ms), false otherwise

Long Press Definition:

  • Button held for 1000ms or longer but less than 3000ms
  • Typically used for context menus or secondary functions
  • May include haptic feedback (buzzer) at threshold

Usage Examples:

◆ getMovement()

int CloudMouse::Hardware::EncoderManager::getMovement ( )

Get accumulated encoder movement and reset counter Returns total rotation movement since last call and automatically resets to zero

Returns
Rotation delta in encoder clicks Positive values = clockwise rotation Negative values = counter-clockwise rotation Zero = no movement since last call

Resolution: 4 hardware counts per physical detent (0.25° per count) Range: -32768 to +32767 (16-bit signed integer)

Usage Examples:

◆ getPressTime()

int CloudMouse::Hardware::EncoderManager::getPressTime ( ) const

Get current button press duration in real-time Returns elapsed time since button press started, zero if not pressed

Returns
Current press duration in milliseconds, 0 if button not pressed

Usage Examples:

◆ getUltraLongPressed()

bool CloudMouse::Hardware::EncoderManager::getUltraLongPressed ( )

Check for ultra-long press event and reset flag Returns true if an ultra-long button press was detected and automatically resets flag

Returns
true if ultra-long press detected (duration >= 3000ms), false otherwise

Ultra-Long Press Definition:

  • Button held for 3000ms (3 seconds) or longer
  • Reserved for critical functions like factory reset
  • Event fires immediately when threshold is reached (not on release)
  • Provides immediate feedback for destructive operations

Usage Examples:

◆ init()

void CloudMouse::Hardware::EncoderManager::init ( )

Initialize encoder hardware and configure GPIO pins Sets up PCNT hardware, configures pin modes, and initializes state variables Must be called once during system initialization before any encoder operations

Hardware Setup:

  • Configures CLK and DT pins for quadrature input via PCNT
  • Enables internal pull-up on SW pin for button detection
  • Initializes encoder position tracking and button state
  • Sets up glitch filtering for noise immunity
Note
Not thread-safe - call only during single-threaded initialization

◆ isButtonDown()

bool CloudMouse::Hardware::EncoderManager::isButtonDown ( ) const

Check if button is currently pressed down Real-time status query that does not affect event flags

Returns
true if button is currently pressed (SW pin LOW), false if released

Usage Examples:

  • Visual feedback: if (isButtonDown()) highlightButton();
  • Continuous actions: while (isButtonDown()) performAction();
  • UI state indication: setButtonState(isButtonDown());

◆ update()

void CloudMouse::Hardware::EncoderManager::update ( )

Update encoder state and process input events Reads current encoder position, detects rotation movement, and processes button state Must be called regularly (10-20ms intervals) for responsive input handling

Processing Steps:

  1. Read current encoder position from PCNT hardware
  2. Calculate movement delta and accumulate for consumption
  3. Sample button state and detect press/release transitions
  4. Analyze press duration and set appropriate event flags
  5. Handle ongoing press feedback (buzzer, ultra-long detection)
Note
Call frequency affects responsiveness - 50-100Hz recommended