Compare commits

..

11 Commits

15 changed files with 3259 additions and 555 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ sdkconfig
sdkconfig.old sdkconfig.old
.vscode/ .vscode/
managed_components/ managed_components/
.DS_Store

Binary file not shown.

Binary file not shown.

View File

@ -15,3 +15,22 @@
| 起始位置 | 2 | 0~65535 | | 起始位置 | 2 | 0~65535 |
| 长度 | 2 | 0~65535 | | 长度 | 2 | 0~65535 |
| 颜色 | 3 | RGB 顺序,$2^3 * 2^3 * 2^3 = 65535$ 真彩色 | | 颜色 | 3 | RGB 顺序,$2^3 * 2^3 * 2^3 = 65535$ 真彩色 |
### 更新电脑显示器亮度 `3`
| 数据 | 长度(字节) | 说明 |
| --- | --- | --- |
| 显示器序号 | 1 | 0~255 |
| 亮度 | 1 | 0~255 |
### 更新电脑音量 `4`
| 数据 | 长度(字节) | 说明 |
| --- | --- | --- |
| 音量 | 1 | 0~255 |
### 更新灯带基本颜色校准 `5`
| 数据 | 长度(字节) | 说明 |
| --- | --- | --- |
| 颜色 | 3 | R、G、B 亮度上限 |

View File

@ -1,6 +1,5 @@
idf_component_register( idf_component_register(
SRCS SRCS
"udp_server.c"
"service_discovery.c" "service_discovery.c"
"app_nvs.c" "app_nvs.c"
"ch1116.c" "ch1116.c"
@ -12,13 +11,14 @@ idf_component_register(
"ui_input.c" "ui_input.c"
# "ambient_light.c" # "ambient_light.c"
# "temperature.c" # "temperature.c"
# "mqtt.c" #
"desktop.c"
"main.c" "main.c"
"wifi.c" "wifi.c"
"light.c" "light.c"
"led_strip_encoder/led_strip_encoder.c" "led_strip_encoder/led_strip_encoder.c"
"gui.c" "gui.c"
"lvgl_demo_ui.c" "net_gateway_monitor.c"
"app_icon_8.c" "app_icon_8.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
) )

View File

@ -35,7 +35,19 @@ static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = {
0xc0, 0xc0,
/* U+F6AB "" */ /* U+F6AB "" */
0x79, 0x8, 0x1, 0x80}; 0x79, 0x8, 0x1, 0x80,
/* U+F354 "" */
0x38, 0x28, 0x28, 0x28, 0xee, 0x82, 0x44, 0x28, 0x10,
/* U+F357 "" */
0x8, 0x14, 0x22, 0x41, 0x77, 0x14, 0x14, 0x14, 0x1c,
/* U+F354 "" */
0x31, 0x45, 0x37, 0x85, 0x23, 0x0,
/* U+F357 "" */
0x31, 0x28, 0x7b, 0x30, 0xc3, 0x0};
/*--------------------- /*---------------------
* GLYPH DESCRIPTION * GLYPH DESCRIPTION
@ -83,7 +95,21 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = {
.box_w = 7, .box_w = 7,
.box_h = 4, .box_h = 4,
.ofs_x = 2, .ofs_x = 2,
.ofs_y = 0}}; .ofs_y = 0},
{.bitmap_index = 48,
.adv_w = 96,
.box_w = 6,
.box_h = 7,
.ofs_x = 0,
.ofs_y = 0},
{.bitmap_index = 54,
.adv_w = 96,
.box_w = 6,
.box_h = 7,
.ofs_x = 0,
.ofs_y = -1}
};
/*--------------------- /*---------------------
* CHARACTER MAPPING * CHARACTER MAPPING

304
main/desktop.c Normal file
View File

@ -0,0 +1,304 @@
#include <lwip/netdb.h>
#include <string.h>
#include <sys/param.h>
#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "gui.c"
#include "light.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "nvs_flash.h"
#include "ui_input.c"
#define UDP_PORT 23042
static const char *UDP_SERVER_TAG = "UDP_SERVER";
static bool desktop_connected = false;
static uint64_t last_desktop_ping_at = 0;
static struct sockaddr *desktop_addr = NULL;
static int sock = -1;
static uint8_t tx_buffer[128];
typedef struct desktop_value {
uint8_t value;
uint8_t max;
uint8_t min;
} desktop_value_t;
static desktop_value_t display1_brightness = {
.value = 20,
.max = 100,
.min = 0,
};
static desktop_value_t display2_brightness = {
.value = 20,
.max = 100,
.min = 0,
};
static desktop_value_t computer_volume = {
.value = 20,
.max = 100,
.min = 0,
};
static void udp_server_task(void *pvParameters) {
char rx_buffer[1024];
char addr_str[128];
int addr_family = (int)pvParameters;
int ip_protocol = 0;
struct sockaddr_in dest_addr;
while (1) {
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(UDP_PORT);
ip_protocol = IPPROTO_IP;
sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Unable to create socket: errno %d. sock: %d",
errno, sock);
break;
}
ESP_LOGI(UDP_SERVER_TAG, "Socket created");
// Set timeout
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to bind: errno %d. sock: %d",
errno, sock);
sock = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(UDP_SERVER_TAG, "Socket bound, port %d", UDP_PORT);
struct sockaddr source_addr; // Large enough for both IPv4 or IPv6
socklen_t socklen = sizeof(source_addr);
while (1) {
// ESP_LOGI(UDP_SERVER_TAG, "Waiting for data");
int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0,
&source_addr, &socklen);
// Error occurred during receiving
if (len < 0) {
if (errno == EAGAIN) {
continue;
}
ESP_LOGE(UDP_SERVER_TAG, "recvfrom failed: errno %d. len: %d", errno,
len);
}
// Data received
else {
// Get the sender's ip address as string
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str,
sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat
// like a string...
switch (rx_buffer[0]) {
case 1:
last_desktop_ping_at = esp_timer_get_time();
ESP_LOGD(UDP_SERVER_TAG, "Received ping from %s", addr_str);
sendto(sock, rx_buffer, 1, 0, &source_addr, sizeof(source_addr));
desktop_addr = &source_addr;
break;
case 2:
set_display_ambient_light_colors(
((uint16_t)rx_buffer[1] << 8 | (uint16_t)rx_buffer[2]),
(uint8_t *)&(rx_buffer[3]), len - 3);
break;
case 3: // computer brightness changed
if (rx_buffer[1] == 0) {
display1_brightness.value = rx_buffer[2];
} else if (rx_buffer[1] == 1) {
display2_brightness.value = rx_buffer[2];
}
break;
case 4: // computer volume changed
computer_volume.value = rx_buffer[1];
break;
case 5: // color correction changed
ESP_LOGD(UDP_SERVER_TAG, "Color correction changed");
led_strip_set_color_calibration((float)rx_buffer[1] / 255,
(float)rx_buffer[2] / 255,
(float)rx_buffer[3] / 255);
break;
default:
ESP_LOG_BUFFER_HEXDUMP(UDP_SERVER_TAG, rx_buffer, len,
ESP_LOG_INFO);
break;
}
ESP_LOGD(UDP_SERVER_TAG, "Received %d bytes from %s:", len, addr_str);
}
if (sock != -1 && len < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Shutting down socket and restarting...");
shutdown(sock, 0);
close(sock);
break;
}
}
}
vTaskDelete(NULL);
}
static uint8_t desktop_change_value(desktop_value_t *target, int8_t delta) {
if (delta > 0) {
if ((target->max - target->value) > delta) {
target->value += delta;
} else {
target->value = target->max;
}
} else {
if ((target->value - target->min) > -delta) {
target->value += delta;
} else {
target->value = target->min;
}
}
return target->value;
}
static uint8_t change_display_brightness(uint8_t display_index, int8_t delta) {
if (desktop_addr == NULL) {
ESP_LOGW(UDP_SERVER_TAG, "No desktop connected");
return 0;
}
switch (display_index) {
case 0: {
uint8_t value = desktop_change_value(&display1_brightness, delta);
tx_buffer[0] = 3;
tx_buffer[1] = 0;
tx_buffer[2] = value;
int err =
sendto(sock, tx_buffer, 3, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d",
errno, sock);
}
gui_change_display_brightness(display_index, value);
return value;
}
case 1: {
uint8_t value = desktop_change_value(&display1_brightness, delta);
tx_buffer[0] = 3;
tx_buffer[1] = 1;
tx_buffer[2] = value;
int err =
sendto(sock, tx_buffer, 3, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d",
errno, sock);
}
gui_change_display_brightness(display_index, value);
return value;
}
default:
ESP_LOGE(UDP_SERVER_TAG, "Invalid display index: %d", display_index);
return 0;
}
}
static uint8_t change_volume(int8_t delta) {
if (desktop_addr == NULL) {
ESP_LOGW(UDP_SERVER_TAG, "No desktop connected");
return 0;
}
uint8_t value = desktop_change_value(&computer_volume, delta);
tx_buffer[0] = 4;
tx_buffer[1] = value;
int err = sendto(sock, tx_buffer, 2, 0, desktop_addr, sizeof(*desktop_addr));
if (err < 0) {
char addr_str[128];
inet_ntoa_r(((struct sockaddr_in *)desktop_addr)->sin_addr, addr_str,
sizeof(addr_str) - 1);
ESP_LOGI(UDP_SERVER_TAG, "addr: %s", addr_str);
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to send: errno %d. sock: %d", errno,
sock);
}
gui_change_volume_level(value);
return value;
}
static void desktop_watch_input_task(void *arg) {
s_ui_input_t input;
for (;;) {
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
switch (input.key) {
case ui_input_key_display_0_brightness:
change_display_brightness(0, input.value);
break;
case ui_input_key_display_1_brightness:
change_display_brightness(1, input.value);
break;
case ui_input_key_computer_volume:
change_volume(input.value);
break;
default:
break;
}
}
}
}
static void change_desktop_connection_status(bool connected) {
desktop_connected = connected;
if (connected) {
gui_set_server_connected();
} else {
gui_set_server_disconnected();
}
}
static void desktop_online_check_task(void *pvParameters) {
while (1) {
if (esp_timer_get_time() - last_desktop_ping_at > 5000000) { // 2 seconds
change_desktop_connection_status(false);
} else {
change_desktop_connection_status(true);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void udp_server_init(void) {
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
xTaskCreate(desktop_online_check_task, "desktop_online_check", 1024, NULL, 10,
NULL);
xTaskCreate(desktop_watch_input_task, "desktop_watch_input", 4096, NULL, 7,
NULL);
}

2461
main/fonts/app_10.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,7 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "fonts/app_10.c"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "lvgl.h" #include "lvgl.h"
@ -37,6 +38,12 @@ static const char *GUI_TAG = "LVGL_GUI";
// EE 8A 9E // EE 8A 9E
#define APP_TIMER_SYMBOL "\xEE\x8A\x9E" #define APP_TIMER_SYMBOL "\xEE\x8A\x9E"
// EF 8D 97
#define APP_UP_SYMBOL "\xEF\x8D\x97"
// EF 8D 94
#define APP_DOWN_SYMBOL "\xEF\x8D\x94"
#define GUI_PANEL_TIMEOUT_US 3000000 // 3s
extern void example_lvgl_demo_ui(lv_disp_t *disp); extern void example_lvgl_demo_ui(lv_disp_t *disp);
@ -64,6 +71,14 @@ static void example_lvgl_set_px_cb(lv_disp_drv_t *disp_drv, uint8_t *buf,
} }
} }
typedef struct value_setting_panel {
lv_obj_t *panel;
lv_obj_t *bar;
lv_obj_t *label;
lv_obj_t *value_label;
int64_t last_updated_at;
} value_setting_panel_t;
static lv_obj_t *wifi_label; static lv_obj_t *wifi_label;
static lv_anim_t wifi_animate; static lv_anim_t wifi_animate;
@ -73,13 +88,21 @@ static lv_anim_t timer_animate;
static lv_obj_t *desktop_label; static lv_obj_t *desktop_label;
static lv_anim_t desktop_animate; static lv_anim_t desktop_animate;
static lv_obj_t *value_setting_panel; value_setting_panel_t *value_setting_panel = NULL;
static lv_obj_t *value_setting_bar; static bool panel_locked = false;
static lv_obj_t *value_setting_label;
static lv_obj_t *value_setting_value_label;
static lv_obj_t *scr; static lv_obj_t *scr;
typedef struct gui_network_speed_panel_s {
lv_obj_t *panel;
lv_obj_t *direct_upload_label;
lv_obj_t *direct_download_label;
lv_obj_t *proxy_upload_label;
lv_obj_t *proxy_download_label;
} gui_network_speed_panel_t;
static gui_network_speed_panel_t *gui_network_speed_panel = NULL;
static void example_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area) { static void example_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area) {
area->y1 = area->y1 & (~0x7); area->y1 = area->y1 & (~0x7);
area->y2 = area->y2 | 0x7; area->y2 = area->y2 | 0x7;
@ -147,7 +170,9 @@ static void gui_set_wifi_connecting() {
static void gui_set_wifi_connected() { static void gui_set_wifi_connected() {
lv_anim_del(wifi_label, NULL); lv_anim_del(wifi_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS); lv_obj_clear_flag(wifi_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(wifi_label, APP_WIFI_GOOD_SYMBOL);
vTaskDelay(100 / portTICK_PERIOD_MS);
lv_obj_clear_flag(wifi_label, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(wifi_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(wifi_label, APP_WIFI_GOOD_SYMBOL); lv_label_set_text(wifi_label, APP_WIFI_GOOD_SYMBOL);
} }
@ -165,7 +190,9 @@ static void gui_set_server_connecting() {
static void gui_set_server_connected() { static void gui_set_server_connected() {
lv_anim_del(desktop_label, NULL); lv_anim_del(desktop_label, NULL);
vTaskDelay(300 / portTICK_PERIOD_MS); lv_obj_clear_flag(desktop_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(desktop_label, APP_CONNECTED_SYMBOL);
vTaskDelay(100 / portTICK_PERIOD_MS);
lv_obj_clear_flag(desktop_label, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_flag(desktop_label, LV_OBJ_FLAG_HIDDEN);
lv_label_set_text(desktop_label, APP_CONNECTED_SYMBOL); lv_label_set_text(desktop_label, APP_CONNECTED_SYMBOL);
} }
@ -178,6 +205,12 @@ static void gui_set_server_disconnected() {
} }
static void gui_bar_value_update_cb(lv_event_t *e) { static void gui_bar_value_update_cb(lv_event_t *e) {
if (value_setting_panel == NULL) {
ESP_LOGW(GUI_TAG, "value_setting_panel is NULL");
return;
}
lv_obj_t *value_label = value_setting_panel->value_label;
lv_obj_draw_part_dsc_t *dsc = lv_event_get_param(e); lv_obj_draw_part_dsc_t *dsc = lv_event_get_param(e);
if (dsc->part != LV_PART_INDICATOR) return; if (dsc->part != LV_PART_INDICATOR) return;
@ -198,101 +231,250 @@ static void gui_bar_value_update_cb(lv_event_t *e) {
/*If the indicator is long enough put the text inside on the right*/ /*If the indicator is long enough put the text inside on the right*/
if (lv_area_get_width(dsc->draw_area) > txt_size.x + 20) { if (lv_area_get_width(dsc->draw_area) > txt_size.x + 20) {
txt_x = dsc->draw_area->x2 - 8 - txt_size.x + 1; txt_x = dsc->draw_area->x2 - 8 - txt_size.x + 1;
lv_obj_set_style_text_color(value_setting_value_label, lv_color_white(), lv_obj_set_style_text_color(value_label, lv_color_white(), LV_PART_MAIN);
LV_PART_MAIN);
label_dsc.color = lv_color_white(); label_dsc.color = lv_color_white();
} }
/*If the indicator is still short put the text out of it on the right*/ /*If the indicator is still short put the text out of it on the right*/
else { else {
txt_x = dsc->draw_area->x2 - 8 + txt_size.x - 1; txt_x = dsc->draw_area->x2 - 8 + txt_size.x - 1;
lv_obj_set_style_text_color(value_setting_value_label, lv_color_black(), lv_obj_set_style_text_color(value_label, lv_color_black(), LV_PART_MAIN);
LV_PART_MAIN);
} }
lv_obj_align(value_setting_value_label, LV_ALIGN_LEFT_MID, txt_x, 0); lv_obj_align(value_label, LV_ALIGN_LEFT_MID, txt_x, 0);
lv_label_set_text(value_setting_value_label, buf); lv_label_set_text(value_label, buf);
lv_obj_set_width(value_setting_value_label, txt_size.x); lv_obj_set_width(value_label, txt_size.x);
}
static void gui_close_value_setting_panel() {
if (value_setting_panel == NULL) return;
lv_obj_del(value_setting_panel->panel);
free(value_setting_panel);
value_setting_panel = NULL;
}
static void gui_check_value_setting_panel_task(void *pvParameters) {
vTaskDelay(GUI_PANEL_TIMEOUT_US / 1000 / portTICK_PERIOD_MS);
for (;;) {
if (!panel_locked &&
esp_timer_get_time() - value_setting_panel->last_updated_at >
GUI_PANEL_TIMEOUT_US) {
gui_close_value_setting_panel();
vTaskDelete(NULL);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} }
static void gui_create_value_setting_panel() { static void gui_create_value_setting_panel() {
if (value_setting_panel != NULL) { if (value_setting_panel != NULL) {
value_setting_panel->last_updated_at = esp_timer_get_time();
return; return;
} }
value_setting_panel =
(value_setting_panel_t *)malloc(sizeof(value_setting_panel_t));
value_setting_panel = lv_obj_create(scr); xTaskCreate(gui_check_value_setting_panel_task, "G_CHK_VAL_SET", 1024, NULL,
lv_obj_set_size(value_setting_panel, 128, 40); configMAX_PRIORITIES - 1, NULL);
lv_obj_align(value_setting_panel, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_style_border_width(value_setting_panel, 1, LV_PART_MAIN); value_setting_panel->panel = lv_obj_create(scr);
lv_obj_set_style_border_color(value_setting_panel, lv_color_black(), lv_obj_set_size(value_setting_panel->panel, 128, 40);
lv_obj_align(value_setting_panel->panel, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_style_border_width(value_setting_panel->panel, 1, LV_PART_MAIN);
lv_obj_set_style_border_color(value_setting_panel->panel, lv_color_black(),
LV_PART_MAIN); LV_PART_MAIN);
lv_obj_set_style_radius(value_setting_panel, 5, LV_PART_MAIN); lv_obj_set_style_radius(value_setting_panel->panel, 5, LV_PART_MAIN);
lv_obj_set_style_pad_all(value_setting_panel, 2, LV_PART_MAIN); lv_obj_set_style_pad_all(value_setting_panel->panel, 2, LV_PART_MAIN);
value_setting_label = lv_label_create(value_setting_panel); value_setting_panel->label = lv_label_create(value_setting_panel->panel);
lv_obj_align(value_setting_label, LV_ALIGN_BOTTOM_LEFT, 0, 0); lv_obj_align(value_setting_panel->label, LV_ALIGN_BOTTOM_LEFT, 0, 0);
value_setting_bar = lv_bar_create(value_setting_panel); value_setting_panel->bar = lv_bar_create(value_setting_panel->panel);
lv_obj_set_size(value_setting_bar, 120, 12); lv_obj_set_size(value_setting_panel->bar, 120, 12);
lv_obj_align(value_setting_bar, LV_ALIGN_TOP_MID, 0, 0); lv_obj_align(value_setting_panel->bar, LV_ALIGN_TOP_MID, 0, 0);
lv_bar_set_range(value_setting_bar, 0, 100); lv_bar_set_range(value_setting_panel->bar, 0, 100);
lv_bar_set_value(value_setting_bar, 50, LV_ANIM_ON); lv_bar_set_value(value_setting_panel->bar, 50, LV_ANIM_ON);
lv_obj_set_style_bg_color(value_setting_bar, lv_color_white(), LV_PART_MAIN); lv_obj_set_style_bg_color(value_setting_panel->bar, lv_color_white(),
lv_obj_set_style_border_color(value_setting_bar, lv_color_black(),
LV_PART_MAIN); LV_PART_MAIN);
lv_obj_set_style_border_width(value_setting_bar, 1, LV_PART_MAIN); lv_obj_set_style_border_color(value_setting_panel->bar, lv_color_black(),
lv_obj_set_style_radius(value_setting_bar, 5, LV_PART_MAIN); LV_PART_MAIN);
lv_obj_set_style_pad_hor(value_setting_bar, 0, LV_PART_MAIN); lv_obj_set_style_border_width(value_setting_panel->bar, 1, LV_PART_MAIN);
lv_obj_set_style_pad_ver(value_setting_bar, 2, LV_PART_MAIN); lv_obj_set_style_radius(value_setting_panel->bar, 5, LV_PART_MAIN);
lv_obj_set_style_bg_color(value_setting_bar, lv_color_black(), lv_obj_set_style_pad_hor(value_setting_panel->bar, 0, LV_PART_MAIN);
lv_obj_set_style_pad_ver(value_setting_panel->bar, 2, LV_PART_MAIN);
lv_obj_set_style_bg_color(value_setting_panel->bar, lv_color_black(),
LV_PART_INDICATOR); LV_PART_INDICATOR);
lv_obj_add_event_cb(value_setting_bar, gui_bar_value_update_cb, lv_obj_add_event_cb(value_setting_panel->bar, gui_bar_value_update_cb,
LV_EVENT_DRAW_PART_END, NULL); LV_EVENT_DRAW_PART_END, NULL);
value_setting_value_label = lv_label_create(value_setting_bar); value_setting_panel->value_label = lv_label_create(value_setting_panel->bar);
lv_obj_align(value_setting_value_label, LV_ALIGN_CENTER, 0, 0); lv_obj_align(value_setting_panel->value_label, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style_text_font(value_setting_value_label, &lv_font_montserrat_10, lv_obj_set_style_text_font(value_setting_panel->value_label,
LV_PART_MAIN); &lv_font_montserrat_10, LV_PART_MAIN);
} }
static void gui_change_strip_level(int32_t target_value) { static void gui_change_strip_level(int32_t target_value) {
panel_locked = true;
gui_create_value_setting_panel(); gui_create_value_setting_panel();
lv_label_set_text(value_setting_label, "Strip Level"); lv_label_set_text(value_setting_panel->label, "Strip Level");
lv_bar_set_range(value_setting_bar, 0, 255); lv_bar_set_range(value_setting_panel->bar, 0, 255);
lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON); lv_bar_set_value(value_setting_panel->bar, target_value, LV_ANIM_ON);
panel_locked = false;
} }
void lv_example_bar_6(lv_obj_t *src) { static void gui_change_display_brightness(int8_t display_index,
static lv_style_t style_bar; int32_t target_value) {
lv_style_init(&style_bar); panel_locked = true;
gui_create_value_setting_panel();
lv_label_set_text_fmt(value_setting_panel->label, "Display#%d Brightness",
display_index);
lv_bar_set_range(value_setting_panel->bar, 0, 100);
lv_bar_set_value(value_setting_panel->bar, target_value, LV_ANIM_ON);
panel_locked = false;
}
lv_style_set_bg_color(&style_bar, lv_color_white()); static void gui_change_volume_level(int32_t target_value) {
lv_style_set_border_width(&style_bar, 1); panel_locked = true;
lv_style_set_border_color(&style_bar, lv_color_black()); gui_create_value_setting_panel();
lv_style_set_pad_hor(&style_bar, 4); lv_label_set_text(value_setting_panel->label, "Volume Level");
lv_style_set_pad_ver(&style_bar, 2); lv_bar_set_range(value_setting_panel->bar, 0, 100);
lv_bar_set_value(value_setting_panel->bar, target_value, LV_ANIM_ON);
panel_locked = false;
}
static lv_style_t style_indic; static void gui_network_speed_human_readable(uint64_t *speed, char *str) {
lv_style_init(&style_indic); if (*speed > 1024 * 1024 * 1024) {
float v = (float)*speed / 1024.0f / 1024.0f / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f GB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f GB/s", v);
} else {
sprintf(str, "% 5.2f GB/s", v);
}
} else if (*speed > 1024 * 1024) {
float v = (float)*speed / 1024.0f / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f MB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f MB/s", v);
} else {
sprintf(str, "% 5.2f MB/s", v);
}
} else if (*speed > 1024) {
float v = (float)*speed / 1024.0f;
if (v >= 1000) {
sprintf(str, "% 5.0f KB/s", v);
} else if (v >= 100) {
sprintf(str, "% 5.1f KB/s", v);
} else {
sprintf(str, "% 5.2f KB/s", v);
}
} else {
sprintf(str, "% 5d B/s", (uint8_t)*speed);
}
}
lv_style_set_bg_color(&style_indic, lv_color_black()); static void gui_update_network_label_style(lv_obj_t *label, uint64_t *speed) {
if (*speed > 1024 * 1024 * 50 / 8) { // 50 Mbps
lv_obj_set_style_bg_color(label, lv_color_black(), LV_PART_MAIN);
lv_obj_set_style_text_color(label, lv_color_white(), LV_PART_MAIN);
} else {
lv_obj_set_style_bg_color(label, lv_color_white(), LV_PART_MAIN);
lv_obj_set_style_text_color(label, lv_color_black(), LV_PART_MAIN);
}
}
lv_obj_t *bar = lv_bar_create(src); static void gui_change_network_speed(uint64_t *direct_upload_speed,
lv_obj_set_size(bar, 100, 10); uint64_t *direct_download_speed,
lv_obj_center(bar); uint64_t *proxy_upload_speed,
lv_obj_add_style(bar, &style_bar, LV_PART_MAIN); uint64_t *proxy_download_speed) {
lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR); if (gui_network_speed_panel == NULL) {
lv_bar_set_range(bar, 0, 100); return;
lv_bar_set_value(bar, 0, LV_ANIM_ON); }
static char str[32];
lv_anim_t a; strcpy(str, APP_UP_SYMBOL);
lv_anim_init(&a); gui_network_speed_human_readable(direct_upload_speed, &str[3]);
lv_anim_set_exec_cb(&a, set_value); lv_label_set_text(gui_network_speed_panel->direct_upload_label, str);
lv_anim_set_time(&a, 3000); gui_network_speed_human_readable(proxy_upload_speed, &str[3]);
lv_anim_set_playback_time(&a, 3000); lv_label_set_text(gui_network_speed_panel->proxy_upload_label, str);
lv_anim_set_var(&a, bar);
lv_anim_set_values(&a, 0, 50); strcpy(str, APP_DOWN_SYMBOL);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); gui_network_speed_human_readable(direct_download_speed, &str[3]);
lv_anim_start(&a); lv_label_set_text(gui_network_speed_panel->direct_download_label, str);
gui_network_speed_human_readable(proxy_download_speed, &str[3]);
lv_label_set_text(gui_network_speed_panel->proxy_download_label, str);
gui_update_network_label_style(gui_network_speed_panel->direct_upload_label,
direct_upload_speed);
gui_update_network_label_style(gui_network_speed_panel->direct_download_label,
direct_download_speed);
gui_update_network_label_style(gui_network_speed_panel->proxy_upload_label,
proxy_upload_speed);
gui_update_network_label_style(gui_network_speed_panel->proxy_download_label,
proxy_download_speed);
}
static void gui_init_network_speed(lv_obj_t *scr) {
gui_network_speed_panel =
(gui_network_speed_panel_t *)malloc(sizeof(gui_network_speed_panel_t));
gui_network_speed_panel->panel = lv_obj_create(scr);
lv_obj_set_size(gui_network_speed_panel->panel, 128, 40);
lv_obj_align(gui_network_speed_panel->panel, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_style_bg_color(gui_network_speed_panel->panel, lv_color_white(),
LV_PART_MAIN);
lv_obj_set_style_border_color(gui_network_speed_panel->panel,
lv_color_black(), LV_PART_MAIN);
lv_obj_set_style_border_width(gui_network_speed_panel->panel, 1,
LV_PART_MAIN);
lv_obj_set_style_radius(gui_network_speed_panel->panel, 5, LV_PART_MAIN);
lv_obj_set_style_pad_hor(gui_network_speed_panel->panel, 1, LV_PART_MAIN);
lv_obj_set_style_pad_ver(gui_network_speed_panel->panel, 1, LV_PART_MAIN);
gui_network_speed_panel->direct_upload_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->direct_upload_label, LV_ALIGN_TOP_LEFT,
0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->direct_upload_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->direct_upload_label,
APP_UP_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->direct_upload_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->direct_download_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->direct_download_label,
LV_ALIGN_BOTTOM_LEFT, 0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->direct_download_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->direct_download_label,
APP_DOWN_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->direct_download_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->proxy_upload_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->proxy_upload_label, LV_ALIGN_TOP_RIGHT,
0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->proxy_upload_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->proxy_upload_label,
APP_UP_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->proxy_upload_label,
LV_OPA_COVER, LV_PART_MAIN);
gui_network_speed_panel->proxy_download_label =
lv_label_create(gui_network_speed_panel->panel);
lv_obj_align(gui_network_speed_panel->proxy_download_label,
LV_ALIGN_BOTTOM_RIGHT, 0, 0);
lv_obj_set_style_text_font(gui_network_speed_panel->proxy_download_label,
&app_10, LV_PART_MAIN);
lv_label_set_text(gui_network_speed_panel->proxy_download_label,
APP_DOWN_SYMBOL " 0.00 KB/s");
lv_obj_set_style_bg_opa(gui_network_speed_panel->proxy_download_label,
LV_OPA_COVER, LV_PART_MAIN);
} }
void gui_status_bar_create(lv_obj_t *scr) { void gui_status_bar_create(lv_obj_t *scr) {
@ -334,15 +516,12 @@ void example_lvgl_demo_ui(lv_disp_t *disp) {
lv_label_set_long_mode(label, lv_label_set_long_mode(label,
LV_LABEL_LONG_SCROLL_CIRCULAR); /* Circular scroll LV_LABEL_LONG_SCROLL_CIRCULAR); /* Circular scroll
*/ */
lv_label_set_text(label, "Hello Espressif, Hello LVGL."); lv_label_set_text(label, "Hello Ivan Li!");
lv_obj_set_width(label, 120); lv_obj_set_width(label, 120);
lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, 0, 0); lv_obj_align(label, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
lv_example_bar_6(scr);
gui_status_bar_create(scr); gui_status_bar_create(scr);
gui_init_network_speed(scr);
gui_create_value_setting_panel();
} }
static void gui_tick(void *pvParameters) { static void gui_tick(void *pvParameters) {

View File

@ -1,19 +0,0 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include "lvgl.h"
void example_lvgl_demo_ui(lv_disp_t *disp) {
lv_obj_t *scr = lv_disp_get_scr_act(disp);
lv_obj_t *label = lv_label_create(scr);
lv_label_set_long_mode(label,
LV_LABEL_LONG_SCROLL_CIRCULAR); /* Circular scroll
*/
lv_label_set_text(label, "Hello Espressif, Hello LVGL.");
lv_obj_set_width(label, 120);
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);
}

View File

@ -13,7 +13,8 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "service_discovery.c" #include "service_discovery.c"
// #include "temperature.c" // #include "temperature.c"
#include "udp_server.c" #include "desktop.c"
#include "net_gateway_monitor.c"
#include "ui_input.c" #include "ui_input.c"
#include "wifi.c" #include "wifi.c"
@ -35,7 +36,7 @@ void app_main(void) {
// display_print8_str(0, 0, "Ambient Light"); // display_print8_str(0, 0, "Ambient Light");
// ci_03t_init(); ci_03t_init();
// apds_9960_init(); // apds_9960_init();
// apds_9960_auto_fetch(); // apds_9960_auto_fetch();
// auto_fetch_temperature(); // auto_fetch_temperature();
@ -52,6 +53,7 @@ void app_main(void) {
gui_set_wifi_disconnected(); gui_set_wifi_disconnected();
} }
udp_server_init(); udp_server_init();
net_gateway_monitor_init();
service_discovery_init(); service_discovery_init();
// vTaskDelay(pdMS_TO_TICKS(1000)); // vTaskDelay(pdMS_TO_TICKS(1000));
// mqtt_app_start(); // mqtt_app_start();

View File

@ -1,287 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "ci_03t.c"
#include "esp_bit_defs.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "mqtt_client.h"
#include "ui_input.c"
#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
#define MQTT_DISCONNECTED_BIT BIT2
#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 "display-ambient-light/board/"
#define MQTT_DESKTOP_KEY_PREFIX "display-ambient-light/desktop/"
#define MQTT_ONLINE_SUFFIX "online"
#define MQTT_COLORS_SUFFIX "colors"
#define MQTT_CMD_SUFFIX "cmd"
#define MQTT_ALL_SUFFIX "#"
#define MQTT_KEY_BOARD_ONLINE MQTT_BOARD_KEY_PREFIX MQTT_ONLINE_SUFFIX
#define MQTT_KEY_DISPLAY_BRIGHTNESS_INPUT MQTT_BOARD_KEY_PREFIX "brightness"
#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_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_DESKTOP_DISPLAY_0_BRIGHTNESS \
MQTT_DESKTOP_KEY_PREFIX "display0/brightness"
#define MQTT_KEY_DESKTOP_DISPLAY_1_BRIGHTNESS \
MQTT_DESKTOP_KEY_PREFIX "display1/brightness"
static const char *MQTT_TAG = "DisplayAmbientLight_MQTT";
static EventGroupHandle_t s_mqtt_event_group;
static QueueHandle_t mqtt_cmd_event = NULL;
static esp_mqtt_client_handle_t client = NULL;
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) {
ESP_LOGE(MQTT_TAG, "Last error %s: 0x%x", message, error_code);
}
}
static void mqtt_event_handler(void *handler_args, esp_event_base_t base,
long event_id, void *event_data) {
ESP_LOGD(MQTT_TAG, "Event dispatched from event loop base=%s, event_id=%ld",
base, event_id);
esp_mqtt_event_handle_t event = event_data;
client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_CONNECTED");
xEventGroupSetBits(s_mqtt_event_group, MQTT_CONNECTED_BIT);
msg_id = esp_mqtt_client_publish(client, MQTT_KEY_BOARD_ONLINE, "ONLINE",
0, 1, 0);
ESP_LOGI(MQTT_TAG, "sent publish successful, msg_id=%d", msg_id);
esp_mqtt_client_subscribe(client, MQTT_KEY_DESKTOP_ALL, 1);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DISCONNECTED");
xEventGroupSetBits(s_mqtt_event_group, MQTT_DISCONNECTED_BIT);
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
// ESP_LOGI(MQTT_TAG, "MQTT_EVENT_DATA");
if (strncmp(event->topic, MQTT_KEY_DESKTOP_ONLINE, event->topic_len) ==
0) {
xEventGroupSetBits(s_mqtt_event_group, MQTT_DESKTOP_ONLINE_BIT);
} else if (strncmp(event->topic, MQTT_KEY_DESKTOP_COLORS,
event->topic_len) == 0) {
// 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 {
if (strncmp(event->topic, MQTT_KEY_DESKTOP_DISPLAY_0_BRIGHTNESS,
event->topic_len) == 0) {
s_ui_input_t mqtt_event = {
.key = ui_input_key_display_0_brightness,
.value = (uint16_t)(event->data[0] << 8 | event->data[1]),
};
xQueueSend(mqtt_cmd_event, &mqtt_event, NULL);
} else if (strncmp(event->topic, MQTT_KEY_DESKTOP_DISPLAY_1_BRIGHTNESS,
event->topic_len) == 0) {
s_ui_input_t mqtt_event = {
.key = ui_input_key_display_1_brightness,
.value = (uint16_t)(event->data[0] << 8 | event->data[1]),
};
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 {
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
}
}
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(MQTT_TAG, "MQTT_EVENT_ERROR");
xEventGroupSetBits(s_mqtt_event_group, MQTT_FAILED_BIT);
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls",
event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack",
event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno",
event->error_handle->esp_transport_sock_errno);
ESP_LOGI(MQTT_TAG, "Last errno string (%s)",
strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(MQTT_TAG, "Other event id:%d", event->event_id);
break;
}
}
static void mqtt_cmd_event_handler(void *arg) {
s_ui_input_t event;
for (;;) {
if (xQueueReceive(mqtt_cmd_event, &event, portMAX_DELAY)) {
ESP_LOGI(MQTT_TAG, "mqtt_cmd_event_handler");
gui_update_config_uint8(ui_input_key_display_0_brightness,
(uint8_t)(event.value & 0xFF));
switch (event.key) {
case ui_input_key_display_0_brightness: {
ESP_LOGI(MQTT_TAG, "ui_input_key_display_0_brightness %x",
event.value);
uint8_t data[] = {0x02, (uint8_t)(event.value & 0xFF)};
ci_03t_send_data(data, sizeof(data));
break;
}
case ui_input_key_display_1_brightness: {
ESP_LOGI(MQTT_TAG, "ui_input_key_display_0_brightness %x",
event.value);
uint8_t data[] = {0x03, (uint8_t)(event.value & 0xFF)};
ci_03t_send_data(data, sizeof(data));
break;
}
default:
break;
}
}
}
}
static void mqtt_app_start() {
mqtt_colors_buffer = (uint8_t *)malloc(sizeof(uint8_t) * NUMBER_OF_LEDS * 3);
s_mqtt_event_group = xEventGroupCreate();
mqtt_cmd_event = xQueueCreate(10, sizeof(s_ui_input_t));
xTaskCreate(mqtt_cmd_event_handler, "mqtt_cmd_event", 2048, NULL, 10, NULL);
const esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = MQTT_BROKER_URL,
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler,
client);
esp_mqtt_client_start(client);
}
static bool waiting_for_mqtt_connected() {
EventBits_t bits = xEventGroupWaitBits(s_mqtt_event_group,
MQTT_CONNECTED_BIT | MQTT_FAILED_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & MQTT_CONNECTED_BIT) {
return 1;
} else if (bits & MQTT_FAILED_BIT) {
ESP_LOGE(MQTT_TAG, "BBB Failed to connect to MQTT.");
return 0;
} else {
ESP_LOGE(MQTT_TAG, "UNEXPECTED EVENT");
return 0;
}
}
static bool waiting_for_desktop_online() {
EventBits_t bits = xEventGroupWaitBits(
s_mqtt_event_group, MQTT_DESKTOP_ONLINE_BIT | MQTT_DESKTOP_SENDING_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & MQTT_DESKTOP_ONLINE_BIT) {
return 1;
} else if (bits & MQTT_DESKTOP_SENDING_BIT) {
return 1;
} else {
ESP_LOGE(MQTT_TAG, "UNEXPECTED EVENT");
return 0;
}
}
static bool waiting_for_desktop_sending_colors() {
EventBits_t bits =
xEventGroupWaitBits(s_mqtt_event_group, MQTT_DESKTOP_SENDING_BIT, pdFALSE,
pdFALSE, portMAX_DELAY);
ESP_LOGE(MQTT_TAG, "MQTT_DESKTOP_SENDING_BIT");
if (bits & MQTT_DESKTOP_SENDING_BIT) {
return 1;
} else {
ESP_LOGE(MQTT_TAG, "UNEXPECTED EVENT");
return 0;
}
}
static bool waiting_and_get_colors() {
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;
}
}
static void mqtt_publish_ui_input(void *arg) {
s_ui_input_t input;
for (;;) {
if (xQueueReceive(ui_input_event, &input, portMAX_DELAY)) {
switch (input.key) {
case ui_input_key_display_0_brightness:
case ui_input_key_display_1_brightness: {
cJSON *publish_json_root, *brightness;
publish_json_root = cJSON_CreateObject();
cJSON_AddNumberToObject(
publish_json_root, "display_index",
ui_input_key_display_0_brightness == input.key ? 0 : 1);
cJSON_AddItemToObject(publish_json_root, "brightness",
brightness = cJSON_CreateObject());
cJSON_AddNumberToObject(brightness, "Relative", input.value);
char *publish_str = cJSON_Print(publish_json_root);
cJSON_Delete(publish_json_root);
esp_mqtt_client_publish(client, MQTT_KEY_DISPLAY_BRIGHTNESS_INPUT,
publish_str, 0, 1, 0);
} break;
default:
break;
}
}
}
}

147
main/net_gateway_monitor.c Normal file
View File

@ -0,0 +1,147 @@
#include <lwip/netdb.h>
#include <string.h>
#include <sys/param.h>
#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "gui.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#define NET_GATEWAY_MONITOR_UDP_PORT 23043
#define NET_GATEWAY_MONITOR_SERVER_ADDR 0xC0A81F05 // 192.168.31.5
#define NET_GATEWAY_MONITOR_SERVER_PORT 17890
static const char *NET_GATEWAY_MONITOR_TAG = "NET_GATE_MONITOR";
static int net_gateway_monitor_socket = -1;
static void net_gateway_monitor_request_task(void *pvParameters) {
int sock = net_gateway_monitor_socket;
struct sockaddr_in server_addr;
server_addr.sin_addr.s_addr = htonl(NET_GATEWAY_MONITOR_SERVER_ADDR);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(NET_GATEWAY_MONITOR_SERVER_PORT);
while (sock == net_gateway_monitor_socket) {
int err = sendto(net_gateway_monitor_socket, "ping", 4, 0, &server_addr,
sizeof(server_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Error occurred during sending: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
static void net_gateway_monitor_run_task(void *pvParameters) {
char rx_buffer[1024];
char addr_str[128];
int addr_family = (int)pvParameters;
int ip_protocol = 0;
struct sockaddr_in dest_addr;
struct sockaddr_in server_addr;
while (1) {
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(NET_GATEWAY_MONITOR_UDP_PORT);
ip_protocol = IPPROTO_IP;
server_addr.sin_addr.s_addr = htonl(NET_GATEWAY_MONITOR_SERVER_ADDR);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(NET_GATEWAY_MONITOR_SERVER_PORT);
net_gateway_monitor_socket = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (net_gateway_monitor_socket < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Unable to create socket: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
break;
}
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Socket created");
// Set timeout
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(net_gateway_monitor_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
sizeof timeout);
int err = bind(net_gateway_monitor_socket, (struct sockaddr *)&dest_addr,
sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Socket unable to bind: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
net_gateway_monitor_socket = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
// err = connect(net_gateway_monitor_socket, (struct sockaddr
// *)&server_addr,
// sizeof(server_addr));
if (err < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Socket unable to connect: errno %d. sock: %d", errno,
net_gateway_monitor_socket);
net_gateway_monitor_socket = -1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Socket bound, port %d",
NET_GATEWAY_MONITOR_UDP_PORT);
xTaskCreate(net_gateway_monitor_request_task, "net_gateway_monitor_request",
1024, NULL, 5, NULL);
while (1) {
uint8_t from_len = sizeof(server_addr);
// ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Waiting for data");
int len =
recv(net_gateway_monitor_socket, rx_buffer, sizeof(rx_buffer) - 1, 0);
// Error occurred during receiving
if (len < 0) {
if (errno == EAGAIN) {
continue;
}
ESP_LOGE(NET_GATEWAY_MONITOR_TAG, "recvfrom failed: errno %d. len: %d",
errno, len);
}
// Data received
else {
// Get the sender's ip address as string
inet_ntoa_r(server_addr.sin_addr, addr_str, sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat
// like a string...
gui_change_network_speed(
(uint64_t *)&rx_buffer[0], (uint64_t *)&rx_buffer[8],
(uint64_t *)&rx_buffer[16], (uint64_t *)&rx_buffer[24]);
}
if (net_gateway_monitor_socket != -1 && len < 0) {
ESP_LOGE(NET_GATEWAY_MONITOR_TAG,
"Shutting down socket and restarting...");
shutdown(net_gateway_monitor_socket, 0);
close(net_gateway_monitor_socket);
break;
}
}
}
vTaskDelete(NULL);
}
static void net_gateway_monitor_init() {
ESP_LOGI(NET_GATEWAY_MONITOR_TAG, "Start net gateway monitor");
xTaskCreate(net_gateway_monitor_run_task, "net_gateway_monitor_run", 4096,
(void *)AF_INET, 5, NULL);
}

View File

@ -1,159 +0,0 @@
#include <lwip/netdb.h>
#include <string.h>
#include <sys/param.h>
#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "gui.c"
#include "light.c"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "nvs_flash.h"
#define UDP_PORT 23042
static const char *UDP_SERVER_TAG = "UDP_SERVER";
static bool desktop_connected = false;
static uint64_t last_desktop_ping_at = 0;
static void udp_server_task(void *pvParameters) {
char rx_buffer[1024];
char addr_str[128];
int addr_family = (int)pvParameters;
int ip_protocol = 0;
struct sockaddr_in6 dest_addr;
while (1) {
if (addr_family == AF_INET) {
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr_ip4->sin_family = AF_INET;
dest_addr_ip4->sin_port = htons(UDP_PORT);
ip_protocol = IPPROTO_IP;
}
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (sock < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Unable to create socket: errno %d. sock: %d",
errno, sock);
break;
}
ESP_LOGI(UDP_SERVER_TAG, "Socket created");
// Set timeout
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Socket unable to bind: errno %d. sock: %d",
errno, sock);
}
ESP_LOGI(UDP_SERVER_TAG, "Socket bound, port %d", UDP_PORT);
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
socklen_t socklen = sizeof(source_addr);
while (1) {
// ESP_LOGI(UDP_SERVER_TAG, "Waiting for data");
int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0,
(struct sockaddr *)&source_addr, &socklen);
// Error occurred during receiving
if (len < 0) {
if (errno == EAGAIN) {
continue;
}
ESP_LOGE(UDP_SERVER_TAG, "recvfrom failed: errno %d. len: %d", errno,
len);
}
// Data received
else {
// Get the sender's ip address as string
if (source_addr.ss_family == PF_INET) {
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str,
sizeof(addr_str) - 1);
rx_buffer[len] = 0; // Null-terminate whatever we received and treat
// like a string...
switch (rx_buffer[0]) {
case 1:
last_desktop_ping_at = esp_timer_get_time();
sendto(sock, rx_buffer, 1, 0, (struct sockaddr *)&source_addr,
sizeof(source_addr));
break;
case 2:
set_display_ambient_light_colors(
((uint16_t)rx_buffer[1] << 8 | (uint16_t)rx_buffer[2]),
(uint8_t *)&(rx_buffer[3]), len - 3);
break;
default:
ESP_LOGI(UDP_SERVER_TAG, "%s", rx_buffer);
break;
}
int err =
sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr,
sizeof(source_addr));
if (err < 0) {
ESP_LOGI(UDP_SERVER_TAG, "Received %d bytes from %s:", len,
addr_str);
ESP_LOGE(UDP_SERVER_TAG,
"Error occurred during sending: errno %d. sock: %d", errno,
sock);
shutdown(sock, 0);
close(sock);
break;
}
}
}
if (sock != -1 && len < 0) {
ESP_LOGE(UDP_SERVER_TAG, "Shutting down socket and restarting...");
shutdown(sock, 0);
close(sock);
break;
}
}
}
vTaskDelete(NULL);
}
static void change_desktop_connection_status(bool connected) {
desktop_connected = connected;
if (connected) {
gui_set_server_connected();
} else {
gui_set_server_disconnected();
}
}
static void desktop_online_check_task(void *pvParameters) {
while (1) {
if (esp_timer_get_time() - last_desktop_ping_at > 5000000) { // 2 seconds
change_desktop_connection_status(false);
} else {
change_desktop_connection_status(true);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void udp_server_init(void) {
xTaskCreate(udp_server_task, "udp_server", 4096, (void *)AF_INET, 5, NULL);
xTaskCreate(desktop_online_check_task, "desktop_online_check", 4096, NULL, 10,
NULL);
}

View File

@ -51,6 +51,10 @@ static encoder_state_t encoder_1_state = {.key = ui_input_raw_key_encoder_1,
uint8_t level_byte; uint8_t level_byte;
int8_t delta = 0; int8_t delta = 0;
int64_t ec0_last_time = 0;
int64_t ec1_last_time = 0;
int64_t ec0_interval = 0;
int64_t ec1_interval = 0;
static void IRAM_ATTR gpio_isr_handler(void *arg) { static void IRAM_ATTR gpio_isr_handler(void *arg) {
xQueueSendFromISR(ui_input_raw_event, NULL, NULL); xQueueSendFromISR(ui_input_raw_event, NULL, NULL);
@ -87,6 +91,8 @@ static void ui_input_update_embedded_display(void *arg) {
strcpy(changing_str, "NC"); strcpy(changing_str, "NC");
break; break;
} }
ESP_LOGI(UI_INPUT_TAG, "%s", changing_str);
} }
} }
} }
@ -145,25 +151,49 @@ static void encoder_value_change(encoder_state_t *state) {
return; return;
} }
ESP_LOGI(UI_INPUT_TAG, "key: %d, delta: %d", state->key, delta);
s_ui_input_t event = {.value = delta}; s_ui_input_t event = {.value = delta};
if (state->key == ui_input_raw_key_encoder_0) { if (state->key == ui_input_raw_key_encoder_0) {
ec0_interval = esp_timer_get_time() - ec0_last_time;
if (ec0_interval < 10000) { // 100ms
event.value = event.value * 5;
} else if (ec0_interval < 20000) { // 100ms
event.value = event.value * 4;
} else if (ec0_interval < 50000) { // 100ms
event.value = event.value * 3;
} else if (ec0_interval < 100000) { // 100ms
event.value = event.value * 2;
}
ec0_last_time = esp_timer_get_time();
if (state->value & 1) { if (state->value & 1) {
event.key = ui_input_key_computer_volume; event.key = ui_input_key_computer_volume;
} else { } else {
event.key = ui_input_key_display_0_brightness; event.key = ui_input_key_display_1_brightness;
} }
} else if (state->key == ui_input_raw_key_encoder_1) { } else if (state->key == ui_input_raw_key_encoder_1) {
if (state->value & 1) { if (state->value & 1) {
ec1_interval = esp_timer_get_time() - ec1_last_time;
if (ec1_interval < 20000) { // 100ms
event.value = event.value * 7;
} else if (ec1_interval < 30000) { // 100ms
event.value = event.value * 5;
} else if (ec1_interval < 40000) { // 100ms
event.value = event.value * 3;
} else if (ec1_interval < 50000) {
event.value = event.value * 2;
}
ec1_last_time = esp_timer_get_time();
event.key = ui_input_key_display_ambient_lighting_level; event.key = ui_input_key_display_ambient_lighting_level;
led_strip_set_brightness(display_ambient_lighting_level + delta); led_strip_set_brightness(display_ambient_lighting_level + event.value);
gui_change_strip_level(display_ambient_lighting_level); gui_change_strip_level(display_ambient_lighting_level);
} else { } else {
event.key = ui_input_key_display_1_brightness; event.key = ui_input_key_display_0_brightness;
} }
} }
xQueueSend(ui_input_event, &event, NULL); xQueueSend(ui_input_event, &event, NULL);
ESP_LOGD(UI_INPUT_TAG, "key: %d, delta: %d. delay: %lld, %lld", state->key,
event.value, ec0_interval, ec1_interval);
} }
static void ui_input_raw_handler(void *arg) { static void ui_input_raw_handler(void *arg) {
@ -192,13 +222,13 @@ void ui_input_init(void) {
gpio_config(&io_conf); gpio_config(&io_conf);
// start encoder task // start encoder task
ui_input_event = xQueueCreate(10, sizeof(s_ui_input_t)); ui_input_event = xQueueCreate(5, sizeof(s_ui_input_t));
ui_input_raw_event = xQueueCreate(10, 0); ui_input_raw_event = xQueueCreate(10, 0);
// hook isr handler for specific gpio pin // hook isr handler for specific gpio pin
gpio_isr_handler_add(ENCODER_INT_GPIO, gpio_isr_handler, NULL); gpio_isr_handler_add(ENCODER_INT_GPIO, gpio_isr_handler, NULL);
xTaskCreate(ui_input_update_embedded_display, "ui_input_event", 2048, NULL, // xTaskCreate(ui_input_update_embedded_display, "ui_input_event", 2048, NULL,
10, NULL); // 10, NULL);
xTaskCreate(ui_input_raw_handler, "ui_input_event", 2048, NULL, 10, NULL); xTaskCreate(ui_input_raw_handler, "ui_input_event", 2048, NULL, 10, NULL);
} }