From 231d561769180fe00f01d0ad6a9df921b61edd49 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Thu, 2 Dec 2021 08:26:53 +0100 Subject: [PATCH] Making it a real lib --- CLC-qthing/CyanLight.cpp | 25 +++++++++++++++ CLC-qthing/CyanLight.hpp | 20 ++++++++++++ CLC-qthing/SiliconTorch/FxCyanF.cpp | 43 ++++++++++++++++---------- CLC-qthing/SiliconTorch/FxCyanF.hpp | 10 +++--- CLC-qthing/SiliconTorch/PWMChannel.cpp | 2 +- CLC-qthing/device_main.cpp | 4 +-- 6 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 CLC-qthing/CyanLight.cpp create mode 100644 CLC-qthing/CyanLight.hpp diff --git a/CLC-qthing/CyanLight.cpp b/CLC-qthing/CyanLight.cpp new file mode 100644 index 0000000..410da48 --- /dev/null +++ b/CLC-qthing/CyanLight.cpp @@ -0,0 +1,25 @@ +#include "CyanLight.hpp" + +#include <cstdlib> // TODO: was this for std::min() ? + + +uint8_t MAX_CHANNELS = 8; + +extern const uint8_t CyanLight::channelGPIOs[] = { 42 }; // !!! + + +CyanLight::CyanLightControl::CyanLightControl(uint8_t channelsConfigured) { + + if (channelsConfigured > MAX_CHANNELS) + ESP_LOGE("", "Cannot create channel#[ %i ]! Hardware limit is #[ %i ]", channelsConfigured, MAX_CHANNELS); + + uint8_t channels = std::min(channelsConfigured, MAX_CHANNELS); + + for (uint8_t i = 0; i < channels; i++) + this->addChannel(CyanLight::channelGPIOs[i]); +} + + +bool CyanLight::CyanLightControl::addChannel() { + // TODO: implement! +} diff --git a/CLC-qthing/CyanLight.hpp b/CLC-qthing/CyanLight.hpp new file mode 100644 index 0000000..de3691c --- /dev/null +++ b/CLC-qthing/CyanLight.hpp @@ -0,0 +1,20 @@ +#pragma once + +// TODO: should we include <cinttypes> here or accepts its ingress via FxCyanF.hpp ??? + +#include "SiliconTorch/FxCyanF.hpp" + + +namespace CyanLight { + + extern const uint8_t channelGPIOs[]; + + class CyanLightControl : SiliconTorch::FxCyanF { + public: + CyanLightControl(uint8_t channelsConfigured); + + bool addChannel(); + + }; + +} diff --git a/CLC-qthing/SiliconTorch/FxCyanF.cpp b/CLC-qthing/SiliconTorch/FxCyanF.cpp index 43a5230..0cf956c 100644 --- a/CLC-qthing/SiliconTorch/FxCyanF.cpp +++ b/CLC-qthing/SiliconTorch/FxCyanF.cpp @@ -33,11 +33,10 @@ static float bytes2float(const char *bytes) { } -SiliconTorch::FxCyanF::FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured) : baseChannel{baseChannel} { +SiliconTorch::FxCyanF::FxCyanF() { - this->channelsConfigured = std::min(channelsConfigured, CyanLight::MAX_CHANNELS); - - this->channels = new SiliconTorch::Impl::PWMChannel*[this->channelsConfigured]; + // TODO: make vector! + this->channels = new SiliconTorch::Impl::PWMChannel*[MAX_CHANNELS]; this->timer_cfg.duty_resolution = (ledc_timer_bit_t)this->resolution; this->timer_cfg.freq_hz = this->frequency; @@ -55,18 +54,6 @@ SiliconTorch::FxCyanF::FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured) else ESP_LOGE(TAG, "Invalid message format: Fragmentation is unsupported"); }); - for (uint8_t i = 0; i < this->channelsConfigured; i++) { - this->channels[i] = new SiliconTorch::Impl::PWMChannel(i, CyanLight::channelGPIOS[i]); - - char topic[32]; - snprintf(topic, sizeof(topic), "pwm/$%i", i); - - add_message_callback(this->genDeviceTopic(topic), [&, i](std::string message) { - this->setPWM(i, strtof(message.c_str(), NULL)); - this->callPacketCallback(); - }); - } - std::string header("fxCyanF"); uint8_t headerLength = header.length(); @@ -163,6 +150,30 @@ SiliconTorch::FxCyanF::FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured) } +bool SiliconTorch::FxCyanF::addChannel(uint8_t gpio) { + if (this->channelsConfigured >= MAX_CHANNELS) { + ESP_LOGE(TAG, "Maximum number of PWMChannels reached"); + return false; + } + + uint8_t channel = this->channelsConfigured; + + // TODO: catch channel creation errors from IDF (e.g. on input-only GPIO) + this->channels[channel] = new SiliconTorch::Impl::PWMChannel(channel, gpio); + + char topic[32]; + snprintf(topic, sizeof(topic), "pwm/$%i", channel); + + add_message_callback(this->genDeviceTopic(topic), [&, channel](std::string message) { + this->setPWM(channel, strtof(message.c_str(), NULL)); + this->callPacketCallback(); + }); + + this->channelsConfigured++; + return true; +} + + bool SiliconTorch::FxCyanF::handleUnicast(const char *data, std::size_t length) { std::size_t size = this->getChannelCount() * sizeof(float); diff --git a/CLC-qthing/SiliconTorch/FxCyanF.hpp b/CLC-qthing/SiliconTorch/FxCyanF.hpp index a0fa589..125fb28 100644 --- a/CLC-qthing/SiliconTorch/FxCyanF.hpp +++ b/CLC-qthing/SiliconTorch/FxCyanF.hpp @@ -15,15 +15,14 @@ namespace SiliconTorch { extern const char *TAG; - - const uint8_t channelGPIOS[MAX_CHANNELS] = { 27, 16, 17, 18, 19, 21 }; - typedef std::function<void()> PacketHandledCallback; typedef std::function<float(float)> GammaCorrector; class FxCyanF { public: - FxCyanF(uint16_t baseChannel, uint8_t channelsConfigured = 1); + FxCyanF(uint16_t baseChannel = 0); + + bool addChannel(uint8_t gpio); void setPWM(uint8_t channel, float value); @@ -61,7 +60,8 @@ namespace SiliconTorch { uint8_t resolution = 10; // bits uint16_t baseChannel; - uint8_t channelsConfigured; + uint8_t channelsConfigured = 0; + Impl::PWMChannel **channels; ledc_timer_config_t timer_cfg; diff --git a/CLC-qthing/SiliconTorch/PWMChannel.cpp b/CLC-qthing/SiliconTorch/PWMChannel.cpp index d5b16c6..411e55e 100644 --- a/CLC-qthing/SiliconTorch/PWMChannel.cpp +++ b/CLC-qthing/SiliconTorch/PWMChannel.cpp @@ -31,7 +31,7 @@ SiliconTorch::Impl::PWMChannel::PWMChannel(uint8_t channel, uint8_t gpio) : chan ledc_channel_config(&ledc_channel); - setPWM(0); + this->setPWM(0); } void SiliconTorch::Impl::PWMChannel::setPWM(uint32_t pwm) { diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index 0468187..fc83f73 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -7,11 +7,11 @@ #include "freertos/task.h" -#include "CyanLightControl.hpp" +#include "CyanLight.hpp" -CyanLight::Controller ctrl(0, 1); +CyanLight::CyanLightControl ctrl(1); void device_main() { -- GitLab