add connect wifi mqtt auto.

This commit is contained in:
2024-06-05 22:59:04 +08:00
parent 7f3491094f
commit 8413ce1fa9
8 changed files with 193 additions and 21 deletions

View File

@ -10,7 +10,7 @@ bool LedController::initialize() {
ledc_timer_config_t ledc_timer;
memset(&ledc_timer, 0, sizeof(ledc_timer));
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
ledc_timer.duty_resolution = LEDC_TIMER_13_BIT;
ledc_timer.duty_resolution = TimerBit;
ledc_timer.timer_num = LEDC_TIMER_0;
ledc_timer.freq_hz = 4000;
ledc_timer.clk_cfg = LEDC_AUTO_CLK;
@ -41,18 +41,60 @@ bool LedController::initialize() {
std::cout << "ledc_timer_config() failed." << std::endl;
}
ledc_set_duty(m_channels[i].speed_mode, m_channels[i].channel, 4096);
ledc_set_duty(m_channels[i].speed_mode, m_channels[i].channel, 0);
ledc_update_duty(m_channels[i].speed_mode, m_channels[i].channel);
}
std::cout << "led controller initialize finished." << std::endl;
return true;
}
void LedController::setDuty(int32_t channel, int32_t duty) {
duty = static_cast<float>(0x1FFF * duty) / 100;
int32_t LedController::brightness() const {
return m_brightness;
}
void LedController::setBrightness(int32_t brightness) {
if (m_brightness != brightness) {
m_brightness = brightness;
update();
}
}
int32_t LedController::colorTemperature() const {
return m_colorTemp;
}
void LedController::setColorTemperature(int32_t temp) {
if (m_colorTemp != temp) {
m_colorTemp = temp;
update();
}
}
bool LedController::enabled() const {
return m_enabled;
}
void LedController::setEnabled(bool enabled) {
if (m_enabled != enabled) {
m_enabled = enabled;
update();
}
}
void LedController::update() {
if (!m_enabled) {
setDuty(Warm, 0);
setDuty(Cold, 0);
return;
}
auto warm = static_cast<float>(m_colorTemp - MinimumColorTemp) / (MaximumColorTemp - MinimumColorTemp);
auto wamDuty = m_brightness * warm;
setDuty(Warm, wamDuty);
setDuty(Cold, m_brightness - wamDuty);
}
void LedController::setDuty(Channel channel, int32_t duty) {
if ((channel < 0) || (channel >= sizeof(m_channels) / sizeof(m_channels[0]))) return;
std::cout<<"set channle "<<channel<<" duty: "<<duty<<std::endl;
std::cout << "set channle " << channel << " duty: " << duty << std::endl;
ledc_set_duty(m_channels[channel].speed_mode, m_channels[channel].channel, duty);
ledc_update_duty(m_channels[channel].speed_mode, m_channels[channel].channel);
}

View File

@ -5,12 +5,38 @@
class LedController {
public:
constexpr static ledc_timer_bit_t TimerBit = LEDC_TIMER_12_BIT;
constexpr static int32_t Resolution = 1 << LEDC_TIMER_12_BIT;
constexpr static int32_t MinimumColorTemp = 153;
constexpr static int32_t MaximumColorTemp = 500;
enum Channel {
Warm = 0,
Cold = 1,
};
static LedController *instance();
bool initialize();
void setDuty(int32_t channel, int32_t duty);
int32_t brightness() const;
void setBrightness(int32_t brightness);
int32_t colorTemperature() const;
void setColorTemperature(int32_t temp);
bool enabled() const;
void setEnabled(bool enabled);
void setDuty(Channel channel, int32_t duty);
protected:
LedController();
void update();
ledc_channel_config_t m_channels[2];
private:
bool m_enabled = false;
int32_t m_brightness = 0;
int32_t m_colorTemp = 0;
};
#endif // __LEDCONTROLLER_H__