ESPHome 2026.3.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>
15#include "wk_reg_def.h"
16
26// #define TEST_COMPONENT
27
28namespace esphome {
29namespace 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
60template<typename T, size_t SIZE> class WKRingBuffer {
61 public:
65 bool push(const T item) {
66 if (is_full())
67 return false;
68 this->rb_[this->head_] = item;
69 this->head_ = (this->head_ + 1) % SIZE;
70 this->count_++;
71 return true;
72 }
73
77 bool pop(T &item) {
78 if (is_empty())
79 return false;
80 item = this->rb_[this->tail_];
81 this->tail_ = (this->tail_ + 1) % SIZE;
82 this->count_--;
83 return true;
84 }
85
89 bool peek(T &item) {
90 if (is_empty())
91 return false;
92 item = this->rb_[this->tail_];
93 return true;
94 }
95
98 inline bool is_empty() { return (this->count_ == 0); }
99
102 inline bool is_full() { return (this->count_ == SIZE); }
103
106 inline size_t count() { return this->count_; }
107
110 inline size_t free() { return SIZE - this->count_; }
111
113 inline void clear() { this->head_ = this->tail_ = this->count_ = 0; }
114
115 protected:
116 std::array<T, SIZE> rb_{0};
117 int tail_{0};
118 int head_{0};
119 size_t count_{0};
120};
121
122class WeikaiComponent;
123// class WeikaiComponentSPI;
139 public:
144 WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
145 : comp_(comp), register_(reg), channel_(channel) {}
146 virtual ~WeikaiRegister() {}
147
151 WeikaiRegister &operator=(uint8_t value);
152
156 WeikaiRegister &operator&=(uint8_t value);
157
161 WeikaiRegister &operator|=(uint8_t value);
162
164 operator uint8_t() const { return read_reg(); }
165
168 virtual uint8_t read_reg() const = 0;
169
172 virtual void write_reg(uint8_t value) = 0;
173
177 virtual void read_fifo(uint8_t *data, size_t length) const = 0;
178
182 virtual void write_fifo(uint8_t *data, size_t length) = 0;
183
185 uint8_t register_;
186 uint8_t channel_;
187};
188
189class WeikaiChannel; // forward declaration
197 public:
199 virtual ~WeikaiComponent() {}
200
203 void set_crystal(uint32_t crystal) { this->crystal_ = crystal; }
204
207 void set_test_mode(int test_mode) { this->test_mode_ = test_mode; }
208
211 void set_name(std::string &&name) { this->name_ = std::move(name); }
212
215 const char *get_name() { return this->name_.c_str(); }
216
218 void loop() override;
219
220 bool page1() { return page1_; }
221
226 virtual WeikaiRegister &reg(uint8_t reg, uint8_t channel) = 0;
227
228 protected:
229 friend class WeikaiChannel;
230
236 float get_setup_priority() const override { return setup_priority::BUS - 0.1F; }
237
238 friend class WeikaiGPIOPin;
240 bool read_pin_val_(uint8_t pin);
241
243 void write_pin_val_(uint8_t pin, bool value);
244
246 void set_pin_direction_(uint8_t pin, gpio::Flags flags);
247
248#ifdef TEST_COMPONENT
252 void test_gpio_input_();
253 void test_gpio_output_();
255#endif
256
257 uint8_t pin_config_{0x00};
258 uint8_t output_state_{0x00};
259 uint8_t input_state_{0x00};
260 uint32_t crystal_;
262 bool page1_{false};
263 std::vector<WeikaiChannel *> children_{};
264 std::string name_;
265};
266
270class WeikaiGPIOPin : public GPIOPin {
271 public:
272 void set_parent(WeikaiComponent *parent) { this->parent_ = parent; }
273 void set_pin(uint8_t pin) { this->pin_ = pin; }
274 void set_inverted(bool inverted) { this->inverted_ = inverted; }
276
277 gpio::Flags get_flags() const override { return this->flags_; }
278
279 void setup() override;
280 size_t dump_summary(char *buffer, size_t len) const override;
281 void pin_mode(gpio::Flags flags) override { this->parent_->set_pin_direction_(this->pin_, flags); }
282 bool digital_read() override { return this->parent_->read_pin_val_(this->pin_) != this->inverted_; }
283 void digital_write(bool value) override { this->parent_->write_pin_val_(this->pin_, value != this->inverted_); }
284
285 protected:
287 uint8_t pin_;
290};
291
298 public:
302 this->parent_ = parent;
303 this->parent_->children_.push_back(this); // add ourself to the list (vector)
304 }
305
308 void set_channel(uint8_t channel) { this->channel_ = channel; }
309
312 void set_channel_name(std::string &&name) { this->name_ = std::move(name); }
313
316 const char *get_channel_name() { return this->name_.c_str(); }
317
319 void virtual setup_channel();
320
322 void virtual dump_channel();
323
327 WeikaiRegister &reg(uint8_t reg) { return this->parent_->reg(reg, channel_); }
328
329 //
330 // we implements/overrides the virtual class from UARTComponent
331 //
332
350 void write_array(const uint8_t *buffer, size_t length) override;
351
366 bool read_array(uint8_t *buffer, size_t length) override;
367
373 bool peek_byte(uint8_t *buffer) override;
374
377 size_t available() override;
378
383 void flush() override;
384
385 protected:
386 friend class WeikaiComponent;
387
389 void check_logger_conflict() override {}
390
392 void reset_fifo_();
393
395 void set_line_param_();
396
398 void set_baudrate_();
399
402 size_t rx_in_fifo_();
403
406 size_t tx_in_fifo_();
407
411
414 size_t xfer_fifo_to_buffer_();
415
418 bool virtual check_channel_down();
419
420#ifdef TEST_COMPONENT
423
426 void uart_send_test_(char *message);
427
431 bool uart_receive_test_(char *message);
433#endif
434
438 uint8_t channel_;
439 uint8_t data_;
440 std::string name_;
441};
442
443} // namespace weikai
444} // namespace esphome
This is an helper class that provides a simple ring buffers that works as a FIFO.
Definition weikai.h:60
size_t count_
count number of element in the buffer
Definition weikai.h:119
bool is_full()
test is the ring buffer is full ?
Definition weikai.h:102
size_t free()
returns the number of free positions in the buffer
Definition weikai.h:110
int tail_
position of the next element to read
Definition weikai.h:117
bool push(const T item)
pushes an item at the tail of the fifo
Definition weikai.h:65
bool peek(T &item)
return the value of the item at fifo's head without removing it
Definition weikai.h:89
std::array< T, SIZE > rb_
the ring buffer
Definition weikai.h:116
int head_
position of the next element to write
Definition weikai.h:118
void clear()
clear the buffer content
Definition weikai.h:113
bool is_empty()
test is the Ring Buffer is empty ?
Definition weikai.h:98
bool pop(T &item)
return and remove the item at head of the fifo
Definition weikai.h:77
size_t count()
return the number of item in the ring buffer
Definition weikai.h:106
The WeikaiChannel class is used to implement all the virtual methods of the ESPHome uart::UARTCompone...
Definition weikai.h:297
size_t available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:404
virtual void dump_channel()
dump channel information
Definition weikai.cpp:267
virtual void setup_channel()
Setup the channel.
Definition weikai.cpp:255
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:327
void flush() override
Flush the output fifo.
Definition weikai.cpp:436
void set_line_param_()
set the line parameters
Definition weikai.cpp:284
void set_baudrate_()
set the baud rate
Definition weikai.cpp:305
uint8_t data_
a one byte buffer for register read storage
Definition weikai.h:439
bool peek_byte(uint8_t *buffer) override
Reads the first byte in FIFO without removing it.
Definition weikai.cpp:397
size_t xfer_fifo_to_buffer_()
transfer bytes from the weikai internal FIFO to the buffer (if any)
Definition weikai.cpp:447
void check_logger_conflict() override
this cannot happen with external uart therefore we do nothing
Definition weikai.h:389
virtual bool check_channel_down()
check if channel is alive
Definition weikai.cpp:378
std::string name_
name of the entity
Definition weikai.h:440
const char * get_channel_name()
Get the channel name.
Definition weikai.h:316
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:437
WKRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:436
bool read_array(uint8_t *buffer, size_t length) override
Reads a specified number of bytes from a serial port.
Definition weikai.cpp:411
void set_channel_name(std::string &&name)
The name as generated by the Python code generator.
Definition weikai.h:312
void reset_fifo_()
reset the weikai internal FIFO
Definition weikai.cpp:277
bool tx_fifo_is_not_empty_()
test if transmit buffer is not empty in the status register (optimization)
Definition weikai.cpp:331
uint8_t channel_
our Channel number
Definition weikai.h:438
void set_parent(WeikaiComponent *parent)
We belongs to this WeikaiComponent.
Definition weikai.h:301
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:347
void set_channel(uint8_t channel)
Sets the channel number.
Definition weikai.h:308
size_t tx_in_fifo_()
Returns the number of bytes in the transmit fifo.
Definition weikai.cpp:333
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:196
void set_name(std::string &&name)
store the name for the component
Definition weikai.h:211
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:261
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:259
uint32_t crystal_
crystal value;
Definition weikai.h:260
void write_pin_val_(uint8_t pin, bool value)
Helper method to write the value of a pin.
Definition weikai.cpp:213
bool read_pin_val_(uint8_t pin)
Helper method to read the value of a pin.
Definition weikai.cpp:205
uint8_t output_state_
output state: 1 means HIGH, 0 means LOW
Definition weikai.h:258
bool page1_
set to true when in "page1 mode"
Definition weikai.h:262
void set_crystal(uint32_t crystal)
store crystal frequency
Definition weikai.h:203
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:263
void set_test_mode(int test_mode)
store if the component is in test mode
Definition weikai.h:207
void loop() override
override the Component loop()
Definition weikai.cpp:98
std::string name_
name of entity
Definition weikai.h:264
void set_pin_direction_(uint8_t pin, gpio::Flags flags)
Helper method to set the pin mode of a pin.
Definition weikai.cpp:225
float get_setup_priority() const override
Get the priority of the component.
Definition weikai.h:236
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:257
virtual ~WeikaiComponent()
virtual destructor
Definition weikai.h:199
const char * get_name()
Get the name of the component.
Definition weikai.h:215
Helper class to expose a WeiKai family IO pin as an internal GPIO pin.
Definition weikai.h:270
void digital_write(bool value) override
Definition weikai.h:283
void set_inverted(bool inverted)
Definition weikai.h:274
bool digital_read() override
Definition weikai.h:282
void set_pin(uint8_t pin)
Definition weikai.h:273
gpio::Flags get_flags() const override
Definition weikai.h:277
void pin_mode(gpio::Flags flags) override
Definition weikai.h:281
void set_parent(WeikaiComponent *parent)
Definition weikai.h:272
void set_flags(gpio::Flags flags)
Definition weikai.h:275
size_t dump_summary(char *buffer, size_t len) const override
Definition weikai.cpp:248
WeikaiComponent * parent_
Definition weikai.h:286
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:138
virtual void write_reg(uint8_t value)=0
writes the register
WeikaiRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition weikai.cpp:89
WeikaiRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition weikai.cpp:83
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:78
uint8_t register_
address of the register
Definition weikai.h:185
WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
WeikaiRegister constructor.
Definition weikai.h:144
uint8_t channel_
channel for this register
Definition weikai.h:186
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:184
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 char * message
Definition component.cpp:38
uint16_t flags
bool uart_receive_test_(char *message)
Test the read_array() method.
Definition weikai.cpp:521
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:507
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:25
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
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:817
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - registers' definition.