Hi Nirmesh,
Can you share the code snippet for this new issue?
Sure Piyush.
As similar to the 2 switch app_driver.c file, I modified app_driver.c for 4 switches.
Code: Select all
/* This is the button that is used for toggling the power */
#define BUTTON_GPIO_1 17
#define BUTTON_GPIO_2 18
#define BUTTON_GPIO_3 19
#define BUTTON_GPIO_4 21
#define BUTTON_ACTIVE_LEVEL 0
/* This is the GPIO on which the power will be set */
#define OUTPUT_GPIO_1 14
#define OUTPUT_GPIO_2 25
#define OUTPUT_GPIO_3 26
#define OUTPUT_GPIO_4 27
static bool g_power_state = DEFAULT_POWER;
static bool g_power_state3 = DEFAULT_POWER;
static bool g_power_state4 = DEFAULT_POWER;
static bool g_power_state_nirmesh = DEFAULT_POWER;
#define WIFI_RESET_BUTTON_TIMEOUT 3
#define FACTORY_RESET_BUTTON_TIMEOUT 10
static void push_btn_cb1(void *arg)
{
bool new_state = !g_power_state;
app_driver_set_state(new_state);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device1, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state));
}
static void push_btn_cb2(void *arg)
{
bool new_state_nirmesh = !g_power_state_nirmesh;
app_driver_set_state2(new_state_nirmesh);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device2, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state_nirmesh));
}
static void push_btn_cb3(void *arg)
{
bool new_state3 = !g_power_state3;
app_driver_set_state3(new_state3);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device3, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state3));
}
static void push_btn_cb4(void *arg)
{
bool new_state4 = !g_power_state4;
app_driver_set_state4(new_state4);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device4, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state4));
}
static void set_power_state(bool target)
{
gpio_set_level(OUTPUT_GPIO_1, target);
}
static void set_power_state2(bool target)
{
gpio_set_level(OUTPUT_GPIO_2, target);
}
static void set_power_state3(bool target)
{
gpio_set_level(OUTPUT_GPIO_3, target);
}
static void set_power_state4(bool target)
{
gpio_set_level(OUTPUT_GPIO_4, target);
}
void app_driver_init()
{
button_handle_t btn_handle1 = iot_button_create(BUTTON_GPIO_1, BUTTON_ACTIVE_LEVEL);
button_handle_t btn_handle2 = iot_button_create(BUTTON_GPIO_2, BUTTON_ACTIVE_LEVEL);
button_handle_t btn_handle3 = iot_button_create(BUTTON_GPIO_3, BUTTON_ACTIVE_LEVEL);
button_handle_t btn_handle4 = iot_button_create(BUTTON_GPIO_4, BUTTON_ACTIVE_LEVEL);
if (btn_handle1) {
/* Register a callback for a button tap (short press) event */
iot_button_set_evt_cb(btn_handle1, BUTTON_CB_TAP, push_btn_cb1, NULL);
/* Register Wi-Fi reset and factory reset functionality on same button */
app_reset_button_register(btn_handle1, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
}
if (btn_handle2) {
/* Register a callback for a button tap (short press) event */
iot_button_set_evt_cb(btn_handle2, BUTTON_CB_TAP, push_btn_cb2, NULL);
/* Register Wi-Fi reset and factory reset functionality on same button */
}
if (btn_handle3) {
/* Register a callback for a button tap (short press) event */
iot_button_set_evt_cb(btn_handle3, BUTTON_CB_TAP, push_btn_cb3, NULL);
/* Register Wi-Fi reset and factory reset functionality on same button */
}
if (btn_handle4) {
/* Register a callback for a button tap (short press) event */
iot_button_set_evt_cb(btn_handle4, BUTTON_CB_TAP, push_btn_cb4, NULL);
/* Register Wi-Fi reset and factory reset functionality on same button */
}
/* Configure power */
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = 1,
};
io_conf.pin_bit_mask = (((uint64_t)1 << OUTPUT_GPIO_1) | ((uint64_t)1 << OUTPUT_GPIO_2) | ((uint64_t)1 << OUTPUT_GPIO_3) | ((uint64_t)1 << OUTPUT_GPIO_4));
/* Configure the GPIO */
gpio_config(&io_conf);
}
int IRAM_ATTR app_driver_set_state(bool state)
{
g_power_state = state;
set_power_state(g_power_state);
return ESP_OK;
}
int IRAM_ATTR app_driver_set_state2(bool state)
{
g_power_state_nirmesh = state;
set_power_state2(g_power_state_nirmesh);
return ESP_OK;
}
int IRAM_ATTR app_driver_set_state3(bool state)
{
g_power_state3 = state;
set_power_state3(g_power_state3);
return ESP_OK;
}
int IRAM_ATTR app_driver_set_state4(bool state)
{
g_power_state4 = state;
set_power_state4(g_power_state4);
return ESP_OK;
}
bool app_driver_get_state(void)
{
return g_power_state;
}
bool app_driver_get_state2(void)
{
return g_power_state_nirmesh;
}
bool app_driver_get_state3(void)
{
return g_power_state3;
}
bool app_driver_get_state4(void)
{
return g_power_state4;
}