[uart][usb_uart] Add debug_prefix option to distinguish multiple defined uarts in log (#14525)

This commit is contained in:
Oliver Kleinecke authored and GitHub committed 2026-03-08 10:16:12 +11:00
1 parent 3f700bac1c
commit f2dfb5e1dc
12 files changed
+138 -48

No files matched your search

+11 -2
View File
@@ -203,8 +203,10 @@ UART_DIRECTIONS = {
# round numbers.
AFTER_DEFAULTS = {CONF_BYTES: 150, CONF_TIMEOUT: "100ms"}
CONF_DEBUG_PREFIX = "debug_prefix"
# By default, log in hex format when no specific sequence is provided.
DEFAULT_DEBUG_OUTPUT = "UARTDebug::log_hex(direction, bytes, ':');"
DEFAULT_DEBUG_OUTPUT = "UARTDebug::log_hex(direction, bytes, ':', debug_prefix);"
DEFAULT_SEQUENCE = [{CONF_LAMBDA: make_data_base(DEFAULT_DEBUG_OUTPUT)}]
@@ -242,6 +244,7 @@ DEBUG_SCHEMA = cv.Schema(
): automation.validate_automation(),
cv.Optional(CONF_DUMMY_RECEIVER, default=False): cv.boolean,
cv.GenerateID(CONF_DUMMY_RECEIVER_ID): cv.declare_id(UARTDummyReceiver),
cv.Optional(CONF_DEBUG_PREFIX, default=""): cv.string,
}
)
@@ -283,7 +286,11 @@ async def debug_to_code(config, parent):
for action in config[CONF_SEQUENCE]:
await automation.build_automation(
trigger,
[(UARTDirection, "direction"), (cg.std_vector.template(cg.uint8), "bytes")],
[
(UARTDirection, "direction"),
(cg.std_vector.template(cg.uint8), "bytes"),
(cg.StringRef, "debug_prefix"),
],
action,
)
cg.add(trigger.set_direction(config[CONF_DIRECTION]))
@@ -299,6 +306,8 @@ async def debug_to_code(config, parent):
if config[CONF_DUMMY_RECEIVER]:
dummy = cg.new_Pvariable(config[CONF_DUMMY_RECEIVER_ID], parent)
await cg.register_component(dummy, {})
if debug_prefix := config[CONF_DEBUG_PREFIX]:
cg.add(trigger.set_debug_prefix(debug_prefix))
cg.add_define("USE_UART_DEBUGGER")
+9 -9
View File
@@ -74,7 +74,7 @@ bool UARTDebugger::has_buffered_bytes_() { return !this->bytes_.empty(); }
void UARTDebugger::fire_trigger_() {
this->is_triggering_ = true;
trigger(this->last_direction_, this->bytes_);
trigger(this->last_direction_, this->bytes_, this->debug_prefix_);
this->bytes_.clear();
this->is_triggering_ = false;
}
@@ -94,7 +94,7 @@ void UARTDummyReceiver::loop() {
// TCP connection(s). Without these delays, debug log lines could go
// missing when UART devices block the main loop for too long.
void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator) {
void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
std::string res;
if (direction == UART_DIRECTION_RX) {
res += "<<< ";
@@ -110,11 +110,11 @@ void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uin
buf_append_printf(buf, sizeof(buf), 0, "%02X", bytes[i]);
res += buf;
}
ESP_LOGD(TAG, "%s", res.c_str());
ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
delay(10);
}
void UARTDebug::log_string(UARTDirection direction, std::vector<uint8_t> bytes) {
void UARTDebug::log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix) {
std::string res;
if (direction == UART_DIRECTION_RX) {
res += "<<< \"";
@@ -154,11 +154,11 @@ void UARTDebug::log_string(UARTDirection direction, std::vector<uint8_t> bytes)
}
}
res += '"';
ESP_LOGD(TAG, "%s", res.c_str());
ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
delay(10);
}
void UARTDebug::log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator) {
void UARTDebug::log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
std::string res;
size_t len = bytes.size();
if (direction == UART_DIRECTION_RX) {
@@ -174,11 +174,11 @@ void UARTDebug::log_int(UARTDirection direction, std::vector<uint8_t> bytes, uin
buf_append_printf(buf, sizeof(buf), 0, "%u", bytes[i]);
res += buf;
}
ESP_LOGD(TAG, "%s", res.c_str());
ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
delay(10);
}
void UARTDebug::log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator) {
void UARTDebug::log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
std::string res;
size_t len = bytes.size();
if (direction == UART_DIRECTION_RX) {
@@ -194,7 +194,7 @@ void UARTDebug::log_binary(UARTDirection direction, std::vector<uint8_t> bytes,
buf_append_printf(buf, sizeof(buf), 0, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]);
res += buf;
}
ESP_LOGD(TAG, "%s", res.c_str());
ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
delay(10);
}
+12 -5
View File
@@ -5,6 +5,7 @@
#include <vector>
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/string_ref.h"
#include "uart.h"
#include "uart_component.h"
@@ -17,7 +18,7 @@ namespace esphome::uart {
/// 'appropriate time' means exactly, is determined by a number of
/// configurable constraints. E.g. when a given number of bytes is gathered
/// and/or when no more data has been seen for a given time interval.
class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector<uint8_t>> {
class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector<uint8_t>, StringRef> {
public:
explicit UARTDebugger(UARTComponent *parent);
void loop() override;
@@ -41,6 +42,8 @@ class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector
/// logging will be triggered.
void add_delimiter_byte(uint8_t byte) { this->after_delimiter_.push_back(byte); }
void set_debug_prefix(const char *prefix) { this->debug_prefix_ = StringRef(prefix); }
protected:
UARTDirection for_direction_;
UARTDirection last_direction_{};
@@ -51,6 +54,7 @@ class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector
std::vector<uint8_t> after_delimiter_{};
size_t after_delimiter_pos_{};
bool is_triggering_{false};
StringRef debug_prefix_{};
bool is_my_direction_(UARTDirection direction);
bool is_recursive_();
@@ -81,18 +85,21 @@ class UARTDebug {
public:
/// Log the bytes as hex values, separated by the provided separator
/// character.
static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
/// Log the bytes as string values, escaping unprintable characters.
static void log_string(UARTDirection direction, std::vector<uint8_t> bytes);
static void log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix = StringRef());
/// Log the bytes as integer values, separated by the provided separator
/// character.
static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
/// Log the bytes as '<binary> (<hex>)' values, separated by the provided
/// separator.
static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
StringRef prefix = StringRef());
};
} // namespace esphome::uart