From f378e68b3d77cecf8abc3177efc72e0289ade320 Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Sat, 14 Jun 2025 16:32:54 +0200 Subject: [PATCH] i2s additions (#23543) --- .../xdrv_42_0_i2s_audio_idf51.ino | 36 ++++++++++++++++++- .../xdrv_42_7_i2s_webradio_idf51.ino | 21 ++--------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino b/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino index 65ffad8b6..00d7e6bec 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino @@ -90,6 +90,7 @@ struct AUDIO_I2S_MP3_t { #if defined(USE_I2S_MP3) || defined(USE_I2S_WEBRADIO) || defined(USE_SHINE) || defined(MP3_MIC_STREAM) TaskHandle_t mp3_task_handle; TaskHandle_t mic_task_handle; + char audio_title[64]; #endif // defined(USE_I2S_MP3) || defined(USE_I2S_WEBRADIO) char mic_path[32]; @@ -121,6 +122,25 @@ struct AUDIO_I2S_MP3_t { #define I2S_AUDIO_MODE_MIC 1 #define I2S_AUDIO_MODE_SPK 2 +/*********************************************************************************************\ + * Presentation +\*********************************************************************************************/ + +#ifdef USE_WEBSERVER +const char HTTP_I2SAUDIO[] PROGMEM = + "{s}" "Audio:" "{m}%s{e}"; + +void I2sWrShow(bool json) { + if (audio_i2s_mp3.decoder) { + if (json) { + ResponseAppend_P(PSTR(",\"Audio\":{\"Title\":\"%s\"}"), audio_i2s_mp3.audio_title); + } else { + WSContentSend_PD(HTTP_I2SAUDIO,audio_i2s_mp3.audio_title); + } + } +} +#endif // USE_WEBSERVER + /*********************************************************************************************\ * Commands definitions \*********************************************************************************************/ @@ -774,6 +794,9 @@ int32_t I2SPlayFile(const char *path, uint32_t decoder_type) { } if (!ufsp->exists(fname)) { return I2S_ERR_FILE_NOT_FOUND; } + strncpy(audio_i2s_mp3.audio_title, fname, sizeof(audio_i2s_mp3.audio_title)); + audio_i2s_mp3.audio_title[sizeof(audio_i2s_mp3.audio_title)-1] = 0; + I2SAudioPower(true); if (audio_i2s_mp3.task_loop_mode == true){ @@ -869,7 +892,7 @@ void CmndI2SPlay(void) { // display return message switch (err) { case I2S_OK: - ResponseCmndDone(); + ResponseCmndChar("Started"); break; case I2S_ERR_OUTPUT_NOT_CONFIGURED: ResponseCmndChar("I2S output not configured"); @@ -968,6 +991,14 @@ void CmndI2SMicRec(void) { } } +void I2sEventHandler(){ + if(audio_i2s_mp3.task_has_ended == true){ + audio_i2s_mp3.task_has_ended = false; + MqttPublishPayloadPrefixTopicRulesProcess_P(RESULT_OR_STAT,PSTR(""),PSTR("{\"Event\":{\"I2SPlay\":\"Ended\"}}")); + // Rule1 ON event#i2splay=ended DO ENDON + } +} + /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -983,6 +1014,9 @@ bool Xdrv42(uint32_t function) { case FUNC_INIT: I2sInit(); break; + case FUNC_EVERY_50_MSECOND: + I2sEventHandler(); + break; case FUNC_COMMAND: result = DecodeCommand(kI2SAudio_Commands, I2SAudio_Command); break; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino b/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino index 7f8133c07..3654ca465 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino @@ -22,7 +22,6 @@ struct AUDIO_I2S_WEBRADIO_t { AudioFileSourceICYStream *ifile = NULL; - char wr_title[64]; } Audio_webradio; void I2sMDCallback(void *cbData, const char *type, bool isUnicode, const char *str) { @@ -30,9 +29,8 @@ void I2sMDCallback(void *cbData, const char *type, bool isUnicode, const char *s (void) isUnicode; // Punt this ball for now (void) ptr; if (strstr_P(type, PSTR("Title"))) { - strncpy(Audio_webradio.wr_title, str, sizeof(Audio_webradio.wr_title)); - Audio_webradio.wr_title[sizeof(Audio_webradio.wr_title)-1] = 0; - //AddLog(LOG_LEVEL_INFO,PSTR("WR-Title: %s"),wr_title); + strncpy(audio_i2s_mp3.audio_title, str, sizeof(audio_i2s_mp3.audio_title)); + audio_i2s_mp3.audio_title[sizeof(audio_i2s_mp3.audio_title)-1] = 0; } else { // Who knows what to do? Not me! } @@ -107,21 +105,6 @@ i2swr_fail: return false; } -#ifdef USE_WEBSERVER -const char HTTP_WEBRADIO[] PROGMEM = - "{s}" "Webradio:" "{m}%s{e}"; - -void I2sWrShow(bool json) { - if (audio_i2s_mp3.decoder) { - if (json) { - ResponseAppend_P(PSTR(",\"WebRadio\":{\"Title\":\"%s\"}"), Audio_webradio.wr_title); - } else { - WSContentSend_PD(HTTP_WEBRADIO,Audio_webradio.wr_title); - } - } -} -#endif // USE_WEBSERVER - void CmndI2SWebRadio(void) { if (I2SPrepareTx() != I2S_OK) return;