Add switch device.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -22,10 +22,17 @@
|
||||
#include <app_insights.h>
|
||||
|
||||
#include "app_priv.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
static const char *TAG = "app_main";
|
||||
|
||||
static const char *gpio_device_name = "GPIO";
|
||||
static const char *rgb_device_name = "RGB-Light";
|
||||
static const char *switch_device_name = "Switch";
|
||||
|
||||
esp_rmaker_device_t *gpio_device;
|
||||
esp_rmaker_device_t *light_device;
|
||||
esp_rmaker_device_t *switch_device;
|
||||
|
||||
#ifdef CONFIG_ESP_RMAKER_CMD_RESP_ENABLE
|
||||
|
||||
@@ -127,9 +134,23 @@ static esp_err_t write_cb(const esp_rmaker_device_t *device, const esp_rmaker_pa
|
||||
if (ctx) {
|
||||
ESP_LOGI(TAG, "Received write request via : %s", esp_rmaker_device_cb_src_to_str(ctx->src));
|
||||
}
|
||||
if (app_driver_set_gpio(esp_rmaker_param_get_name(param), val.val.b) == ESP_OK) {
|
||||
esp_rmaker_param_update(param, val);
|
||||
const char *device_name = esp_rmaker_device_get_name(device);
|
||||
// const char *param_name = esp_rmaker_param_get_name(param);
|
||||
esp_err_t ret = ESP_OK;
|
||||
|
||||
if (strcmp(device_name, gpio_device_name) == 0) {
|
||||
ret = app_driver_set_gpio(esp_rmaker_param_get_name(param), val.val.b);
|
||||
}
|
||||
else if (strcmp(device_name, switch_device_name) == 0) {
|
||||
ret = app_switch_set_state(val.val.b);
|
||||
}
|
||||
else {
|
||||
ESP_LOGI(TAG, "Unexpected device %s", device_name);
|
||||
}
|
||||
|
||||
if (ret == ESP_OK)
|
||||
esp_rmaker_param_update(param, val);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -159,7 +180,7 @@ void app_main()
|
||||
esp_rmaker_config_t rainmaker_cfg = {
|
||||
.enable_time_sync = false,
|
||||
};
|
||||
esp_rmaker_node_t *node = esp_rmaker_node_init(&rainmaker_cfg, "ESP RainMaker Device", "GPIO-Device");
|
||||
esp_rmaker_node_t *node = esp_rmaker_node_init(&rainmaker_cfg, "ESP RainMaker Multi Device", "Multi Device");
|
||||
if (!node) {
|
||||
ESP_LOGE(TAG, "Could not initialise node. Aborting!!!");
|
||||
vTaskDelay(5000/portTICK_PERIOD_MS);
|
||||
@@ -167,25 +188,25 @@ void app_main()
|
||||
}
|
||||
|
||||
/* Create a device and add the relevant parameters to it */
|
||||
esp_rmaker_device_t *gpio_device = esp_rmaker_device_create("GPIO-Device", NULL, NULL);
|
||||
gpio_device = esp_rmaker_device_create(gpio_device_name, NULL, NULL);
|
||||
esp_rmaker_device_add_cb(gpio_device, write_cb, NULL);
|
||||
|
||||
esp_rmaker_param_t *red_param = esp_rmaker_param_create("Red", NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_t *red_param = esp_rmaker_param_create(GPIO_DEVICE_OUT1_NAME, NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_add_ui_type(red_param, ESP_RMAKER_UI_TOGGLE);
|
||||
esp_rmaker_device_add_param(gpio_device, red_param);
|
||||
|
||||
esp_rmaker_param_t *green_param = esp_rmaker_param_create("Green", NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_t *green_param = esp_rmaker_param_create(GPIO_DEVICE_OUT2_NAME, NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_add_ui_type(green_param, ESP_RMAKER_UI_TOGGLE);
|
||||
esp_rmaker_device_add_param(gpio_device, green_param);
|
||||
|
||||
esp_rmaker_param_t *blue_param = esp_rmaker_param_create("Blue", NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_t *blue_param = esp_rmaker_param_create(GPIO_DEVICE_OUT3_NAME, NULL, esp_rmaker_bool(false), PROP_FLAG_READ | PROP_FLAG_WRITE);
|
||||
esp_rmaker_param_add_ui_type(blue_param, ESP_RMAKER_UI_TOGGLE);
|
||||
esp_rmaker_device_add_param(gpio_device, blue_param);
|
||||
|
||||
esp_rmaker_node_add_device(node, gpio_device);
|
||||
|
||||
// Create the light device
|
||||
light_device = esp_rmaker_lightbulb_device_create("Light", NULL, RGB_POWER_DEFAULT);
|
||||
light_device = esp_rmaker_lightbulb_device_create(rgb_device_name, NULL, RGB_POWER_DEFAULT);
|
||||
esp_rmaker_device_add_bulk_cb(light_device, bulk_write_cb, NULL);
|
||||
|
||||
esp_rmaker_device_add_param(light_device, esp_rmaker_brightness_param_create(ESP_RMAKER_DEF_BRIGHTNESS_NAME, RGB_BRIGHTNESS_DEFAULT));
|
||||
@@ -194,6 +215,11 @@ void app_main()
|
||||
|
||||
esp_rmaker_node_add_device(node, light_device);
|
||||
|
||||
// Create the switch device
|
||||
switch_device = esp_rmaker_switch_device_create(switch_device_name, NULL, false);
|
||||
esp_rmaker_device_add_cb(switch_device, write_cb, NULL);
|
||||
esp_rmaker_node_add_device(node, switch_device);
|
||||
|
||||
/* Enable OTA */
|
||||
esp_rmaker_ota_enable_default();
|
||||
|
||||
|
||||
@@ -15,7 +15,13 @@
|
||||
#define RGB_SATURATION_DEFAULT 100
|
||||
#define RGB_BRIGHTNESS_DEFAULT 25
|
||||
|
||||
#define GPIO_DEVICE_OUT1_NAME "Output-1"
|
||||
#define GPIO_DEVICE_OUT2_NAME "Output-2"
|
||||
#define GPIO_DEVICE_OUT3_NAME "Output-3"
|
||||
|
||||
extern esp_rmaker_device_t *gpio_device;
|
||||
extern esp_rmaker_device_t *light_device;
|
||||
extern esp_rmaker_device_t *switch_device;
|
||||
|
||||
void app_driver_init(void);
|
||||
esp_err_t app_driver_set_gpio(const char *name, bool state);
|
||||
@@ -24,3 +30,5 @@ esp_err_t app_light_set_power(bool power);
|
||||
esp_err_t app_light_set_brightness(uint16_t brightness);
|
||||
esp_err_t app_light_set_hue(uint16_t hue);
|
||||
esp_err_t app_light_set_saturation(uint16_t saturation);
|
||||
int app_switch_set_state(bool state);
|
||||
bool app_switch_get_state(void);
|
||||
Reference in New Issue
Block a user