From 8feb1ec760e611ebf75b5a0015ca117b377db241 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 8 Aug 2019 06:49:35 +0200 Subject: [PATCH] Fixed RFID scanning and implemented calling actions on Player. --- include/config.h | 2 +- include/controller.h | 2 ++ src/controller.cpp | 33 +++++++++++++++++++++++++++------ src/player.cpp | 13 +++++++------ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/include/config.h b/include/config.h index 77ea9e0..f358ffc 100644 --- a/include/config.h +++ b/include/config.h @@ -39,4 +39,4 @@ #define VOLUME_MAX 0xC0 #define VOLUME_STEP 0x08 -#define RFID_SCAN_INTERVAL 500 +#define RFID_SCAN_INTERVAL 100 diff --git a/include/controller.h b/include/controller.h index 4727998..e36bf6e 100644 --- a/include/controller.h +++ b/include/controller.h @@ -11,6 +11,8 @@ private: bool _rfid_enabled = true; void _check_rfid(); void _check_serial(); + uint32_t _get_rfid_card_uid(); + uint32_t _last_rfid_card_uid = 0; Player* _player; unsigned long _last_rfid_scan_at = 0; public: diff --git a/src/controller.cpp b/src/controller.cpp index 8647190..651d5fc 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -21,16 +21,37 @@ void Controller::loop() { _check_serial(); } -void Controller::_check_rfid() { +uint32_t Controller::_get_rfid_card_uid() { SPIMaster::enable(PIN_RC522_CS); - if (!_rfid->PICC_IsNewCardPresent()) { - return; - } if (!_rfid->PICC_ReadCardSerial()) { - return; + if (!_rfid->PICC_IsNewCardPresent()) { + return 0; + } + if (!_rfid->PICC_ReadCardSerial()) { + return 0; + } } - _rfid->PICC_DumpToSerial(&(_rfid->uid)); + uint32_t uid = _rfid->uid.uidByte[0]<<24 | _rfid->uid.uidByte[1]<<16 | _rfid->uid.uidByte[2]<<8 | _rfid->uid.uidByte[3]; SPIMaster::disable(); + 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; + } } void Controller::_check_serial() { diff --git a/src/player.cpp b/src/player.cpp index d9cdd6f..57c57af 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -191,12 +191,12 @@ std::list Player::_files_in_dir(String path) { while (entry = dir.openNextFile()) { String filename = entry.name(); if (!entry.isDirectory() && - !filename.startsWith(".") && + !filename.startsWith(".") && ( filename.endsWith(".mp3") || filename.endsWith(".ogg") || filename.endsWith(".wma") || filename.endsWith(".mp4") || - filename.endsWith(".mpa")) { + filename.endsWith(".mpa"))) { //Serial.printf("Adding file %s\n", filename.c_str()); result.push_back(path + filename); } else { @@ -211,6 +211,7 @@ std::list Player::_files_in_dir(String path) { } bool Player::play_album(String album) { + //if (_state==playing) stop(); album_state s = _last_tracks[album.c_str()]; Serial.printf("Last index for album %s was %d,%d\n", album.c_str(), s.index, s.position); return play_song(album, s.index, s.position); @@ -269,7 +270,7 @@ void Player::_play_file(String file, uint32_t file_offset) { Serial.println("Resetting SS_DO_NOT_JUMP..."); _write_control_register(SCI_STATUS, _read_control_register(SCI_STATUS) & ~SS_DO_NOT_JUMP); delay(100); - + _refills = 0; _skip_to = file_offset; if (_skip_to>0) _mute(); @@ -358,7 +359,7 @@ void Player::_refill() { _finish_stopping(); return; } - + _finish_stopping(); bool result = play_song(_playing_album, _playing_index + 1); if (!result) { @@ -387,8 +388,8 @@ void Player::_refill() { bool Player::_refill_needed() { return _state==playing || - _state==stopping || - _state==system_sound_while_playing || + _state==stopping || + _state==system_sound_while_playing || _state==system_sound_while_stopped; }