Add missing buttons.h

pull/15/head
D.R.racer 2021-05-19 08:52:33 +02:00 committed by DRracer
parent ba8eacfe33
commit 7ece1fb28d
1 changed files with 28 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <stdint.h>
#include "../hal/adc.h" #include "../hal/adc.h"
/// Buttons are built on top of the raw ADC API /// Buttons are built on top of the raw ADC API
@ -8,16 +9,38 @@
namespace modules { namespace modules {
struct Button { struct Button {
inline constexpr Button()
: state(State::Waiting)
, tmp(false)
, pressed(false)
, timeLastChange(0) {}
/// @returns true if button is currently considered as pressed /// @returns true if button is currently considered as pressed
bool Pressed() const; inline bool Pressed() const { return pressed; }
/// State machine stepping routine
void Step(uint16_t time, bool press);
private: private:
uint8_t lastState; constexpr static const uint16_t debounce = 100; ///< time interval for debouncing @@TODO specify units
uint16_t timeElapsedLastChange; enum class State : uint_fast8_t { Waiting = 0,
Detected,
WaitForRelease,
Update };
State state;
bool tmp; ///< temporary state of button before the debouncing state machine finishes
bool pressed; ///< real state of button after debouncing
uint16_t timeLastChange;
}; };
class Buttons { class Buttons {
constexpr static const uint8_t N = 3; ///< number of buttons currently supported
constexpr static const uint8_t adc = 1; ///< ADC index - will be some define or other constant later on
static uint16_t tmpTiming;
public: public:
inline constexpr Buttons() = default;
/// State machine step - reads the ADC, processes debouncing, updates states of individual buttons /// State machine step - reads the ADC, processes debouncing, updates states of individual buttons
void Step(); void Step();
@ -26,7 +49,8 @@ public:
inline bool ButtonPressed(uint8_t index) const { return buttons[index].Pressed(); } inline bool ButtonPressed(uint8_t index) const { return buttons[index].Pressed(); }
private: private:
Button buttons[3]; ///< @@TODO parametrize/generalize? Button buttons[N];
static int8_t Sample();
}; };
} // namespace modules } // namespace modules