ESPHome  2024.11.1
uart_debugger.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #ifdef USE_UART_DEBUGGER
4 
5 #include <vector>
8 #include "uart.h"
9 #include "uart_component.h"
10 
11 namespace esphome {
12 namespace uart {
13 
21 class UARTDebugger : public Component, public Trigger<UARTDirection, std::vector<uint8_t>> {
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  protected:
48  std::vector<uint8_t> bytes_{};
49  size_t after_bytes_;
50  uint32_t after_timeout_;
51  uint32_t last_time_{};
52  std::vector<uint8_t> after_delimiter_{};
54  bool is_triggering_{false};
55 
57  bool is_recursive_();
58  void store_byte_(UARTDirection direction, uint8_t byte);
60  void trigger_after_delimiter_(uint8_t byte);
61  void trigger_after_bytes_();
63  bool has_buffered_bytes_();
64  void fire_trigger_();
65 };
66 
73 class UARTDummyReceiver : public Component, public UARTDevice {
74  public:
76  void loop() override;
77 };
78 
81 class UARTDebug {
82  public:
85  static void log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
86 
88  static void log_string(UARTDirection direction, std::vector<uint8_t> bytes);
89 
92  static void log_int(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
93 
96  static void log_binary(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator);
97 };
98 
99 } // namespace uart
100 } // namespace esphome
101 #endif
void set_after_bytes(size_t size)
Set the maximum number of bytes to accumulate.
Definition: uart_debugger.h:33
bool is_my_direction_(UARTDirection direction)
This class contains some static methods, that can be used to easily create a logging action for the d...
Definition: uart_debugger.h:81
void set_direction(UARTDirection direction)
Set the direction in which to inspect the bytes: incoming, outgoing or both.
Definition: uart_debugger.h:29
FanDirection direction
Definition: fan.h:37
UARTDummyReceiver(UARTComponent *parent)
Definition: uart_debugger.h:75
void store_byte_(UARTDirection direction, uint8_t byte)
std::vector< uint8_t > bytes_
Definition: uart_debugger.h:48
UARTDebugger(UARTComponent *parent)
void trigger_after_direction_change_(UARTDirection direction)
This UARTDevice is used by the serial debugger to read data from a serial interface when the &#39;dummy_r...
Definition: uart_debugger.h:73
void trigger_after_delimiter_(uint8_t byte)
void set_after_timeout(uint32_t timeout)
Set a timeout for the data stream.
Definition: uart_debugger.h:37
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
void add_delimiter_byte(uint8_t byte)
Add a delimiter byte.
Definition: uart_debugger.h:43
std::vector< uint8_t > after_delimiter_
Definition: uart_debugger.h:52
The UARTDebugger class adds debugging support to a UART bus.
Definition: uart_debugger.h:21