Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins: Allow view specific config #201

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
444 changes: 177 additions & 267 deletions lib/DateTimePlugin/src/DateTimePlugin.cpp

Large diffs are not rendered by default.

45 changes: 14 additions & 31 deletions lib/DateTimePlugin/src/DateTimePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,6 @@ class DateTimePlugin : public PluginWithConfig
/** Default date format according to strftime(). */
static const char DATE_FORMAT_DEFAULT[];

/** Color key names for the analog clock configuration. */
static const char* ANALOG_CLOCK_COLOR_KEYS[IDateTimeView::ANA_CLK_COL_MAX];

/**
* If the slot duration is infinite (0s), the default duration of 30s shall be assumed as base
* for toggling between time and date on the display.
Expand Down Expand Up @@ -333,6 +330,20 @@ class DateTimePlugin : public PluginWithConfig
*/
bool setConfiguration(const JsonObjectConst& jsonCfg) final;

/**
* Merge JSON configuration with local settings to create a complete set.
*
* The received configuration may not contain all single key/value pair.
* Therefore create a complete internal configuration and overwrite it
* with the received one.
*
* @param[out] jsonMerged The complete config set with merge content from jsonSource.
* @param[in] jsonSource The recevied congi set, which may not cover all keys.
* @return true Keys needed merging.
* @return false Nothing needed merging.
*/
bool mergeConfiguration(JsonObject& jsonMerged, const JsonObjectConst& jsonSource);

/**
* Get current date/time and update the text, which to be displayed.
* The update takes only place, if the date changed.
Expand All @@ -355,34 +366,6 @@ class DateTimePlugin : public PluginWithConfig
*/
bool getTimeAsString(String& time, const String& format, const tm *currentTime = nullptr);

/**
* Convert color to HTML format.
*
* @param[in] color Color
*
* @return Color in HTML format
*/
static String colorToHtml(const Color& color);

/**
* Convert color from HTML format.
*
* @param[in] htmlColor Color in HTML format
*
* @return Color
*/
static Color colorFromHtml(const String& htmlColor);

/**
* Check if analog cfg is valid when present.
*
* @param jsonCfg[in] The json configuration, may be isNull().
* @param cfg[out] The parsed config data if json present and valid.
* @return true If no configuration or valid json values.
*/
static bool checkAnalogClockConfig(
JsonVariantConst& jsonCfg,
IDateTimeView::AnalogClockConfig & cfg);
};

/******************************************************************************
Expand Down
2 changes: 0 additions & 2 deletions lib/DateTimePlugin/web/DateTimePlugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ <h3>Display</h3>
startOfWeek: $("#startOfWeek").val(),
dayOnColor: $("#dayOnColor").val(),
dayOffColor: $("#dayOffColor").val(),
handHourCol: $("#handHourCol").val(),
viewMode: $('input[name=viewMode]:checked').val()

};

if ((64 <= ~DISPLAY_HEIGHT~) && (64 <= ~DISPLAY_WIDTH~))
Expand Down
1 change: 0 additions & 1 deletion lib/Plugin/src/PluginWithConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ class PluginWithConfig : public Plugin
SimpleTimer m_cfgReloadTimer; /**< Timer is used to cyclic reload the configuration from persistent memory. */
bool m_storeConfigReq; /**< Is requested to store the configuration in persistent memory? */
bool m_reloadConfigReq; /**< Is requested to reload the configuration from persistent memory? */

};

/******************************************************************************
Expand Down
2 changes: 2 additions & 0 deletions lib/Utilities/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"owner": "bblanchon",
"name": "ArduinoJson",
"version": "~6.21.5"
}, {
"name": "YAGfx"
}, {
"name": "FS"
}, {
Expand Down
21 changes: 21 additions & 0 deletions lib/Utilities/src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,27 @@ extern uint32_t Util::hexToUInt32(const String& str)
return value;
}

String Util::colorToHtml(const Color& color)
{
char buffer[8]; /* '#' + 3x byte in hex + '\0' */

(void)snprintf(buffer, sizeof(buffer), "#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue());

return String(buffer);
}

Color Util::colorFromHtml(const String& htmlColor)
{
Color color;

if ('#' == htmlColor[0])
{
color = Util::hexToUInt32(htmlColor.substring(1U));
}

return color;
}

/******************************************************************************
* Local Functions
*****************************************************************************/
19 changes: 19 additions & 0 deletions lib/Utilities/src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include <WString.h>
#include <cerrno>

#include <YAColor.h>

/******************************************************************************
* Macros
*****************************************************************************/
Expand Down Expand Up @@ -214,6 +216,23 @@ constexpr T max(T valA, T valB)
return (valA > valB) ? valA : valB;
}

/**
* Convert color to HTML format.
*
* @param[in] color Color
*
* @return Color in HTML format
*/
extern String colorToHtml(const Color& color);

/**
* Convert color from HTML format.
*
* @param[in] htmlColor Color in HTML format
*
* @return Color
*/
extern Color colorFromHtml(const String& htmlColor);
}

#endif /* UTILITY_H */
Expand Down
71 changes: 27 additions & 44 deletions lib/Views/src/IDateTimeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
/******************************************************************************
* Includes
*****************************************************************************/
#include <ArduinoJson.h>
#include <YAGfx.h>
#include <Fonts.h>
#include <WString.h>
Expand Down Expand Up @@ -81,38 +82,6 @@ class IDateTimeView
VIEW_MODE_MAX /**< Number of configurations */
};

/**
* Options for displaying seconds in analog clock.
*/
enum SecondsDisplayMode
{
SECOND_DISP_OFF = 0U, /**< No second indicator display. */
SECOND_DISP_HAND = 1U, /**< Draw second clock hand. */
SECOND_DISP_RING = 2U, /**< Show passed seconds on minute tick ring. */
SECOND_DISP_BOTH = 3U, /**< Show hand and on ring. */
SECONDS_DISP_MAX /**< Number of configurations. */
};

/**
* Color array indexes for the analog clock drawing.
*/
enum AnalogClockColor
{
ANA_CLK_COL_HAND_HOUR = 0U, /**< Hour clock hand color. */
ANA_CLK_COL_HAND_MIN, /**< Minutes clock hand color. */
ANA_CLK_COL_HAND_SEC, /**< Seconds colock hand color */
ANA_CLK_COL_RING_MIN5_MARK, /**< Ring five minute marks color. */
ANA_CLK_COL_RING_MIN_DOT, /**< Ring minut dots color. */
ANA_CLK_COL_MAX /**< Number of colors. */
};

/** Analog clock appearance configuration. */
struct AnalogClockConfig
{
SecondsDisplayMode m_secondsMode; /**< Seconds visualisation mode. */
Color m_colors[ANA_CLK_COL_MAX]; /**< Clock colors to use. */
};

/**
* Initialize view, which will prepare the widgets and the default values.
*/
Expand Down Expand Up @@ -219,27 +188,41 @@ class IDateTimeView
virtual bool setViewMode(ViewMode mode) = 0;

/**
* Get the analog clock clonfiguration.
* @brief Update current time values in view.
*
* @return AnalogClockConfig or nullptr if unsupported.
* @param[in] now current time
*/
virtual const AnalogClockConfig* getAnalogClockConfig() const = 0;
virtual void setCurrentTime(const tm& now) = 0;

/**
* Set the analog clock configuration.
*
* @param[in] cfg The new configuration to apply.
/**
* Get current active configuration in JSON format.
*
* @return success or failure
* @param[out] cfg Configuration
*/
virtual bool setAnalogClockConfig(const AnalogClockConfig& cfg) = 0;
virtual void getConfiguration(JsonObject& jsonCfg) const = 0;

/**
* @brief Update current time values in view.
* Apply configuration from JSON.
*
* @param[in] now current time
* @param[in] cfg Configuration
*
* @return If successful set, it will return true otherwise false.
*/
virtual void setCurrentTime(const tm& now) = 0;
virtual bool setConfiguration(const JsonObjectConst& jsonCfg) = 0;

/**
* Merge JSON configuration with local settings to create a complete set.
*
* The received configuration may not contain all single key/value pair.
* Therefore create a complete internal configuration and overwrite it
* with the received one.
*
* @param[out] jsonMerged The complete config set with merge content from jsonSource.
* @param[in] jsonSource The recevied congi set, which may not cover all keys.
* @return true Keys needed merging.
* @return false Nothing needed merging.
*/
virtual bool mergeConfiguration(JsonObject& jsonMerged, const JsonObjectConst& jsonSource) = 0;

protected:

Expand Down
40 changes: 32 additions & 8 deletions lib/Views/src/layouts/DateTimeView32x8.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,47 @@ class DateTimeView32x8 : public IDateTimeView
}

/**
* Get the analog clock seconds display mode (none, ring, hand or both).
* Get current active configuration in JSON format.
*
* @return SecondsDisplayMode pointer or nullptr if unsupported.
* @param[out] cfg Configuration
*/
const AnalogClockConfig* getAnalogClockConfig() const override
void getConfiguration(JsonObject& jsonCfg) const final
{
return nullptr; /* 32X8 layout can only do digital.*/
(void)jsonCfg; /* No configuration for 32x8 */
}

/**
* Set the analog clock seconds display mode (none, ring, hand or both).
* Apply configuration from JSON.
*
* @return success of failure
* @param[in] cfg Configuration
*
* @return If successful set, it will return true otherwise false.
*/
bool setConfiguration(const JsonObjectConst& jsonCfg) final
{
(void)jsonCfg;

return true;
}

/**
* Merge JSON configuration with local settings to create a complete set.
*
* The received configuration may not contain all single key/value pair.
* Therefore create a complete internal configuration and overwrite it
* with the received one.
*
* @param[out] jsonMerged The complete config set with merge content from jsonSource.
* @param[in] jsonSource The recevied congi set, which may not cover all keys.
* @return true Keys needed merging.
* @return false Nothing needed merging.
*/
bool setAnalogClockConfig(const AnalogClockConfig& cfg) override
bool mergeConfiguration(JsonObject& jsonMerged, const JsonObjectConst& jsonSource) final
{
return true; /* No analog clock in 32x8 layout, ignore request. */
(void)jsonMerged;
(void)jsonSource;

return false; /* Nothing to merge. */
}

/**
Expand Down
Loading
Loading