feat: 数据同步。

This commit is contained in:
Ivan Li 2022-11-22 20:08:11 +08:00
parent 3184a00bc6
commit cd16a29389
4 changed files with 83 additions and 9 deletions

22
.vscode/settings.json vendored
View File

@ -22,6 +22,26 @@
"*.inc": "c",
"*.tcc": "c",
"*.ipp": "c",
"cstring": "cpp"
"cstring": "cpp",
"system_error": "c",
"chrono": "c",
"random": "c",
"limits": "c",
"array": "c",
"string": "c",
"string_view": "c",
"esp_bit_defs.h": "c",
"bitset": "c",
"initializer_list": "c",
"regex": "c",
"utility": "c",
"deque": "c",
"list": "c",
"unordered_map": "c",
"unordered_set": "c",
"vector": "c",
"freertos.h": "c",
"task.h": "c",
"led_strip.h": "c"
}
}

View File

@ -17,7 +17,7 @@ static const char *LIGHT_TAG = "ScreenBgLight_Light";
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO 1
#define STRIP_LED_NUMBER 60
#define STRIP_LED_NUMBER CONFIG_NUMBER_OF_LEDS
#define EXAMPLE_CHASE_SPEED_MS (10)
typedef enum light_mode_e {
@ -194,8 +194,6 @@ void light_strip_running_task(void *pv_parameters) {
if (!light_led_strip) {
ESP_LOGE(LIGHT_TAG, "install WS2812 driver failed 2");
}
// Show simple rainbow chasing pattern
ESP_LOGI(LIGHT_TAG, "light_strip_running_task: %d", light_mode);
switch (light_mode) {
case light_mode_init:
light_for_init();
@ -242,7 +240,35 @@ void light_init_strip() {
1, NULL);
}
void light_play_colors(uint16_t len, uint8_t *buffer) {
light_mode = light_mode_desktop_sending_colors;
printf("COLORS=%.*s\r\n", len, buffer);
ESP_ERROR_CHECK(light_led_strip->clear(light_led_strip, 100));
for (uint16_t led_index = 0, buffer_cursor = 0;
led_index < STRIP_LED_NUMBER && buffer_cursor < len;
led_index++, buffer_cursor += 3) {
uint8_t r = buffer[buffer_cursor], g = buffer[buffer_cursor + 1],
b = buffer[buffer_cursor + 2];
ESP_LOGI(LIGHT_TAG, "RGB: %d %d %d", r, g, b);
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(10));
for (uint16_t led_index = 0, buffer_cursor = 0;
led_index < STRIP_LED_NUMBER && buffer_cursor < len;
led_index++, buffer_cursor += 3) {
uint8_t r = buffer[buffer_cursor], g = buffer[buffer_cursor + 1], b = 0;
ESP_LOGI(LIGHT_TAG, "RGB: %d %d %d", r, g, b);
ESP_ERROR_CHECK(
light_led_strip->set_pixel(light_led_strip, led_index, r, g, b));
}
ESP_ERROR_CHECK(light_led_strip->refresh(light_led_strip, 100));
vTaskDelay(pdMS_TO_TICKS(10));
}
void light_play(light_mode_t mode) {
ESP_LOGI(LIGHT_TAG, "light_play: %d", mode);
light_mode = mode;
}
}

View File

@ -23,7 +23,7 @@ void app_main(void) {
if (waiting_for_desktop_online()) {
light_play(light_mode_desktop_online);
}
if (waiting_for_desktop_sending_colors()) {
light_play(light_mode_desktop_sending_colors);
while (waiting_and_get_colors()) {
light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
}
}

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_bit_defs.h"
@ -13,6 +14,7 @@
#include "mqtt_client.h"
#define MQTT_BROKER_URL CONFIG_MQTT_BROKER_URL
#define NUMBER_OF_LEDS CONFIG_NUMBER_OF_LEDS
#define MQTT_FAILED_BIT BIT0
#define MQTT_CONNECTED_BIT BIT1
@ -20,6 +22,7 @@
#define MQTT_DESKTOP_ONLINE_BIT BIT3
#define MQTT_DESKTOP_OFFLINE_BIT BIT4
#define MQTT_DESKTOP_SENDING_BIT BIT5
#define MQTT_COLORS_STAND_BY_BIT BIT6
#define MQTT_BOARD_KEY_PREFIX "screen-bg-light/board/"
#define MQTT_DESKTOP_KEY_PREFIX "screen-bg-light/desktop/"
@ -35,7 +38,13 @@
static const char *MQTT_TAG = "ScreenBgLight_MQTT";
static EventGroupHandle_t s_mqtt_event_group;
static uint8_t *colors;
typedef struct colors {
uint8_t *buffer;
uint8_t number;
} s_colors_t;
static uint8_t *mqtt_colors_buffer;
static void log_error_if_nonzero(const char *message, int error_code) {
if (error_code != 0) {
@ -80,7 +89,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
xEventGroupSetBits(s_mqtt_event_group, MQTT_DESKTOP_ONLINE_BIT);
} else if (strncmp(event->topic, MQTT_KEY_DESKTOP_COLORS,
event->topic_len) == 0) {
xEventGroupSetBits(s_mqtt_event_group, MQTT_DESKTOP_SENDING_BIT);
printf("LEN=%d, DATA=%.*s\r\n", event->data_len, event->data_len,
event->data);
memcpy(mqtt_colors_buffer, event->data,
MIN(event->data_len, NUMBER_OF_LEDS * 3));
xEventGroupSetBits(s_mqtt_event_group,
MQTT_DESKTOP_SENDING_BIT | MQTT_COLORS_STAND_BY_BIT);
} else {
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
@ -107,6 +121,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
}
static void mqtt_app_start() {
mqtt_colors_buffer = (uint8_t *)malloc(sizeof(uint8_t) * NUMBER_OF_LEDS * 3);
s_mqtt_event_group = xEventGroupCreate();
const esp_mqtt_client_config_t mqtt_cfg = {
@ -160,4 +175,17 @@ static bool waiting_for_desktop_sending_colors() {
ESP_LOGE(MQTT_TAG, "UNEXPECTED EVENT");
return 0;
}
}
static bool waiting_and_get_colors() {
ESP_LOGI(MQTT_TAG, "aaa bits: %x", xEventGroupGetBits(s_mqtt_event_group));
EventBits_t bits =
xEventGroupWaitBits(s_mqtt_event_group, MQTT_COLORS_STAND_BY_BIT, pdFALSE,
pdFALSE, portMAX_DELAY);
if (bits & MQTT_COLORS_STAND_BY_BIT) {
xEventGroupClearBits(s_mqtt_event_group, MQTT_COLORS_STAND_BY_BIT);
return 1;
} else {
ESP_LOGE(MQTT_TAG, "UNEXPECTED EVENT");
return 0;
}
}