From 3278495391d99ae4d9c03d7a8f3bbff2a0afc55a Mon Sep 17 00:00:00 2001 From: Jonny Bergdahl Date: Wed, 20 Dec 2023 14:59:21 +0100 Subject: [PATCH] Version 1.0.3 Fix date format list Add config logging Fix effect list in JBWopr_Firmware.ino Effects code cleanup Fix HA effects entity name --- examples/JBWopr_Firmware/JBWopr_Firmware.ino | 6 ++-- library.properties | 2 +- src/effects/jbwopreffects.cpp | 31 +++++++------------- src/jbwoprha.cpp | 14 ++++++--- src/jbwoprha.h | 5 ++++ src/jbwoprmqtt.cpp | 11 +++++++ src/jbwoprmqtt.h | 5 ++++ src/jbwoprwifi.cpp | 19 ++++++++---- src/jbwoprwifi.h | 5 ++++ 9 files changed, 64 insertions(+), 34 deletions(-) diff --git a/examples/JBWopr_Firmware/JBWopr_Firmware.ino b/examples/JBWopr_Firmware/JBWopr_Firmware.ino index c1076f8..7086f3a 100644 --- a/examples/JBWopr_Firmware/JBWopr_Firmware.ino +++ b/examples/JBWopr_Firmware/JBWopr_Firmware.ino @@ -69,6 +69,9 @@ void setup() { // Set log level wopr.setLogLevel(LOG_LEVEL); + // Load and get the list of registered effects + registerEffects(); + // Initialize wopr wopr.begin(JBWoprBoardVariant::HAXORZ); @@ -78,9 +81,6 @@ void setup() { wopr.buttonBackTopSetClickCallback(buttonBackTopClick); wopr.buttonBackBottomSetClickCallback(buttonBackBottomClick); - // Load and get the list of registred effects - registerEffects(); - // Show instructions wopr.displayScrollText("Left - Select effect, Right - Run effect"); Serial.println("Use back top/front left button to select next effect"); diff --git a/library.properties b/library.properties index a1f2b60..85335d3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=JBWopr -version=1.0.2 +version=1.0.3 author=Jonny Bergdahl maintainer=Jonny Bergdahl sentence=Support library for the Unexpected Maker W.O.P.R. boards diff --git a/src/effects/jbwopreffects.cpp b/src/effects/jbwopreffects.cpp index af6109f..7707741 100644 --- a/src/effects/jbwopreffects.cpp +++ b/src/effects/jbwopreffects.cpp @@ -255,7 +255,7 @@ void JBWoprTimeDisplayEffect::start() { } void JBWoprTimeDisplayEffect::loop() { - char timeChars[12]; + char timeChars[13]; tm timeinfo {}; if (!_isRunning) { @@ -275,7 +275,7 @@ void JBWoprTimeDisplayEffect::loop() { } else { _evenFormat = !_evenFormat; auto timeFormat = _evenFormat ? _timeFormatEven : _timeFormatOdd; - strftime(timeChars, 12, timeFormat.c_str(), &timeinfo); + strftime(timeChars, sizeof(timeChars), timeFormat.c_str(), &timeinfo); _displayText(timeChars, JBTextAlignment::CENTER); } @@ -315,13 +315,10 @@ JBWoprTimeDisplayRainbowEffect::JBWoprTimeDisplayRainbowEffect(JBWoprDevice *wop std::string timeFormat, uint32_t duration, const std::string& name) : - JBWoprTimeDisplayEffect(woprDevice, timeFormat, duration, name) { + JBWoprTimeDisplayEffect(woprDevice, std::move(timeFormat), duration, name) { } void JBWoprTimeDisplayRainbowEffect::loop() { - char timeChars[12]; - tm timeinfo {}; - if (!_isRunning) { return; } @@ -354,13 +351,12 @@ JBWoprDateDisplayEffect::JBWoprDateDisplayEffect(JBWoprDevice *woprDevice, } void JBWoprDateDisplayEffect::start() { - auto config = _woprDevice->getConfiguration(); setDateFormat(_rawDateFormat); JBWoprEffectBase::start(); } void JBWoprDateDisplayEffect::loop() { - char text[12]; + char text[13]; tm timeinfo{}; if (!_isRunning) { @@ -377,7 +373,7 @@ void JBWoprDateDisplayEffect::loop() { _log.error("Failed to obtain time"); _displayText("Time failed"); } else { - strftime(text, 12, _dateFormat.c_str(), &timeinfo); + strftime(text, sizeof(text), _dateFormat.c_str(), &timeinfo); _displayText(text, JBTextAlignment::CENTER); } @@ -403,13 +399,10 @@ JBWoprDateDisplayRainbowEffect::JBWoprDateDisplayRainbowEffect(JBWoprDevice *wop std::string dateFormat, uint32_t duration, const std::string& name) : - JBWoprDateDisplayEffect(woprDevice, dateFormat, duration, name) { + JBWoprDateDisplayEffect(woprDevice, std::move(dateFormat), duration, name) { } void JBWoprDateDisplayRainbowEffect::loop() { - char text[12]; - tm timeinfo{}; - if (!_isRunning) { return; } @@ -449,7 +442,7 @@ void JBWoprDateTimeDisplayEffect::start() { } void JBWoprDateTimeDisplayEffect::loop() { - char text[12]; + char text[13]; tm timeinfo{}; if (!_isRunning) { @@ -474,11 +467,11 @@ void JBWoprDateTimeDisplayEffect::loop() { if (_displayCounter < 7) { _evenFormat = !_evenFormat; auto timeFormat = _evenFormat ? _timeFormatEven : _timeFormatOdd; - strftime(text, 12, timeFormat.c_str(), &timeinfo); + strftime(text, sizeof(text), timeFormat.c_str(), &timeinfo); _displayText(text, JBTextAlignment::CENTER); } else { - strftime(text, 12, _dateFormat.c_str(), &timeinfo); + strftime(text, sizeof(text), _dateFormat.c_str(), &timeinfo); _displayText(text, JBTextAlignment::CENTER); } } @@ -531,13 +524,10 @@ JBWoprDateTimeDisplayRainbowEffect::JBWoprDateTimeDisplayRainbowEffect(JBWoprDev std::string dateFormat, uint32_t duration, const std::string& name) : - JBWoprDateTimeDisplayEffect(woprDevice, timeFormat, dateFormat, duration, name) { + JBWoprDateTimeDisplayEffect(woprDevice, std::move(timeFormat), std::move(dateFormat), duration, name) { } void JBWoprDateTimeDisplayRainbowEffect::loop() { - char text[12]; - tm timeinfo{}; - if (!_isRunning) { return; } @@ -838,7 +828,6 @@ void JBWoprSongEffect::loop() { } if (_step >= _song->size()) { -Serial.println("Song done"); _woprDevice->audioClear(); _done = true; _isRunning = false; diff --git a/src/jbwoprha.cpp b/src/jbwoprha.cpp index 70a0c43..43aa2dd 100644 --- a/src/jbwoprha.cpp +++ b/src/jbwoprha.cpp @@ -77,6 +77,12 @@ void JBWoprHADevice::_setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc) { jsonDoc[JSON_KEY_HA_DISCOVERY_PREFIX] = _haConfig.homeAssistantDiscoveryPrefix; } +void JBWoprHADevice::_dumpConfig() { + JBWoprMqttDevice::_dumpConfig(); + _log->trace("Home Assistant configuration:"); + _log->trace(" Use Home Assistant: %s", _haConfig.useHomeAssistant ? "true" : "false"); + _log->trace(" Discovery prefix: %s", _haConfig.homeAssistantDiscoveryPrefix.c_str()); +} // ==================================================================== // WiFiManager // @@ -208,8 +214,8 @@ bool JBWoprHADevice::_homeAssistantSendDiscovery() { dateOptions.add("%Y-%m-%d"); dateOptions.add("%m/%d/%Y"); dateOptions.add("%d/%m/%Y"); - dateOptions.add("%D-%M-%Y"); - dateOptions.add("%D.%M.%Y"); + dateOptions.add("%d-%m-%Y"); + dateOptions.add("%d.%m.%Y"); jsonDoc["command_topic"] = "wopr/" + _getDeviceName() + "/config/date_format/set"; mqttPublishMessage(topic, jsonDoc, true); } @@ -314,8 +320,8 @@ bool JBWoprHADevice::_homeAssistantSendDiscovery() { topic = "homeassistant/select/" + _getDeviceName() + "/effect/config"; jsonDoc["name"] = "Effect"; _addDeviceData(jsonDoc); - jsonDoc["unique_id"] = _getDeviceName() + "_effect"; - jsonDoc["object_id"] = _getDeviceName() + "_effect"; + jsonDoc["unique_id"] = _getDeviceName() + "_effect_name"; + jsonDoc["object_id"] = _getDeviceName() + "_effect_name"; _addAvailabilityData(jsonDoc); jsonDoc["state_topic"] = "wopr/" + _getDeviceName() + "/effect/name"; jsonDoc["command_topic"] = "wopr/" + _getDeviceName() + "/effect/name/set"; diff --git a/src/jbwoprha.h b/src/jbwoprha.h index c9c9540..f57d949 100644 --- a/src/jbwoprha.h +++ b/src/jbwoprha.h @@ -83,6 +83,11 @@ class JBWoprHADevice: public JBWoprMqttDevice { /// @param jsonDoc JSON document void _setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc) override; + /// @brief Dump configuration to logger + /// @ingroup ConfigurationGroup + /// @details Called when configuration is to be written to the logger. + void _dumpConfig() override; + // ==================================================================== // Wifi // diff --git a/src/jbwoprmqtt.cpp b/src/jbwoprmqtt.cpp index 5294a91..f0d3087 100644 --- a/src/jbwoprmqtt.cpp +++ b/src/jbwoprmqtt.cpp @@ -276,6 +276,17 @@ void JBWoprMqttDevice::_setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc) jsonDoc[JSON_KEY_CONF_MQTT_PREFIX] = _mqttConfig.mqttPrefix; } +void JBWoprMqttDevice::_dumpConfig() { + JBWoprWiFiDevice::_dumpConfig(); + _log->trace("MQTT configuration:"); + _log->trace(" Use MQTT: %s", _mqttConfig.useMqtt ? "True" : "False"); + _log->trace(" MQTT server name: %s", _mqttConfig.mqttServerName.c_str()); + _log->trace(" MQTT server port: %i", _mqttConfig.mqttServerPort); + _log->trace(" MQTT user name: %s", _mqttConfig.mqttUserName.c_str()); + _log->trace(" MQTT password: ----------"); + _log->trace(" MQTT prefix: %s", _mqttConfig.mqttPrefix.c_str()); +} + // ==================================================================== // WiFi // diff --git a/src/jbwoprmqtt.h b/src/jbwoprmqtt.h index ed8f68e..09de09c 100644 --- a/src/jbwoprmqtt.h +++ b/src/jbwoprmqtt.h @@ -327,6 +327,11 @@ class JBWoprMqttDevice: public JBWoprWiFiDevice { /// @param jsonDoc JSON document void _setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc); + /// @brief Dump configuration to logger + /// @ingroup ConfigurationGroup + /// @details Called when configuration is to be written to the logger. + void _dumpConfig() override; + // ==================================================================== // WiFi // diff --git a/src/jbwoprwifi.cpp b/src/jbwoprwifi.cpp index 5126629..4cbce69 100644 --- a/src/jbwoprwifi.cpp +++ b/src/jbwoprwifi.cpp @@ -154,7 +154,7 @@ void JBWoprWiFiDevice::webPortalStop() void JBWoprWiFiDevice::_loadConfiguration() { _log->trace("Load configuration"); - if (_wifiConfig.hostName == "") + if (_wifiConfig.hostName.empty()) { _wifiConfig.hostName = _getDeviceName(); } @@ -168,8 +168,6 @@ void JBWoprWiFiDevice::_loadConfiguration() DynamicJsonDocument jsonDoc(1024); DeserializationError error = deserializeJson(jsonDoc, settingsFile); settingsFile.close(); -serializeJson(jsonDoc, Serial); -Serial.println(); if (error) { _log->error("Error parsing settings JSON file!"); return; @@ -180,6 +178,7 @@ Serial.println(); return; } _setConfigFromJsonDocument(jsonDoc); + _dumpConfig(); } void JBWoprWiFiDevice::_saveConfiguration() @@ -190,8 +189,7 @@ void JBWoprWiFiDevice::_saveConfiguration() // Set the values in the JSON document _setJsonDocumentFromConfig(jsonDoc); - -serializeJson(jsonDoc, Serial); + _dumpConfig(); File settingsFile = LittleFS.open(CONFIG_FILE_NAME, "w"); if (!settingsFile) { _log->error("Failed to open configuration file for writing!"); @@ -236,6 +234,17 @@ void JBWoprWiFiDevice::_setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc) jsonDoc[JSON_KEY_WIFI_USE_WEB_PORTAL] = _wifiConfig.useWebPortal; } +void JBWoprWiFiDevice::_dumpConfig() { + _log->trace("Current configuration"); + _log->trace(" Time format: %s", _config.timeFormat.c_str()); + _log->trace(" Date format: %s", _config.dateFormat.c_str()); + _log->trace(" DEFCON LEDs brightness: %d", _config.defconLedsBrightness); + _log->trace(" Display brightness: %d", _config.displayBrightness); + _log->trace(" Effects timeout: %d", _config.effectsTimeout); + _log->trace(" Host name: %s", _wifiConfig.hostName.c_str()); + _log->trace(" Use web portal: %s", _wifiConfig.useWebPortal ? "true" : "false"); +} + // ==================================================================== // WiFi // diff --git a/src/jbwoprwifi.h b/src/jbwoprwifi.h index 39d3bcd..b8f7402 100644 --- a/src/jbwoprwifi.h +++ b/src/jbwoprwifi.h @@ -133,6 +133,11 @@ class JBWoprWiFiDevice: public JBWoprDevice { /// @param jsonDoc JSON document virtual void _setJsonDocumentFromConfig(DynamicJsonDocument& jsonDoc); + /// @brief Dump configuration to logger + /// @ingroup ConfigurationGroup + /// @details Called when configuration is to be written to the logger. + virtual void _dumpConfig(); + // ==================================================================== // WiFi //