Skip to content

Commit

Permalink
Implemented led type CCT
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 26, 2024
1 parent b6c7e7d commit 46ff570
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 102 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ portability-simd-intrinsics,
readability-avoid-const-params-in-decls,
readability-const-return-type,
readability-container-size-empty,
readability-convert-member-functions-to-static,
readability-delete-null-pointer,
readability-deleted-default,
readability-inconsistent-declaration-parameter-name,
Expand Down
23 changes: 19 additions & 4 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "application.h"

#include <utils/color.h>

void Application::begin() {
D_PRINT("Starting application...");

Expand All @@ -23,9 +25,10 @@ void Application::begin() {
.mqtt_password = sys_config.mqtt_password,
});

if (sys_config.rgb_mode) {
_led = std::make_unique<LedController>(
sys_config.led_r_pin, sys_config.led_g_pin, sys_config.led_b_pin);
if (sys_config.led_type == LedType::RGB) {
_led = std::make_unique<LedController>(sys_config.led_r_pin, sys_config.led_g_pin, sys_config.led_b_pin);
} else if (sys_config.led_type == LedType::CCT) {
_led = std::make_unique<LedController>(sys_config.led_r_pin, sys_config.led_g_pin);
} else {
_led = std::make_unique<LedController>(sys_config.led_r_pin);
}
Expand Down Expand Up @@ -91,6 +94,16 @@ void Application::_handle_property_change(const AbstractParameter *parameter) {
auto type = it->second;
if (type == PacketType::POWER) {
set_power(config().power);
} else if (type == PacketType::TEMPERATURE) {
if (_led->led_type() == LedType::RGB) {
uint32_t kelvin = sys_config().led_min_temperature + config().color_temperature *
(sys_config().led_max_temperature - sys_config().led_min_temperature) / LED_TEMPERATURE_MAX_VALUE;

config().color = temperature_to_rgb(kelvin);
NotificationBus::get().notify_parameter_changed(this, _metadata->color.get_parameter());
}

update();
} else if (type >= PacketType::NIGHT_MODE_ENABLED && type <= PacketType::NIGHT_MODE_BRIGHTNESS) {
_night_mode_manager->reset();
update();
Expand All @@ -102,9 +115,11 @@ void Application::_handle_property_change(const AbstractParameter *parameter) {
void Application::load() {
_led->set_brightness(config().power ? config().brightness : PIN_DISABLED);

if (_led->rgb_mode()) {
if (_led->led_type() == LedType::RGB) {
_led->set_calibration(config().calibration);
_led->set_color(config().color);
} else if (_led->led_type() == LedType::CCT) {
_led->set_temperature(config().color_temperature);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/app/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ MAKE_ENUM_AUTO(AppState, uint8_t,
TURNING_OFF
);

enum class LedType: uint8_t {
SINGLE = 0,
RGB = 1,
CCT = 2,
};

typedef char ConfigString[CONFIG_STRING_SIZE];

struct __attribute ((packed)) SysConfig {
Expand All @@ -27,12 +33,15 @@ struct __attribute ((packed)) SysConfig {
uint32_t wifi_connection_check_interval = WIFI_CONNECTION_CHECK_INTERVAL;
uint32_t wifi_max_connection_attempt_interval = WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL;

bool rgb_mode = RGB_MODE_ENABLED;
LedType led_type = LED_MODE;
uint8_t led_r_pin = LED_R_PIN;
uint8_t led_g_pin = LED_G_PIN;
uint8_t led_b_pin = LED_B_PIN;

uint16_t led_min_brightness = LED_MIN_BRIGHTNESS;
uint16_t led_min_temperature = LED_MIN_TEMPERATURE;
uint16_t led_max_temperature = LED_MAX_TEMPERATURE;

uint32_t power_change_timeout = POWER_CHANGE_TIMEOUT;
uint32_t wifi_connect_flash_timeout = WIFI_CONNECT_FLASH_TIMEOUT;

Expand Down Expand Up @@ -66,6 +75,8 @@ struct __attribute ((packed)) Config {
uint32_t color = ~0u; // All colors
uint32_t calibration = ~0u; // No calibration

uint16_t color_temperature = PWM_MAX_VALUE;

NightModeConfig night_mode{};

SysConfig sys_config{};
Expand Down
57 changes: 22 additions & 35 deletions src/app/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,9 @@
#include <lib/utils/metadata.h>

#include "app/config.h"
#include "app/parameters.h"
#include "network/cmd.h"
#include "utils/math.h"

class BrightnessParameter : public Parameter<uint16_t> {
const SysConfig &_config;

public:
BrightnessParameter(uint16_t *value, const SysConfig &config) : Parameter(value), _config(config) {}

bool parse(const String &data) override {
if (_config.mqtt_convert_brightness) {
if (data.length() == 0) return false;

uint16_t value = map16(data.toInt(), 100, PWM_MAX_VALUE);
Parameter::set_value(&value, sizeof(value));
return true;
}

return Parameter::parse(data);
}

[[nodiscard]] String to_string() const override {
if (_config.mqtt_convert_brightness) {
uint16_t value;
memcpy(&value, Parameter::get_value(), sizeof(value));
auto converted = map16(value, PWM_MAX_VALUE, 100);
return String(converted);
}

return Parameter::to_string();
}
};

DECLARE_META_TYPE(AppMetaProperty, PacketType)

Expand All @@ -54,11 +25,13 @@ DECLARE_META(SysConfigMeta, AppMetaProperty,
MEMBER(FixedString, wifi_password),
MEMBER(Parameter<uint32_t>, wifi_connection_check_interval),
MEMBER(Parameter<uint32_t>, wifi_max_connection_attempt_interval),
MEMBER(Parameter<bool>, rgb_mode),
MEMBER(Parameter<uint8_t>, led_type),
MEMBER(Parameter<uint8_t>, led_r_pin),
MEMBER(Parameter<uint8_t>, led_g_pin),
MEMBER(Parameter<uint8_t>, led_b_pin),
MEMBER(Parameter<uint16_t>, led_min_brightness),
MEMBER(Parameter<uint16_t>, led_min_temperature),
MEMBER(Parameter<uint16_t>, led_max_temperature),
MEMBER(Parameter<uint32_t>, power_change_timeout),
MEMBER(Parameter<uint32_t>, wifi_connect_flash_timeout),
MEMBER(Parameter<float>, time_zone),
Expand All @@ -82,6 +55,7 @@ DECLARE_META(ConfigMetadata, AppMetaProperty,
MEMBER(BrightnessParameter, brightness),
MEMBER(Parameter<uint32_t>, color),
MEMBER(Parameter<uint32_t>, calibration),
MEMBER(TemperatureParameter, color_temperature),
SUB_TYPE(NightModeConfigMeta, night_mode),
SUB_TYPE(SysConfigMeta, sys_config),

Expand All @@ -98,7 +72,7 @@ inline ConfigMetadata build_metadata(Config &config) {
.brightness = {
PacketType::BRIGHTNESS,
MQTT_TOPIC_BRIGHTNESS, MQTT_OUT_TOPIC_BRIGHTNESS,
BrightnessParameter(&config.brightness, config.sys_config)
{&config.brightness, config.sys_config}
},
.color = {
PacketType::COLOR,
Expand All @@ -109,6 +83,11 @@ inline ConfigMetadata build_metadata(Config &config) {
PacketType::CALIBRATION,
&config.calibration
},
.color_temperature = {
PacketType::TEMPERATURE,
MQTT_TOPIC_TEMPERATURE,MQTT_OUT_TOPIC_TEMPERATURE,
{&config.color_temperature, config.sys_config}
},
.night_mode = {
.enabled = {
PacketType::NIGHT_MODE_ENABLED,
Expand Down Expand Up @@ -157,9 +136,9 @@ inline ConfigMetadata build_metadata(Config &config) {
PacketType::SYS_CONFIG_WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL,
&config.sys_config.wifi_max_connection_attempt_interval
},
.rgb_mode = {
PacketType::SYS_RGB_MODE,
&config.sys_config.rgb_mode
.led_type = {
PacketType::SYS_LED_TYPE,
(uint8_t *) &config.sys_config.led_type
},
.led_r_pin = {
PacketType::SYS_CONFIG_LED_R_PIN,
Expand All @@ -177,6 +156,14 @@ inline ConfigMetadata build_metadata(Config &config) {
PacketType::SYS_CONFIG_LED_MIN_BRIGHTNESS,
&config.sys_config.led_min_brightness
},
.led_min_temperature = {
PacketType::SYS_CONFIG_LED_MIN_TEMPERATURE,
&config.sys_config.led_min_temperature
},
.led_max_temperature = {
PacketType::SYS_CONFIG_LED_MAX_TEMPERATURE,
&config.sys_config.led_max_temperature
},
.power_change_timeout = {
PacketType::SYS_CONFIG_POWER_CHANGE_TIMEOUT,
&config.sys_config.power_change_timeout
Expand Down
60 changes: 60 additions & 0 deletions src/app/parameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <lib/base/metadata.h>

#include "app/config.h"
#include "utils/math.h"

class BrightnessParameter : public Parameter<uint16_t> {
const SysConfig &_config;

public:
BrightnessParameter(uint16_t *value, const SysConfig &config) : Parameter(value), _config(config) {}

bool parse(const String &data) override {
if (_config.mqtt_convert_brightness) {
if (data.length() == 0) return false;

uint16_t value = map16(data.toInt(), 100, PWM_MAX_VALUE);
Parameter::set_value(&value, sizeof(value));
return true;
}

return Parameter::parse(data);
}

[[nodiscard]] String to_string() const override {
if (_config.mqtt_convert_brightness) {
uint16_t value;
memcpy(&value, Parameter::get_value(), sizeof(value));
auto converted = map16(value, PWM_MAX_VALUE, 100);
return String(converted);
}

return Parameter::to_string();
}
};

class TemperatureParameter : public Parameter<uint16_t> {
const SysConfig &_config;

public:
TemperatureParameter(uint16_t *value, const SysConfig &config) : Parameter(value), _config(config) {}

bool parse(const String &data) override {
if (data.length() == 0) return false;

auto parsed = std::max<uint16_t>(data.toInt() - _config.led_min_temperature, 0);
uint16_t value = map16(parsed, _config.led_max_temperature - _config.led_min_temperature, LED_TEMPERATURE_MAX_VALUE);
Parameter::set_value(&value, sizeof(value));
return true;
}

[[nodiscard]] String to_string() const override {
uint16_t value;
memcpy(&value, Parameter::get_value(), sizeof(value));
auto converted = _config.led_min_temperature + map16(value, LED_TEMPERATURE_MAX_VALUE,
_config.led_max_temperature - _config.led_min_temperature);
return String(converted);
}
};
2 changes: 1 addition & 1 deletion src/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
#define MQTT_HOST "example.com"
#define MQTT_PORT (1234u)
#define MQTT_USER "esp_user"
#define MQTT_PASSWORD "esp_pass"
#define MQTT_PASSWORD "esp_pass"
Loading

0 comments on commit 46ff570

Please sign in to comment.