Update InputManager to record any button hold time
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#ifndef INPUT_MANAGER_H
|
||||
#define INPUT_MANAGER_H
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
@@ -8,52 +7,100 @@ class InputManager {
|
||||
InputManager();
|
||||
void begin();
|
||||
uint8_t getState();
|
||||
|
||||
/**
|
||||
* Updates the button states. Should be called regularly in the main loop.
|
||||
*/
|
||||
void update();
|
||||
bool isPressed(uint8_t buttonIndex);
|
||||
bool wasPressed(uint8_t buttonIndex);
|
||||
bool wasReleased(uint8_t buttonIndex);
|
||||
|
||||
/**
|
||||
* Returns true if the button was being held at the time of the last #update() call.
|
||||
*
|
||||
* @param buttonIndex the button indexes
|
||||
* @return the button current press state
|
||||
*/
|
||||
bool isPressed(uint8_t buttonIndex) const;
|
||||
|
||||
/**
|
||||
* Returns true if the button went from unpressed to pressed between the last two #update() calls.
|
||||
*
|
||||
* This differs from #isPressed() in that pressing and holding a button will cause this function
|
||||
* to return true after the first #update() call, but false on subsequent calls, whereas #isPressed()
|
||||
* will continue to return true.
|
||||
*
|
||||
* @param buttonIndex
|
||||
* @return the button pressed state
|
||||
*/
|
||||
bool wasPressed(uint8_t buttonIndex) const;
|
||||
|
||||
/**
|
||||
* Returns true if any button started being pressed between the last two #update() calls
|
||||
*
|
||||
* @return true if any button started being pressed between the last two #update() calls
|
||||
*/
|
||||
bool wasAnyPressed() const;
|
||||
|
||||
/**
|
||||
* Returns true if the button went from pressed to unpressed between the last two #update() calls
|
||||
*
|
||||
* @param buttonIndex the button indexes
|
||||
* @return the button release state
|
||||
*/
|
||||
bool wasReleased(uint8_t buttonIndex) const;
|
||||
|
||||
/**
|
||||
* Returns true if any button was released between the last two #update() calls
|
||||
*
|
||||
* @return true if any button was released between the last two #update() calls
|
||||
*/
|
||||
bool wasAnyReleased() const;
|
||||
|
||||
/**
|
||||
* Returns the time between any button starting to be depressed and all buttons between released
|
||||
*
|
||||
* @return duration in milliseconds
|
||||
*/
|
||||
unsigned long getHeldTime() const;
|
||||
|
||||
// Button indices
|
||||
static const uint8_t BTN_BACK = 0;
|
||||
static const uint8_t BTN_CONFIRM = 1;
|
||||
static const uint8_t BTN_LEFT = 2;
|
||||
static const uint8_t BTN_RIGHT = 3;
|
||||
static const uint8_t BTN_UP = 4;
|
||||
static const uint8_t BTN_DOWN = 5;
|
||||
static const uint8_t BTN_POWER = 6;
|
||||
static constexpr uint8_t BTN_BACK = 0;
|
||||
static constexpr uint8_t BTN_CONFIRM = 1;
|
||||
static constexpr uint8_t BTN_LEFT = 2;
|
||||
static constexpr uint8_t BTN_RIGHT = 3;
|
||||
static constexpr uint8_t BTN_UP = 4;
|
||||
static constexpr uint8_t BTN_DOWN = 5;
|
||||
static constexpr uint8_t BTN_POWER = 6;
|
||||
|
||||
// Pins
|
||||
static constexpr int BUTTON_ADC_PIN_1 = 1;
|
||||
static constexpr int BUTTON_ADC_PIN_2 = 2;
|
||||
static constexpr int POWER_BUTTON_PIN = 3;
|
||||
|
||||
// Power button methods
|
||||
bool isPowerButtonPressed();
|
||||
bool isPowerButtonPressed() const;
|
||||
|
||||
// Button names
|
||||
static const char* getButtonName(uint8_t buttonIndex);
|
||||
|
||||
private:
|
||||
int getButtonFromADC(int adcValue, const int thresholds[], int numButtons);
|
||||
int getButtonFromADC(int adcValue, const int ranges[], int numButtons);
|
||||
|
||||
uint8_t currentState;
|
||||
uint8_t lastState;
|
||||
uint8_t pressedEvents;
|
||||
uint8_t releasedEvents;
|
||||
unsigned long lastDebounceTime;
|
||||
unsigned long powerButtonPressStart;
|
||||
bool powerButtonWasPressed;
|
||||
unsigned long buttonPressStart;
|
||||
unsigned long buttonPressFinish;
|
||||
|
||||
static const int BUTTON_ADC_PIN_1 = 1;
|
||||
static const int NUM_BUTTONS_1 = 4;
|
||||
static const int ADC_THRESHOLDS_1[];
|
||||
static constexpr int NUM_BUTTONS_1 = 4;
|
||||
static const int ADC_RANGES_1[];
|
||||
|
||||
static const int POWER_BUTTON_PIN = 3;
|
||||
static constexpr int NUM_BUTTONS_2 = 2;
|
||||
static const int ADC_RANGES_2[];
|
||||
|
||||
static const int BUTTON_ADC_PIN_2 = 2;
|
||||
static const int NUM_BUTTONS_2 = 2;
|
||||
static const int ADC_THRESHOLDS_2[];
|
||||
|
||||
static const int ADC_TOLERANCE = 200;
|
||||
static const int ADC_NO_BUTTON = 3800;
|
||||
static const unsigned long DEBOUNCE_DELAY = 5;
|
||||
static constexpr int ADC_NO_BUTTON = 3800;
|
||||
static constexpr unsigned long DEBOUNCE_DELAY = 5;
|
||||
|
||||
static const char* BUTTON_NAMES[];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user