ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
weikai.h
Go to the documentation of this file.
1
9
10#pragma once
11#include <memory>
12#include <cinttypes>
16#include "wk_reg_def.h"
17
27// #define TEST_COMPONENT
28
29namespace esphome::weikai {
30
32#if defined(I2C_BUFFER_LENGTH)
33constexpr size_t XFER_MAX_SIZE = I2C_BUFFER_LENGTH;
34#else
35constexpr size_t XFER_MAX_SIZE = 128;
36#endif
37
39constexpr size_t FIFO_SIZE = 256;
40
42constexpr size_t RING_BUFFER_SIZE = FIFO_SIZE;
43
45const LogString *reg_to_str(int reg, bool page1);
46
48void print_buffer(const char *tag, const uint8_t *data, size_t length, int level = ESPHOME_LOG_LEVEL_VERY_VERBOSE);
49
50class WeikaiComponent;
51// class WeikaiComponentSPI;
67 public:
72 WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
73 : comp_(comp), register_(reg), channel_(channel) {}
74 virtual ~WeikaiRegister() {}
75
79 WeikaiRegister &operator=(uint8_t value);
80
84 WeikaiRegister &operator&=(uint8_t value);
85
89 WeikaiRegister &operator|=(uint8_t value);
90
92 operator uint8_t() const { return read_reg(); }
93
96 virtual uint8_t read_reg() const = 0;
97
100 virtual void write_reg(uint8_t value) = 0;
101
105 virtual void read_fifo(uint8_t *data, size_t length) const = 0;
106
110 virtual void write_fifo(uint8_t *data, size_t length) = 0;
111
113 uint8_t register_;
114 uint8_t channel_;
115};
116
117class WeikaiChannel; // forward declaration
125 public:
127 virtual ~WeikaiComponent() {}
128
131 void set_crystal(uint32_t crystal) { this->crystal_ = crystal; }
132
135 void set_test_mode(int test_mode) { this->test_mode_ = test_mode; }
136
139 void set_name(std::string &&name) { this->name_ = std::move(name); }
140
143 const char *get_name() { return this->name_.c_str(); }
144
146 void loop() override;
147
148 bool page1() { return page1_; }
149
154 virtual WeikaiRegister &reg(uint8_t reg, uint8_t channel) = 0;
155
156 protected:
157 friend class WeikaiChannel;
158
164 float get_setup_priority() const override { return setup_priority::BUS - 0.1F; }
165
166 friend class WeikaiGPIOPin;
168 bool read_pin_val_(uint8_t pin);
169
171 void write_pin_val_(uint8_t pin, bool value);
172
174 void set_pin_direction_(uint8_t pin, gpio::Flags flags);
175
176#ifdef TEST_COMPONENT
180 void test_gpio_input_();
181 void test_gpio_output_();
183#endif
184
185 uint8_t pin_config_{0x00};
186 uint8_t output_state_{0x00};
187 uint8_t input_state_{0x00};
190 bool page1_{false};
191 std::vector<WeikaiChannel *> children_{};
192 std::string name_;
193};
194
198class WeikaiGPIOPin final : public GPIOPin {
199 public:
200 void set_parent(WeikaiComponent *parent) { this->parent_ = parent; }
201 void set_pin(uint8_t pin) { this->pin_ = pin; }
202 void set_inverted(bool inverted) { this->inverted_ = inverted; }
204
205 gpio::Flags get_flags() const override { return this->flags_; }
206
207 void setup() override;
208 size_t dump_summary(char *buffer, size_t len) const override;
209 void pin_mode(gpio::Flags flags) override { this->parent_->set_pin_direction_(this->pin_, flags); }
210 bool digital_read() override { return this->parent_->read_pin_val_(this->pin_) != this->inverted_; }
211 void digital_write(bool value) override { this->parent_->write_pin_val_(this->pin_, value != this->inverted_); }
212
213 protected:
215 uint8_t pin_;
218};
219
225class WeikaiChannel final : public uart::UARTComponent {
226 public:
230 this->parent_ = parent;
231 this->parent_->children_.push_back(this); // add ourself to the list (vector)
232 }
233
236 void set_channel(uint8_t channel) { this->channel_ = channel; }
237
240 void set_channel_name(std::string &&name) { this->name_ = std::move(name); }
241
244 const char *get_channel_name() { return this->name_.c_str(); }
245
247 void setup_channel();
248
250 void dump_channel();
251
255 WeikaiRegister &reg(uint8_t reg) { return this->parent_->reg(reg, channel_); }
256
257 //
258 // we implements/overrides the virtual class from UARTComponent
259 //
260
278 void write_array(const uint8_t *buffer, size_t length) override;
279
294 bool read_array(uint8_t *buffer, size_t length) override;
295
301 bool peek_byte(uint8_t *buffer) override;
302
305 size_t available() override;
306
311 uart::UARTFlushResult flush() override;
312
313#if defined(USE_ESP8266) || defined(USE_ESP32)
315 void load_settings(bool dump_config) override {
316 this->set_line_param_();
317 this->set_baudrate_();
318 }
319 using UARTComponent::load_settings; // also bring in the no-arg overload for convenience
320#endif
321
322 protected:
323 friend class WeikaiComponent;
324
326 void check_logger_conflict() override {}
327
329 void reset_fifo_();
330
332 void set_line_param_();
333
335 void set_baudrate_();
336
339 size_t rx_in_fifo_();
340
343 size_t tx_in_fifo_();
344
348
351 size_t xfer_fifo_to_buffer_();
352
355 bool check_channel_down_();
356
357#ifdef TEST_COMPONENT
360
363 void uart_send_test_(char *message);
364
368 bool uart_receive_test_(char *message);
370#endif
371
375 uint8_t channel_;
376 uint8_t data_;
377 std::string name_;
378};
379
380} // namespace esphome::weikai
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:390
The WeikaiChannel class is used to implement all the virtual methods of the ESPHome uart::UARTCompone...
Definition weikai.h:225
size_t available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:403
void dump_channel()
dump channel information
Definition weikai.cpp:262
void setup_channel()
Setup the channel.
Definition weikai.cpp:250
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:255
void set_line_param_()
set the line parameters
Definition weikai.cpp:279
void set_baudrate_()
set the baud rate
Definition weikai.cpp:300
uint8_t data_
a one byte buffer for register read storage
Definition weikai.h:376
bool check_channel_down_()
check if channel is alive
Definition weikai.cpp:377
bool peek_byte(uint8_t *buffer) override
Reads the first byte in FIFO without removing it.
Definition weikai.cpp:396
size_t xfer_fifo_to_buffer_()
transfer bytes from the weikai internal FIFO to the buffer (if any)
Definition weikai.cpp:448
void load_settings(bool dump_config) override
Re-apply the current line settings (baud, parity, etc) to the channel.
Definition weikai.h:315
void check_logger_conflict() override
this cannot happen with external uart therefore we do nothing
Definition weikai.h:326
std::string name_
name of the entity
Definition weikai.h:377
uart::UARTFlushResult flush() override
Flush the output fifo.
Definition weikai.cpp:436
const char * get_channel_name()
Get the channel name.
Definition weikai.h:244
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:374
bool read_array(uint8_t *buffer, size_t length) override
Reads a specified number of bytes from a serial port.
Definition weikai.cpp:410
void set_channel_name(std::string &&name)
The name as generated by the Python code generator.
Definition weikai.h:240
void reset_fifo_()
reset the weikai internal FIFO
Definition weikai.cpp:272
bool tx_fifo_is_not_empty_()
test if transmit buffer is not empty in the status register (optimization)
Definition weikai.cpp:326
StaticRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:373
uint8_t channel_
our Channel number
Definition weikai.h:375
void set_parent(WeikaiComponent *parent)
We belongs to this WeikaiComponent.
Definition weikai.h:229
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:342
void set_channel(uint8_t channel)
Sets the channel number.
Definition weikai.h:236
size_t tx_in_fifo_()
Returns the number of bytes in the transmit fifo.
Definition weikai.cpp:328
void write_array(const uint8_t *buffer, size_t length) override
Writes a specified number of bytes to a serial port.
Definition weikai.cpp:428
The WeikaiComponent class stores the information global to the WeiKai component and provides methods ...
Definition weikai.h:124
void set_name(std::string &&name)
store the name for the component
Definition weikai.h:139
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:189
virtual WeikaiRegister & reg(uint8_t reg, uint8_t channel)=0
Factory method to create a Register object.
uint8_t input_state_
input pin states: 1 means HIGH, 0 means LOW
Definition weikai.h:187
uint32_t crystal_
crystal value;
Definition weikai.h:188
void write_pin_val_(uint8_t pin, bool value)
Helper method to write the value of a pin.
Definition weikai.cpp:212
bool read_pin_val_(uint8_t pin)
Helper method to read the value of a pin.
Definition weikai.cpp:204
uint8_t output_state_
output state: 1 means HIGH, 0 means LOW
Definition weikai.h:186
bool page1_
set to true when in "page1 mode"
Definition weikai.h:190
void set_crystal(uint32_t crystal)
store crystal frequency
Definition weikai.h:131
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:191
void set_test_mode(int test_mode)
store if the component is in test mode
Definition weikai.h:135
void loop() override
override the Component loop()
Definition weikai.cpp:97
std::string name_
name of entity
Definition weikai.h:192
void set_pin_direction_(uint8_t pin, gpio::Flags flags)
Helper method to set the pin mode of a pin.
Definition weikai.cpp:220
float get_setup_priority() const override
Get the priority of the component.
Definition weikai.h:164
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:185
virtual ~WeikaiComponent()
virtual destructor
Definition weikai.h:127
const char * get_name()
Get the name of the component.
Definition weikai.h:143
Helper class to expose a WeiKai family IO pin as an internal GPIO pin.
Definition weikai.h:198
void digital_write(bool value) override
Definition weikai.h:211
void set_inverted(bool inverted)
Definition weikai.h:202
bool digital_read() override
Definition weikai.h:210
void set_pin(uint8_t pin)
Definition weikai.h:201
gpio::Flags get_flags() const override
Definition weikai.h:205
void pin_mode(gpio::Flags flags) override
Definition weikai.h:209
void set_parent(WeikaiComponent *parent)
Definition weikai.h:200
void set_flags(gpio::Flags flags)
Definition weikai.h:203
size_t dump_summary(char *buffer, size_t len) const override
Definition weikai.cpp:243
WeikaiComponent * parent_
Definition weikai.h:214
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:66
virtual void write_reg(uint8_t value)=0
writes the register
WeikaiRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition weikai.cpp:88
WeikaiRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition weikai.cpp:82
virtual void write_fifo(uint8_t *data, size_t length)=0
write an array of bytes to the transmitter fifo
WeikaiRegister & operator=(uint8_t value)
overloads the = operator.
Definition weikai.cpp:77
uint8_t register_
address of the register
Definition weikai.h:113
WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
WeikaiRegister constructor.
Definition weikai.h:72
uint8_t channel_
channel for this register
Definition weikai.h:114
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:112
virtual uint8_t read_reg() const =0
reads the register
virtual void read_fifo(uint8_t *data, size_t length) const =0
read an array of bytes from the receiver fifo
const LogString * message
Definition component.cpp:35
uint16_t flags
uint32_t buffer
Definition gif.cpp:50
bool uart_receive_test_(char *message)
Test the read_array() method.
Definition weikai.cpp:510
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:496
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
UARTFlushResult
Result of a flush() call.
When the TEST_COMPONENT flag is defined we include some auto-test methods.
Definition weikai.cpp:10
const LogString * reg_to_str(int reg, bool page1)
Name of a register for the log messages.
Definition weikai.cpp:62
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:33
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition weikai.h:42
void print_buffer(const char *tag, const uint8_t *data, size_t length, int level)
Display a buffer in hexadecimal format (32 hex values / line) for debug.
Definition weikai.cpp:41
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:39
const void size_t len
Definition hal.h:68
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - registers' definition.