Add switch device.

This commit is contained in:
2025-05-27 11:44:44 +03:00
parent ebaebef24c
commit 45d644c54c
3 changed files with 102 additions and 14 deletions

View File

@@ -13,9 +13,12 @@
#include <app_reset.h>
#include <ws2812_led.h>
#include "esp_attr.h"
#include "esp_err.h"
#include "esp_rmaker_core.h"
#include "esp_rmaker_standard_types.h"
#include "esp_rmaker_standard_params.h"
#include "iot_button.h"
#include "app_priv.h"
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -31,18 +34,25 @@
#define WIFI_RESET_BUTTON_TIMEOUT 3
#define FACTORY_RESET_BUTTON_TIMEOUT 10
// These values correspond to H,S,V = 120,100,10
#define RGB_DEFAULT_RED 0
#define RGB_DEFAULT_GREEN 25
#define RGB_DEFAULT_BLUE 0
static uint16_t rgb_hue = RGB_HUE_DEFAULT;
static uint16_t rgb_saturation = RGB_SATURATION_DEFAULT;
static uint16_t rgb_brightness = RGB_BRIGHTNESS_DEFAULT;
static bool rgb_power = RGB_POWER_DEFAULT;
static bool switch_state = false;
esp_err_t app_driver_set_gpio(const char *name, bool state)
{
if (strcmp(name, "Red") == 0) {
if (strcmp(name, GPIO_DEVICE_OUT1_NAME) == 0) {
gpio_set_level(OUTPUT_GPIO_RED, state);
} else if (strcmp(name, "Green") == 0) {
} else if (strcmp(name, GPIO_DEVICE_OUT2_NAME) == 0) {
gpio_set_level(OUTPUT_GPIO_GREEN, state);
} else if (strcmp(name, "Blue") == 0) {
} else if (strcmp(name, GPIO_DEVICE_OUT3_NAME) == 0) {
gpio_set_level(OUTPUT_GPIO_BLUE, state);
} else {
return ESP_FAIL;
@@ -106,10 +116,40 @@ esp_err_t app_light_set_saturation(uint16_t saturation)
return app_light_set_led(rgb_hue, saturation, rgb_brightness);
}
static void app_indicator_set(bool state)
{
if (state) {
app_driver_set_gpio(GPIO_DEVICE_OUT1_NAME, true);
app_driver_set_gpio(GPIO_DEVICE_OUT2_NAME, true);
app_driver_set_gpio(GPIO_DEVICE_OUT3_NAME, true);
} else {
app_driver_set_gpio(GPIO_DEVICE_OUT1_NAME, false);
app_driver_set_gpio(GPIO_DEVICE_OUT2_NAME, false);
app_driver_set_gpio(GPIO_DEVICE_OUT3_NAME, false);
}
esp_rmaker_param_update(esp_rmaker_device_get_param_by_name(gpio_device, GPIO_DEVICE_OUT1_NAME), esp_rmaker_bool(state));
esp_rmaker_param_update(esp_rmaker_device_get_param_by_name(gpio_device, GPIO_DEVICE_OUT2_NAME), esp_rmaker_bool(state));
esp_rmaker_param_update(esp_rmaker_device_get_param_by_name(gpio_device, GPIO_DEVICE_OUT3_NAME), esp_rmaker_bool(state));
}
static void push_button_cb(void *arg)
{
bool new_state = !switch_state;
app_switch_set_state(new_state);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state));
}
void app_driver_init()
{
app_reset_button_register(app_reset_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL),
WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
// Register a callback for a button tap (short press) event
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_button_cb, NULL);
// Register Wi-Fi reset and factory reset functionality on the same button
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
/* Configure power */
gpio_config_t io_conf = {
@@ -126,3 +166,17 @@ void app_driver_init()
app_light_init();
}
int IRAM_ATTR app_switch_set_state(bool state)
{
if (switch_state != state) {
switch_state = state;
app_indicator_set(state);
}
return ESP_OK;
}
bool app_switch_get_state(void)
{
return switch_state;
}