From 26855819925c4eeef131f655bdc6cb76133ab072 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Mon, 25 Apr 2022 08:38:39 +0200 Subject: [PATCH] Implemented callback multiplexing map --- CLC-qthing/SpiderLib/CallbackMap.hpp | 87 ++++++++++++++++++++++++++++ CLC-qthing/device_main.cpp | 25 +++++++- 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 CLC-qthing/SpiderLib/CallbackMap.hpp diff --git a/CLC-qthing/SpiderLib/CallbackMap.hpp b/CLC-qthing/SpiderLib/CallbackMap.hpp new file mode 100644 index 0000000..ddcb538 --- /dev/null +++ b/CLC-qthing/SpiderLib/CallbackMap.hpp @@ -0,0 +1,87 @@ +#pragma once + +// C++ system level +#include <map> +#include <string> +#include <cstring> // memcpy +#include <functional> + +// ESP32 specific +// #include "esp_log.h" + +// project specific +// #include "CyanBusCRC.hpp" + +// qthing stuff +// #include "" + + +namespace SpiderLib { + + // Forward declaration + template <class> + class CallbackMap; + + + template <typename... Args> + class CallbackMap<void(Args...)> { + + public: + typedef std::function<void(Args...)> CallbackF; + + CallbackMap(uint32_t maxPrefixLength = 16) : maxPrefixLength(maxPrefixLength) {}; + + void operator()(const uint8_t* const buffer, uint32_t length, Args&&... args) const { + + uint32_t _len = std::min(maxPrefixLength, length); + + char* _prefix[_len + 1]; + _prefix[_len] = 0x00; + std::memcpy(_prefix, buffer, _len); + std::string prefix(_prefix); + + uint32_t bestMatchLEN = 0; + CallbackF callback = NULL; + for (auto it = callbackMap.begin(); it != callbackMap.end(); ++it) { + + // input buffer is >= current iterators magic string length + if (_len >= it->first.length() && prefix.find(it->first) == 0) { + + uint32_t matchLEN = it->first.length(); + if (matchLEN > bestMatchLEN) { + bestMatchLEN = it->first.length(); + callback = it->second; + } + } + } + + if (callback != NULL) callback(std::forward<Args>(args)...); + + } + + void add(std::string& magicString, CallbackF callback) const { + + if (magicString.length() > maxPrefixLength) { + // TODO: raise exception? log error? + return; + } + + if (callbackMap.count(magicString) > 0) { + + CallbackF oldCallback = callbackMap.at(magicString); + + callbackMap[magicString] = [oldCallback, callback](Args&&... args) { + oldCallback(std::forward<Args>(args)...); + callback(std::forward<Args>(args)...); + }; + } else { + callbackMap[magicString] = callback; + } + } + + private: + uint32_t maxPrefixLength; + mutable std::map<std::string, CallbackF> callbackMap; + }; + +} diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index 7505081..bdfacc1 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -17,11 +17,17 @@ #include "SiliconTorch/CyanBusCRC.hpp" #include "SiliconTorch/CyanBus.hpp" + +#include <memory> +#include "driver/gpio.h" #include "SiliconTorch/FxCyanRGB8.hpp" + // ### END LIBS ### +#include "SpiderLib/CallbackMap.hpp" + SiliconTorch::CyanBus::CyanBus* cyanBus = NULL; @@ -29,8 +35,8 @@ SiliconTorch::FxCyanRGB8::FxCyanRGB8* cyanRGB = NULL; CyanLight::CyanLightControl* ctrl; -#include "driver/gpio.h" -#include <memory> + + void device_main() { qthing::Config cfg; @@ -40,6 +46,21 @@ void device_main() { + SpiderLib::CallbackMap<void(const int)> cbM; + + std::string magic0("a"); + cbM.add(magic0, [](const int x){ ESP_LOGE("_a_", "Got int[ %d ]", x); }); + + + + + + + + + + + cyanBus = new SiliconTorch::CyanBus::CyanBus(13, 14, 12, 15, 2000000); // Pinout of CyanStripe -- GitLab