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

Adding more getters and setters…

parent b25a691f
No related branches found
No related tags found
No related merge requests found
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include <string> #include <string>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <function>
#include <algorithm> #include <algorithm>
#include <functional>
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
...@@ -28,7 +28,7 @@ float CyanLight::bytes2float(uint8_t *bytes) { ...@@ -28,7 +28,7 @@ float CyanLight::bytes2float(uint8_t *bytes) {
} }
CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfigured) : networkChannel{networkChannel} { CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigured) : baseChannel{baseChannel} {
this->channelsConfigured = std::max(channelsConfigured, CyanLight::MAX_CHANNELS); this->channelsConfigured = std::max(channelsConfigured, CyanLight::MAX_CHANNELS);
...@@ -54,7 +54,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -54,7 +54,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
add_message_callback(topic, [&, i](std::string message) { add_message_callback(topic, [&, i](std::string message) {
this->setChannel(i, strtof(message.c_str(), NULL)); this->setPWM(i, strtof(message.c_str(), NULL));
// float f = strtof(message.c_str(), NULL); // float f = strtof(message.c_str(), NULL);
// ESP_LOGW(TAG, "i: %d msg: %s f: %f", i, message.c_str(), f); // ESP_LOGW(TAG, "i: %d msg: %s f: %f", i, message.c_str(), f);
...@@ -62,6 +62,11 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -62,6 +62,11 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
} }
std::function<void(std::string)> setCh = [&](std::string message) {
long int ch = strtol(message.c_str(), NULL, 0);
this->setBaseChannel(ch);
};
std::function<void(std::string)> setFrq = [&](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);
...@@ -95,6 +100,14 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -95,6 +100,14 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
}; };
std::function<void(std::string)> getBCh = [&](std::string ignored) {
this->publishBaseChannel();
};
std::function<void(std::string)> getChs = [&](std::string ignored) {
this->publishChannelCount();
};
std::function<void(std::string)> getFrq = [&](std::string ignored) { std::function<void(std::string)> getFrq = [&](std::string ignored) {
this->publishFrequency(); this->publishFrequency();
}; };
...@@ -110,6 +123,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -110,6 +123,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
// device-local setters // device-local setters
add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", setFrqRes); add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/set", setFrqRes);
add_message_callback(DEVICE_NAMESPACE "CyanLight/channel/set", setCh);
add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/set", setFrq); add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/set", setFrq);
add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/set", setRes); add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/set", setRes);
...@@ -117,9 +131,12 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -117,9 +131,12 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/get", getFrq); add_message_callback(DEVICE_NAMESPACE "CyanLight/frequency/get", getFrq);
add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/get", getRes); add_message_callback(DEVICE_NAMESPACE "CyanLight/resolution/get", getRes);
add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/get", getFrqRes); add_message_callback(DEVICE_NAMESPACE "CyanLight/frqres/get", getFrqRes);
add_message_callback(DEVICE_NAMESPACE "CyanLight/channel/get", getBCh);
add_message_callback(DEVICE_NAMESPACE "CyanLight/channels/get", getChs);
// global setters // global setters
add_message_callback("service/CyanLight/frqres/set", setFrqRes); add_message_callback("service/CyanLight/frqres/set", setFrqRes);
add_message_callback("service/CyanLight/channel/set", setCh);
add_message_callback("service/CyanLight/frequency/set", setFrq); add_message_callback("service/CyanLight/frequency/set", setFrq);
add_message_callback("service/CyanLight/resolution/set", setRes); add_message_callback("service/CyanLight/resolution/set", setRes);
...@@ -127,10 +144,12 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig ...@@ -127,10 +144,12 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
add_message_callback("service/CyanLight/frequency/get", getFrq); add_message_callback("service/CyanLight/frequency/get", getFrq);
add_message_callback("service/CyanLight/resolution/get", getRes); add_message_callback("service/CyanLight/resolution/get", getRes);
add_message_callback("service/CyanLight/frqres/get", getFrqRes); add_message_callback("service/CyanLight/frqres/get", getFrqRes);
add_message_callback("service/CyanLight/channel/get", getBCh);
add_message_callback("service/CyanLight/channels/get", getChs);
} }
void CyanLight::Controller::setChannel(uint8_t channel, float value) { void CyanLight::Controller::setPWM(uint8_t channel, float value) {
if (channel >= this->channelsConfigured) { if (channel >= this->channelsConfigured) {
ESP_LOGW(TAG, "ChannelID out of range: channel[ %i ]", channel); ESP_LOGW(TAG, "ChannelID out of range: channel[ %i ]", channel);
...@@ -167,6 +186,19 @@ bool CyanLight::Controller::setFrqRes(uint32_t frq_hz, uint8_t res_bits) { ...@@ -167,6 +186,19 @@ bool CyanLight::Controller::setFrqRes(uint32_t frq_hz, uint8_t res_bits) {
} }
} }
void CyanLight::Controller::setBaseChannel(uint16_t baseChannel) {
this->baseChannel = baseChannel;
}
uint16_t CyanLight::Controller::getBaseChannel() {
return this->baseChannel;
}
uint8_t CyanLight::Controller::getChannelCount() {
return this->channelsConfigured;
}
bool CyanLight::Controller::setFrequency(uint32_t frq_hz) { bool CyanLight::Controller::setFrequency(uint32_t frq_hz) {
return this->setFrqRes(frq_hz, this->resolution); return this->setFrqRes(frq_hz, this->resolution);
} }
...@@ -183,6 +215,19 @@ uint8_t CyanLight::Controller::getResolution() { ...@@ -183,6 +215,19 @@ uint8_t CyanLight::Controller::getResolution() {
return this->resolution; return this->resolution;
} }
void CyanLight::Controller::publishBaseChannel() {
char tmp[16];
snprintf(tmp, sizeof(tmp), "%i", this->getBaseChannel());
publish_message(DEVICE_NAMESPACE "CyanLight/channel", tmp);
}
void CyanLight::Controller::publishChannelCount() {
char tmp[16];
snprintf(tmp, sizeof(tmp), "%i", this->getChannelCount());
publish_message(DEVICE_NAMESPACE "CyanLight/channelCnt", tmp);
}
void CyanLight::Controller::publishFrequency() { void CyanLight::Controller::publishFrequency() {
char tmp[16]; char tmp[16];
snprintf(tmp, sizeof(tmp), "%i", this->getFrequency()); snprintf(tmp, sizeof(tmp), "%i", this->getFrequency());
......
...@@ -17,9 +17,15 @@ namespace CyanLight { ...@@ -17,9 +17,15 @@ namespace CyanLight {
class Controller { class Controller {
public: public:
Controller(uint8_t networkChannel, uint8_t channelsConfigured = 1); Controller(uint16_t baseChannel, uint8_t channelsConfigured = 1);
void setChannel(uint8_t channel, float value); void setPWM(uint8_t channel, float value);
uint8_t getChannelCount();
void setBaseChannel(uint16_t baseChannel);
uint16_t getBaseChannel();
bool setFrequency(uint32_t frq_hz); bool setFrequency(uint32_t frq_hz);
uint32_t getFrequency(); uint32_t getFrequency();
...@@ -32,11 +38,13 @@ namespace CyanLight { ...@@ -32,11 +38,13 @@ namespace CyanLight {
void publishFrqRes(); void publishFrqRes();
void publishFrequency(); void publishFrequency();
void publishResolution(); void publishResolution();
void publishBaseChannel();
void publishChannelCount();
private: private:
uint32_t frequency = 1000; // Hz uint32_t frequency = 1000; // Hz
uint8_t resolution = 10; // bits uint8_t resolution = 10; // bits
uint8_t networkChannel; uint16_t baseChannel;
uint8_t channelsConfigured; uint8_t channelsConfigured;
PWMChannel **channels; PWMChannel **channels;
......
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