129 lines
3.7 KiB
C
129 lines
3.7 KiB
C
/* Simple GPIO Demo
|
||
|
||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||
|
||
Unless required by applicable law or agreed to in writing, this
|
||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||
CONDITIONS OF ANY KIND, either express or implied.
|
||
*/
|
||
|
||
#include <sdkconfig.h>
|
||
#include <string.h>
|
||
#include <esp_log.h>
|
||
|
||
#include <app_reset.h>
|
||
#include <ws2812_led.h>
|
||
#include "esp_err.h"
|
||
#include "esp_rmaker_core.h"
|
||
#include "esp_rmaker_standard_types.h"
|
||
#include "app_priv.h"
|
||
|
||
#define RMT_TX_CHANNEL RMT_CHANNEL_0
|
||
/* This is the button that is used for toggling the power */
|
||
#define BUTTON_GPIO CONFIG_EXAMPLE_BOARD_BUTTON_GPIO
|
||
#define BUTTON_ACTIVE_LEVEL 0
|
||
/* This is the GPIO on which the power will be set */
|
||
|
||
#define OUTPUT_GPIO_RED CONFIG_EXAMPLE_OUTPUT_GPIO_RED
|
||
#define OUTPUT_GPIO_GREEN CONFIG_EXAMPLE_OUTPUT_GPIO_GREEN
|
||
#define OUTPUT_GPIO_BLUE CONFIG_EXAMPLE_OUTPUT_GPIO_BLUE
|
||
|
||
#define WIFI_RESET_BUTTON_TIMEOUT 3
|
||
#define FACTORY_RESET_BUTTON_TIMEOUT 10
|
||
|
||
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;
|
||
|
||
esp_err_t app_driver_set_gpio(const char *name, bool state)
|
||
{
|
||
if (strcmp(name, "Red") == 0) {
|
||
gpio_set_level(OUTPUT_GPIO_RED, state);
|
||
} else if (strcmp(name, "Green") == 0) {
|
||
gpio_set_level(OUTPUT_GPIO_GREEN, state);
|
||
} else if (strcmp(name, "Blue") == 0) {
|
||
gpio_set_level(OUTPUT_GPIO_BLUE, state);
|
||
} else {
|
||
return ESP_FAIL;
|
||
}
|
||
return ESP_OK;
|
||
}
|
||
|
||
|
||
esp_err_t app_light_set_led(uint32_t hue, uint32_t saturation, uint32_t brightness)
|
||
{
|
||
// Her ne zaman bu bu işlev çağrılırsa, RGB ışığın gücü açılır
|
||
if (!rgb_power) {
|
||
rgb_power = true;
|
||
esp_rmaker_param_update_and_report(
|
||
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER),
|
||
esp_rmaker_bool(rgb_power));
|
||
}
|
||
return ws2812_led_set_hsv(hue, saturation, brightness);
|
||
}
|
||
|
||
esp_err_t app_light_set_power(bool power)
|
||
{
|
||
rgb_power = power;
|
||
if (power) {
|
||
ws2812_led_set_hsv(rgb_hue, rgb_saturation, rgb_brightness);
|
||
} else {
|
||
ws2812_led_clear();
|
||
}
|
||
return ESP_OK;
|
||
}
|
||
|
||
esp_err_t app_light_init(void)
|
||
{
|
||
esp_err_t ret = ws2812_led_init();
|
||
if (ret != ESP_OK) {
|
||
return ret;
|
||
}
|
||
if (rgb_power) {
|
||
ret = ws2812_led_set_hsv(rgb_hue, rgb_saturation, rgb_brightness);
|
||
} else {
|
||
ret = ws2812_led_clear();
|
||
}
|
||
return ESP_OK;
|
||
}
|
||
|
||
esp_err_t app_light_set_brightness(uint16_t brightness)
|
||
{
|
||
rgb_brightness = brightness;
|
||
return app_light_set_led(rgb_hue, rgb_saturation, brightness);
|
||
}
|
||
|
||
esp_err_t app_light_set_hue(uint16_t hue)
|
||
{
|
||
rgb_hue = hue;
|
||
return app_light_set_led(hue, rgb_saturation, rgb_brightness);
|
||
}
|
||
|
||
esp_err_t app_light_set_saturation(uint16_t saturation)
|
||
{
|
||
rgb_saturation = saturation;
|
||
return app_light_set_led(rgb_hue, saturation, rgb_brightness);
|
||
}
|
||
|
||
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);
|
||
|
||
/* Configure power */
|
||
gpio_config_t io_conf = {
|
||
.mode = GPIO_MODE_OUTPUT,
|
||
.pull_up_en = 1,
|
||
};
|
||
uint64_t pin_mask = (((uint64_t)1 << OUTPUT_GPIO_RED ) | ((uint64_t)1 << OUTPUT_GPIO_GREEN ) | ((uint64_t)1 << OUTPUT_GPIO_BLUE ));
|
||
io_conf.pin_bit_mask = pin_mask;
|
||
/* Configure the GPIO */
|
||
gpio_config(&io_conf);
|
||
gpio_set_level(OUTPUT_GPIO_RED, false);
|
||
gpio_set_level(OUTPUT_GPIO_GREEN, false);
|
||
gpio_set_level(OUTPUT_GPIO_BLUE, false);
|
||
|
||
app_light_init();
|
||
}
|