ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_debugger.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_UART_DEBUGGER
4
5#include <vector>
9#include "uart.h"
10#include "uart_component.h"
11
12namespace esphome::uart {
13
21class UARTDebugger final : public Component, public Trigger<UARTDirection, std::vector<uint8_t>, StringRef> {
22 public:
23 explicit UARTDebugger(UARTComponent *parent);
24 void loop() override;
25
30
33 void set_after_bytes(size_t size) { this->after_bytes_ = size; }
34
37 void set_after_timeout(uint32_t timeout) { this->after_timeout_ = timeout; }
38
42 void set_after_delimiter(const uint8_t *delimiter, uint8_t len) {
43 this->after_delimiter_ = delimiter;
45 }
46
47 void set_debug_prefix(const char *prefix) { this->debug_prefix_ = StringRef(prefix); }
48
49 protected:
52 std::vector<uint8_t> bytes_{};
56 const uint8_t *after_delimiter_{nullptr};
59 bool is_triggering_{false};
61
63 bool is_recursive_();
64 void store_byte_(UARTDirection direction, uint8_t byte);
66 void trigger_after_delimiter_(uint8_t byte);
70 void fire_trigger_();
71};
72
79class UARTDummyReceiver final : public Component, public UARTDevice {
80 public:
82 void loop() override;
83};
84
87class UARTDebug {
88 public:
91 static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
92 StringRef prefix = StringRef());
93
95 static void log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix = StringRef());
96
99 static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
100 StringRef prefix = StringRef());
101
104 static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
105 StringRef prefix = StringRef());
106};
107
108} // namespace esphome::uart
109#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
This class contains some static methods, that can be used to easily create a logging action for the d...
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.
The UARTDebugger class adds debugging support to a UART bus.
void store_byte_(UARTDirection direction, uint8_t byte)
void set_after_timeout(uint32_t timeout)
Set a timeout for the data stream.
bool is_my_direction_(UARTDirection direction)
const uint8_t * after_delimiter_
void set_after_delimiter(const uint8_t *delimiter, uint8_t len)
Set a delimiter byte sequence (a typical example would be '\r ').
void trigger_after_direction_change_(UARTDirection direction)
void set_direction(UARTDirection direction)
Set the direction in which to inspect the bytes: incoming, outgoing or both.
void set_after_bytes(size_t size)
Set the maximum number of bytes to accumulate.
std::vector< uint8_t > bytes_
void trigger_after_delimiter_(uint8_t byte)
void set_debug_prefix(const char *prefix)
UARTDebugger(UARTComponent *parent)
This UARTDevice is used by the serial debugger to read data from a serial interface when the 'dummy_r...
UARTDummyReceiver(UARTComponent *parent)
FanDirection direction
Definition fan.h:5
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
static void uint32_t