ESPHome 2025.5.0
Loading...
Searching...
No Matches
weikai.h
Go to the documentation of this file.
1
9
10#pragma once
11#include <bitset>
12#include <memory>
13#include <cinttypes>
16#include "wk_reg_def.h"
17
27// #define TEST_COMPONENT
28
29namespace esphome {
30namespace weikai {
31
33#if defined(I2C_BUFFER_LENGTH)
34constexpr size_t XFER_MAX_SIZE = I2C_BUFFER_LENGTH;
35#else
36constexpr size_t XFER_MAX_SIZE = 128;
37#endif
38
40constexpr size_t FIFO_SIZE = 256;
41
43constexpr size_t RING_BUFFER_SIZE = FIFO_SIZE;
44
61template<typename T, size_t SIZE> class WKRingBuffer {
62 public:
66 bool push(const T item) {
67 if (is_full())
68 return false;
69 this->rb_[this->head_] = item;
70 this->head_ = (this->head_ + 1) % SIZE;
71 this->count_++;
72 return true;
73 }
74
78 bool pop(T &item) {
79 if (is_empty())
80 return false;
81 item = this->rb_[this->tail_];
82 this->tail_ = (this->tail_ + 1) % SIZE;
83 this->count_--;
84 return true;
85 }
86
90 bool peek(T &item) {
91 if (is_empty())
92 return false;
93 item = this->rb_[this->tail_];
94 return true;
95 }
96
99 inline bool is_empty() { return (this->count_ == 0); }
100
103 inline bool is_full() { return (this->count_ == SIZE); }
104
107 inline size_t count() { return this->count_; }
108
111 inline size_t free() { return SIZE - this->count_; }
112
114 inline void clear() { this->head_ = this->tail_ = this->count_ = 0; }
115
116 protected:
117 std::array<T, SIZE> rb_{0};
118 int tail_{0};
119 int head_{0};
120 size_t count_{0};
121};
122
123class WeikaiComponent;
124// class WeikaiComponentSPI;
140 public:
145 WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
146 : comp_(comp), register_(reg), channel_(channel) {}
147 virtual ~WeikaiRegister() {}
148
152 WeikaiRegister &operator=(uint8_t value);
153
157 WeikaiRegister &operator&=(uint8_t value);
158
162 WeikaiRegister &operator|=(uint8_t value);
163
165 operator uint8_t() const { return read_reg(); }
166
169 virtual uint8_t read_reg() const = 0;
170
173 virtual void write_reg(uint8_t value) = 0;
174
178 virtual void read_fifo(uint8_t *data, size_t length) const = 0;
179
183 virtual void write_fifo(uint8_t *data, size_t length) = 0;
184
186 uint8_t register_;
187 uint8_t channel_;
188};
189
190class WeikaiChannel; // forward declaration
198 public:
200 virtual ~WeikaiComponent() {}
201
204 void set_crystal(uint32_t crystal) { this->crystal_ = crystal; }
205
208 void set_test_mode(int test_mode) { this->test_mode_ = test_mode; }
209
212 void set_name(std::string &&name) { this->name_ = std::move(name); }
213
216 const char *get_name() { return this->name_.c_str(); }
217
219 void loop() override;
220
221 bool page1() { return page1_; }
222
227 virtual WeikaiRegister &reg(uint8_t reg, uint8_t channel) = 0;
228
229 protected:
230 friend class WeikaiChannel;
231
237 float get_setup_priority() const override { return setup_priority::BUS - 0.1F; }
238
239 friend class WeikaiGPIOPin;
241 bool read_pin_val_(uint8_t pin);
242
244 void write_pin_val_(uint8_t pin, bool value);
245
247 void set_pin_direction_(uint8_t pin, gpio::Flags flags);
248
249#ifdef TEST_COMPONENT
253 void test_gpio_input_();
254 void test_gpio_output_();
256#endif
257
258 uint8_t pin_config_{0x00};
259 uint8_t output_state_{0x00};
260 uint8_t input_state_{0x00};
261 uint32_t crystal_;
263 bool page1_{false};
264 std::vector<WeikaiChannel *> children_{};
265 std::string name_;
266};
267
271class WeikaiGPIOPin : public GPIOPin {
272 public:
273 void set_parent(WeikaiComponent *parent) { this->parent_ = parent; }
274 void set_pin(uint8_t pin) { this->pin_ = pin; }
275 void set_inverted(bool inverted) { this->inverted_ = inverted; }
276 void set_flags(gpio::Flags flags) { this->flags_ = flags; }
277
278 gpio::Flags get_flags() const override { return this->flags_; }
279
280 void setup() override;
281 std::string dump_summary() const override;
282 void pin_mode(gpio::Flags flags) override { this->parent_->set_pin_direction_(this->pin_, flags); }
283 bool digital_read() override { return this->parent_->read_pin_val_(this->pin_) != this->inverted_; }
284 void digital_write(bool value) override { this->parent_->write_pin_val_(this->pin_, value != this->inverted_); }
285
286 protected:
288 uint8_t pin_;
291};
292
299 public:
303 this->parent_ = parent;
304 this->parent_->children_.push_back(this); // add ourself to the list (vector)
305 }
306
309 void set_channel(uint8_t channel) { this->channel_ = channel; }
310
313 void set_channel_name(std::string &&name) { this->name_ = std::move(name); }
314
317 const char *get_channel_name() { return this->name_.c_str(); }
318
320 void virtual setup_channel();
321
323 void virtual dump_channel();
324
328 WeikaiRegister &reg(uint8_t reg) { return this->parent_->reg(reg, channel_); }
329
330 //
331 // we implements/overrides the virtual class from UARTComponent
332 //
333
351 void write_array(const uint8_t *buffer, size_t length) override;
352
367 bool read_array(uint8_t *buffer, size_t length) override;
368
374 bool peek_byte(uint8_t *buffer) override;
375
378 int available() override;
379
384 void flush() override;
385
386 protected:
387 friend class WeikaiComponent;
388
390 void check_logger_conflict() override {}
391
393 void reset_fifo_();
394
396 void set_line_param_();
397
399 void set_baudrate_();
400
403 size_t rx_in_fifo_();
404
407 size_t tx_in_fifo_();
408
412
415 size_t xfer_fifo_to_buffer_();
416
419 bool virtual check_channel_down();
420
421#ifdef TEST_COMPONENT
424
427 void uart_send_test_(char *message);
428
432 bool uart_receive_test_(char *message);
434#endif
435
439 uint8_t channel_;
440 uint8_t data_;
441 std::string name_;
442};
443
444} // namespace weikai
445} // namespace esphome
This is an helper class that provides a simple ring buffers that works as a FIFO.
Definition weikai.h:61
size_t count_
count number of element in the buffer
Definition weikai.h:120
bool is_full()
test is the ring buffer is full ?
Definition weikai.h:103
size_t free()
returns the number of free positions in the buffer
Definition weikai.h:111
int tail_
position of the next element to read
Definition weikai.h:118
bool push(const T item)
pushes an item at the tail of the fifo
Definition weikai.h:66
bool peek(T &item)
return the value of the item at fifo's head without removing it
Definition weikai.h:90
std::array< T, SIZE > rb_
the ring buffer
Definition weikai.h:117
int head_
position of the next element to write
Definition weikai.h:119
void clear()
clear the buffer content
Definition weikai.h:114
bool is_empty()
test is the Ring Buffer is empty ?
Definition weikai.h:99
bool pop(T &item)
return and remove the item at head of the fifo
Definition weikai.h:78
size_t count()
return the number of item in the ring buffer
Definition weikai.h:107
The WeikaiChannel class is used to implement all the virtual methods of the ESPHome uart::UARTCompone...
Definition weikai.h:298
virtual void dump_channel()
dump channel information
Definition weikai.cpp:269
virtual void setup_channel()
Setup the channel.
Definition weikai.cpp:257
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition weikai.h:328
void flush() override
Flush the output fifo.
Definition weikai.cpp:431
void set_line_param_()
set the line parameters
Definition weikai.cpp:284
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:440
bool peek_byte(uint8_t *buffer) override
Reads the first byte in FIFO without removing it.
Definition weikai.cpp:392
size_t xfer_fifo_to_buffer_()
transfer bytes from the weikai internal FIFO to the buffer (if any)
Definition weikai.cpp:442
void check_logger_conflict() override
this cannot happen with external uart therefore we do nothing
Definition weikai.h:390
virtual bool check_channel_down()
check if channel is alive
Definition weikai.cpp:373
std::string name_
name of the entity
Definition weikai.h:441
const char * get_channel_name()
Get the channel name.
Definition weikai.h:317
WeikaiComponent * parent_
our WK2168component parent
Definition weikai.h:438
WKRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition weikai.h:437
bool read_array(uint8_t *buffer, size_t length) override
Reads a specified number of bytes from a serial port.
Definition weikai.cpp:406
void set_channel_name(std::string &&name)
The name as generated by the Python code generator.
Definition weikai.h:313
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:330
uint8_t channel_
our Channel number
Definition weikai.h:439
void set_parent(WeikaiComponent *parent)
We belongs to this WeikaiComponent.
Definition weikai.h:302
size_t rx_in_fifo_()
Returns the number of bytes in the receive fifo.
Definition weikai.cpp:345
void set_channel(uint8_t channel)
Sets the channel number.
Definition weikai.h:309
int available() override
Returns the number of bytes in the receive buffer.
Definition weikai.cpp:399
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:423
The WeikaiComponent class stores the information global to the WeiKai component and provides methods ...
Definition weikai.h:197
void set_name(std::string &&name)
store the name for the component
Definition weikai.h:212
int test_mode_
test mode value (0 -> no tests)
Definition weikai.h:262
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:260
uint32_t crystal_
crystal value;
Definition weikai.h:261
void write_pin_val_(uint8_t pin, bool value)
Helper method to write the value of a pin.
Definition weikai.cpp:215
bool read_pin_val_(uint8_t pin)
Helper method to read the value of a pin.
Definition weikai.cpp:209
uint8_t output_state_
output state: 1 means HIGH, 0 means LOW
Definition weikai.h:259
bool page1_
set to true when in "page1 mode"
Definition weikai.h:263
void set_crystal(uint32_t crystal)
store crystal frequency
Definition weikai.h:204
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition weikai.h:264
void set_test_mode(int test_mode)
store if the component is in test mode
Definition weikai.h:208
void loop() override
override the Component loop()
Definition weikai.cpp:104
std::string name_
name of entity
Definition weikai.h:265
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:237
uint8_t pin_config_
pin config mask: 1 means OUTPUT, 0 means INPUT
Definition weikai.h:258
virtual ~WeikaiComponent()
virtual destructor
Definition weikai.h:200
const char * get_name()
Get the name of the component.
Definition weikai.h:216
Helper class to expose a WeiKai family IO pin as an internal GPIO pin.
Definition weikai.h:271
void digital_write(bool value) override
Definition weikai.h:284
void set_inverted(bool inverted)
Definition weikai.h:275
bool digital_read() override
Definition weikai.h:283
void set_pin(uint8_t pin)
Definition weikai.h:274
gpio::Flags get_flags() const override
Definition weikai.h:278
void pin_mode(gpio::Flags flags) override
Definition weikai.h:282
void set_parent(WeikaiComponent *parent)
Definition weikai.h:273
std::string dump_summary() const override
Definition weikai.cpp:248
void set_flags(gpio::Flags flags)
Definition weikai.h:276
WeikaiComponent * parent_
Definition weikai.h:287
WeikaiRegister objects acts as proxies to access remote register independently of the bus type.
Definition weikai.h:139
virtual void write_reg(uint8_t value)=0
writes the register
WeikaiRegister & operator|=(uint8_t value)
overloads the compound |= operator.
Definition weikai.cpp:95
WeikaiRegister & operator&=(uint8_t value)
overloads the compound &= operator.
Definition weikai.cpp:89
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:84
uint8_t register_
address of the register
Definition weikai.h:186
WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
WeikaiRegister constructor.
Definition weikai.h:145
uint8_t channel_
channel for this register
Definition weikai.h:187
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition weikai.h:185
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
bool uart_receive_test_(char *message)
Test the read_array() method.
Definition weikai.cpp:516
void uart_send_test_(char *message)
Test the write_array() method.
Definition weikai.cpp:502
const float BUS
For communication buses like i2c/spi.
Definition component.cpp:16
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition weikai.h:34
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition weikai.h:43
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition weikai.h:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t length
Definition tt21100.cpp:0
WeiKai component family - registers' definition.