ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_debugger.cpp
Go to the documentation of this file.
2#ifdef USE_UART_DEBUGGER
3
4#include <vector>
5#include "uart_debugger.h"
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10namespace esphome::uart {
11
12ESPHOME_LOG_TAG(TAG, "uart_debug");
13
15 parent->add_debug_callback([this](UARTDirection direction, uint8_t byte) {
16 if (!this->is_my_direction_(direction) || this->is_recursive_()) {
17 return;
18 }
19 this->trigger_after_direction_change_(direction);
20 this->store_byte_(direction, byte);
21 this->trigger_after_delimiter_(byte);
23 });
24}
25
27
31
33
40
42 this->bytes_.push_back(byte);
44 this->last_time_ = millis();
45}
46
48 if (this->after_delimiter_len_ == 0 || !this->has_buffered_bytes_()) {
49 return;
50 }
51 if (progmem_read_byte(this->after_delimiter_ + this->after_delimiter_pos_) != byte) {
52 this->after_delimiter_pos_ = 0;
53 return;
54 }
56 if (this->after_delimiter_pos_ == this->after_delimiter_len_) {
57 this->fire_trigger_();
58 this->after_delimiter_pos_ = 0;
59 }
60}
61
63 if (this->has_buffered_bytes_() && this->after_bytes_ > 0 && this->bytes_.size() >= this->after_bytes_) {
64 this->fire_trigger_();
65 }
66}
67
69 if (this->has_buffered_bytes_() && this->after_timeout_ > 0 && millis() - this->last_time_ >= this->after_timeout_) {
70 this->fire_trigger_();
71 }
72}
73
74bool UARTDebugger::has_buffered_bytes_() { return !this->bytes_.empty(); }
75
77 this->is_triggering_ = true;
78 trigger(this->last_direction_, this->bytes_, this->debug_prefix_);
79 this->bytes_.clear();
80 this->is_triggering_ = false;
81}
82
84 // Reading up to a limited number of bytes, to make sure that this loop()
85 // won't lock up the system on a continuous incoming stream of bytes.
86 uint8_t data;
87 int count = 50;
88 while (this->available() && count--) {
89 this->read_byte(&data);
90 }
91}
92
93// In the upcoming log functions, a delay was added after all log calls.
94// This is done to allow the system to ship the log lines via the API
95// TCP connection(s). Without these delays, debug log lines could go
96// missing when UART devices block the main loop for too long.
97
98void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
99 std::string res;
101 res += "<<< ";
102 } else {
103 res += ">>> ";
104 }
105 size_t len = bytes.size();
106 char buf[5];
107 for (size_t i = 0; i < len; i++) {
108 if (i > 0) {
109 res += separator;
110 }
111 buf_append_printf(buf, sizeof(buf), 0, "%02X", bytes[i]);
112 res += buf;
113 }
114 ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
115 delay(10);
116}
117
118void UARTDebug::log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix) {
119 std::string res;
121 res += "<<< \"";
122 } else {
123 res += ">>> \"";
124 }
125 size_t len = bytes.size();
126 char buf[5];
127 for (size_t i = 0; i < len; i++) {
128 if (bytes[i] == 7) {
129 res += "\\a";
130 } else if (bytes[i] == 8) {
131 res += "\\b";
132 } else if (bytes[i] == 9) {
133 res += "\\t";
134 } else if (bytes[i] == 10) {
135 res += "\\n";
136 } else if (bytes[i] == 11) {
137 res += "\\v";
138 } else if (bytes[i] == 12) {
139 res += "\\f";
140 } else if (bytes[i] == 13) {
141 res += "\\r";
142 } else if (bytes[i] == 27) {
143 res += "\\e";
144 } else if (bytes[i] == 34) {
145 res += "\\\"";
146 } else if (bytes[i] == 39) {
147 res += "\\'";
148 } else if (bytes[i] == 92) {
149 res += "\\\\";
150 } else if (bytes[i] < 32 || bytes[i] > 127) {
151 buf_append_printf(buf, sizeof(buf), 0, "\\x%02X", bytes[i]);
152 res += buf;
153 } else {
154 res += bytes[i];
155 }
156 }
157 res += '"';
158 ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
159 delay(10);
160}
161
162void UARTDebug::log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
163 std::string res;
164 size_t len = bytes.size();
166 res += "<<< ";
167 } else {
168 res += ">>> ";
169 }
170 char buf[4]; // max 3 digits for uint8_t (255) + null
171 for (size_t i = 0; i < len; i++) {
172 if (i > 0) {
173 res += separator;
174 }
175 buf_append_printf(buf, sizeof(buf), 0, "%u", bytes[i]);
176 res += buf;
177 }
178 ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
179 delay(10);
180}
181
182void UARTDebug::log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator, StringRef prefix) {
183 std::string res;
184 size_t len = bytes.size();
186 res += "<<< ";
187 } else {
188 res += ">>> ";
189 }
190 char buf[20];
191 for (size_t i = 0; i < len; i++) {
192 if (i > 0) {
193 res += separator;
194 }
195 buf_append_printf(buf, sizeof(buf), 0, "0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(bytes[i]), bytes[i]);
196 res += buf;
197 }
198 ESP_LOGD(TAG, "%s%s", prefix.c_str(), res.c_str());
199 delay(10);
200}
201
202} // namespace esphome::uart
203#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
void add_debug_callback(F &&callback)
static void log_string(UARTDirection direction, std::vector< uint8_t > bytes, StringRef prefix=StringRef())
Log the bytes as string values, escaping unprintable characters.
static void log_hex(UARTDirection direction, std::vector< uint8_t > bytes, uint8_t separator, StringRef prefix=StringRef())
Log the bytes as hex values, separated by the provided separator character.
static void log_int(UARTDirection direction, std::vector< uint8_t > bytes, uint8_t separator, StringRef prefix=StringRef())
Log the bytes as integer values, separated by the provided separator character.
static void log_binary(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.
void store_byte_(UARTDirection direction, uint8_t byte)
bool is_my_direction_(UARTDirection direction)
const uint8_t * after_delimiter_
void trigger_after_direction_change_(UARTDirection direction)
std::vector< uint8_t > bytes_
void trigger_after_delimiter_(uint8_t byte)
UARTDebugger(UARTComponent *parent)
bool read_byte(uint8_t *data)
Definition uart.h:39
FanDirection direction
Definition fan.h:5
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
const void size_t len
Definition hal.h:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51