esmp3/src/buttons.cpp

47 lines
1.1 KiB
C++

#include <Arduino.h>
#include "buttons.h"
#include "config.h"
void Buttons::setup() {
pinMode(PIN_BTN_VOL_UP, INPUT);
pinMode(PIN_BTN_VOL_DOWN, INPUT);
pinMode(PIN_BTN_TRACK_NEXT, INPUT);
pinMode(PIN_BTN_TRACK_PREV, INPUT);
}
void Buttons::_debounce() {
unsigned long now = millis();
_debounce_until = now + DEBOUNCE_MILLIS;
if (_debounce_until < now) _debounce_until = now;
}
void Buttons::loop() {
bool vol_up = digitalRead(PIN_BTN_VOL_UP);
bool vol_down = digitalRead(PIN_BTN_VOL_DOWN);
bool track_next = digitalRead(PIN_BTN_TRACK_NEXT);
bool track_prev = digitalRead(PIN_BTN_TRACK_PREV);
if (_debounce_until > millis()) {
if (vol_up || vol_down || track_next || track_prev) {
_debounce();
}
return;
}
if (vol_up) {
_controller->vol_up();
} else if (vol_down) {
_controller->vol_down();
} else if (track_next) {
_controller->track_next();
} else if (track_prev) {
_controller->track_prev();
} else {
// If we reach this, no button was pressed and we are not debouncing -> do nothing.
return;
}
// If we reach this, some button was pressed. So enable debouncing.
_debounce();
}