Add UART multiplexer and INA226 monitoring
This commit is contained in:
@@ -18,6 +18,8 @@
|
||||
#include "tinyusb.h"
|
||||
#include "tusb_cdc_acm.h"
|
||||
#include "ws2812_status.h"
|
||||
#include "ina226_monitor.h"
|
||||
#include "uart_mux.h"
|
||||
|
||||
#define CLI_LINE_MAX_LEN 128
|
||||
#define CLI_QUEUE_LEN 8
|
||||
@@ -25,6 +27,9 @@
|
||||
#ifndef CONFIG_TINYUSB_CDC_RX_BUFSIZE
|
||||
#define CONFIG_TINYUSB_CDC_RX_BUFSIZE 64
|
||||
#endif
|
||||
#ifndef CONFIG_WATCH_UART_MUX_DEFAULT_READ_LEN
|
||||
#define CONFIG_WATCH_UART_MUX_DEFAULT_READ_LEN 128
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
@@ -147,6 +152,84 @@ static void usb_cli_handle_switch(const char *cmd, char *args)
|
||||
}
|
||||
}
|
||||
|
||||
static void usb_cli_print_measurement(size_t channel, const ina226_reading_t *reading)
|
||||
{
|
||||
usb_cli_printf("\r\nCH%u: %.2f В, %.1f мА, %.1f мВт",
|
||||
(unsigned)channel, reading->voltage_v, reading->current_ma, reading->power_mw);
|
||||
}
|
||||
|
||||
static void usb_cli_handle_sense(char *args)
|
||||
{
|
||||
if (!ina226_monitor_ready()) {
|
||||
usb_cli_printf("\r\nМоніторинг INA226 недоступний\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
ina226_reading_t reading = {0};
|
||||
if (ina226_monitor_sample(&reading) == ESP_OK) {
|
||||
usb_cli_print_measurement(0, &reading);
|
||||
} else {
|
||||
usb_cli_printf("\r\nНе вдалося зчитати дані з INA226\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void usb_cli_handle_uart(char *args)
|
||||
{
|
||||
if (!uart_mux_ready()) {
|
||||
usb_cli_printf("\r\nUART мультиплексор недоступний\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *ctx = NULL;
|
||||
char *action = strtok_r(args, " ", &ctx);
|
||||
if (!action) {
|
||||
usb_cli_printf("\r\nUART usage: uart send <channel> <text> | uart read <channel> [len]\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *channel_str = strtok_r(NULL, " ", &ctx);
|
||||
dcdc_channel_t channel;
|
||||
if (!usb_cli_parse_channel(channel_str, &channel)) {
|
||||
usb_cli_printf("\r\nНевірний номер каналу\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcasecmp(action, "send") == 0) {
|
||||
char *payload = ctx;
|
||||
if (!payload || *payload == '\0') {
|
||||
usb_cli_printf("\r\nНемає даних для відправлення\r\n");
|
||||
return;
|
||||
}
|
||||
size_t len = strlen(payload);
|
||||
if (uart_mux_write(channel, (const uint8_t *)payload, len, pdMS_TO_TICKS(200)) == ESP_OK) {
|
||||
usb_cli_printf("\r\nВідправлено %u байт\r\n", (unsigned)len);
|
||||
} else {
|
||||
usb_cli_printf("\r\nПомилка відправлення UART\r\n");
|
||||
}
|
||||
} else if (strcasecmp(action, "read") == 0) {
|
||||
char *len_str = ctx ? strtok_r(NULL, " ", &ctx) : NULL;
|
||||
size_t req_len = len_str ? strtoul(len_str, NULL, 10) : CONFIG_WATCH_UART_MUX_DEFAULT_READ_LEN;
|
||||
if (req_len == 0) {
|
||||
req_len = CONFIG_WATCH_UART_MUX_DEFAULT_READ_LEN;
|
||||
}
|
||||
if (req_len > 256) {
|
||||
req_len = 256;
|
||||
}
|
||||
uint8_t buffer[256];
|
||||
size_t received = 0;
|
||||
if (uart_mux_read(channel, buffer, req_len, &received, pdMS_TO_TICKS(200)) == ESP_OK && received > 0) {
|
||||
usb_cli_printf("\r\nCH%u UART (%u байт): ", (unsigned)channel, (unsigned)received);
|
||||
for (size_t i = 0; i < received; ++i) {
|
||||
usb_cli_write_raw((char *)&buffer[i], 1);
|
||||
}
|
||||
} else {
|
||||
usb_cli_printf("\r\nДані відсутні\r\n");
|
||||
}
|
||||
} else {
|
||||
usb_cli_printf("\r\nНевірна дія '%s'\r\n", action);
|
||||
}
|
||||
}
|
||||
|
||||
static void usb_cli_process_line(char *line)
|
||||
{
|
||||
char *trimmed = usb_cli_trim(line);
|
||||
@@ -167,13 +250,20 @@ static void usb_cli_process_line(char *line)
|
||||
" status - стан всіх каналів\r\n"
|
||||
" enable <n> - увімкнути канал n\r\n"
|
||||
" disable <n> - вимкнути канал n\r\n"
|
||||
" toggle <n> - перемкнути канал n\r\n");
|
||||
" toggle <n> - перемкнути канал n\r\n"
|
||||
" sense [n] - показати напругу/струм/потужність (опц. канал)\r\n"
|
||||
" uart send <n> <msg> - відправити повідомлення Pi n\r\n"
|
||||
" uart read <n> [len] - прочитати дані з Pi n\r\n");
|
||||
} else if (strcasecmp(cmd, "status") == 0) {
|
||||
usb_cli_print_status();
|
||||
} else if (strcasecmp(cmd, "enable") == 0 ||
|
||||
strcasecmp(cmd, "disable") == 0 ||
|
||||
strcasecmp(cmd, "toggle") == 0) {
|
||||
usb_cli_handle_switch(cmd, save_ptr);
|
||||
} else if (strcasecmp(cmd, "sense") == 0) {
|
||||
usb_cli_handle_sense(save_ptr);
|
||||
} else if (strcasecmp(cmd, "uart") == 0) {
|
||||
usb_cli_handle_uart(save_ptr);
|
||||
} else {
|
||||
usb_cli_printf("\r\nНевідома команда '%s'\r\n", cmd);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user