Skip to content
Snippets Groups Projects
Commit b25a691f authored by fxk8y's avatar fxk8y :spider:
Browse files

Adding global getters and setters

parent 69c5f7cf
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <string> #include <string>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <function>
#include <algorithm> #include <algorithm>
#include "esp_err.h" #include "esp_err.h"
...@@ -61,18 +62,17 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -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); long int frq = strtol(message.c_str(), NULL, 0);
this->setFrequency(frq); 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); long int res = strtol(message.c_str(), NULL, 0);
this->setResolution(res); this->setResolution(res);
}); };
// atomic setter for both values std::function<void(std::string)> setFrqRes = [&](std::string message) {
add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", [&](std::string message) {
std::string::size_type found = message.find(delimiter); std::string::size_type found = message.find(delimiter);
if (found == std::string::npos) { if (found == std::string::npos) {
...@@ -92,27 +92,41 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -92,27 +92,41 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
} }
this->setFrqRes(frq, res); this->setFrqRes(frq, res);
}); };
add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/get", [&](std::string ignored) { std::function<void(std::string)> getFrq = [&](std::string ignored) {
char tmp[16]; this->publishFrequency();
snprintf(tmp, sizeof(tmp), "%i", this->getFrequency()); };
publish_message(DEVICE_NAMESPACE "CyanLight/frequency", tmp);
}); std::function<void(std::string)> getRes = [&](std::string ignored) {
this->publishResolution();
add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/get", [&](std::string ignored) { };
char tmp[16];
snprintf(tmp, sizeof(tmp), "%i", this->getResolution()); std::function<void(std::string)> getFrqRes = [&](std::string ignored) {
publish_message(DEVICE_NAMESPACE "CyanLight/resolution", tmp); this->publishFrqRes();
}); };
// 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) { // device-local setters
char tmp[32]; add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", setFrqRes);
snprintf(tmp, sizeof(tmp), "%i%s%i", this->getFrequency(), delimiter.c_str(), this->getResolution()); add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/set", setFrq);
publish_message(DEVICE_NAMESPACE "CyanLight/frqres", tmp); 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) { ...@@ -168,3 +182,22 @@ bool CyanLight::Controller::setResolution(uint8_t res_bits) {
uint8_t CyanLight::Controller::getResolution() { uint8_t CyanLight::Controller::getResolution() {
return this->resolution; 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);
}
...@@ -28,6 +28,10 @@ namespace CyanLight { ...@@ -28,6 +28,10 @@ namespace CyanLight {
uint8_t getResolution(); uint8_t getResolution();
bool setFrqRes(uint32_t frq_hz, uint8_t res_bits); bool setFrqRes(uint32_t frq_hz, uint8_t res_bits);
void publishFrqRes();
void publishFrequency();
void publishResolution();
private: private:
uint32_t frequency = 1000; // Hz uint32_t frequency = 1000; // Hz
uint8_t resolution = 10; // bits uint8_t resolution = 10; // bits
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment