feat: 语音模块氛围灯控制及亮度调整动画。

This commit is contained in:
Ivan Li 2023-02-25 23:13:56 +08:00
parent 13a00fbe1b
commit ded155d9b5
4 changed files with 42 additions and 24 deletions

View File

@ -67,23 +67,31 @@ static void rx_task(void *arg) {
ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO); ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
if (data[0] == 0x11 && rxBytes >= 2) { if (data[0] == 0x98 && data[1] == 0x77 && data[rxBytes - 1] == 0x98 &&
if (data[1] <= 6) { data[rxBytes - 2] == 0x77) {
light_mode = data[1]; 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;
} }
tx_buff[0] = data[0]; if (data[3] == 0x01) { // 开灯、正常模式
tx_buff[1] = light_mode; tx_buff[0] = 0x06;
uart_write_bytes(CI_03T_UART_NUM, tx_buff, 2); led_strip_set_brightness(20);
} else if (data[0] == 0x12 && rxBytes >= 2) { ci_03t_send_data(tx_buff, 1);
tx_buff[0] = 0x12; continue;
tx_buff[1] = data[1]; }
led_strip_set_brightness(data[1]); if (data[3] == 0x00) { // 关灯
uart_write_bytes(CI_03T_UART_NUM, tx_buff, 2); led_strip_set_brightness(0);
} else if (data[0] == 0x13 && rxBytes >= 2) { tx_buff[0] = 0x04;
tx_buff[0] = 0x12; ci_03t_send_data(tx_buff, 1);
tx_buff[1] = display_ambient_lighting_level + (int8_t)data[1]; continue;
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, xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES,
NULL); NULL);
// xTaskCreate(tx_task, "uart_tx_task", 1024 * 2, NULL, configMAX_PRIORITIES -
// 1,
// NULL);
} }

View File

@ -36,9 +36,25 @@ 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;
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) { void led_strip_set_brightness(uint8_t level) {
if (display_ambient_lighting_level == level) {
return;
}
display_ambient_lighting_level = level; 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);
} }
/** /**

View File

@ -44,7 +44,7 @@ void init_i2c() {
static const char *TAG = "DisplayAmbientLight"; static const char *TAG = "DisplayAmbientLight";
void app_main(void) { void app_main(void) {
// light_init_strip(); light_init_strip();
init_i2c(); init_i2c();
init_display(); init_display();
display_print8_str(0, 0, "Ambient Light"); display_print8_str(0, 0, "Ambient Light");
@ -70,6 +70,5 @@ void app_main(void) {
} }
while (waiting_and_get_colors()) { while (waiting_and_get_colors()) {
light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer); light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
vTaskDelay(pdMS_TO_TICKS(10));
} }
} }

View File

@ -22,14 +22,12 @@
#define TEMPERATURE_TAG "temperature" #define TEMPERATURE_TAG "temperature"
void fetch_temperature(void* arg) { void fetch_temperature(void* arg) {
ESP_LOGI(TEMPERATURE_TAG, "fetch_temperature");
esp_err_t error; esp_err_t error;
float temperature = DEFAULT_TEMPERATURE; float temperature = DEFAULT_TEMPERATURE;
char temperature_str[10]; char temperature_str[10];
uint8_t temperature_buffer[] = {0, 0}; uint8_t temperature_buffer[] = {0, 0};
display_fill_rect(0, 0, 128, 2, 0x00); display_fill_rect(0, 0, 128, 2, 0x00);
for (;;) { for (;;) {
ESP_LOGI(TEMPERATURE_TAG, "fetching temperature");
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_WRITE, i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_WRITE,