Skip to content

Commit

Permalink
Merge branch 'bugfix/led_indicator_color' into 'master'
Browse files Browse the repository at this point in the history
Bugfix/led indicator color

See merge request ae_group/esp-iot-solution!888
  • Loading branch information
leeebo committed Nov 16, 2023
2 parents a3a1caf + 747e8de commit 18fefde
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
10 changes: 10 additions & 0 deletions components/led/led_indicator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## v0.7.1 - 2023-10-15

### BUG FIX

* Fix set HSV hue color.

### Enhancements:

* Added API for set color by temperature.

# ChangeLog

## v0.7.0 - 2023-11-09
Expand Down
2 changes: 1 addition & 1 deletion components/led/led_indicator/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.7.0"
version: "0.7.1"
description: LED indicator driver
url: https://github.com/espressif/esp-iot-solution/tree/master/components/led/led_indicator
dependencies:
Expand Down
15 changes: 15 additions & 0 deletions components/led/led_indicator/include/led_indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ uint32_t led_indicator_get_rgb(led_indicator_handle_t handle);
*/
esp_err_t led_indicator_set_rgb(led_indicator_handle_t handle, uint32_t irgb_value);

/**
* @brief Set the color temperature for the LED indicator.
*
* @param handle LED indicator handle.
* @param temperature Color temperature of LED (0xIITTTTTT)
* I: 0-126, set 127 to control all, TTTTTT: 0-1000000
* @note Index settings are only supported for LED_RGB_MODE.
* @return esp_err_t
* - ESP_OK: Success
* - ESP_FAIL: Failure
* - ESP_ERR_INVALID_ARG: Invalid parameter
*/
esp_err_t led_indicator_set_color_temperature(led_indicator_handle_t handle, const uint32_t temperature);

#ifdef __cplusplus
}
#endif
Expand Down
54 changes: 54 additions & 0 deletions components/led/led_indicator/src/led_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ static const char *TAG = "led_indicator";
#define NULL_ACTIVE_BLINK -1
#define NULL_PREEMPT_BLINK -1

typedef struct {
uint16_t hue;
uint8_t saturation;
} HS_color_t;
static const HS_color_t temp_table[] = {
{4, 100}, {8, 100}, {11, 100}, {14, 100}, {16, 100}, {18, 100}, {20, 100}, {22, 100}, {24, 100}, {25, 100},
{27, 100}, {28, 100}, {30, 100}, {31, 100}, {31, 95}, {30, 89}, {30, 85}, {29, 80}, {29, 76}, {29, 73},
{29, 69}, {28, 66}, {28, 63}, {28, 60}, {28, 57}, {28, 54}, {28, 52}, {27, 49}, {27, 47}, {27, 45},
{27, 43}, {27, 41}, {27, 39}, {27, 37}, {27, 35}, {27, 33}, {27, 31}, {27, 30}, {27, 28}, {27, 26},
{27, 25}, {27, 23}, {27, 22}, {27, 21}, {27, 19}, {27, 18}, {27, 17}, {27, 15}, {28, 14}, {28, 13},
{28, 12}, {29, 10}, {29, 9}, {30, 8}, {31, 7}, {32, 6}, {34, 5}, {36, 4}, {41, 3}, {49, 2},
{0, 0}, {294, 2}, {265, 3}, {251, 4}, {242, 5}, {237, 6}, {233, 7}, {231, 8}, {229, 9}, {228, 10},
{227, 11}, {226, 11}, {226, 12}, {225, 13}, {225, 13}, {224, 14}, {224, 14}, {224, 15}, {224, 15}, {223, 16},
{223, 16}, {223, 17}, {223, 17}, {223, 17}, {222, 18}, {222, 18}, {222, 19}, {222, 19}, {222, 19}, {222, 19},
{222, 20}, {222, 20}, {222, 20}, {222, 21}, {222, 21}
};

static const char *led_indicator_mode_str[4] = {"GPIO mode", "LEDC mode", "LED Strips mode","custom mode"};

/**
Expand Down Expand Up @@ -836,3 +853,40 @@ esp_err_t led_indicator_set_rgb(led_indicator_handle_t handle, uint32_t irgb_val
xSemaphoreGive(p_led_indicator->mutex);
return ESP_OK;
}

esp_err_t led_indicator_set_color_temperature(led_indicator_handle_t handle, const uint32_t temperature)
{
LED_INDICATOR_CHECK(handle != NULL, "invalid p_handle", return ESP_ERR_INVALID_ARG);
_led_indicator_t *p_led_indicator = (_led_indicator_t *)handle;
if (!p_led_indicator->hal_indicator_set_hsv) {
ESP_LOGW(TAG, "LED indicator does not have the hal_indicator_set_hsv function");
return ESP_FAIL;
}
uint16_t hue;
uint8_t saturation;

xSemaphoreTake(p_led_indicator->mutex, portMAX_DELAY);
uint32_t ihsv_value = p_led_indicator->current_fade_value & 0x1FFFFFFF;

if ((temperature & 0xFFFFFF) < 600) {
hue = 0;
saturation = 100;
} else if ((temperature & 0xFFFFFF) > 10000) {
hue = 222;
saturation = 21 + ((temperature & 0xFFFFFF) - 10000) * 41 / 990000;
} else {
hue = temp_table[((temperature & 0xFFFFFF) - 600) / 100].hue;
saturation = temp_table[((temperature & 0xFFFFFF) - 600) / 100].saturation;
}
saturation = (saturation * 255) / 100;

SET_SATURATION(ihsv_value, saturation);
SET_HUE(ihsv_value, hue);
ihsv_value = (ihsv_value & 0x1FFFFFF) | (temperature & 0x7F000000);

p_led_indicator->hal_indicator_set_hsv(p_led_indicator->hardware_data, ihsv_value);
p_led_indicator->current_fade_value = ihsv_value;
p_led_indicator->last_fade_value = ihsv_value;
xSemaphoreGive(p_led_indicator->mutex);
return ESP_OK;
}
9 changes: 6 additions & 3 deletions components/led/led_indicator/src/led_strips.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ esp_err_t led_indicator_strips_deinit(void *strips)
esp_err_t led_indicator_strips_set_on_off(void *strips, bool on_off)
{
led_strips_t *p_strip = (led_strips_t *)strips;
uint8_t i,h,s,v;
uint8_t i,s,v;
uint16_t h;
uint32_t iihsv_value = p_strip->ihsv;
SET_BRIGHTNESS(iihsv_value, on_off ? MAX_BRIGHTNESS : 0);
i = GET_INDEX(iihsv_value);
Expand Down Expand Up @@ -230,7 +231,8 @@ esp_err_t led_indicator_strips_set_rgb(void *strips, uint32_t irgb_value)
esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t iihsv_value)
{
led_strips_t *p_strip = (led_strips_t *)strips;
uint8_t i,h,s,v;
uint8_t i,s,v;
uint16_t h;
i = GET_INDEX(iihsv_value);
h = GET_HUE(iihsv_value);
s = GET_SATURATION(iihsv_value);
Expand Down Expand Up @@ -260,7 +262,8 @@ esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t iihsv_value)
esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ihsv)
{
led_strips_t *p_strip = (led_strips_t *)strips;
uint8_t i,h,s,v;
uint8_t i,s,v;
uint16_t h;
uint32_t ihsv_value;
ihsv_value = p_strip->ihsv;
SET_BRIGHTNESS(ihsv_value, GET_BRIGHTNESS(ihsv));
Expand Down

0 comments on commit 18fefde

Please sign in to comment.