ESPHome 2026.3.2
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 : 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
43 void add_delimiter_byte(uint8_t byte) { this->after_delimiter_.push_back(byte); }
44
45 void set_debug_prefix(const char *prefix) { this->debug_prefix_ = StringRef(prefix); }
46
47 protected:
50 std::vector<uint8_t> bytes_{};
54 std::vector<uint8_t> after_delimiter_{};
56 bool is_triggering_{false};
58
60 bool is_recursive_();
61 void store_byte_(UARTDirection direction, uint8_t byte);
63 void trigger_after_delimiter_(uint8_t byte);
67 void fire_trigger_();
68};
69
76class UARTDummyReceiver : public Component, public UARTDevice {
77 public:
79 void loop() override;
80};
81
84class UARTDebug {
85 public:
88 static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
89 StringRef prefix = StringRef());
90
92 static void log_string(UARTDirection direction, std::vector<uint8_t> bytes, StringRef prefix = StringRef());
93
96 static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
97 StringRef prefix = StringRef());
98
101 static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator,
102 StringRef prefix = StringRef());
103};
104
105} // namespace esphome::uart
106#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
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)
void add_delimiter_byte(uint8_t byte)
Add a delimiter byte.
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_
std::vector< uint8_t > after_delimiter_
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
size_t size
Definition helpers.h:929
static void uint32_t