mirror of
https://github.com/clockworkpi/DevTerm.git
synced 2026-03-21 19:32:37 +01:00
borrwoed a lot code from https://github.com/foriequal0/devterm_keyboard in order to optimize the trackball
This commit is contained in:
53
Code/devterm_keyboard/debouncer.h
Normal file
53
Code/devterm_keyboard/debouncer.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef DEBOUNCER_H
|
||||
#define DEBOUNCER_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
typedef uint8_t millis_t;
|
||||
|
||||
const millis_t DEBOUNCE_MS = 5;
|
||||
|
||||
/**
|
||||
@brief Asymmetric debouncer
|
||||
*/
|
||||
class Debouncer {
|
||||
public:
|
||||
Debouncer();
|
||||
void updateTime(millis_t delta);
|
||||
bool sample(bool value);
|
||||
private:
|
||||
millis_t timeout;
|
||||
};
|
||||
|
||||
template<typename T, T millis>
|
||||
class Timeout {
|
||||
public:
|
||||
Timeout() {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
void updateTime(millis_t delta) {
|
||||
if (timeout > delta) {
|
||||
timeout -= delta;
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void expire() {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
bool get() const {
|
||||
return timeout == 0;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
timeout = millis;
|
||||
}
|
||||
private:
|
||||
uint16_t timeout;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user