codex init
This commit is contained in:
28
OpticalChannelTester/Buttons.cpp
Normal file
28
OpticalChannelTester/Buttons.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "Buttons.h"
|
||||
#include "Config.h"
|
||||
|
||||
void Button::begin() {
|
||||
pinMode(pin_, BUTTON_ACTIVE_LEVEL == LOW ? INPUT_PULLUP : INPUT_PULLDOWN);
|
||||
raw_ = stable_ = (digitalRead(pin_) == BUTTON_ACTIVE_LEVEL);
|
||||
changedAt_ = millis();
|
||||
}
|
||||
|
||||
ButtonEvent Button::update(uint32_t now) {
|
||||
const bool sample = (digitalRead(pin_) == BUTTON_ACTIVE_LEVEL);
|
||||
if (sample != raw_) { raw_ = sample; changedAt_ = now; }
|
||||
if (raw_ != stable_ && now - changedAt_ >= BUTTON_DEBOUNCE_MS) {
|
||||
stable_ = raw_;
|
||||
if (stable_) {
|
||||
pressedAt_ = now; nextRepeat_ = now + BUTTON_LONG_PRESS_MS + BUTTON_REPEAT_DELAY_MS;
|
||||
longSent_ = false;
|
||||
} else if (!longSent_) return ButtonEvent::SHORT;
|
||||
}
|
||||
if (stable_ && !longSent_ && now - pressedAt_ >= BUTTON_LONG_PRESS_MS) {
|
||||
longSent_ = true; return ButtonEvent::LONG;
|
||||
}
|
||||
if (stable_ && longSent_ && now >= nextRepeat_) {
|
||||
nextRepeat_ += BUTTON_REPEAT_MS; return ButtonEvent::REPEAT;
|
||||
}
|
||||
return ButtonEvent::NONE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user