It's working more or less...
This commit is contained in:
@@ -1,13 +1,100 @@
|
||||
#include "controller.h"
|
||||
#include "esmp3.h"
|
||||
|
||||
void Controller::handle() {
|
||||
|
||||
if (last_rfid_check + 500 < millis() || last_rfid_check > millis()) {
|
||||
handle_rfid();
|
||||
last_rfid_check = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::handle_buttons() {
|
||||
|
||||
if (is_button_pressed(PIN_BTN_VOL_UP)) {
|
||||
audio.setVolume(min(audio.getVolume()+1, 21));
|
||||
} else if (is_button_pressed(PIN_BTN_VOL_DOWN)) {
|
||||
audio.setVolume(max(audio.getVolume()-1, 1));
|
||||
} else if (is_button_pressed(PIN_BTN_TRACK_NEXT)) {
|
||||
next_track();
|
||||
} else if (is_button_pressed(PIN_BTN_TRACK_PREV)) {
|
||||
prev_track();
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::handle_rfid() {
|
||||
|
||||
if (is_rfid_present) {
|
||||
byte buffer[2];
|
||||
byte buffer_size = 2;
|
||||
MFRC522Constants::StatusCode status = rfid->PICC_WakeupA(buffer, &buffer_size);
|
||||
if (status == MFRC522Constants::STATUS_OK) {
|
||||
// Card is still present.
|
||||
rfid->PICC_HaltA();
|
||||
} else {
|
||||
Serial.printf("RFID status is %s\n", MFRC522Debug::GetStatusCodeName(status));
|
||||
is_rfid_present = false;
|
||||
Serial.println("No more RFID card.\n");
|
||||
stop();
|
||||
}
|
||||
} else {
|
||||
if (rfid->PICC_IsNewCardPresent()) {
|
||||
if (rfid->PICC_ReadCardSerial()) {
|
||||
uint32_t uid = rfid->uid.uidByte[0]<<24 | rfid->uid.uidByte[1]<<16 | rfid->uid.uidByte[2]<<8 | rfid->uid.uidByte[3];
|
||||
Serial.printf("Found new rfid card with uid %x\n", uid);
|
||||
is_rfid_present = true;
|
||||
if (uid > 0) {
|
||||
String temp = String(uid, HEX);
|
||||
String s_uid = "";
|
||||
for (int i=0; i<(8-temp.length()); i++) {
|
||||
s_uid.concat("0");
|
||||
}
|
||||
s_uid.concat(temp);
|
||||
play(s_uid);
|
||||
}
|
||||
rfid->PICC_HaltA();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::play(String rfid_id) {
|
||||
if (!rfid_id.equals(current_playlist.get_rfid_id())) {
|
||||
if (pm->has_playlist(rfid_id)) {
|
||||
current_playlist = pm->get_playlist(rfid_id);
|
||||
play();
|
||||
} else {
|
||||
Serial.printf("There is no playlist for rfid_id %s\n", rfid_id.c_str());
|
||||
}
|
||||
} else {
|
||||
if (!audio.isRunning()) {
|
||||
play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::play() {
|
||||
String file = current_playlist.get_current_file_name();
|
||||
Serial.printf("Playing file: %s\n", file.c_str());
|
||||
audio.connecttoFS(SD, file.c_str(), current_playlist.get_current_time());
|
||||
}
|
||||
|
||||
void Controller::next_track() {
|
||||
if (current_playlist.next_track()) {
|
||||
play();
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::prev_track() {
|
||||
if (audio.getAudioCurrentTime() <= 5) {
|
||||
current_playlist.restart();
|
||||
play();
|
||||
} else {
|
||||
if (current_playlist.prev_track()) {
|
||||
play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::stop() {
|
||||
if (audio.isRunning()) {
|
||||
current_playlist.set_current_time(audio.stopSong());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user