ESPHome 2026.6.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::weikai {
29
31#if defined(I2C_BUFFER_LENGTH)
32constexpr size_t XFER_MAX_SIZE = I2C_BUFFER_LENGTH;
33#else
34constexpr size_t XFER_MAX_SIZE = 128;
35#endif
36
38constexpr size_t FIFO_SIZE = 256;
39
41constexpr size_t RING_BUFFER_SIZE = FIFO_SIZE;
42
59template<typename T, size_t SIZE> class WKRingBuffer {
60 public:
64 bool push(const T item) {
65 if (is_full())
66 return false;
67 this->rb_[this->head_] = item;
68 this->head_ = (this->head_ + 1) % SIZE;
69 this->count_++;
70 return true;
71 }
72
76 bool pop(T &item) {
77 if (is_empty())
78 return false;
79 item = this->rb_[this->tail_];
80 this->tail_ = (this->tail_ + 1) % SIZE;
81 this->count_--;
82 return true;
83 }
84
88 bool peek(T &item) {
89 if (is_empty())
90 return false;
91 item = this->rb_[this->tail_];
92 return true;
93 }
94
97 inline bool is_empty() { return (this->count_ == 0); }
98
101 inline bool is_full() { return (this->count_ == SIZE); }
102
105 inline size_t count() { return this->count_; }
106
109 inline size_t free() { return SIZE - this->count_; }
110
112 inline void clear() { this->head_ = this->tail_ = this->count_ = 0; }
113
114 protected:
115 std::array<T, SIZE> rb_{0};
116 int tail_{0};
117 int head_{0};
118 size_t count_{0};
119};
120
121class WeikaiComponent;
122// class WeikaiComponentSPI;
138 public:
143 WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
144 : comp_(comp), register_(reg), channel_(channel) {}
145 virtual ~WeikaiRegister() {}
146
150 WeikaiRegister &operator=(uint8_t value);
151
155 WeikaiRegister &operator&=(uint8_t value);
156
160 WeikaiRegister &operator|=(uint8_t value);
161
163 operator uint8_t() const { return read_reg(); }
164
167 virtual uint8_t read_reg() const = 0;
168
171 virtual void write_reg(uint8_t value) = 0;
172
176 virtual void read_fifo(uint8_t *data, size_t length) const = 0;
177
181 virtual void write_fifo(uint8_t *data, size_t length) = 0;
182
184 uint8_t register_;
185 uint8_t channel_;
186};
187
188class WeikaiChannel; // forward declaration
196 public:
198 virtual ~WeikaiComponent() {}
199
202 void set_crystal(uint32_t crystal) { this->crystal_ = crystal; }
203
206 void set_test_mode(int test_mode) { this->test_mode_ = test_mode; }
207
210 void set_name(std::string &&name) { this->name_ = std::move(name); }
211
214 const char *get_name() { return this->name_.c_str(); }
215
217 void loop() override;
218
219 bool page1() { return page1_; }
220
225 virtual WeikaiRegister &reg(uint8_t reg, uint8_t channel) = 0;
226
227 protected:
228 friend class WeikaiChannel;
229
235 float get_setup_priority() const override { return setup_priority::BUS - 0.1F; }
236
237 friend class WeikaiGPIOPin;
239 bool read_pin_val_(uint8_t pin);
240
242 void write_pin_val_(uint8_t pin, bool value);
243
245 void set_pin_direction_(uint8_t pin, gpio::Flags flags);
246
247#ifdef TEST_COMPONENT
251 void test_gpio_input_();
252 void test_gpio_output_();
254#endif
255
256 uint8_t pin_config_{0x00};
257 uint8_t output_state_{0x00};
258 uint8_t input_state_{0x00};
261 bool page1_{false};
262 std::vector<WeikaiChannel *> children_{};
263 std::string name_;
264};
265
269class WeikaiGPIOPin : public GPIOPin {
270 public:
271 void set_parent(WeikaiComponent *parent) { this->parent_ = parent; }
272 void set_pin(uint8_t pin) { this->pin_ = pin; }
273 void set_inverted(bool inverted) { this->inverted_ = inverted; }
275
276 gpio::Flags get_flags() const override { return this->flags_; }
277
278 void setup() override;
279 size_t dump_summary(char *buffer, size_t len) const override;
280 void pin_mode(gpio::Flags flags) override { this->parent_->set_pin_direction_(this->pin_, flags); }
281 bool digital_read() override { return this->parent_->read_pin_val_(this->pin_) != this->inverted_; }
282 void digital_write(bool value) override { this->parent_->write_pin_val_(this->pin_, value != this->inverted_); }
283
284 protected:
286 uint8_t pin_;
289};
290
297 public:
301 this->parent_ = parent;
302 this->parent_->children_.push_back(this); // add ourself to the list (vector)
303 }
304
307 void set_channel(uint8_t channel) { this->channel_ = channel; }
308
311 void set_channel_name(std::string &&name) { this->name_ = std::move(name); }
312
315 const char *get_channel_name() { return this->name_.c_str(); }
316
318 void virtual setup_channel();
319
321 void virtual dump_channel();
322
326 WeikaiRegister &reg(uint8_t reg) { return this->parent_->reg(reg, channel_); }
327
328 //
329 // we implements/overrides the virtual class from UARTComponent
330 //
331
349 void write_array(const uint8_t *buffer, size_t length) override;
350
365 bool read_array(uint8_t *buffer, size_t length) override;
366
372 bool peek_byte(uint8_t *buffer) override;
373
376 size_t available() override;
377
382 uart::UARTFlushResult flush() override;
383
384 protected:
385 friend class WeikaiComponent;
386
388 void check_logger_conflict() override {}
389
391 void reset_fifo_();
392
394 void set_line_param_();
395
397 void set_baudrate_();
398
401 size_t rx_in_fifo_();
402
405 size_t tx_in_fifo_();
406
410
413 size_t xfer_fifo_to_buffer_();
414
417 bool virtual check_channel_down();
418
419#ifdef TEST_COMPONENT
422
425 void uart_send_test_(char *message);
426
430 bool uart_receive_test_(char *message);
432#endif
433
437 uint8_t channel_;
438 uint8_t data_;
439 std::string name_;
440};
441
442} // namespace esphome::weikai
This is an helper class that provides a simple ring buffers that works as a FIFO.
Definition weikai.h:59
size_t count_
count number of element in the buffer
Definition weikai.h:118
bool is_full()
test is the ring buffer is full ?
Definition weikai.h:101
size_t free()
returns the number of free positions in the buffer
Definition weikai.h:109
int tail_
position of the next element to read
Definition weikai.h:116
bool push(const T item)
pushes an item at the tail of the fifo
Definition weikai.h:64
bool peek(T &item)
return the value of the item at fifo's head without removing it
Definition weikai.h:88
std::array< T, SIZE > rb_
the ring buffer
Definition weikai.h:115
int head_
position of the next element to write
Definition weikai.h:117
void clear()
clear the buffer content
Definition weikai.h:112
bool is_empty()
test is the Ring Buffer is empty ?
Definition weikai.h:97
bool pop(T &item)
return and remove the item at head of the fifo
Definition weikai.h:76
size_t count()
return the number of item in the ring buffer
Definition weikai.h:105
The WeikaiChannel class is used to implement all the virtual methods of the ESPHome uart::UARTCompone...
Definition weikai.h:296
size_t available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:403
virtual void dump_channel()
dump channel information
Definition weikai.cpp:266
virtual void setup_channel()
Setup the channel.
Definition weikai.cpp:254
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:326
void set_line_param_()
set the line parameters
Definition weikai.cpp:283
void set_baudrate_()
set the baud rate
Definition weikai.cpp:304
uint8_t data_
a one byte buffer for register read storage
Definition weikai.h:438
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:447
void check_logger_conflict() override
this cannot happen with external uart therefore we do nothing
Definition weikai.h:388
virtual bool check_channel_down()
check if channel is alive
Definition weikai.cpp:377
std::string name_
name of the entity
Definition weikai.h:439
uart::UARTFlushResult flush() override
Flush the output fifo.
Definition weikai.cpp:435
const char * get_channel_name()
Get the channel name.
Definition weikai.h:315
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:436
WKRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:435
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:311
void reset_fifo_()
reset the weikai internal FIFO
Definition weikai.cpp:276
bool tx_fifo_is_not_empty_()
test if transmit buffer is not empty in the status register (optimization)
Definition weikai.cpp:330
uint8_t channel_
our Channel number
Definition weikai.h:437
void set_parent(WeikaiComponent *parent)
We belongs to this WeikaiComponent.
Definition weikai.h:300
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:346
void set_channel(uint8_t channel)
Sets the channel number.
Definition weikai.h:307
size_t tx_in_fifo_()
Returns the number of bytes in the transmit fifo.
Definition weikai.cpp:332
void write_array(const uint8_t *buffer, size_t length) override
Writes a specified number of bytes to a serial port.
Definition weikai.cpp:427
The WeikaiComponent class stores the information global to the WeiKai component and provides methods ...
Definition weikai.h:195
void set_name(std::string &&name)
store the name for the component
Definition weikai.h:210
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:260
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:258
uint32_t crystal_
crystal value;
Definition weikai.h:259
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:257
bool page1_
set to true when in "page1 mode"
Definition weikai.h:261
void set_crystal(uint32_t crystal)
store crystal frequency
Definition weikai.h:202
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:262
void set_test_mode(int test_mode)
store if the component is in test mode
Definition weikai.h:206
void loop() override
override the Component loop()
Definition weikai.cpp:97
std::string name_
name of entity
Definition weikai.h:263
void set_pin_direction_(uint8_t pin, gpio::Flags flags)
Helper method to set the pin mode of a pin.
Definition weikai.cpp:224
float get_setup_priority() const override
Get the priority of the component.
Definition weikai.h:235
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:256
virtual ~WeikaiComponent()
virtual destructor
Definition weikai.h:198
const char * get_name()
Get the name of the component.
Definition weikai.h:214
Helper class to expose a WeiKai family IO pin as an internal GPIO pin.
Definition weikai.h:269
void digital_write(bool value) override
Definition weikai.h:282
void set_inverted(bool inverted)
Definition weikai.h:273
bool digital_read() override
Definition weikai.h:281
void set_pin(uint8_t pin)
Definition weikai.h:272
gpio::Flags get_flags() const override
Definition weikai.h:276
void pin_mode(gpio::Flags flags) override
Definition weikai.h:280
void set_parent(WeikaiComponent *parent)
Definition weikai.h:271
void set_flags(gpio::Flags flags)
Definition weikai.h:274
size_t dump_summary(char *buffer, size_t len) const override
Definition weikai.cpp:247
WeikaiComponent * parent_
Definition weikai.h:285
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:137
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:184
WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
WeikaiRegister constructor.
Definition weikai.h:143
uint8_t channel_
channel for this register
Definition weikai.h:185
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:183
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
bool uart_receive_test_(char *message)
Test the read_array() method.
Definition weikai.cpp:523
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:509
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:37
UARTFlushResult
Result of a flush() call.
When the TEST_COMPONENT flag is defined we include some auto-test methods.
Definition weikai.cpp:9
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:32
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition weikai.h:41
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:38
const void size_t len
Definition hal.h:64
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - registers' definition.