From d251da40ef4c4978130d3d9652faa15e2070fb9f Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Tue, 21 Dec 2021 08:23:50 +0100
Subject: [PATCH] =?UTF-8?q?Fixing=20all=20refactoring=20errors=20and=20sli?=
 =?UTF-8?q?ghtly=20polishing=20code=F0=9F=98=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CLC-qthing/CMakeLists.txt           |  2 +-
 CLC-qthing/CyanLight.cpp            | 29 +++++++++++++---------
 CLC-qthing/CyanLight.hpp            |  6 ++---
 CLC-qthing/SiliconTorch/FxCyanF.cpp | 38 ++++++++++++++---------------
 CLC-qthing/SiliconTorch/FxCyanF.hpp |  4 +--
 5 files changed, 42 insertions(+), 37 deletions(-)

diff --git a/CLC-qthing/CMakeLists.txt b/CLC-qthing/CMakeLists.txt
index 2dd04d5..8b29cc8 100644
--- a/CLC-qthing/CMakeLists.txt
+++ b/CLC-qthing/CMakeLists.txt
@@ -1,6 +1,6 @@
 idf_component_register(
   SRC_DIRS
-  "."
+  "." "SiliconTorch"
 
   INCLUDE_DIRS
   "."
diff --git a/CLC-qthing/CyanLight.cpp b/CLC-qthing/CyanLight.cpp
index 317ccc7..9f6b972 100644
--- a/CLC-qthing/CyanLight.cpp
+++ b/CLC-qthing/CyanLight.cpp
@@ -5,23 +5,30 @@
 
 uint8_t MAX_CHANNELS = 8;
 
-extern const uint8_t CyanLight::channelGPIOs[] = { 42 };  // !!!
+const char* TAG = "CyanLight";
 
+const uint8_t channelGPIOs[] = { 27, 16, 17, 18, 19, 21, 22, 23 };
 
-CyanLight::CyanLightControl::CyanLightControl(uint8_t channelsConfigured) {
+namespace CyanLight {
 
-  if (channelsConfigured > MAX_CHANNELS)
-    ESP_LOGE("", "Cannot create channel#[ %i ]! Hardware limit is #[ %i ]", channelsConfigured, MAX_CHANNELS);
+  CyanLightControl::CyanLightControl(uint8_t channelsConfigured, uint32_t baseChannel) : SiliconTorch::FxCyanF(baseChannel) {
 
-  uint8_t channels = std::min(channelsConfigured, MAX_CHANNELS);
+    uint8_t channels = std::min(channelsConfigured, MAX_CHANNELS);
+
+    for (uint8_t i = 0; i < channels; i++) this->addChannel();
+  }
 
-  for (uint8_t i = 0; i < channels; i++)
-    this->addChannel((uint8_t)CyanLight::channelGPIOs[i]);
-}
 
+  bool CyanLightControl::addChannel() {
 
-bool CyanLight::CyanLightControl::addChannel() {
-  // TODO: implement!
+    auto ch = this->getChannelCount();
+
+    if (ch < MAX_CHANNELS) {
+      return FxCyanF::addChannel((uint8_t)channelGPIOs[ch]);
+    } else {
+      ESP_LOGE(TAG, "Cannot create channel#[ %i ]! CLC hardware limit is #[ %i ]", ch, MAX_CHANNELS);
+      return false;
+    }
+}
 
-  return false;
 }
diff --git a/CLC-qthing/CyanLight.hpp b/CLC-qthing/CyanLight.hpp
index de3691c..300ce77 100644
--- a/CLC-qthing/CyanLight.hpp
+++ b/CLC-qthing/CyanLight.hpp
@@ -7,11 +7,9 @@
 
 namespace CyanLight {
 
-  extern const uint8_t channelGPIOs[];
-
-  class CyanLightControl : SiliconTorch::FxCyanF {
+  class CyanLightControl : public SiliconTorch::FxCyanF {
     public:
-      CyanLightControl(uint8_t channelsConfigured);
+      CyanLightControl(uint8_t channelsConfigured, uint32_t baseChannel = 0);
 
       bool addChannel();
 
diff --git a/CLC-qthing/SiliconTorch/FxCyanF.cpp b/CLC-qthing/SiliconTorch/FxCyanF.cpp
index 0cf956c..d212edf 100644
--- a/CLC-qthing/SiliconTorch/FxCyanF.cpp
+++ b/CLC-qthing/SiliconTorch/FxCyanF.cpp
@@ -17,9 +17,9 @@
 using namespace qthing;
 
 
-extern const char *TAG = "fxCyanF";
+const char* SiliconTorch::TAG = "fxCyan";  // TODO: maybe suffix with exact protocol…?
 
-const uint8_t MAX_CHANNELS = 8;
+const uint8_t MAX_CHANNELS = 8;  // Maybe 16…?
 
 const std::string delimiter = ":";
 
@@ -33,7 +33,7 @@ static float bytes2float(const char *bytes) {
 }
 
 
-SiliconTorch::FxCyanF::FxCyanF() {
+SiliconTorch::FxCyanF::FxCyanF(uint32_t baseChannel) : baseChannel(baseChannel) {
 
   // TODO: make vector!
   this->channels = new SiliconTorch::Impl::PWMChannel*[MAX_CHANNELS];
@@ -42,7 +42,7 @@ SiliconTorch::FxCyanF::FxCyanF() {
   this->timer_cfg.freq_hz         = this->frequency;
   this->timer_cfg.speed_mode      = LEDC_HIGH_SPEED_MODE;
   this->timer_cfg.timer_num       = LEDC_TIMER_0;
-  this->ledc_timer.clk_cfg        = LEDC_AUTO_CLK;
+  this->timer_cfg.clk_cfg         = LEDC_AUTO_CLK;
 
   ledc_timer_config(&this->timer_cfg);
 
@@ -63,22 +63,22 @@ SiliconTorch::FxCyanF::FxCyanF() {
   });
 
 
-  std::function<void(std::string)> setCh = [&](std::string message) {
+  std::function<void(const std::string&)> setCh = [&](const 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(const std::string&)> setFrq = [&](const std::string& message) {
     long int frq = strtol(message.c_str(), NULL, 0);
     this->setFrequency(frq);
   };
 
-  std::function<void(std::string)> setRes = [&](std::string message) {
+  std::function<void(const std::string&)> setRes = [&](const std::string& message) {
     long int res = strtol(message.c_str(), NULL, 0);
     this->setResolution(res);
   };
 
-  std::function<void(std::string)> setFrqRes = [&](std::string message) {
+  std::function<void(const std::string&)> setFrqRes = [&](const std::string& message) {
     std::string::size_type found = message.find(delimiter);
 
     if (found == std::string::npos) {
@@ -101,23 +101,23 @@ SiliconTorch::FxCyanF::FxCyanF() {
   };
 
 
-  std::function<void(std::string)> getBCh = [&](std::string ignored) {
+  std::function<void(const std::string&)> getBCh = [&](const std::string& ignored) {
     this->publishBaseChannel();
   };
 
-  std::function<void(std::string)> getChs = [&](std::string ignored) {
+  std::function<void(const std::string&)> getChs = [&](const std::string& ignored) {
     this->publishChannelCount();
   };
 
-  std::function<void(std::string)> getFrq = [&](std::string ignored) {
+  std::function<void(const std::string&)> getFrq = [&](const std::string& ignored) {
     this->publishFrequency();
   };
 
-  std::function<void(std::string)> getRes = [&](std::string ignored) {
+  std::function<void(const std::string&)> getRes = [&](const std::string& ignored) {
     this->publishResolution();
   };
 
-  std::function<void(std::string)> getFrqRes = [&](std::string ignored) {
+  std::function<void(const std::string&)> getFrqRes = [&](const std::string& ignored) {
     this->publishFrqRes();
   };
 
@@ -152,7 +152,7 @@ SiliconTorch::FxCyanF::FxCyanF() {
 
 bool SiliconTorch::FxCyanF::addChannel(uint8_t gpio) {
   if (this->channelsConfigured >= MAX_CHANNELS) {
-    ESP_LOGE(TAG, "Maximum number of PWMChannels reached");
+    ESP_LOGE(TAG, "Cannot create channel#[ %i ]! ESP32 hardware limit is #[ %i ]", this->channelsConfigured, MAX_CHANNELS);
     return false;
   }
 
@@ -164,7 +164,7 @@ bool SiliconTorch::FxCyanF::addChannel(uint8_t gpio) {
   char topic[32];
   snprintf(topic, sizeof(topic), "pwm/$%i", channel);
 
-  add_message_callback(this->genDeviceTopic(topic), [&, channel](std::string message) {
+  add_message_callback(this->genDeviceTopic(topic), [&, channel](const std::string& message) {
     this->setPWM(channel, strtof(message.c_str(), NULL));
     this->callPacketCallback();
   });
@@ -233,7 +233,7 @@ void SiliconTorch::FxCyanF::setPWM(uint8_t channel, float value) {
 }
 
 
-void SiliconTorch::FxCyanF::setGammaCorrector(CyanLight::GammaCorrector gammaCorrector) {
+void SiliconTorch::FxCyanF::setGammaCorrector(SiliconTorch::GammaCorrector gammaCorrector) {
   this->gammaCorrector = gammaCorrector;
 }
 
@@ -287,17 +287,17 @@ uint8_t SiliconTorch::FxCyanF::getResolution() {
   return this->resolution;
 }
 
-void SiliconTorch::FxCyanF::setPacketHandledCallback(CyanLight::PacketHandledCallback callback) {
+void SiliconTorch::FxCyanF::setPacketHandledCallback(SiliconTorch::PacketHandledCallback callback) {
   this->packetCallback = callback;
 }
 
 
 std::string SiliconTorch::FxCyanF::genDeviceTopic(const char *suffix) {
-  return std::string(DEVICE_NAMESPACE + "fxCyanF/") + std::string(suffix);
+  return std::string(DEVICE_NAMESPACE + "fxCyan/") + std::string(suffix);
 }
 
 std::string SiliconTorch::FxCyanF::genServiceTopic(const char *suffix) {
-  return std::string("service/fxCyanF/") + std::string(suffix);
+  return std::string("service/fxCyan/") + std::string(suffix);
 }
 
 
diff --git a/CLC-qthing/SiliconTorch/FxCyanF.hpp b/CLC-qthing/SiliconTorch/FxCyanF.hpp
index 125fb28..4a71a88 100644
--- a/CLC-qthing/SiliconTorch/FxCyanF.hpp
+++ b/CLC-qthing/SiliconTorch/FxCyanF.hpp
@@ -20,7 +20,7 @@ namespace SiliconTorch {
 
   class FxCyanF {
     public:
-      FxCyanF(uint16_t baseChannel = 0);
+      FxCyanF(uint32_t baseChannel = 0);
 
       bool addChannel(uint8_t gpio);
 
@@ -59,7 +59,7 @@ namespace SiliconTorch {
       uint32_t frequency = 1000;  // Hz
       uint8_t resolution =   10;  // bits
 
-      uint16_t baseChannel;
+      uint32_t baseChannel;
       uint8_t channelsConfigured = 0;
 
       Impl::PWMChannel **channels;
-- 
GitLab