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

Beginning the real controller dev

parent d9f8cb1a
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <cinttypes>
namespace CyanLight {
const uint8_t MAX_CHANNELS = 6;
class Controller {
public:
Controller();
private:
PWMChannel channels[MAX_CHANNELS];
};
}
#include "FastDimmer.hpp"
#include "driver/gpio.h"
#include "driver/ledc.h"
static uint8_t _channel_cnt = 0;
uint8_t getNextPWMChannel() {
return _channel_cnt++;
}
static bool timerConfigured = false;
static void configureTimer() {
if (!timerConfigured) {
ledc_timer_config_t ledc_timer;
ledc_timer.duty_resolution = LEDC_TIMER_10_BIT, // resolution of PWM duty
ledc_timer.freq_hz = 1000, // frequency of PWM signal; 2KHz is max for CN5711
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE, // timer mode
ledc_timer.timer_num = LEDC_TIMER_0, // timer index
// Required for newer esp-idf:
//ledc_timer.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
ledc_timer_config(&ledc_timer);
ledc_fade_func_install(0);
timerConfigured = true;
}
}
PWMChannel::PWMChannel(uint8_t gpio) {
configureTimer();
gpio_config_t conf;
conf.intr_type = GPIO_INTR_DISABLE;
conf.mode = GPIO_MODE_OUTPUT;
conf.pin_bit_mask = 1ULL << gpio;
conf.pull_up_en = GPIO_PULLUP_DISABLE;
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config(&conf);
gpio_set_level((gpio_num_t)gpio, 0);
ledc_channel_config_t ledc_channel;
ledc_channel.channel = this->channel;
ledc_channel.duty = 0;
ledc_channel.gpio_num = (gpio_num_t)gpio;
ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
ledc_channel.hpoint = 0;
ledc_channel.timer_sel = LEDC_TIMER_0;
ledc_channel_config(&ledc_channel);
setBrightness(0);
}
const float maxPWM = 1024.0f;
void PWMChannel::setBrightness(float b) {
uint16_t pwm = (uint16_t)( b*b * maxPWM );
ledc_set_duty(LEDC_HIGH_SPEED_MODE, this->channel, pwm);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, this->channel);
}
#pragma once
#include <qthing.h>
#include "driver/ledc.h"
ledc_channel_t getNextLEDChannel();
namespace CyanLight {
class PWMChannel {
public:
PWMChannel(uint8_t gpio);
float getBrightness();
void setBrightness(float b);
private:
ledc_channel_t channel = getNextLEDChannel();
};
}
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