From b25a691f39699bba51995b7bbdca44238c943cd8 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Tue, 23 Nov 2021 07:25:50 +0100 Subject: [PATCH] Adding global getters and setters --- CLC-qthing/Controller.cpp | 87 +++++++++++++++++++++++---------- CLC-qthing/CyanLightControl.hpp | 4 ++ 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/CLC-qthing/Controller.cpp b/CLC-qthing/Controller.cpp index bbcbb4a..30495be 100644 --- a/CLC-qthing/Controller.cpp +++ b/CLC-qthing/Controller.cpp @@ -6,6 +6,7 @@ #include <string> #include <cstdio> #include <cstdlib> +#include <function> #include <algorithm> #include "esp_err.h" @@ -61,18 +62,17 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig } - add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/set", [&](std::string message) { + std::function<void(std::string)> setFrq = [&](std::string message) { long int frq = strtol(message.c_str(), NULL, 0); this->setFrequency(frq); - }); + }; - add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/set", [&](std::string message) { + std::function<void(std::string)> setRes = [&](std::string message) { long int res = strtol(message.c_str(), NULL, 0); this->setResolution(res); - }); + }; - // atomic setter for both values - add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", [&](std::string message) { + std::function<void(std::string)> setFrqRes = [&](std::string message) { std::string::size_type found = message.find(delimiter); if (found == std::string::npos) { @@ -92,27 +92,41 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig } this->setFrqRes(frq, res); - }); - - - add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/get", [&](std::string ignored) { - char tmp[16]; - snprintf(tmp, sizeof(tmp), "%i", this->getFrequency()); - publish_message(DEVICE_NAMESPACE "CyanLight/frequency", tmp); - }); - - add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/get", [&](std::string ignored) { - char tmp[16]; - snprintf(tmp, sizeof(tmp), "%i", this->getResolution()); - publish_message(DEVICE_NAMESPACE "CyanLight/resolution", tmp); - }); - - // and for symmetric reasons: here comes the counterpart to the atomic frq-res-setter - add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/get", [&](std::string ignored) { - char tmp[32]; - snprintf(tmp, sizeof(tmp), "%i%s%i", this->getFrequency(), delimiter.c_str(), this->getResolution()); - publish_message(DEVICE_NAMESPACE "CyanLight/frqres", tmp); - }); + }; + + + std::function<void(std::string)> getFrq = [&](std::string ignored) { + this->publishFrequency(); + }; + + std::function<void(std::string)> getRes = [&](std::string ignored) { + this->publishResolution(); + }; + + std::function<void(std::string)> getFrqRes = [&](std::string ignored) { + this->publishFrqRes(); + }; + + + // device-local setters + add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", setFrqRes); + add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/set", setFrq); + add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/set", setRes); + + // device-lokal getters + add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/get", getFrq); + add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/get", getRes); + add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/get", getFrqRes); + + // global setters + add_message_callback("service/CyanLight/frqres/set", setFrqRes); + add_message_callback("service/CyanLight/frequency/set", setFrq); + add_message_callback("service/CyanLight/resolution/set", setRes); + + // global getters + add_message_callback("service/CyanLight/frequency/get", getFrq); + add_message_callback("service/CyanLight/resolution/get", getRes); + add_message_callback("service/CyanLight/frqres/get", getFrqRes); } @@ -168,3 +182,22 @@ bool CyanLight::Controller::setResolution(uint8_t res_bits) { uint8_t CyanLight::Controller::getResolution() { return this->resolution; } + +void CyanLight::Controller::publishFrequency() { + char tmp[16]; + snprintf(tmp, sizeof(tmp), "%i", this->getFrequency()); + publish_message(DEVICE_NAMESPACE "CyanLight/frequency", tmp); +} + +void CyanLight::Controller::publishResolution() { + char tmp[16]; + snprintf(tmp, sizeof(tmp), "%i", this->getResolution()); + publish_message(DEVICE_NAMESPACE "CyanLight/resolution", tmp); +} + +void CyanLight::Controller::publishFrqRes() { + char tmp[32]; + snprintf(tmp, sizeof(tmp), "%i%s%i", this->getFrequency(), delimiter.c_str(), this->getResolution()); + publish_message(DEVICE_NAMESPACE "CyanLight/frqres", tmp); +} + diff --git a/CLC-qthing/CyanLightControl.hpp b/CLC-qthing/CyanLightControl.hpp index 2a45baa..1afdd3e 100644 --- a/CLC-qthing/CyanLightControl.hpp +++ b/CLC-qthing/CyanLightControl.hpp @@ -28,6 +28,10 @@ namespace CyanLight { uint8_t getResolution(); bool setFrqRes(uint32_t frq_hz, uint8_t res_bits); + + void publishFrqRes(); + void publishFrequency(); + void publishResolution(); private: uint32_t frequency = 1000; // Hz uint8_t resolution = 10; // bits -- GitLab