Compare commits

...

2 Commits

2 changed files with 73 additions and 21 deletions

View File

@ -62,6 +62,22 @@ void led_strip_set_brightness(uint8_t level) {
xTaskCreate(led_strip_fade_in_light_level, "LED_STRIP_FADE_IN_LIGHT_LEVEL",
4096, NULL, 1, NULL);
nvs_handle_t nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
err = nvs_set_u8(nvs_handle, "brightness", level);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) saving light level!\n",
esp_err_to_name(err));
nvs_close(nvs_handle);
return;
}
err = nvs_commit(nvs_handle);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) saving light level!\n",
esp_err_to_name(err));
}
nvs_close(nvs_handle);
}
void led_strip_set_color_calibration(float red, float green, float blue) {
@ -210,22 +226,6 @@ void light_for_init() {
ESP_LOGI(LIGHT_TAG, "light_for_init");
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
uint32_t red = 0, green = 0, blue = 0;
int8_t i = 0;
do {
for (; i < 50; i++) {
for (int j = 0; j < STRIP_LED_NUMBER; j++) {
led_strip_hsv2rgb(0, 0, i, &red, &green, &blue);
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(20));
}
vTaskDelay(pdMS_TO_TICKS(100));
} while (light_mode == light_mode_init);
nvs_handle local_nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle);
if (err != ESP_OK) {
@ -233,6 +233,7 @@ void light_for_init() {
}
uint8_t r = 255, g = 255, b = 255;
uint8_t brightness = 200;
err = nvs_get_u8(local_nvs_handle, "calibration_r", &r);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_r!",
@ -248,9 +249,39 @@ void light_for_init() {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_b!",
esp_err_to_name(err));
}
err = nvs_get_u8(local_nvs_handle, "brightness", &brightness);
if (err != ESP_OK) {
ESP_LOGW(LIGHT_TAG, "Error (%s) reading brightness!", esp_err_to_name(err));
}
nvs_close(local_nvs_handle);
// set brightness
led_strip_set_brightness(brightness);
// play init light
float r_f = (float)r / 255.0;
float g_f = (float)g / 255.0;
float b_f = (float)b / 255.0;
uint8_t init_r, init_g, init_b;
do {
for (uint8_t i = 0; i < 50; i++) {
init_r = (uint8_t)(r_f * (float)i);
init_g = (uint8_t)(g_f * (float)i);
init_b = (uint8_t)(b_f * (float)i);
for (int j = 0; j < STRIP_LED_NUMBER; j++) {
ESP_ERROR_CHECK(light_led_strip->set_pixel(light_led_strip, j, init_r,
init_g, init_b));
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(20));
}
vTaskDelay(pdMS_TO_TICKS(100));
} while (light_mode == light_mode_init);
led_strip_set_color_calibration((float)r / 255.0, (float)g / 255.0,
(float)b / 255.0);
}
@ -287,9 +318,11 @@ void light_for_idle() {
// Build RGB values
led_strip_hsv2rgb(hue, 50, 30, &red, &green, &blue);
red = red * display_ambient_light_brightness;
green = green * display_ambient_light_brightness;
blue = blue * display_ambient_light_brightness;
red = red * display_ambient_light_brightness * led_strip_red_calibration;
green = green * display_ambient_light_brightness *
led_strip_green_calibration;
blue =
blue * display_ambient_light_brightness * led_strip_blue_calibration;
// Write RGB values to strip driver
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
@ -354,6 +387,8 @@ void light_init_strip() {
void light_play_colors(uint16_t len, uint8_t *buffer) {
light_mode = light_mode_desktop_sending_colors;
uint16_t black_count = 0; // count of black pixels. r/g/b <= 10
for (uint16_t led_index = 0, buffer_cursor = 0;
led_index < STRIP_LED_NUMBER && buffer_cursor < len;
led_index++, buffer_cursor += 3) {
@ -366,9 +401,26 @@ void light_play_colors(uint16_t len, uint8_t *buffer) {
b = (uint8_t)((float)buffer[buffer_cursor + 2] *
display_ambient_light_brightness *
led_strip_blue_calibration);
if (r <= 10 && g <= 10 && b <= 10) {
black_count++;
}
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
if (black_count > STRIP_LED_NUMBER / 4 * 3) {
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
uint8_t r = (uint8_t)((float)100 * display_ambient_light_brightness *
led_strip_red_calibration),
g = (uint8_t)((float)100 * display_ambient_light_brightness *
led_strip_green_calibration),
b = (uint8_t)((float)100 * display_ambient_light_brightness *
led_strip_blue_calibration);
for (uint16_t led_index = 0; led_index < STRIP_LED_NUMBER; led_index++) {
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(10));
}

View File

@ -25,8 +25,8 @@ void app_main(void) {
display_print8_str(0, 0, "Ambient Light");
ci_03t_init();
apes_9960_init();
apes_9960_auto_fetch();
auto_fetch_temperature();
// apes_9960_auto_fetch();
// auto_fetch_temperature();
pca9555_init();
ui_input_init();
xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10,