2 Commits

Author SHA1 Message Date
c27418aaf1 feat: 记住颜色校准的值。 2023-04-17 21:27:09 +08:00
40dde8cc89 feat: 支持颜色校准 2023-04-17 20:49:10 +08:00
6 changed files with 127 additions and 6 deletions

9
dependencies.lock Normal file
View File

@ -0,0 +1,9 @@
dependencies:
idf:
component_hash: null
source:
type: idf
version: 4.4.4
manifest_hash: dcf4d39b94252de130019eadceb989d72b0dbc26b552cfdcbb50f6da531d2b92
target: esp32c3
version: 1.0.0

View File

@ -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 ".") INCLUDE_DIRS ".")

19
main/app_nvs.c Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <stdio.h>
#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);
}

View File

@ -9,9 +9,12 @@
#pragma once #pragma once
#include "driver/rmt.h" #include "driver/rmt.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "led_strip.h" #include "led_strip.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "sdkconfig.h" #include "sdkconfig.h"
static const char *LIGHT_TAG = "DisplayAmbientLight_Light"; static const char *LIGHT_TAG = "DisplayAmbientLight_Light";
@ -36,6 +39,10 @@ light_mode_t light_mode;
float display_ambient_light_brightness = 1; float display_ambient_light_brightness = 1;
uint8_t display_ambient_lighting_level = 255; uint8_t display_ambient_lighting_level = 255;
float led_strip_red_calibration = 1.0;
float led_strip_green_calibration = 1.0;
float led_strip_blue_calibration = 1.0;
void led_strip_fade_in_light_level(void *pvParameter) { void led_strip_fade_in_light_level(void *pvParameter) {
float target = (float)display_ambient_lighting_level / 255.0; float target = (float)display_ambient_lighting_level / 255.0;
float step_length = (target - display_ambient_light_brightness) / 40.0; float step_length = (target - display_ambient_light_brightness) / 40.0;
@ -57,6 +64,52 @@ void led_strip_set_brightness(uint8_t level) {
4096, NULL, 1, NULL); 4096, NULL, 1, NULL);
} }
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);
}
/** /**
* @brief Simple helper function, converting HSV color space to RGB color * @brief Simple helper function, converting HSV color space to RGB color
* space * space
@ -161,17 +214,45 @@ void light_for_init() {
int8_t i = 0; int8_t i = 0;
do { do {
for (; i < 100; i++) { for (; i < 50; i++) {
for (int j = 0; j < STRIP_LED_NUMBER; j++) { for (int j = 0; j < STRIP_LED_NUMBER; j++) {
led_strip_hsv2rgb(0, 0, i, &red, &green, &blue); led_strip_hsv2rgb(0, 0, i, &red, &green, &blue);
ESP_ERROR_CHECK( ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, j, red, green, blue)); light_led_strip->set_pixel(light_led_strip, j, red, green, blue));
} }
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100)); 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)); vTaskDelay(pdMS_TO_TICKS(100));
} while (light_mode == light_mode_init); } 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() { void light_for_connecting_wifi() {
@ -277,11 +358,14 @@ void light_play_colors(uint16_t len, uint8_t *buffer) {
led_index < STRIP_LED_NUMBER && buffer_cursor < len; led_index < STRIP_LED_NUMBER && buffer_cursor < len;
led_index++, buffer_cursor += 3) { led_index++, buffer_cursor += 3) {
uint8_t r = (uint8_t)((float)buffer[buffer_cursor] * uint8_t r = (uint8_t)((float)buffer[buffer_cursor] *
display_ambient_light_brightness), display_ambient_light_brightness *
led_strip_red_calibration),
g = (uint8_t)((float)buffer[buffer_cursor + 1] * g = (uint8_t)((float)buffer[buffer_cursor + 1] *
display_ambient_light_brightness), display_ambient_light_brightness *
led_strip_green_calibration),
b = (uint8_t)((float)buffer[buffer_cursor + 2] * b = (uint8_t)((float)buffer[buffer_cursor + 2] *
display_ambient_light_brightness); display_ambient_light_brightness *
led_strip_blue_calibration);
ESP_ERROR_CHECK( ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b)); light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
} }

View File

@ -1,4 +1,5 @@
#include "apds_9960.c" #include "apds_9960.c"
#include "app_nvs.c"
#include "ci_03t.c" #include "ci_03t.c"
#include "embedded_display.c" #include "embedded_display.c"
#include "esp_log.h" #include "esp_log.h"
@ -16,6 +17,7 @@
static const char *TAG = "DisplayAmbientLight"; static const char *TAG = "DisplayAmbientLight";
void app_main(void) { void app_main(void) {
app_nvs_init();
light_init_strip(); light_init_strip();
init_i2c(); init_i2c();
i2c_check_slaves(); i2c_check_slaves();

View File

@ -40,6 +40,8 @@
#define MQTT_KEY_DESKTOP_ONLINE MQTT_DESKTOP_KEY_PREFIX MQTT_ONLINE_SUFFIX #define MQTT_KEY_DESKTOP_ONLINE MQTT_DESKTOP_KEY_PREFIX MQTT_ONLINE_SUFFIX
#define MQTT_KEY_DESKTOP_COLORS MQTT_DESKTOP_KEY_PREFIX MQTT_COLORS_SUFFIX #define MQTT_KEY_DESKTOP_COLORS MQTT_DESKTOP_KEY_PREFIX MQTT_COLORS_SUFFIX
#define MQTT_KEY_DESKTOP_ALL MQTT_DESKTOP_KEY_PREFIX MQTT_ALL_SUFFIX #define MQTT_KEY_DESKTOP_ALL MQTT_DESKTOP_KEY_PREFIX MQTT_ALL_SUFFIX
#define MQTT_KEY_DESKTOP_COLOR_CALIBRATION \
MQTT_DESKTOP_KEY_PREFIX "color-calibration"
#define MQTT_KEY_BOARD_CMD MQTT_BOARD_KEY_PREFIX MQTT_CMD_SUFFIX #define MQTT_KEY_BOARD_CMD MQTT_BOARD_KEY_PREFIX MQTT_CMD_SUFFIX
#define MQTT_KEY_DESKTOP_DISPLAY_0_BRIGHTNESS \ #define MQTT_KEY_DESKTOP_DISPLAY_0_BRIGHTNESS \
@ -124,6 +126,11 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
.value = (uint16_t)(event->data[0] << 8 | event->data[1]), .value = (uint16_t)(event->data[0] << 8 | event->data[1]),
}; };
xQueueSend(mqtt_cmd_event, &mqtt_event, NULL); xQueueSend(mqtt_cmd_event, &mqtt_event, NULL);
} else if (strncmp(event->topic, MQTT_KEY_DESKTOP_COLOR_CALIBRATION,
event->topic_len) == 0) {
led_strip_set_color_calibration((float)event->data[0] / 255.0,
(float)event->data[1] / 255.0,
(float)event->data[2] / 255.0);
} else { } else {
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data); printf("DATA=%.*s\r\n", event->data_len, event->data);