Skip to content

Commit

Permalink
Version 1.0.3
Browse files Browse the repository at this point in the history
Fix date format list
Add config logging
Fix effect list in JBWopr_Firmware.ino
Effects code cleanup
Fix HA effects entity name
  • Loading branch information
bergdahl committed Dec 20, 2023
1 parent 1c4cb93 commit 3278495
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 34 deletions.
6 changes: 3 additions & 3 deletions examples/JBWopr_Firmware/JBWopr_Firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=JBWopr
version=1.0.2
version=1.0.3
author=Jonny Bergdahl
maintainer=Jonny Bergdahl <[email protected]>
sentence=Support library for the Unexpected Maker W.O.P.R. boards
Expand Down
31 changes: 10 additions & 21 deletions src/effects/jbwopreffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void JBWoprTimeDisplayEffect::start() {
}

void JBWoprTimeDisplayEffect::loop() {
char timeChars[12];
char timeChars[13];
tm timeinfo {};

if (!_isRunning) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -449,7 +442,7 @@ void JBWoprDateTimeDisplayEffect::start() {
}

void JBWoprDateTimeDisplayEffect::loop() {
char text[12];
char text[13];
tm timeinfo{};

if (!_isRunning) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -838,7 +828,6 @@ void JBWoprSongEffect::loop() {
}

if (_step >= _song->size()) {
Serial.println("Song done");
_woprDevice->audioClear();
_done = true;
_isRunning = false;
Expand Down
14 changes: 10 additions & 4 deletions src/jbwoprha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions src/jbwoprha.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
11 changes: 11 additions & 0 deletions src/jbwoprmqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
5 changes: 5 additions & 0 deletions src/jbwoprmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
19 changes: 14 additions & 5 deletions src/jbwoprwifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void JBWoprWiFiDevice::webPortalStop()
void JBWoprWiFiDevice::_loadConfiguration()
{
_log->trace("Load configuration");
if (_wifiConfig.hostName == "")
if (_wifiConfig.hostName.empty())
{
_wifiConfig.hostName = _getDeviceName();
}
Expand All @@ -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;
Expand All @@ -180,6 +178,7 @@ Serial.println();
return;
}
_setConfigFromJsonDocument(jsonDoc);
_dumpConfig();
}

void JBWoprWiFiDevice::_saveConfiguration()
Expand All @@ -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!");
Expand Down Expand Up @@ -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
//
Expand Down
5 changes: 5 additions & 0 deletions src/jbwoprwifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down

0 comments on commit 3278495

Please sign in to comment.