Initial commit: ESP32-C6 Zigbee sensor switch project
This commit is contained in:
5
common/light_driver/CMakeLists.txt
Normal file
5
common/light_driver/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
idf_component_register(SRC_DIRS "src"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
led_strip
|
||||
)
|
||||
125
common/light_driver/include/light_driver.h
Normal file
125
common/light_driver/include/light_driver.h
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*
|
||||
* Zigbee light driver example
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* light intensity level */
|
||||
#define LIGHT_DEFAULT_ON 1
|
||||
#define LIGHT_DEFAULT_OFF 0
|
||||
|
||||
/* LED strip configuration */
|
||||
#define CONFIG_EXAMPLE_STRIP_LED_GPIO 8
|
||||
#define CONFIG_EXAMPLE_STRIP_LED_NUMBER 1
|
||||
|
||||
|
||||
/** Convert Hue,Saturation,V to RGB
|
||||
* RGB - [0..0xffff]
|
||||
* hue - [0..0xff]
|
||||
* Sat - [0..0xff]
|
||||
* V always = (ZB_UINT16_MAX-1)
|
||||
*/
|
||||
#define HSV_to_RGB(h, s, v, r, g, b ) \
|
||||
{ \
|
||||
uint8_t i; \
|
||||
uint8_t sector = UINT8_MAX/6; \
|
||||
float f, p, q, t; \
|
||||
if( s == 0 ) { /* achromatic (grey)*/ \
|
||||
r = g = b = (v); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
i = h / sector; /* sector 0 to 5 */ \
|
||||
f = h % sector; /* factorial part of h*/ \
|
||||
p = (float)(v * ( 1.0 - (float)s/UINT8_MAX )); \
|
||||
q = (float)(v * ( 1.0 - (float)s/UINT8_MAX * f/(float)sector )); \
|
||||
t = (float)(v * ( 1.0 - (float)s/UINT8_MAX * ( 1 - f/(float)sector ) )); \
|
||||
switch( i ) { \
|
||||
case 0: r = (v); g = t; b = p; break; \
|
||||
case 1: r = q; g = (v); b = p; break; \
|
||||
case 2: r = p; g = (v); b = t; break; \
|
||||
case 3: r = p; g = q; b = (v); break; \
|
||||
case 4: r = t; g = p; b = (v); break; \
|
||||
case 5: \
|
||||
default: r = (v); g = p; b = q; break; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define XYZ_to_RGB(X, Y, Z, r, g, b) \
|
||||
{ \
|
||||
r = (float)( 3.240479*(X) -1.537150*(Y) -0.498535*(Z)); \
|
||||
g = (float)(-0.969256*(X) +1.875992*(Y) +0.041556*(Z)); \
|
||||
b = (float)( 0.055648*(X) -0.204043*(Y) +1.057311*(Z)); \
|
||||
if(r>1){r=1;} \
|
||||
if(g>1){g=1;} \
|
||||
if(b>1){b=1;} \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set light power (on/off).
|
||||
*
|
||||
* @param power The light power to be set
|
||||
*/
|
||||
void light_driver_set_power(bool power);
|
||||
|
||||
/**
|
||||
* @brief color light driver init, be invoked where you want to use color light
|
||||
*
|
||||
* @param power power on/off
|
||||
*/
|
||||
void light_driver_init(bool power);
|
||||
|
||||
/**
|
||||
* @brief Set light level
|
||||
*
|
||||
* @param level The light level to be set
|
||||
*/
|
||||
void light_driver_set_level(uint8_t level);
|
||||
|
||||
/**
|
||||
* @brief Set light color from RGB
|
||||
*
|
||||
* @param red The red color to be set
|
||||
* @param green The green color to be set
|
||||
* @param blue The blue color to be set
|
||||
*/
|
||||
void light_driver_set_color_RGB(uint8_t red, uint8_t green, uint8_t blue);
|
||||
|
||||
/**
|
||||
* @brief Set light color from color xy
|
||||
*
|
||||
* @param color_currentx The color x to be set
|
||||
* @param color_currenty The color y to be set
|
||||
*/
|
||||
void light_driver_set_color_xy(uint16_t color_current_x, uint16_t color_current_y);
|
||||
|
||||
/**
|
||||
* @brief Set light color from hue saturation
|
||||
*
|
||||
* @param hue The hue to be set
|
||||
* @param sat The sat to be set
|
||||
*/
|
||||
void light_driver_set_color_hue_sat(uint8_t hue, uint8_t sat);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
89
common/light_driver/src/light_driver.c
Normal file
89
common/light_driver/src/light_driver.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*
|
||||
* Zigbee light driver example
|
||||
*
|
||||
* 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 "esp_log.h"
|
||||
#include "led_strip.h"
|
||||
#include "light_driver.h"
|
||||
|
||||
static led_strip_handle_t s_led_strip;
|
||||
static uint8_t s_red = 255, s_green = 255, s_blue = 255, s_level = 255;
|
||||
|
||||
void light_driver_set_color_xy(uint16_t color_current_x, uint16_t color_current_y)
|
||||
{
|
||||
float red_f = 0, green_f = 0, blue_f = 0, color_x, color_y;
|
||||
color_x = (float)color_current_x / 65535;
|
||||
color_y = (float)color_current_y / 65535;
|
||||
/* assume color_Y is full light level value 1 (0-1.0) */
|
||||
float color_X = color_x / color_y;
|
||||
float color_Z = (1 - color_x - color_y) / color_y;
|
||||
/* change from xy to linear RGB NOT sRGB */
|
||||
XYZ_to_RGB(color_X, 1, color_Z, red_f, green_f, blue_f);
|
||||
float ratio = (float)s_level / 255;
|
||||
s_red = (uint8_t)(red_f * (float)255);
|
||||
s_green = (uint8_t)(green_f * (float)255);
|
||||
s_blue = (uint8_t)(blue_f * (float)255);
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(s_led_strip, 0, s_red * ratio, s_green * ratio, s_blue * ratio));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(s_led_strip));
|
||||
}
|
||||
|
||||
void light_driver_set_color_hue_sat(uint8_t hue, uint8_t sat)
|
||||
{
|
||||
float red_f, green_f, blue_f;
|
||||
HSV_to_RGB(hue, sat, UINT8_MAX, red_f, green_f, blue_f);
|
||||
float ratio = (float)s_level / 255;
|
||||
s_red = (uint8_t)red_f;
|
||||
s_green = (uint8_t)green_f;
|
||||
s_blue = (uint8_t)blue_f;
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(s_led_strip, 0, s_red * ratio, s_green * ratio, s_blue * ratio));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(s_led_strip));
|
||||
}
|
||||
|
||||
void light_driver_set_color_RGB(uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
float ratio = (float)s_level / 255;
|
||||
s_red = red;
|
||||
s_green = green;
|
||||
s_blue = blue;
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(s_led_strip, 0, red * ratio, green * ratio, blue * ratio));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(s_led_strip));
|
||||
}
|
||||
|
||||
void light_driver_set_power(bool power)
|
||||
{
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(s_led_strip, 0, s_red * power, s_green * power, s_blue * power));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(s_led_strip));
|
||||
}
|
||||
|
||||
void light_driver_set_level(uint8_t level)
|
||||
{
|
||||
s_level = level;
|
||||
float ratio = (float)s_level / 255;
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(s_led_strip, 0, s_red * ratio, s_green * ratio, s_blue * ratio));
|
||||
ESP_ERROR_CHECK(led_strip_refresh(s_led_strip));
|
||||
}
|
||||
|
||||
void light_driver_init(bool power)
|
||||
{
|
||||
led_strip_config_t led_strip_conf = {
|
||||
.max_leds = CONFIG_EXAMPLE_STRIP_LED_NUMBER,
|
||||
.strip_gpio_num = CONFIG_EXAMPLE_STRIP_LED_GPIO,
|
||||
};
|
||||
led_strip_rmt_config_t rmt_conf = {
|
||||
.resolution_hz = 10 * 1000 * 1000, // 10MHz
|
||||
};
|
||||
ESP_ERROR_CHECK(led_strip_new_rmt_device(&led_strip_conf, &rmt_conf, &s_led_strip));
|
||||
|
||||
light_driver_set_power(power);
|
||||
}
|
||||
Reference in New Issue
Block a user