Compare commits

..

2 Commits

Author SHA1 Message Date
9a36831658 config: implement reading app-specific configs from profile
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2023-02-18 18:45:34 +01:00
7152d70a02 figments: input: add pointer type to variant, and new config update event 2023-02-18 18:44:17 +01:00
6 changed files with 41 additions and 5 deletions

View File

@ -13,5 +13,8 @@
"Acid": ["Chimes", "Pulse", "MPU5060", "IdleColors", "Rainbow"],
"Flashlight": ["Flashlight"]
},
"surfaceMap": "default"
"surfaceMap": "default",
"defaults": {
"mqtt.ip": "10.0.0.2"
}
}

View File

@ -13,6 +13,7 @@ struct Variant {
Integer,
String,
Color,
Pointer,
};
Variant(int v)
@ -24,6 +25,9 @@ struct Variant {
Variant(const CRGB &v)
: type(Color), m_value{.asRGB={v.r, v.g, v.b}} {}
Variant(void* p)
: type(Pointer), m_value{.asPointer=p} {}
Variant()
: type(Null) {}
@ -33,12 +37,16 @@ struct Variant {
CRGB asRGB() const;
int asInt() const;
bool asBool() const;
template<typename T> T* as() const {
return (T*)m_value.asPointer;
}
private:
union {
int asInt;
const char* asString;
uint8_t asRGB[3];
void* asPointer;
} m_value;
};
@ -88,6 +96,7 @@ struct InputEvent: public Variant {
LoadConfigurationByName,
SetColor,
SaveConfigurationRequest,
ConfigurationChanged,
// Firmware events
FirmwareUpdate,
@ -166,6 +175,17 @@ private:
std::function<InputEvent(const InputEvent)> m_func;
};
class ConfigTaskMixin : public virtual Loopable {
public:
void handleEvent(const InputEvent &evt) override {
if (evt.intent == InputEvent::ConfigurationChanged) {
handleConfigChange(evt);
}
}
virtual void handleConfigChange(const InputEvent& evt) {}
};
class OnlineTaskMixin : public virtual Loopable {
public:
void handleEvent(const InputEvent &evt) override {

View File

@ -146,6 +146,9 @@ ConfigService::loadProfile(const char* profileName)
Log.warning("config: Could not load profile %s!", profileName);
}
JsonObject defaults = jsonConfig["defaults"];
MainLoop::instance()->dispatch(InputEvent{InputEvent::ConfigurationChanged, &defaults});
String configName = jsonConfig["surfaceMap"];
jsonConfig.clear();
loadMap(configName);

View File

@ -37,6 +37,7 @@ struct ConfigService: public Task {
const CoordinateMapping* coordMap() const { return &m_jsonMap; }
const char* loadedProfile() const;
void overrideProfile(const char* profileName);
const char* getConfigValue(const char* key) const;
private:
HardwareConfig m_config;

View File

@ -198,20 +198,27 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
}
}
void
MQTTTelemetry::handleConfigChange(const InputEvent& event)
{
const JsonObject* obj = static_cast<JsonObject*>(event.value().asPointer());
strncpy(m_hostBuf, obj["mqtt.ip"].c_str(), sizeof(m_hostBuf));
m_mqtt.disconnect();
}
void
MQTTTelemetry::loop()
{
BufferedInputSource::loop();
OnlineTaskMixin::loop();
ConfigTaskMixin::loop();
}
void
MQTTTelemetry::onOnline()
{
const IPAddress server(10, 0, 0, 2);
m_needHeartbeat = true;
m_mqtt.setServer(server, 1883);
m_mqtt.setServer(m_hostBuf, 1883);
m_mqtt.setBufferSize(1024);
m_mqtt.setCallback(&MQTTTelemetry::s_callback);
}

View File

@ -12,7 +12,7 @@
#include <WiFi.h>
#endif
class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin {
class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin, ConfigTaskMixin {
public:
MQTTTelemetry();
void setSequencer(Sequencer* seq) { m_sequencer = seq; }
@ -46,6 +46,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin {
}
void handleEventOnline(const InputEvent& evt) override;
void handleConfigChange(const InputEvent& evt) override;
void loop() override;
void loopOnline() override;
@ -65,6 +66,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin {
bool m_isOn = true;
static char s_topicBuf[128];
static char s_payloadBuf[512];
static char s_hostBuf[15];
void publishDoc(const char* topic);
void publishDoc(const char* topic, bool retain);