From 9c3627d6a47e7f5470a6dd19fa88d56332fa4fc0 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Tue, 23 Nov 2021 21:21:48 +0100
Subject: [PATCH] =?UTF-8?q?Adding=20more=20getters=20and=20setters?=
 =?UTF-8?q?=E2=80=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CLC-qthing/Controller.cpp       | 53 ++++++++++++++++++++++++++++++---
 CLC-qthing/CyanLightControl.hpp | 14 +++++++--
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/CLC-qthing/Controller.cpp b/CLC-qthing/Controller.cpp
index 30495be..6c3a9d2 100644
--- a/CLC-qthing/Controller.cpp
+++ b/CLC-qthing/Controller.cpp
@@ -6,8 +6,8 @@
 #include <string>
 #include <cstdio>
 #include <cstdlib>
-#include <function>
 #include <algorithm>
+#include <functional>
 
 #include "esp_err.h"
 #include "esp_log.h"
@@ -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);
 
@@ -54,7 +54,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
 
     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);
       // 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
   }
 
 
+  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) {
     long int frq = strtol(message.c_str(), NULL, 0);
     this->setFrequency(frq);
@@ -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) {
     this->publishFrequency();
   };
@@ -110,6 +123,7 @@ CyanLight::Controller::Controller(uint8_t networkChannel, uint8_t channelsConfig
 
   // device-local setters
   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/resolution/set", setRes);
 
@@ -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/resolution/get", getRes);
   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
   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/resolution/set", setRes);
 
@@ -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/resolution/get", getRes);
   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) {
     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) {
   }
 }
 
+
+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) {
   return this->setFrqRes(frq_hz, this->resolution);
 }
@@ -183,6 +215,19 @@ uint8_t CyanLight::Controller::getResolution() {
   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() {
   char tmp[16];
   snprintf(tmp, sizeof(tmp), "%i", this->getFrequency());
diff --git a/CLC-qthing/CyanLightControl.hpp b/CLC-qthing/CyanLightControl.hpp
index 1afdd3e..afeb7d1 100644
--- a/CLC-qthing/CyanLightControl.hpp
+++ b/CLC-qthing/CyanLightControl.hpp
@@ -17,9 +17,15 @@ namespace CyanLight {
 
   class Controller {
     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);
       uint32_t getFrequency();
@@ -32,11 +38,13 @@ namespace CyanLight {
       void publishFrqRes();
       void publishFrequency();
       void publishResolution();
+      void publishBaseChannel();
+      void publishChannelCount();
     private:
       uint32_t frequency = 1000;  // Hz
       uint8_t resolution =  10;  // bits
 
-      uint8_t networkChannel;
+      uint16_t baseChannel;
       uint8_t channelsConfigured;
       PWMChannel **channels;
 
-- 
GitLab