Compare commits
5 Commits
feature/lv
...
ac8520f31f
Author | SHA1 | Date | |
---|---|---|---|
ac8520f31f | |||
df476e4f9c | |||
6e95095db0 | |||
a3abff89c5 | |||
3f5ca686cf |
13
docs/udp.md
13
docs/udp.md
@ -15,3 +15,16 @@
|
|||||||
| 起始位置 | 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 |
|
||||||
|
@ -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"
|
||||||
@ -13,6 +12,7 @@ idf_component_register(
|
|||||||
# "ambient_light.c"
|
# "ambient_light.c"
|
||||||
# "temperature.c"
|
# "temperature.c"
|
||||||
# "mqtt.c"
|
# "mqtt.c"
|
||||||
|
"desktop.c"
|
||||||
"main.c"
|
"main.c"
|
||||||
"wifi.c"
|
"wifi.c"
|
||||||
"light.c"
|
"light.c"
|
||||||
|
285
main/desktop.c
Normal file
285
main/desktop.c
Normal file
@ -0,0 +1,285 @@
|
|||||||
|
#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;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
ESP_LOGI(UDP_SERVER_TAG, "%s", rx_buffer);
|
||||||
|
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);
|
||||||
|
}
|
16
main/gui.c
16
main/gui.c
@ -261,6 +261,22 @@ static void gui_change_strip_level(int32_t target_value) {
|
|||||||
lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON);
|
lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void gui_change_display_brightness(int8_t display_index,
|
||||||
|
int32_t target_value) {
|
||||||
|
gui_create_value_setting_panel();
|
||||||
|
lv_label_set_text_fmt(value_setting_label, "Display#%d Brightness",
|
||||||
|
display_index);
|
||||||
|
lv_bar_set_range(value_setting_bar, 0, 100);
|
||||||
|
lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gui_change_volume_level(int32_t target_value) {
|
||||||
|
gui_create_value_setting_panel();
|
||||||
|
lv_label_set_text(value_setting_label, "Volume Level");
|
||||||
|
lv_bar_set_range(value_setting_bar, 0, 100);
|
||||||
|
lv_bar_set_value(value_setting_bar, target_value, LV_ANIM_ON);
|
||||||
|
}
|
||||||
|
|
||||||
void lv_example_bar_6(lv_obj_t *src) {
|
void lv_example_bar_6(lv_obj_t *src) {
|
||||||
static lv_style_t style_bar;
|
static lv_style_t style_bar;
|
||||||
lv_style_init(&style_bar);
|
lv_style_init(&style_bar);
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#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 "ui_input.c"
|
#include "ui_input.c"
|
||||||
#include "wifi.c"
|
#include "wifi.c"
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -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_LOGI(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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user