From 80321e8c6ac62a402eaee27ce203c3fc938f9926 Mon Sep 17 00:00:00 2001 From: Simon Weidacher Date: Sun, 26 Dec 2021 14:00:30 +0100 Subject: [PATCH] Adds play/pause by tag support. --- device/main/AudioPlayer.cpp | 5 +++++ device/main/AudioPlayer.hpp | 1 + device/main/RFIDInterface.cpp | 21 ++++++++++++--------- device/main/TagPlayerControl.cpp | 9 +++++++++ device/main/TagPlayerControl.hpp | 4 ++++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/device/main/AudioPlayer.cpp b/device/main/AudioPlayer.cpp index ac24783..1630155 100644 --- a/device/main/AudioPlayer.cpp +++ b/device/main/AudioPlayer.cpp @@ -12,5 +12,10 @@ AudioPlayer::AudioPlayer() { } void AudioPlayer::play(char* uri) { + esp_player_music_stop(); esp_player_sdcard_music_play(uri, 0); +} + +void AudioPlayer::pause() { + esp_player_music_pause(); } \ No newline at end of file diff --git a/device/main/AudioPlayer.hpp b/device/main/AudioPlayer.hpp index 61388b7..51db038 100644 --- a/device/main/AudioPlayer.hpp +++ b/device/main/AudioPlayer.hpp @@ -6,4 +6,5 @@ class AudioPlayer { public: AudioPlayer(); void play(char* uri); + void pause(); }; \ No newline at end of file diff --git a/device/main/RFIDInterface.cpp b/device/main/RFIDInterface.cpp index 59b3875..d4c01dd 100644 --- a/device/main/RFIDInterface.cpp +++ b/device/main/RFIDInterface.cpp @@ -23,17 +23,20 @@ RFIDInterface::RFIDInterface() { .mosi_io = 13, .sck_io = 23, .sda_io = 18, - .callback = [](uint8_t* sn) { // serial number is always 5 bytes long - ESP_LOGI(TAG, "Tag: %#x %#x %#x %#x %#x", - sn[0], sn[1], sn[2], sn[3], sn[4] - ); + .callback = [](uint8_t* sn) { + if (sn == NULL) { + ESP_LOGI(TAG, "Tag removed"); - sprintf(lastTagId, "%#x_%#x_%#x_%#x_%#x", sn[0], sn[1], sn[2], sn[3], sn[4]); + if (callback) { + callback(NULL); + } + } else { // serial number is always 5 bytes long + sprintf(lastTagId, "%#x_%#x_%#x_%#x_%#x", sn[0], sn[1], sn[2], sn[3], sn[4]); + ESP_LOGI(TAG, "tag id: %s", lastTagId); - ESP_LOGI(TAG, "sprintf tag id: %s", lastTagId); - - if (callback) { - callback(getLastTagId()); + if (callback) { + callback(getLastTagId()); + } } } }; diff --git a/device/main/TagPlayerControl.cpp b/device/main/TagPlayerControl.cpp index 0f42ba9..5ef0b16 100644 --- a/device/main/TagPlayerControl.cpp +++ b/device/main/TagPlayerControl.cpp @@ -15,6 +15,15 @@ TagPlayerControl::TagPlayerControl(AudioPlayer* player) { } void TagPlayerControl::onTagChanged(char* tagId) { + if (tagId == NULL) { + // tag has been removed. stop playback + audioPlayer->pause(); + } else { + playTag(tagId); + } +} + +void TagPlayerControl::playTag(char* tagId) { std::string filePath = "/sdcard/internal/"; filePath.append(tagId); diff --git a/device/main/TagPlayerControl.hpp b/device/main/TagPlayerControl.hpp index a23ff78..e42c0f1 100644 --- a/device/main/TagPlayerControl.hpp +++ b/device/main/TagPlayerControl.hpp @@ -7,5 +7,9 @@ class TagPlayerControl { public: TagPlayerControl(AudioPlayer* player); + void onTagChanged(char* tagId); + + private: + void playTag(char* tagId); }; \ No newline at end of file