|
| 1 | +/* Switch demo implementation using button and RGB LED |
| 2 | + |
| 3 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 4 | +
|
| 5 | + Unless required by applicable law or agreed to in writing, this |
| 6 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 7 | + CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include <sdkconfig.h> |
| 11 | + |
| 12 | +#include <iot_button.h> |
| 13 | +#include <esp_rmaker_core.h> |
| 14 | +#include <esp_rmaker_standard_params.h> |
| 15 | + |
| 16 | +#include <app_reset.h> |
| 17 | +#include <ws2812_led.h> |
| 18 | +#include "app_priv.h" |
| 19 | + |
| 20 | +/* This is the button that is used for toggling the power */ |
| 21 | +#define BUTTON_GPIO 0 |
| 22 | +#define BUTTON_ACTIVE_LEVEL 0 |
| 23 | + |
| 24 | +/* This is the GPIO on which the power will be set */ |
| 25 | +#define OUTPUT_GPIO 19 |
| 26 | +static bool g_power_state = DEFAULT_POWER; |
| 27 | + |
| 28 | +/* These values correspoind to H,S,V = 120,100,10 */ |
| 29 | +#define DEFAULT_RED 0 |
| 30 | +#define DEFAULT_GREEN 25 |
| 31 | +#define DEFAULT_BLUE 0 |
| 32 | + |
| 33 | +#define WIFI_RESET_BUTTON_TIMEOUT 3 |
| 34 | +#define FACTORY_RESET_BUTTON_TIMEOUT 10 |
| 35 | + |
| 36 | +void app_indicator_set(bool state) |
| 37 | +{ |
| 38 | + if (state) { |
| 39 | + ws2812_led_set_rgb(DEFAULT_RED, DEFAULT_GREEN, DEFAULT_BLUE); |
| 40 | + } else { |
| 41 | + ws2812_led_clear(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static void app_indicator_init(void) |
| 46 | +{ |
| 47 | + ws2812_led_init(); |
| 48 | + app_indicator_set(g_power_state); |
| 49 | +} |
| 50 | +static void push_btn_cb(void *arg) |
| 51 | +{ |
| 52 | + bool new_state = !g_power_state; |
| 53 | + app_driver_set_state(new_state); |
| 54 | + esp_rmaker_param_update_and_report( |
| 55 | + esp_rmaker_device_get_param_by_name(switch_device, "power"), |
| 56 | + esp_rmaker_bool(new_state)); |
| 57 | + app_homekit_update_state(new_state); |
| 58 | +} |
| 59 | + |
| 60 | +static void set_power_state(bool target) |
| 61 | +{ |
| 62 | + gpio_set_level(OUTPUT_GPIO, target); |
| 63 | + app_indicator_set(target); |
| 64 | +} |
| 65 | + |
| 66 | +void app_driver_init() |
| 67 | +{ |
| 68 | + button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL); |
| 69 | + if (btn_handle) { |
| 70 | + /* Register a callback for a button tap (short press) event */ |
| 71 | + iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL); |
| 72 | + /* Register Wi-Fi reset and factory reset functionality on same button */ |
| 73 | + app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT); |
| 74 | + } |
| 75 | + |
| 76 | + /* Configure power */ |
| 77 | + gpio_config_t io_conf = { |
| 78 | + .mode = GPIO_MODE_OUTPUT, |
| 79 | + .pull_up_en = 1, |
| 80 | + }; |
| 81 | + io_conf.pin_bit_mask = ((uint64_t)1 << OUTPUT_GPIO); |
| 82 | + /* Configure the GPIO */ |
| 83 | + gpio_config(&io_conf); |
| 84 | + app_indicator_init(); |
| 85 | +} |
| 86 | + |
| 87 | +int IRAM_ATTR app_driver_set_state(bool state) |
| 88 | +{ |
| 89 | + if(g_power_state != state) { |
| 90 | + g_power_state = state; |
| 91 | + set_power_state(g_power_state); |
| 92 | + } |
| 93 | + return ESP_OK; |
| 94 | +} |
| 95 | + |
| 96 | +bool app_driver_get_state(void) |
| 97 | +{ |
| 98 | + return g_power_state; |
| 99 | +} |
0 commit comments