diff --git a/CLC-qthing/PWMChannel.hpp b/CLC-qthing/PWMChannel.hpp deleted file mode 100644 index 61b57f13a8fb4f42ffba31edc1672a7f7fe05975..0000000000000000000000000000000000000000 --- a/CLC-qthing/PWMChannel.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include <qthing> - -#include "driver/ledc.h" - - -namespace CyanLight { - - class PWMChannel { - public: - PWMChannel(uint8_t channel, uint8_t gpio); - - uint32_t getPWM(); - void setPWM(uint32_t pwm); - - private: - uint32_t pwm = 0; - ledc_channel_t channel; - }; - -} diff --git a/CLC-qthing/Controller.cpp b/CLC-qthing/SiliconTorch/FxCyanF.cpp similarity index 79% rename from CLC-qthing/Controller.cpp rename to CLC-qthing/SiliconTorch/FxCyanF.cpp index 0d8b251b5e18831543eca5f7f23a2e16b8e1fd9c..43a523034c508aa50895d515400b7a2b1fb46a98 100644 --- a/CLC-qthing/Controller.cpp +++ b/CLC-qthing/SiliconTorch/FxCyanF.cpp @@ -1,4 +1,4 @@ -#include "CyanLightControl.hpp" +#include "FxCyanF.hpp" #include <qthing> #include <qthing/mqtt_common.hpp> @@ -17,7 +17,10 @@ using namespace qthing; -const char *TAG = "CLC"; +extern const char *TAG = "fxCyanF"; + +const uint8_t MAX_CHANNELS = 8; + const std::string delimiter = ":"; @@ -30,11 +33,11 @@ static float bytes2float(const char *bytes) { } -CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigured) : baseChannel{baseChannel} { +SiliconTorch::FxCyanF::FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured) : baseChannel{baseChannel} { this->channelsConfigured = std::min(channelsConfigured, CyanLight::MAX_CHANNELS); - this->channels = new CyanLight::PWMChannel*[this->channelsConfigured]; + this->channels = new SiliconTorch::Impl::PWMChannel*[this->channelsConfigured]; this->timer_cfg.duty_resolution = (ledc_timer_bit_t)this->resolution; this->timer_cfg.freq_hz = this->frequency; @@ -47,8 +50,13 @@ CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigur ledc_fade_func_install(0); + add_binary_message_callback(this->genDeviceTopic("pwm/$all"), [&](qthing::multipart_message_t message) { + if (message.offset == 0) this->handleUnicast(message.payload, message.length); + else ESP_LOGE(TAG, "Invalid message format: Fragmentation is unsupported"); + }); + for (uint8_t i = 0; i < this->channelsConfigured; i++) { - this->channels[i] = new CyanLight::PWMChannel(i, CyanLight::channelGPIOS[i]); + this->channels[i] = new SiliconTorch::Impl::PWMChannel(i, CyanLight::channelGPIOS[i]); char topic[32]; snprintf(topic, sizeof(topic), "pwm/$%i", i); @@ -87,7 +95,7 @@ CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigur std::string::size_type found = message.find(delimiter); if (found == std::string::npos) { - ESP_LOGW("CLC", "Invalid message format[ '%s' ]", message.c_str()); + ESP_LOGE(TAG, "Invalid message format[ '%s' ]", message.c_str()); return; } @@ -98,7 +106,7 @@ CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigur long int res = strtol(_res.c_str(), NULL, 0); if (frq == 0 || res == 0) { - ESP_LOGW("CLC", "Invalid message format[ '%s' ]", message.c_str()); + ESP_LOGE(TAG, "Invalid message format[ '%s' ]", message.c_str()); return; } @@ -155,7 +163,7 @@ CyanLight::Controller::Controller(uint16_t baseChannel, uint8_t channelsConfigur } -bool CyanLight::Controller::handleUnicast(const char *data, std::size_t length) { +bool SiliconTorch::FxCyanF::handleUnicast(const char *data, std::size_t length) { std::size_t size = this->getChannelCount() * sizeof(float); int32_t diff = length - size; @@ -174,7 +182,7 @@ bool CyanLight::Controller::handleUnicast(const char *data, std::size_t length) return true; } -bool CyanLight::Controller::handleBroadcast(const char *data, std::size_t length) { +bool SiliconTorch::FxCyanF::handleBroadcast(const char *data, std::size_t length) { std::size_t size = this->getChannelCount() * sizeof(float); std::size_t offset = this->getBaseChannel() * sizeof(float); @@ -195,7 +203,7 @@ bool CyanLight::Controller::handleBroadcast(const char *data, std::size_t length } -void CyanLight::Controller::setPWM(uint8_t channel, float value) { +void SiliconTorch::FxCyanF::setPWM(uint8_t channel, float value) { if (channel >= this->channelsConfigured) { ESP_LOGW(TAG, "ChannelID out of range: channel[ %i ]", channel); @@ -214,12 +222,12 @@ void CyanLight::Controller::setPWM(uint8_t channel, float value) { } -void CyanLight::Controller::setGammaCorrector(CyanLight::GammaCorrector gammaCorrector) { +void SiliconTorch::FxCyanF::setGammaCorrector(CyanLight::GammaCorrector gammaCorrector) { this->gammaCorrector = gammaCorrector; } -bool CyanLight::Controller::setFrqRes(uint32_t frq_hz, uint8_t res_bits) { +bool SiliconTorch::FxCyanF::setFrqRes(uint32_t frq_hz, uint8_t res_bits) { this->timer_cfg.duty_resolution = (ledc_timer_bit_t)res_bits; this->timer_cfg.freq_hz = frq_hz; @@ -240,78 +248,78 @@ bool CyanLight::Controller::setFrqRes(uint32_t frq_hz, uint8_t res_bits) { } -void CyanLight::Controller::setBaseChannel(uint16_t baseChannel) { +void SiliconTorch::FxCyanF::setBaseChannel(uint16_t baseChannel) { this->baseChannel = baseChannel; } -uint16_t CyanLight::Controller::getBaseChannel() { +uint16_t SiliconTorch::FxCyanF::getBaseChannel() { return this->baseChannel; } -uint8_t CyanLight::Controller::getChannelCount() { +uint8_t SiliconTorch::FxCyanF::getChannelCount() { return this->channelsConfigured; } -bool CyanLight::Controller::setFrequency(uint32_t frq_hz) { +bool SiliconTorch::FxCyanF::setFrequency(uint32_t frq_hz) { return this->setFrqRes(frq_hz, this->resolution); } -uint32_t CyanLight::Controller::getFrequency() { +uint32_t SiliconTorch::FxCyanF::getFrequency() { return this->frequency; } -bool CyanLight::Controller::setResolution(uint8_t res_bits) { +bool SiliconTorch::FxCyanF::setResolution(uint8_t res_bits) { return this->setFrqRes(this->frequency, res_bits); } -uint8_t CyanLight::Controller::getResolution() { +uint8_t SiliconTorch::FxCyanF::getResolution() { return this->resolution; } -void CyanLight::Controller::setPacketHandledCallback(CyanLight::PacketHandledCallback callback) { +void SiliconTorch::FxCyanF::setPacketHandledCallback(CyanLight::PacketHandledCallback callback) { this->packetCallback = callback; } -std::string CyanLight::Controller::genDeviceTopic(const char *suffix) { +std::string SiliconTorch::FxCyanF::genDeviceTopic(const char *suffix) { return std::string(DEVICE_NAMESPACE + "fxCyanF/") + std::string(suffix); } -std::string CyanLight::Controller::genServiceTopic(const char *suffix) { +std::string SiliconTorch::FxCyanF::genServiceTopic(const char *suffix) { return std::string("service/fxCyanF/") + std::string(suffix); } -void CyanLight::Controller::callPacketCallback() { +void SiliconTorch::FxCyanF::callPacketCallback() { this->packetCallback(); } -void CyanLight::Controller::publishBaseChannel() { +void SiliconTorch::FxCyanF::publishBaseChannel() { char tmp[16]; snprintf(tmp, sizeof(tmp), "%i", this->getBaseChannel()); publish_message(DEVICE_NAMESPACE + "CyanLight/channel", tmp); } -void CyanLight::Controller::publishChannelCount() { +void SiliconTorch::FxCyanF::publishChannelCount() { char tmp[16]; snprintf(tmp, sizeof(tmp), "%i", this->getChannelCount()); publish_message(DEVICE_NAMESPACE + "CyanLight/channelCnt", tmp); } -void CyanLight::Controller::publishFrequency() { +void SiliconTorch::FxCyanF::publishFrequency() { char tmp[16]; snprintf(tmp, sizeof(tmp), "%i", this->getFrequency()); publish_message(DEVICE_NAMESPACE + "CyanLight/frequency", tmp); } -void CyanLight::Controller::publishResolution() { +void SiliconTorch::FxCyanF::publishResolution() { char tmp[16]; snprintf(tmp, sizeof(tmp), "%i", this->getResolution()); publish_message(DEVICE_NAMESPACE + "CyanLight/resolution", tmp); } -void CyanLight::Controller::publishFrqRes() { +void SiliconTorch::FxCyanF::publishFrqRes() { char tmp[32]; snprintf(tmp, sizeof(tmp), "%i%s%i", this->getFrequency(), delimiter.c_str(), this->getResolution()); publish_message(DEVICE_NAMESPACE + "CyanLight/frqres", tmp); diff --git a/CLC-qthing/CyanLightControl.hpp b/CLC-qthing/SiliconTorch/FxCyanF.hpp similarity index 87% rename from CLC-qthing/CyanLightControl.hpp rename to CLC-qthing/SiliconTorch/FxCyanF.hpp index 650b598a777c0dd58529fb904c8d537dcf1a8502..a0fa5899394072d2359a4fbf4daa317abbca2b23 100644 --- a/CLC-qthing/CyanLightControl.hpp +++ b/CLC-qthing/SiliconTorch/FxCyanF.hpp @@ -1,5 +1,7 @@ #pragma once +#include <string> +#include <functional> #include <cinttypes> #include "driver/ledc.h" @@ -9,18 +11,19 @@ -namespace CyanLight { +namespace SiliconTorch { + + extern const char *TAG; - const uint8_t MAX_CHANNELS = 6; const uint8_t channelGPIOS[MAX_CHANNELS] = { 27, 16, 17, 18, 19, 21 }; typedef std::function<void()> PacketHandledCallback; typedef std::function<float(float)> GammaCorrector; - class Controller { + class FxCyanF { public: - Controller(uint16_t baseChannel, uint8_t channelsConfigured = 1); + FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured = 1); void setPWM(uint8_t channel, float value); @@ -59,7 +62,7 @@ namespace CyanLight { uint16_t baseChannel; uint8_t channelsConfigured; - PWMChannel **channels; + Impl::PWMChannel **channels; ledc_timer_config_t timer_cfg; diff --git a/CLC-qthing/PWMChannel.cpp b/CLC-qthing/SiliconTorch/PWMChannel.cpp similarity index 80% rename from CLC-qthing/PWMChannel.cpp rename to CLC-qthing/SiliconTorch/PWMChannel.cpp index c50a5042effeee27d184327143369a55d19f2d88..d5b16c68638f49791676c1bad1a91097957f2d14 100644 --- a/CLC-qthing/PWMChannel.cpp +++ b/CLC-qthing/SiliconTorch/PWMChannel.cpp @@ -5,7 +5,7 @@ -CyanLight::PWMChannel::PWMChannel(uint8_t channel, uint8_t gpio) : channel{(ledc_channel_t)channel} { +SiliconTorch::Impl::PWMChannel::PWMChannel(uint8_t channel, uint8_t gpio) : channel{(ledc_channel_t)channel} { gpio_config_t conf; @@ -34,13 +34,13 @@ CyanLight::PWMChannel::PWMChannel(uint8_t channel, uint8_t gpio) : channel{(ledc setPWM(0); } -void CyanLight::PWMChannel::setPWM(uint32_t pwm) { +void SiliconTorch::Impl::PWMChannel::setPWM(uint32_t pwm) { ledc_set_duty(LEDC_HIGH_SPEED_MODE, this->channel, pwm); ledc_update_duty(LEDC_HIGH_SPEED_MODE, this->channel); this->pwm = pwm; } -uint32_t CyanLight::PWMChannel::getPWM() { +uint32_t SiliconTorch::Impl::PWMChannel::getPWM() { return this->pwm; } diff --git a/CLC-qthing/SiliconTorch/PWMChannel.hpp b/CLC-qthing/SiliconTorch/PWMChannel.hpp new file mode 100644 index 0000000000000000000000000000000000000000..cde5785a3b06aa40c7b552978aea71e6fc78ba6d --- /dev/null +++ b/CLC-qthing/SiliconTorch/PWMChannel.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <qthing> + +#include "driver/ledc.h" + + +namespace SiliconTorch { + + namespace Impl { + + class PWMChannel { + public: + PWMChannel(uint8_t channel, uint8_t gpio); + + uint32_t getPWM(); + void setPWM(uint32_t pwm); + + private: + uint32_t pwm = 0; + ledc_channel_t channel; + }; + + } +}