Skip to content
Snippets Groups Projects
Commit 231d5617 authored by fxk8y's avatar fxk8y :spider:
Browse files

Making it a real lib

parent c6bfe82a
No related branches found
No related tags found
No related merge requests found
#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!
}
#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();
};
}
......@@ -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);
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment