feat: 支持向桌面端发送音量和显示器亮度数据。
This commit is contained in:
parent
a3abff89c5
commit
6e95095db0
13
docs/udp.md
13
docs/udp.md
@ -15,3 +15,16 @@
|
||||
| 起始位置 | 2 | 0~65535 |
|
||||
| 长度 | 2 | 0~65535 |
|
||||
| 颜色 | 3 | RGB 顺序,$2^3 * 2^3 * 2^3 = 65535$ 真彩色 |
|
||||
|
||||
### 更新电脑显示器亮度
|
||||
|
||||
| 数据 | 长度(字节) | 说明 |
|
||||
| --- | --- | --- |
|
||||
| 显示器序号 | 1 | 0~255 |
|
||||
| 亮度 | 1 | 0~255 |
|
||||
|
||||
### 更新电脑音量
|
||||
|
||||
| 数据 | 长度(字节) | 说明 |
|
||||
| --- | --- | --- |
|
||||
| 音量 | 1 | 0~255 |
|
||||
|
108
main/desktop.c
108
main/desktop.c
@ -24,6 +24,10 @@ 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;
|
||||
@ -52,18 +56,15 @@ static void udp_server_task(void *pvParameters) {
|
||||
char addr_str[128];
|
||||
int addr_family = (int)pvParameters;
|
||||
int ip_protocol = 0;
|
||||
struct sockaddr_in6 dest_addr;
|
||||
struct sockaddr_in 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);
|
||||
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;
|
||||
}
|
||||
|
||||
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
|
||||
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);
|
||||
@ -81,16 +82,19 @@ static void udp_server_task(void *pvParameters) {
|
||||
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_storage source_addr; // Large enough for both IPv4 or IPv6
|
||||
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,
|
||||
(struct sockaddr *)&source_addr, &socklen);
|
||||
&source_addr, &socklen);
|
||||
// Error occurred during receiving
|
||||
if (len < 0) {
|
||||
if (errno == EAGAIN) {
|
||||
@ -102,7 +106,6 @@ static void udp_server_task(void *pvParameters) {
|
||||
// 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);
|
||||
|
||||
@ -112,8 +115,9 @@ static void udp_server_task(void *pvParameters) {
|
||||
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));
|
||||
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:
|
||||
@ -127,21 +131,7 @@ static void udp_server_task(void *pvParameters) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
ESP_LOGD(UDP_SERVER_TAG, "Received %d bytes from %s:", len, addr_str);
|
||||
}
|
||||
|
||||
if (sock != -1 && len < 0) {
|
||||
@ -173,25 +163,71 @@ static uint8_t desktop_change_value(desktop_value_t *target, int8_t delta) {
|
||||
}
|
||||
|
||||
static uint8_t change_display_brightness(uint8_t display_index, int8_t delta) {
|
||||
switch (display_index) {
|
||||
case 0:
|
||||
uint8_t value = desktop_change_value(&display1_brightness, delta);
|
||||
gui_change_display_brightness(display_index, value);
|
||||
return value;
|
||||
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);
|
||||
}
|
||||
|
||||
case 1:
|
||||
return desktop_change_value(&display1_brightness, delta);
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user