42 lines
1.1 KiB
C++
42 lines
1.1 KiB
C++
#include "Switch.hpp"
|
|
|
|
void Switch::Init(int pin,
|
|
float update_rate,
|
|
Type t,
|
|
Polarity pol,
|
|
uint8_t mode) {
|
|
last_update_ = millis();
|
|
updated_ = false;
|
|
state_ = 0x00;
|
|
t_ = t;
|
|
// Flip may seem opposite to logical direction,
|
|
// but here 1 is pressed, 0 is not.
|
|
flip_ = pol == POLARITY_INVERTED ? true : false;
|
|
pinMode(pin, mode);
|
|
pin_ = pin;
|
|
}
|
|
void Switch::Init(int pin, float update_rate) {
|
|
Init(pin,
|
|
update_rate,
|
|
TYPE_MOMENTARY,
|
|
POLARITY_INVERTED,
|
|
INPUT_PULLUP);
|
|
}
|
|
|
|
void Switch::Debounce() {
|
|
// update no faster than 1kHz
|
|
uint32_t now = millis();
|
|
updated_ = false;
|
|
|
|
if (now - last_update_ >= 1) {
|
|
last_update_ = now;
|
|
updated_ = true;
|
|
|
|
// shift over, and introduce new state.
|
|
const bool new_val = digitalRead(pin_);
|
|
state_ = (state_ << 1) | (flip_ ? !new_val : new_val);
|
|
// Set time at which button was pressed
|
|
if (state_ == 0x7f)
|
|
rising_edge_time_ = millis();
|
|
}
|
|
} |