From c8eb0817e88afecdf17ba6869fc50c95c418a0fd Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 19 Apr 2023 20:24:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E6=A0=A1=E5=87=86?= =?UTF-8?q?=E7=9A=84=E9=A2=9C=E8=89=B2=E6=9D=A5=E6=98=BE=E7=A4=BA=E7=A9=BA?= =?UTF-8?q?=E9=97=B2=E7=81=AF=E5=85=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/light.c | 37 ++++++++++++++++++++++++++++++++----- main/main.c | 4 ++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/main/light.c b/main/light.c index 660f380..a4fd6ae 100644 --- a/main/light.c +++ b/main/light.c @@ -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) { @@ -217,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!", @@ -232,9 +249,17 @@ 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; @@ -293,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)); @@ -382,7 +409,7 @@ void light_play_colors(uint16_t len, uint8_t *buffer) { } if (black_count > STRIP_LED_NUMBER / 4 * 3) { - ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 50)); + 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 * @@ -394,7 +421,7 @@ void light_play_colors(uint16_t len, uint8_t *buffer) { light_led_strip->set_pixel(light_led_strip, led_index, r, g, b)); } } - ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 50)); + ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100)); vTaskDelay(pdMS_TO_TICKS(10)); } diff --git a/main/main.c b/main/main.c index 1fbf99e..58a16ea 100644 --- a/main/main.c +++ b/main/main.c @@ -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,