2019-08-02 21:48:36 +00:00
|
|
|
#include "controller.h"
|
2019-08-06 18:50:11 +00:00
|
|
|
#include "spi_master.h"
|
2019-08-02 21:48:36 +00:00
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
Controller::Controller(Player* p) {
|
|
|
|
_player = p;
|
|
|
|
_rfid = new MFRC522(PIN_RC522_CS, MFRC522::UNUSED_PIN);
|
|
|
|
|
|
|
|
SPIMaster::enable(PIN_RC522_CS);
|
|
|
|
Serial.println("Initializing MFRC522...");
|
|
|
|
_rfid->PCD_Init();
|
|
|
|
_rfid->PCD_DumpVersionToSerial();
|
|
|
|
SPIMaster::disable();
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Controller::loop() {
|
2019-08-08 03:31:27 +00:00
|
|
|
unsigned long now = millis();
|
|
|
|
if ((_last_rfid_scan_at < now - RFID_SCAN_INTERVAL) || (now < _last_rfid_scan_at)) {
|
|
|
|
_check_rfid();
|
|
|
|
_last_rfid_scan_at = now;
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
_check_serial();
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 04:49:35 +00:00
|
|
|
uint32_t Controller::_get_rfid_card_uid() {
|
2019-08-06 18:50:11 +00:00
|
|
|
SPIMaster::enable(PIN_RC522_CS);
|
|
|
|
if (!_rfid->PICC_ReadCardSerial()) {
|
2019-08-08 04:49:35 +00:00
|
|
|
if (!_rfid->PICC_IsNewCardPresent()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!_rfid->PICC_ReadCardSerial()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
2019-08-08 04:49:35 +00:00
|
|
|
uint32_t uid = _rfid->uid.uidByte[0]<<24 | _rfid->uid.uidByte[1]<<16 | _rfid->uid.uidByte[2]<<8 | _rfid->uid.uidByte[3];
|
2019-08-06 18:50:11 +00:00
|
|
|
SPIMaster::disable();
|
2019-08-08 04:49:35 +00:00
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::_check_rfid() {
|
|
|
|
uint32_t uid = _get_rfid_card_uid();
|
|
|
|
//Serial.printf("Found card: %08x\n", uid);
|
|
|
|
if (uid != _last_rfid_card_uid) {
|
|
|
|
if (uid > 0) {
|
|
|
|
Serial.printf("New RFID card uid: %08x\n", uid);
|
|
|
|
// Play
|
|
|
|
String s_uid = String(uid, HEX);
|
|
|
|
_player->play_album(s_uid);
|
|
|
|
} else {
|
|
|
|
Serial.println("No more RFID card.");
|
|
|
|
// Stop
|
|
|
|
_player->stop();
|
|
|
|
}
|
|
|
|
_last_rfid_card_uid = uid;
|
|
|
|
}
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Controller::_check_serial() {
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
char c = Serial.read();
|
|
|
|
if (c == 'n') {
|
|
|
|
_player->track_next();
|
|
|
|
} else if (c=='p') {
|
|
|
|
_player->track_prev();
|
|
|
|
} else if (c=='s') {
|
|
|
|
_player->stop();
|
|
|
|
} else if (c=='+') {
|
|
|
|
_player->vol_up();
|
|
|
|
} else if (c=='-') {
|
|
|
|
_player->vol_down();
|
|
|
|
} else if (c==' ') {
|
|
|
|
_player->play_album("12345678");
|
2019-08-08 03:31:27 +00:00
|
|
|
} else if (c=='q') {
|
|
|
|
_player->play_system_sound("12345678/Biene Maja.mp3");
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|