Skip to content

Commit

Permalink
Allow to set Button type (high/low)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Sep 28, 2024
1 parent f0874bd commit e3c0074
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void Application::begin() {
_api->begin(_bootstrap->web_server());

if (sys_config.button_enabled) {
_btn = std::make_unique<Button>(sys_config.button_pin);
_btn = std::make_unique<Button>(sys_config.button_pin, sys_config.button_high_state);

_btn->set_on_click([this](auto cnt) {
if (cnt == 1) {
Expand Down Expand Up @@ -181,18 +181,18 @@ void Application::brightness_increase() {

config().brightness = std::min<uint16_t>(PWM_MAX_VALUE,
config().brightness + max<uint16_t>(1, config().brightness / BRIGHTNESS_CHANGE_DIVIDER));
_bootstrap->save_changes();

D_PRINTF("Increase brightness: %u\r\n", config().brightness);
update();
}

void Application::brightness_decrease() {
if (config().brightness == 0) return;

config().brightness = std::max(0, config().brightness - std::max<uint16_t>(1, config().brightness / BRIGHTNESS_CHANGE_DIVIDER));
_bootstrap->save_changes();

D_PRINTF("Decrease brightness: %u\r\n", config().brightness);
update();
}

void Application::trigger_temperature() {
Expand All @@ -205,6 +205,8 @@ void Application::trigger_temperature() {
config().color_temperature = std::min<uint16_t>(current_step * value_step, LED_TEMPERATURE_MAX_VALUE);

D_PRINTF("Change temperature: %u\r\n", config().color_temperature);

update();
NotificationBus::get().notify_parameter_changed(this, _metadata->color_temperature.get_parameter());
}

Expand Down
1 change: 1 addition & 0 deletions src/app/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct __attribute ((packed)) SysConfig {

bool button_enabled = BUTTON_ENABLED;
uint8_t button_pin = BUTTON_PIN;
bool button_high_state = BUTTON_HIGH_STATE;

uint32_t power_change_timeout = POWER_CHANGE_TIMEOUT;
uint32_t wifi_connect_flash_timeout = WIFI_CONNECT_FLASH_TIMEOUT;
Expand Down
15 changes: 15 additions & 0 deletions src/app/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ DECLARE_META(SysConfigMeta, AppMetaProperty,
MEMBER(Parameter<uint16_t>, led_min_brightness),
MEMBER(Parameter<uint16_t>, led_min_temperature),
MEMBER(Parameter<uint16_t>, led_max_temperature),
MEMBER(Parameter<bool>, button_enabled),
MEMBER(Parameter<uint8_t>, button_pin),
MEMBER(Parameter<bool>, button_high_state),
MEMBER(Parameter<uint32_t>, power_change_timeout),
MEMBER(Parameter<uint32_t>, wifi_connect_flash_timeout),
MEMBER(Parameter<float>, time_zone),
Expand Down Expand Up @@ -164,6 +167,18 @@ inline ConfigMetadata build_metadata(Config &config) {
PacketType::SYS_CONFIG_LED_MAX_TEMPERATURE,
&config.sys_config.led_max_temperature
},
.button_enabled = {
PacketType::SYS_CONFIG_BUTTON_ENABLED,
&config.sys_config.button_enabled
},
.button_pin = {
PacketType::SYS_CONFIG_BUTTON_PIN,
&config.sys_config.button_pin
},
.button_high_state = {
PacketType::SYS_CONFIG_BUTTON_HIGH_STATE,
&config.sys_config.button_high_state
},
.power_change_timeout = {
PacketType::SYS_CONFIG_POWER_CHANGE_TIMEOUT,
&config.sys_config.power_change_timeout
Expand Down
1 change: 1 addition & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define LED_MIN_BRIGHTNESS (1u)

#define BUTTON_ENABLED (false)
#define BUTTON_HIGH_STATE (true)
#if ARDUINO_ARCH_ESP32
#define BUTTON_PIN (3u)
#else
Expand Down
2 changes: 1 addition & 1 deletion src/lib
Submodule lib updated from 8b5928 to e8118d
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <LittleFS.h>

#include "app/application.h"

Application ApplicationInstance;
Expand Down
5 changes: 3 additions & 2 deletions src/network/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ MAKE_ENUM(PacketType, uint8_t,
SYS_CONFIG_LED_MIN_TEMPERATURE, 0x77,
SYS_CONFIG_LED_MAX_TEMPERATURE, 0x78,

MOTION_CONFIG_BUTTON_ENABLED, 0x80,
MOTION_CONFIG_BUTTON_PIN, 0x81,
SYS_CONFIG_BUTTON_ENABLED, 0x80,
SYS_CONFIG_BUTTON_PIN, 0x81,
SYS_CONFIG_BUTTON_HIGH_STATE, 0x82,

GET_CONFIG, 0xa0,
RESTART, 0xb0,
Expand Down
2 changes: 1 addition & 1 deletion src/sys_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
#define LED_MIN_TEMPERATURE (2700u)
#define LED_MAX_TEMPERATURE (6000u)

#define BRIGHTNESS_CHANGE_DIVIDER (14u)
#define BRIGHTNESS_CHANGE_DIVIDER (20u)
#define TEMPERATURE_CHANGE_STEPS (4u)
5 changes: 3 additions & 2 deletions www/src/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export const PacketType = {
SYS_CONFIG_LED_MIN_TEMPERATURE: 0x77,
SYS_CONFIG_LED_MAX_TEMPERATURE: 0x78,

MOTION_CONFIG_BUTTON_ENABLED: 0x80,
MOTION_CONFIG_BUTTON_PIN: 0x81,
SYS_CONFIG_BUTTON_ENABLED: 0x80,
SYS_CONFIG_BUTTON_PIN: 0x81,
SYS_CONFIG_BUTTON_HIGH_STATE: 0x82,

GET_CONFIG: 0xa0,

Expand Down
1 change: 1 addition & 0 deletions www/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class Config extends AppConfigBase {

button_enabled: parser.readBoolean(),
button_pin: parser.readUint8(),
button_high_state: parser.readBoolean(),

powerChangeTimeout: parser.readUint32(),
wifiConnectFlashTimeout: parser.readUint32(),
Expand Down
5 changes: 3 additions & 2 deletions www/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ export const PropertyConfig = [{
{key: "sysConfig.wifiMaxConnectionAttemptInterval", title: "Max Connection Attempt Interval", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL},

{type: "title", label: "Button"},
{key: "sysConfig.button_enabled", title: "Enabled", type: "trigger", kind: "Boolean", cmd: PacketType.MOTION_CONFIG_BUTTON_ENABLED},
{key: "sysConfig.button_pin", title: "Pin", type: "int", kind: "Uint8", cmd: PacketType.MOTION_CONFIG_BUTTON_PIN, min: 0, max: 32},
{key: "sysConfig.button_enabled", title: "Enabled", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_ENABLED},
{key: "sysConfig.button_pin", title: "Pin", type: "int", kind: "Uint8", cmd: PacketType.SYS_CONFIG_BUTTON_PIN, min: 0, max: 32},
{key: "sysConfig.button_high_state", title: "High State", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_HIGH_STATE},

{type: "title", label: "LED"},
{key: "ledType", title: "LED Type", type: "select", kind: "Uint8", cmd: PacketType.SYS_LED_TYPE, list: "ledType"},
Expand Down

0 comments on commit e3c0074

Please sign in to comment.