Button handling implemented.

This commit is contained in:
Fabian Schlenz 2022-08-21 14:00:52 +02:00
parent 9c31f70c57
commit 4840c150c2
4 changed files with 46 additions and 8 deletions

View File

@ -14,6 +14,9 @@ class Controller {
Playlist current_playlist;
bool is_rfid_present = false;
unsigned long last_rfid_check = 0;
uint8_t button_pressed = 0;
unsigned long button_pressed_since = 0;
bool button_already_processed = false;
public:
void handle();

View File

@ -9,8 +9,8 @@
#define PIN_BTN_VOL_UP 32
#define PIN_BTN_VOL_DOWN 33
#define PIN_BTN_TRACK_NEXT 34
#define PIN_BTN_TRACK_PREV 35
#define PIN_BTN_TRACK_NEXT 17
#define PIN_BTN_TRACK_PREV 16
#define I2S_DOUT 25
#define I2S_BCLK 26

View File

@ -6,16 +6,25 @@ void Controller::handle() {
handle_rfid();
last_rfid_check = millis();
}
handle_buttons();
}
void Controller::handle_buttons() {
if (is_button_pressed(PIN_BTN_VOL_UP)) {
audio.setVolume(min(audio.getVolume()+1, 21));
log_i("BTN_VOL_UP pressed");
uint8_t vol = min(audio.getVolume()+1, 21);
log_d("Setting new volume %d", vol);
audio.setVolume(vol);
} else if (is_button_pressed(PIN_BTN_VOL_DOWN)) {
audio.setVolume(max(audio.getVolume()-1, 1));
log_i("BTN_VOL_DOWN pressed");
uint8_t vol = max(audio.getVolume()-1, 1);
log_d("Setting new volume %d", vol);
audio.setVolume(vol);
} else if (is_button_pressed(PIN_BTN_TRACK_NEXT)) {
log_i("BTN_TRACK_NEXT pressed");
next_track();
} else if (is_button_pressed(PIN_BTN_TRACK_PREV)) {
log_i("BTN_TRACK_PREV pressed");
prev_track();
}
}
@ -83,7 +92,10 @@ void Controller::next_track() {
}
void Controller::prev_track() {
if (audio.getAudioCurrentTime() <= 5) {
uint32_t time = audio.getAudioCurrentTime();
log_d("prev_track() called. getAudioCurrentTime() returns %d", time);
if (time >= 5) {
log_d("Restarting current track.");
current_playlist.restart();
play();
} else {
@ -98,3 +110,25 @@ void Controller::stop() {
current_playlist.set_current_time(audio.stopSong());
}
}
bool Controller::is_button_pressed(uint8_t pin) {
//log_d("Button %d state is %d", pin, digitalRead(pin));
if (!digitalRead(pin)) {
// Button is pressed - let's debounce it.
if (button_pressed == pin) {
if (button_pressed_since + 150 < millis() && !button_already_processed) {
button_already_processed = true;
return true;
}
} else {
button_pressed = pin;
button_pressed_since = millis();
button_already_processed = false;
}
} else {
if (button_pressed == pin) {
button_pressed = 0;
}
}
return false;
}

View File

@ -40,11 +40,12 @@ bool Playlist::next_track() {
}
bool Playlist::prev_track() {
log_d("Playlist::prev_track called. current_file is %d", current_file);
if (current_file == 0) {
return false;
}
set_current_position(0, 0);
} else {
set_current_position(current_file - 1, 0);
}
return true;
}