|
| 1 | +/* LED Lightbulb demo implementation using 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 | +#include <esp_log.h> |
| 12 | +#include <iot_button.h> |
| 13 | +#include <esp_rmaker_core.h> |
| 14 | +#include <esp_rmaker_standard_types.h> |
| 15 | +#include <esp_rmaker_standard_params.h> |
| 16 | + |
| 17 | +#include <app_reset.h> |
| 18 | + |
| 19 | +#if CONFIG_IDF_TARGET_ESP32C2 |
| 20 | +#include <ledc_driver.h> |
| 21 | +#else |
| 22 | +#include <ws2812_led.h> |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "app_priv.h" |
| 26 | + |
| 27 | +/* This is the button that is used for toggling the power */ |
| 28 | +#define BUTTON_GPIO CONFIG_EXAMPLE_BOARD_BUTTON_GPIO |
| 29 | +#define BUTTON_ACTIVE_LEVEL 0 |
| 30 | + |
| 31 | +#define WIFI_RESET_BUTTON_TIMEOUT 3 |
| 32 | +#define FACTORY_RESET_BUTTON_TIMEOUT 10 |
| 33 | + |
| 34 | +static uint16_t g_hue = DEFAULT_HUE; |
| 35 | +static uint16_t g_saturation = DEFAULT_SATURATION; |
| 36 | +static uint16_t g_value = DEFAULT_BRIGHTNESS; |
| 37 | +static bool g_power = DEFAULT_POWER; |
| 38 | + |
| 39 | +#if CONFIG_IDF_TARGET_ESP32C2 |
| 40 | +esp_err_t app_light_set_led(uint32_t hue, uint32_t saturation, uint32_t brightness) |
| 41 | +{ |
| 42 | + /* Whenever this function is called, light power will be ON */ |
| 43 | + if (!g_power) { |
| 44 | + g_power = true; |
| 45 | + esp_rmaker_param_update_and_report( |
| 46 | + esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER), |
| 47 | + esp_rmaker_bool(g_power)); |
| 48 | + } |
| 49 | + ledc_set_hsv(hue, saturation, brightness); |
| 50 | + return ESP_OK; |
| 51 | +} |
| 52 | + |
| 53 | +esp_err_t app_light_set_power(bool power) |
| 54 | +{ |
| 55 | + g_power = power; |
| 56 | + if (power) { |
| 57 | + ledc_set_hsv(g_hue, g_saturation, g_value); |
| 58 | + } else { |
| 59 | + ledc_clear(); |
| 60 | + } |
| 61 | + return ESP_OK; |
| 62 | +} |
| 63 | + |
| 64 | +esp_err_t app_light_init(void) |
| 65 | +{ |
| 66 | + ledc_init(); |
| 67 | + return ESP_OK; |
| 68 | +} |
| 69 | +#else |
| 70 | +esp_err_t app_light_set_led(uint32_t hue, uint32_t saturation, uint32_t brightness) |
| 71 | +{ |
| 72 | + /* Whenever this function is called, light power will be ON */ |
| 73 | + if (!g_power) { |
| 74 | + g_power = true; |
| 75 | + esp_rmaker_param_update_and_report( |
| 76 | + esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER), |
| 77 | + esp_rmaker_bool(g_power)); |
| 78 | + } |
| 79 | + return ws2812_led_set_hsv(hue, saturation, brightness); |
| 80 | +} |
| 81 | + |
| 82 | +esp_err_t app_light_set_power(bool power) |
| 83 | +{ |
| 84 | + g_power = power; |
| 85 | + if (power) { |
| 86 | + ws2812_led_set_hsv(g_hue, g_saturation, g_value); |
| 87 | + } else { |
| 88 | + ws2812_led_clear(); |
| 89 | + } |
| 90 | + return ESP_OK; |
| 91 | +} |
| 92 | + |
| 93 | +esp_err_t app_light_init(void) |
| 94 | +{ |
| 95 | + esp_err_t err = ws2812_led_init(); |
| 96 | + if (err != ESP_OK) { |
| 97 | + return err; |
| 98 | + } |
| 99 | + if (g_power) { |
| 100 | + ws2812_led_set_hsv(g_hue, g_saturation, g_value); |
| 101 | + } else { |
| 102 | + ws2812_led_clear(); |
| 103 | + } |
| 104 | + return ESP_OK; |
| 105 | +} |
| 106 | +#endif |
| 107 | + |
| 108 | +esp_err_t app_light_set_brightness(uint16_t brightness) |
| 109 | +{ |
| 110 | + g_value = brightness; |
| 111 | + return app_light_set_led(g_hue, g_saturation, g_value); |
| 112 | +} |
| 113 | +esp_err_t app_light_set_hue(uint16_t hue) |
| 114 | +{ |
| 115 | + g_hue = hue; |
| 116 | + return app_light_set_led(g_hue, g_saturation, g_value); |
| 117 | +} |
| 118 | +esp_err_t app_light_set_saturation(uint16_t saturation) |
| 119 | +{ |
| 120 | + g_saturation = saturation; |
| 121 | + return app_light_set_led(g_hue, g_saturation, g_value); |
| 122 | +} |
| 123 | + |
| 124 | +static void push_btn_cb(void *arg) |
| 125 | +{ |
| 126 | + app_light_set_power(!g_power); |
| 127 | + esp_rmaker_param_update_and_report( |
| 128 | + esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER), |
| 129 | + esp_rmaker_bool(g_power)); |
| 130 | +} |
| 131 | + |
| 132 | +void app_driver_init() |
| 133 | +{ |
| 134 | + app_light_init(); |
| 135 | + button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL); |
| 136 | + if (btn_handle) { |
| 137 | + /* Register a callback for a button tap (short press) event */ |
| 138 | + iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL); |
| 139 | + /* Register Wi-Fi reset and factory reset functionality on same button */ |
| 140 | + app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT); |
| 141 | + } |
| 142 | +} |
0 commit comments