diff --git a/CLC-qthing/SiliconTorch/FxCyanF-classImplementation.cpp b/CLC-qthing/SiliconTorch/FxCyanF-classImplementation.cpp index 162f12664ef49b1140f98c1d1d0ef6dbeff0bcf2..bffc535fe4441ca18542b7b6fa49f1a96c2181f4 100644 --- a/CLC-qthing/SiliconTorch/FxCyanF-classImplementation.cpp +++ b/CLC-qthing/SiliconTorch/FxCyanF-classImplementation.cpp @@ -33,9 +33,10 @@ using json = nlohmann::json; namespace SiliconTorch { - //namespace FxCyanF { + namespace FxCyanF { const char* TAG = "FxCyanF"; + const char* nvsNameSpace = TAG; const std::string HEADER("fxCyanF"); @@ -417,5 +418,5 @@ namespace SiliconTorch { } } } -// } + } } diff --git a/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp b/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp index f5cd73e02095cfe79edc0f1c6c48260ffa0e912c..75349dc2d77260de88f67205848f39768373558a 100644 --- a/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp +++ b/CLC-qthing/SiliconTorch/FxCyanF-configureFromNVS.cpp @@ -2,7 +2,7 @@ // C++ system level #include <string> -// #include <cstdio> +#include <cstdio> // snprintf // #include <cstdlib> // #include <algorithm> // #include <functional> @@ -28,23 +28,62 @@ namespace SiliconTorch { - // namespace FxCyanF { + namespace FxCyanF { - const std::string nameSpace = "FxCyanF"; - bool configureFromNVS(FxCyanF& target) { + if (target.getChannelCount() > 0) { + ESP_LOGW(TAG, "Target mustn't have any channels configured"); + return false; + } + + nvs_flash_init(); nvs_handle_t nvs; - nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs); + nvs_open(nvsNameSpace, NVS_READONLY, &nvs); + + + uint32_t frq = 0; + nvs_get_u32(nvs, "frequency", &frq); + + uint8_t res = 0; + nvs_get_u8(nvs, "resolution", &res); + + + if (frq == 0 || res == 0) { + ESP_LOGW(TAG, "No config for CyanLight found"); // TODO: Log-Level Debug + return false; + } + + + ESP_LOGW(TAG, "Found config for FxCyanF{ frequency[ %d Hz ] resolution[ %d Bit ] } in NVS", frq, res); // TODO: Log-Level Debug + + for (uint8_t ch = 0; ch < MAX_CHANNELS; ch++) { + + int8_t gpio = -1; + + char channel[32]; + snprintf(channel, 32, "ch%d_gpio", ch); + + nvs_get_i8(nvs, channel, &gpio); + + if (gpio >= 0) { + + target.addChannel((uint8_t)gpio); + + ESP_LOGW(TAG, "channel[ %d ] gpio[ %d ]", ch, gpio); // TODO: Log-Level Info + } else { + break; + } + } - return false; + return true; } - // } + } } diff --git a/CLC-qthing/SiliconTorch/FxCyanF.hpp b/CLC-qthing/SiliconTorch/FxCyanF.hpp index a3fc5e0637b5857d71ecc894740d9ebb336ea4b6..eceac4d8a6603353d8d8e8cb0ffbcf91661c8921 100644 --- a/CLC-qthing/SiliconTorch/FxCyanF.hpp +++ b/CLC-qthing/SiliconTorch/FxCyanF.hpp @@ -17,10 +17,14 @@ namespace SiliconTorch { - //namespace FxCyanF { + namespace FxCyanF { + extern const char* TAG; + extern const char* nvsNameSpace; extern const std::string HEADER; + extern const uint8_t MAX_CHANNELS; + typedef std::function<void()> PacketHandledCallback; typedef std::function<float(float)> GammaCorrector; @@ -89,5 +93,5 @@ namespace SiliconTorch { bool configureFromNVS(FxCyanF& target); - //} + } } diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.cpp b/CLC-qthing/SiliconTorch/NVSExplorer.cpp index b3230b3989d1fb9154222539b228ec5371710ab7..d276935ae67fd7b102913480cd6c2ff0016f0314 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.cpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.cpp @@ -1,9 +1,9 @@ -#include "CapMan.hpp" +#include "NVSExplorer.hpp" // C++ system level #include <set> -// #include <cstring> // memset, strncmp -// #include <cstdlib> // TODO: is this for memcpy? +#include <cstring> // memset, memcpy, strncmp +// #include <cstdlib> #include <cinttypes> // #include <functional> @@ -21,17 +21,28 @@ + + namespace SiliconTorch { namespace NVSExplorer { - // NVSExplorer::NVSExplorer() { - void BullShit() { + const char* TAG = "NVSExplorer"; + + + NVSExplorer::NVSExplorer() { + + ESP_LOGI(TAG, "Starting NVSExplorer[📡]"); nvs_flash_init(); + } + - ESP_LOGW("NVS", "Scanning 👀"); + void NVSExplorer::BullShit() { + + + ESP_LOGW(TAG, "Scanning 👀"); nvs_iterator_t it = nvs_entry_find("nvs", NULL, NVS_TYPE_ANY); @@ -40,21 +51,89 @@ namespace SiliconTorch { nvs_entry_info(it, &info); it = nvs_entry_next(it); - ESP_LOGW("NVS", "namespace[ %s ] key[ %s ] type[ %d ]", info.namespace_name, info.key, info.type); + ESP_LOGW(TAG, "namespace[ %s ] key[ %s ] type[ %d ]", info.namespace_name, info.key, info.type); } } - /* std::set<std::string> listKeys(std::string& _namespace) { + std::set<std::string> NVSExplorer::listKeys(std::string& nameSpace) { + + std::set<std::string> keys; + + nvs_iterator_t it = nvs_entry_find("nvs", nameSpace.c_str(), NVS_TYPE_ANY); + + while (it != NULL) { + nvs_entry_info_t info; + nvs_entry_info(it, &info); + it = nvs_entry_next(it); + + ESP_LOGW(TAG, "namespace[ %s ] key[ %s ] type[ %d ]", info.namespace_name, info.key, info.type); // TODO: remove after dev! + + keys.insert(info.key); + } + + return keys; } - std::set<std::string> listNamespaces() { - std::string _namespace = NULL; - return listKeys(_namespace); - } */ + std::set<std::string> NVSExplorer::listNamespaces() { + + std::set<std::string> namespaces; + + + nvs_iterator_t it = nvs_entry_find("nvs", NULL, NVS_TYPE_ANY); + + while (it != NULL) { + nvs_entry_info_t info; + nvs_entry_info(it, &info); + it = nvs_entry_next(it); + + ESP_LOGW(TAG, "namespace[ %s ] key[ %s ] type[ %d ]", info.namespace_name, info.key, info.type); // TODO: remove after dev! + + namespaces.insert(info.namespace_name); + } + + return namespaces; + } + + + + std::string toString(std::set<std::string>& set, std::string delimiter) { + + uint32_t length = 0; + + // caching this temporarily + // TODO: do we need this or would the compiler be smart enough to cache it itself? + // or couldn't he cache it itself because C++ is strict and it is a method-call? + // questions over questions……… + uint32_t delimiterLength = delimiter.length(); + + for (auto const& item : set) length += item.length() + delimiterLength; // TODO: replace delimiterLength -> delimiter.length() ? + + + length++; + char str[length]; + char* ptr = str; + memset(str, 0x00, length); + + for (auto const& item : set) { + + uint32_t len = item.length(); + + std::memcpy(ptr, item.c_str(), len); + ptr += len; + + std::memcpy(ptr, delimiter.c_str(), delimiterLength); + ptr += delimiterLength; + } + + ptr -= delimiterLength; + memset(ptr, 0x00, delimiterLength); // clear out the trailing delimiter + + return std::string(str); + } } diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.hpp b/CLC-qthing/SiliconTorch/NVSExplorer.hpp index 6f0a9e2a8361b6c00bb215205d59db411436c27a..488bd467cb27e326785374d16590378e834e1d87 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.hpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.hpp @@ -1,6 +1,7 @@ #pragma once // C++ system level +#include <set> // #include <cstring> // memset, strncmp // #include <cstdlib> // TODO: is this for memcpy? #include <cinttypes> @@ -24,22 +25,23 @@ namespace SiliconTorch { namespace NVSExplorer { - //class NVSExplorer { + extern const char* TAG; - //public: - //NVSExplorer(); + class NVSExplorer { - static void BullShit(); + public: + void BullShit(); - // std::set<std::string> listNamespaces(); - // std::set<std::string> listKeys(std::string& _namespace); + + std::set<std::string> listNamespaces(); + std::set<std::string> listKeys(std::string& _namespace); // ⬇⬇ Singleton stuff ⬇⬇ -/* static NVSExplorer& instance() { + static NVSExplorer& instance() { static NVSExplorer _instance; return _instance; } @@ -48,12 +50,18 @@ namespace SiliconTorch { void operator=(NVSExplorer const&) = delete; // ⬆⬆ Singleton stuff ⬆⬆ -*/ - //private: + + private: + + NVSExplorer(); + + + + }; + std::string toString(std::set<std::string>& set, std::string delimiter = " "); - //}; } } diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index 0bd8b40b9222467b16a9530014b873b222618917..511a57c2ca57cb870ca90c82ce435a9672dd2f42 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -8,6 +8,7 @@ #include "CyanLight.hpp" +#include "SiliconTorch/FxCyanF.hpp" // ### LIBS FOR TESTING ### @@ -25,6 +26,10 @@ //#include "SiliconTorch/FxPublish.hpp" + +#include "SiliconTorch/NVSExplorer.hpp" +#include <set> // remove after NVSExplorer development! + // ### END LIBS ### @@ -40,8 +45,6 @@ CyanLight::CyanLightControl* ctrl; -#include "SiliconTorch/NVSExplorer.hpp" - void device_main() { @@ -56,7 +59,41 @@ void device_main() { qthing::power_managment_max_power(); + qthing::add_binary_message_callback("+/+/test", [&](qthing::multipart_message_t msg) { + + // if (msg.length != msg.total_length) { + // ESP_LOGD("+/+/test", "multipart messages not supported atm"); + // return; + // } + + char buf[128]; + memset(buf, 0x00, 128); + memcpy(buf, msg.payload, msg.length); + + ESP_LOGE("+/+/test", "HDL_0 topic[ %s ] message[ %s ]", msg.topic.c_str(), buf); + + }); + +/* qthing::add_binary_message_callback("#", [&](qthing::multipart_message_t msg) { + char buf[128]; + memset(buf, 0x00, 128); + memcpy(buf, msg.payload, msg.length); + + ESP_LOGE("#", "HDL_1 topic[ %s ] message[ %s ]", msg.topic.c_str(), buf); + + }); + + qthing::add_binary_message_callback("#", [&](qthing::multipart_message_t msg) { + + char buf[128]; + memset(buf, 0x00, 128); + memcpy(buf, msg.payload, msg.length); + + ESP_LOGE("####", "HDL_2 topic[ %s ] message[ %s ]", msg.topic.c_str(), buf); + + }); +*/ //cyanBus = new SiliconTorch::CyanBus::CyanBus(13, 14, 12, 15, 2000000); // Pinout of CyanStripe @@ -79,14 +116,38 @@ void device_main() { // TODO: ??? - qthing::enable_wifi(); + // Trigger singleton initialization + SiliconTorch::NVSExplorer::NVSExplorer::instance(); + + + std::set<std::string> namespaces = SiliconTorch::NVSExplorer::NVSExplorer::instance().listNamespaces(); + + std::string _ns = SiliconTorch::NVSExplorer::toString(namespaces, " ~=[]=~ "); + + ESP_LOGW("__SET__", "Listing Namespaces: %s", _ns.c_str()); + + + + cfg.apply(); + qthing::enable_wifi(); - //return; + return; + ctrl = new CyanLight::CyanLightControl(0); + + auto fxCyanF = (SiliconTorch::FxCyanF::FxCyanF) *ctrl; + SiliconTorch::FxCyanF::configureFromNVS(fxCyanF); + + qthing::enable_wifi(); + return; + + + /* + ctrl = new CyanLight::CyanLightControl(3); //ctrl->addChannel(16); //ctrl->addChannel(17); @@ -113,6 +174,8 @@ void device_main() { qthing::enable_wifi(); return; + */ + auto delay = 10 / portTICK_PERIOD_MS;