From c27418aaf1f0c943fa2e7967212d537868166282 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Mon, 17 Apr 2023 21:27:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=B0=E4=BD=8F=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E6=A0=A1=E5=87=86=E7=9A=84=E5=80=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/CMakeLists.txt | 2 +- main/app_nvs.c | 19 ++++++++++++ main/light.c | 75 +++++++++++++++++++++++++++++++++++++++++++-- main/main.c | 2 ++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 main/app_nvs.c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e6cd9ff..080fac4 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" +idf_component_register(SRCS "app_nvs.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/main/app_nvs.c b/main/app_nvs.c new file mode 100644 index 0000000..20be452 --- /dev/null +++ b/main/app_nvs.c @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "esp_system.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "nvs.h" +#include "nvs_flash.h" + +void app_nvs_init() { + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES || + err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + ESP_ERROR_CHECK(err); +} \ No newline at end of file diff --git a/main/light.c b/main/light.c index b1686a9..be0e453 100644 --- a/main/light.c +++ b/main/light.c @@ -9,9 +9,12 @@ #pragma once #include "driver/rmt.h" #include "esp_log.h" +#include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "led_strip.h" +#include "nvs.h" +#include "nvs_flash.h" #include "sdkconfig.h" static const char *LIGHT_TAG = "DisplayAmbientLight_Light"; @@ -65,6 +68,46 @@ void led_strip_set_color_calibration(float red, float green, float blue) { led_strip_red_calibration = red; led_strip_green_calibration = green; led_strip_blue_calibration = blue; + + nvs_handle_t local_nvs_handle; + + esp_err_t err = nvs_open("storage", NVS_READWRITE, &local_nvs_handle); + + if (err != ESP_OK) { + ESP_LOGW(LIGHT_TAG, "Error (%s) opening NVS handle!\n", + esp_err_to_name(err)); + return; + } + + err = nvs_set_u8(local_nvs_handle, "calibration_r", (uint32_t)(red * 255)); + if (err != ESP_OK) { + nvs_close(local_nvs_handle); + ESP_LOGW(LIGHT_TAG, "Error (%s) write calibration_r failed!", + esp_err_to_name(err)); + return; + } + err = nvs_set_u8(local_nvs_handle, "calibration_g", (uint8_t)(green * 255)); + if (err != ESP_OK) { + nvs_close(local_nvs_handle); + ESP_LOGW(LIGHT_TAG, "Error (%s) calibration_g failed!", + esp_err_to_name(err)); + return; + } + err = nvs_set_u8(local_nvs_handle, "calibration_b", (uint8_t)(blue * 255)); + if (err != ESP_OK) { + nvs_close(local_nvs_handle); + ESP_LOGW(LIGHT_TAG, "Error (%s) calibration_b failed!", + esp_err_to_name(err)); + return; + } + + err = nvs_commit(local_nvs_handle); + if (err != ESP_OK) { + ESP_LOGW(LIGHT_TAG, "Error (%s) save led_strip_red_calibration failed!", + esp_err_to_name(err)); + } + + nvs_close(local_nvs_handle); } /** @@ -171,17 +214,45 @@ void light_for_init() { int8_t i = 0; do { - for (; i < 100; i++) { + 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(10)); + 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) { + ESP_LOGW(LIGHT_TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err)); + } + + uint8_t r = 255, g = 255, b = 255; + err = nvs_get_u8(local_nvs_handle, "calibration_r", &r); + if (err != ESP_OK) { + ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_r!", + esp_err_to_name(err)); + } + err = nvs_get_u8(local_nvs_handle, "calibration_g", &g); + if (err != ESP_OK) { + ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_g!", + esp_err_to_name(err)); + } + err = nvs_get_u8(local_nvs_handle, "calibration_b", &b); + if (err != ESP_OK) { + ESP_LOGW(LIGHT_TAG, "Error (%s) reading calibration_b!", + esp_err_to_name(err)); + } + + nvs_close(local_nvs_handle); + + led_strip_set_color_calibration((float)r / 255.0, (float)g / 255.0, + (float)b / 255.0); } void light_for_connecting_wifi() { diff --git a/main/main.c b/main/main.c index e032d84..1fbf99e 100644 --- a/main/main.c +++ b/main/main.c @@ -1,4 +1,5 @@ #include "apds_9960.c" +#include "app_nvs.c" #include "ci_03t.c" #include "embedded_display.c" #include "esp_log.h" @@ -16,6 +17,7 @@ static const char *TAG = "DisplayAmbientLight"; void app_main(void) { + app_nvs_init(); light_init_strip(); init_i2c(); i2c_check_slaves();