From e7feceb5a63e0dcff2c3882aa6bd8d4df88022c5 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Thu, 9 Jun 2022 11:57:37 +0200
Subject: [PATCH] Better API; fixing bugs; loading default PWM value from NVS
 by now

---
 .../SiliconTorch/FxCyanF-configureFromNVS.cpp | 37 ++++++++++---------
 CLC-qthing/SiliconTorch/FxCyanF.hpp           |  2 +-
 2 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp b/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp
index aa15ee3..49e83fd 100644
--- a/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp
+++ b/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp
@@ -22,6 +22,8 @@
 #include <SpiderLib/ObjectTypes.hpp>
 
 
+const char* TAG_nvs = "FxCyanF-NVS";
+
 
 namespace SiliconTorch {
 
@@ -31,23 +33,19 @@ namespace SiliconTorch {
     const str ServiceNamespace = "fxCyan";
 
 
-    bool configureFromNVS(FxCyanF& target) {
-
-      if (target.getChannelCount() > 0) {
-        ESP_LOGW(TAG, "Target mustn't have any channels configured");
-        return false;
-      }
-
+    FxCyanF* configureFromNVS() {
 
       u8  state = 0;  // disabled -> 0   enabled -> 1
       state = NVSExplorer::NVSExplorer::instance().getUnsignedInt(ServiceNamespace, "state", state);
 
       if (state == 0) {
-        ESP_LOGI(TAG, "Service[ FxCyanF ] disabled[]");
-        return false;
+        ESP_LOGI(TAG_nvs, "Service[ FxCyanF ] disabled[❌]");
+        return NULL;
       }
 
-      ESP_LOGI(TAG, "Enabling Service[ FxCyanF ]");
+      FxCyanF* target = new FxCyanF();
+
+      ESP_LOGI(TAG_nvs, "Service[ FxCyanF ] enabling[✅]…");
 
       u32 frq = DefaultFrequency;
       u8  res = DefaultResolution;
@@ -56,8 +54,8 @@ namespace SiliconTorch {
       res = NVSExplorer::NVSExplorer::instance().getUnsignedInt(ServiceNamespace, "resolution", res);
 
 
-      ESP_LOGI(TAG, "Configuring: FxCyanF{ frequency[ %d Hz ]  resolution[ %d Bit ] }", frq, res);
-      target.setFrqRes(frq, res);
+      ESP_LOGI(TAG_nvs, "Configuring: FxCyanF{ frequency[ %d Hz ]  resolution[ %d Bit ] }", frq, res);
+      target->setFrqRes(frq, res);
 
       for (u8 ch = 0; ch < MAX_CHANNELS; ch++) {
 
@@ -73,20 +71,25 @@ namespace SiliconTorch {
         // NaN == "no value stored" or any other reading error
         if (std::isnan(pwm)) pwm = 0.0f;
 
-        if (gpio != 0xFF) {
+        if (gpio != 0xFF) {  // key exists
 
-          bool result = target.addChannel(gpio, pwm);
+          if ( target->addChannel(gpio, pwm) ) {  // channel creation successful
+  
+            ESP_LOGI(TAG_nvs, "Configuring: FxCyanF{ channel[ %d ]  gpio[ %d ]  pwm[ %f ] }", ch, gpio, pwm);
 
-          // TODO: do something with result…?
+            target->setPWM(ch, pwm);
 
-          ESP_LOGI(TAG, "Configuring: FxCyanF{ channel[ %d ]  gpio[ %d ]  pwm[ %f ] }", ch, gpio, pwm);
+          } else {
+            ESP_LOGW(TAG_nvs, "Configuring: FxCyanF{ channel[ %d ]  gpio[ %d ] }  FAILED[❌]", ch, gpio);
+            break;
+          }
 
         } else {
           break;
         }
       }
 
-      return true;
+      return target;
     }
 
 
diff --git a/CLC-qthing/SiliconTorch/FxCyanF.hpp b/CLC-qthing/SiliconTorch/FxCyanF.hpp
index 52c8427..0a7d19e 100644
--- a/CLC-qthing/SiliconTorch/FxCyanF.hpp
+++ b/CLC-qthing/SiliconTorch/FxCyanF.hpp
@@ -98,7 +98,7 @@ namespace SiliconTorch {
     };
 
 
-    bool configureFromNVS(FxCyanF& target);
+    FxCyanF* configureFromNVS(FxCyanF);
 
   }
 }
-- 
GitLab