From ded155d9b59b331812835d745cb5b801e24fdbc2 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 25 Feb 2023 23:13:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=AD=E9=9F=B3=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=B0=9B=E5=9B=B4=E7=81=AF=E6=8E=A7=E5=88=B6=E5=8F=8A=E4=BA=AE?= =?UTF-8?q?=E5=BA=A6=E8=B0=83=E6=95=B4=E5=8A=A8=E7=94=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/ci_03t.c | 43 ++++++++++++++++++++++++------------------- main/light.c | 18 +++++++++++++++++- main/main.c | 3 +-- main/temperature.c | 2 -- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/main/ci_03t.c b/main/ci_03t.c index ee8f322..cba1ba4 100644 --- a/main/ci_03t.c +++ b/main/ci_03t.c @@ -67,23 +67,31 @@ static void rx_task(void *arg) { ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO); - if (data[0] == 0x11 && rxBytes >= 2) { - if (data[1] <= 6) { - light_mode = data[1]; + if (data[0] == 0x98 && data[1] == 0x77 && data[rxBytes - 1] == 0x98 && + data[rxBytes - 2] == 0x77) { + if (data[2] == 0x10) { // 氛围灯模式 + if (data[3] == 0x02) { // 开灯、正常模式 + tx_buff[0] = 0x05; + led_strip_set_brightness(150); + ci_03t_send_data(tx_buff, 1); + continue; + } + if (data[3] == 0x01) { // 开灯、正常模式 + tx_buff[0] = 0x06; + led_strip_set_brightness(20); + ci_03t_send_data(tx_buff, 1); + continue; + } + if (data[3] == 0x00) { // 关灯 + led_strip_set_brightness(0); + tx_buff[0] = 0x04; + ci_03t_send_data(tx_buff, 1); + continue; + } } - tx_buff[0] = data[0]; - tx_buff[1] = light_mode; - uart_write_bytes(CI_03T_UART_NUM, tx_buff, 2); - } else if (data[0] == 0x12 && rxBytes >= 2) { - tx_buff[0] = 0x12; - tx_buff[1] = data[1]; - led_strip_set_brightness(data[1]); - uart_write_bytes(CI_03T_UART_NUM, tx_buff, 2); - } else if (data[0] == 0x13 && rxBytes >= 2) { - tx_buff[0] = 0x12; - tx_buff[1] = display_ambient_lighting_level + (int8_t)data[1]; - led_strip_set_brightness(tx_buff[1]); - uart_write_bytes(CI_03T_UART_NUM, tx_buff, 2); + ESP_LOGW(RX_TASK_TAG, "Unknow command"); + } else { + ESP_LOGW(RX_TASK_TAG, "Invalid data"); } } } @@ -107,7 +115,4 @@ void ci_03t_init() { xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES, NULL); - // xTaskCreate(tx_task, "uart_tx_task", 1024 * 2, NULL, configMAX_PRIORITIES - - // 1, - // NULL); } \ No newline at end of file diff --git a/main/light.c b/main/light.c index b9f69da..703b625 100644 --- a/main/light.c +++ b/main/light.c @@ -36,9 +36,25 @@ light_mode_t light_mode; float display_ambient_light_brightness = 1; uint8_t display_ambient_lighting_level = 255; +void led_strip_fade_in_light_level(void *pvParameter) { + float target = (float)display_ambient_lighting_level / 255.0; + float step_length = (target - display_ambient_light_brightness) / 40.0; + for (int t = 0; t < 40; t++) { + display_ambient_light_brightness += step_length; + vTaskDelay(pdMS_TO_TICKS(10)); + } + display_ambient_light_brightness = target; + vTaskDelete(NULL); +} + void led_strip_set_brightness(uint8_t level) { + if (display_ambient_lighting_level == level) { + return; + } display_ambient_lighting_level = level; - display_ambient_light_brightness = (float)level / 255.0; + + xTaskCreate(led_strip_fade_in_light_level, "LED_STRIP_FADE_IN_LIGHT_LEVEL", + 4096, NULL, 1, NULL); } /** diff --git a/main/main.c b/main/main.c index 642175d..a149d1c 100644 --- a/main/main.c +++ b/main/main.c @@ -44,7 +44,7 @@ void init_i2c() { static const char *TAG = "DisplayAmbientLight"; void app_main(void) { - // light_init_strip(); + light_init_strip(); init_i2c(); init_display(); display_print8_str(0, 0, "Ambient Light"); @@ -70,6 +70,5 @@ void app_main(void) { } while (waiting_and_get_colors()) { light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer); - vTaskDelay(pdMS_TO_TICKS(10)); } } diff --git a/main/temperature.c b/main/temperature.c index 50b2f59..517d6a0 100644 --- a/main/temperature.c +++ b/main/temperature.c @@ -22,14 +22,12 @@ #define TEMPERATURE_TAG "temperature" void fetch_temperature(void* arg) { - ESP_LOGI(TEMPERATURE_TAG, "fetch_temperature"); esp_err_t error; float temperature = DEFAULT_TEMPERATURE; char temperature_str[10]; uint8_t temperature_buffer[] = {0, 0}; display_fill_rect(0, 0, 128, 2, 0x00); for (;;) { - ESP_LOGI(TEMPERATURE_TAG, "fetching temperature"); i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_WRITE,