diff --git a/CLC-qthing/SpiderLib/CallbackMap.hpp b/CLC-qthing/SpiderLib/CallbackMap.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ddcb538982aa2b4ab73eb8ca624f205353a9ff71 --- /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 7505081a8d79fac31e1ee99b1c88a91c92d5595f..bdfacc15156e536225ff1f8755b4a119ac1ebcfc 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