Skip to content

Commit

Permalink
some fixes for mp3 player
Browse files Browse the repository at this point in the history
stop player when disabled in UI
suppress playback commands if DAC mute is active
disable sounds on power on/off
apply options config on device config
  • Loading branch information
vortigont committed Feb 16, 2024
1 parent a6737e4 commit 4bbbe35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/interface_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ void getset_dfplayer_device(Interface *interf, const JsonObject *data, const cha
JsonVariantConst cfg(dst);
// reconfig DFPlayer device
dfplayer_setup_device(cfg);
// need to apply options config also in case player was reenabled with previous config
cfg = doc[T_opt];
dfplayer_setup_opt(cfg);
}

if (interf) ui_page_setup_devices(interf, nullptr, NULL);
Expand Down
21 changes: 19 additions & 2 deletions src/mp3player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ MP3PlayerController::MP3PlayerController(HardwareSerial& serial, DfMp3Type type,
dfp = new DFMiniMp3(serial, type, ackTimeout);
}

MP3PlayerController::~MP3PlayerController(){
unsubscribe();
dfp->stop();
delete dfp; dfp = nullptr;
}


void MP3PlayerController::begin(int8_t rxPin, int8_t txPin){
LOG(println, "DFplayer: Initializing...");
LOGD(T_DFPlayer, println, "Initializing...");
_serial.begin(MP3_SERIAL_SPEED, SERIAL_8N1, rxPin, txPin);

// event poller
Expand Down Expand Up @@ -141,6 +148,7 @@ void MP3PlayerController::event_hndlr(void* handler, esp_event_base_t base, int3
void MP3PlayerController::_lmpChEventHandler(esp_event_base_t base, int32_t id, void* data){
switch (static_cast<evt::lamp_t>(id)){
// Power control
/*
case evt::lamp_t::pwron :
LOGI(T_DFPlayer, println, T_Notification);
dfp->playFolderTrack(2, 1);
Expand All @@ -149,6 +157,7 @@ void MP3PlayerController::_lmpChEventHandler(esp_event_base_t base, int32_t id,
LOGI(T_DFPlayer, println, T_Notification);
dfp->playFolderTrack(2, 1);
break;
*/
case evt::lamp_t::effSwitchTo :
if (flags.eff_playtrack)
playEffect(*reinterpret_cast<uint32_t*>(data));
Expand All @@ -166,10 +175,12 @@ void MP3PlayerController::_lmpSetEventHandler(esp_event_base_t base, int32_t id,
case evt::lamp_t::mp3mute :
LOGI(T_DFPlayer, println, "mute");
dfp->disableDac();
flags.mute = true;
break;
case evt::lamp_t::mp3unmute :
LOGI(T_DFPlayer, println, "unmute");
dfp->enableDac();
flags.mute = true;
break;
}
}
Expand Down Expand Up @@ -300,7 +311,8 @@ void MP3PlayerController::printSatusDetail(){
*/

void MP3PlayerController::playTime(int hours, int minutes){
if(!isReady()) return;
// do not play anything if player is on Mute
if (flags.mute) return;

if( dfp->getStatus().state == DfMp3_StatusState_Playing ){
dfp->playAdvertisement(100*hours+minutes);
Expand All @@ -311,6 +323,11 @@ void MP3PlayerController::playTime(int hours, int minutes){
}

void MP3PlayerController::playEffect(uint32_t effnb){
// do not play anything if player is on Mute
if (flags.mute){
return;
LOGD(T_DFPlayer, println, "suppress play due to mute flag");
}
LOGI(T_DFPlayer, printf, "effect folder:%u, track:%u, effnb:%u\n", MP3_EFF_FOLDER, effnb%256, effnb);
dfp->playFolderTrack(MP3_EFF_FOLDER, effnb%256);
_state = DfMp3_StatusState_Playing;
Expand Down
6 changes: 2 additions & 4 deletions src/mp3player.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ class MP3PlayerController {
//bool on:1; // включен ли...
bool eff_playtrack:1; // режим проигрывания треков эффектов
bool eff_looptrack:1; // зацикливать дорожку эффекта
//bool alarm:1; // сейчас будильник
//bool isplayname:1; // проигрывается имя
//bool isadvert:1; // воспроизводится ли сейчас время в ADVERT (для совместимости между 24SS и GD3200B)
bool mute:1; // Player's DAC disabled
bool isplaying:1; // воспроизводится ли сейчас песня или эффект
bool looptrack:1; // if current track is looped (cmd has been sent to player)
};
Expand Down Expand Up @@ -100,7 +98,7 @@ class MP3PlayerController {
public:
MP3PlayerController(HardwareSerial& serial, DfMp3Type type = DfMp3Type::origin, uint32_t ackTimeout = DF_ACK_TIMEOUT);
// d-tor
~MP3PlayerController(){ unsubscribe(); delete dfp; dfp = nullptr; }
~MP3PlayerController();

// Player instance
DFMiniMp3 *dfp = nullptr;
Expand Down

0 comments on commit 4bbbe35

Please sign in to comment.