ESPHome  2024.11.0
weikai.h
Go to the documentation of this file.
1 
10 #pragma once
11 #include <bitset>
12 #include <memory>
13 #include <cinttypes>
14 #include "esphome/core/component.h"
16 #include "wk_reg_def.h"
17 
27 // #define TEST_COMPONENT
28 
29 namespace esphome {
30 namespace weikai {
31 
33 #if defined(I2C_BUFFER_LENGTH)
34 constexpr size_t XFER_MAX_SIZE = I2C_BUFFER_LENGTH;
35 #else
36 constexpr size_t XFER_MAX_SIZE = 128;
37 #endif
38 
40 constexpr size_t FIFO_SIZE = 256;
41 
43 constexpr size_t RING_BUFFER_SIZE = FIFO_SIZE;
44 
61 template<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 
123 class 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 
190 class WeikaiChannel; // forward declaration
197 class WeikaiComponent : public Component {
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
250  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 
271 class 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  void setup() override;
279  std::string dump_summary() 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:
285  WeikaiComponent *parent_{nullptr};
286  uint8_t pin_;
287  bool inverted_;
289 };
290 
297  public:
300  void set_parent(WeikaiComponent *parent) {
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  int available() override;
377 
382  void 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 
409  bool tx_fifo_is_not_empty_();
410 
413  size_t xfer_fifo_to_buffer_();
414 
417  bool virtual check_channel_down();
418 
419 #ifdef TEST_COMPONENT
420 
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 weikai
443 } // namespace esphome
void clear()
clear the buffer content
Definition: weikai.h:114
void setup()
const char * name
Definition: stm32flash.h:78
void loop()
void set_channel_name(std::string &&name)
The name as generated by the Python code generator.
Definition: weikai.h:311
std::array< T, SIZE > rb_
the ring buffer
Definition: weikai.h:117
void set_channel(uint8_t channel)
Sets the channel number.
Definition: weikai.h:307
bool is_empty()
test is the Ring Buffer is empty ?
Definition: weikai.h:99
void check_logger_conflict() override
this cannot happen with external uart therefore we do nothing
Definition: weikai.h:388
void set_name(std::string &&name)
store the name for the component
Definition: weikai.h:212
WeikaiRegister(WeikaiComponent *const comp, uint8_t reg, uint8_t channel)
WeikaiRegister constructor.
Definition: weikai.h:145
WKRingBuffer< uint8_t, RING_BUFFER_SIZE > receive_buffer_
the buffer where we store temporarily the bytes received
Definition: weikai.h:435
const char * get_name()
Get the name of the component.
Definition: weikai.h:216
WeiKai component family - registers&#39; definition.
size_t count_
count number of element in the buffer
Definition: weikai.h:120
constexpr size_t XFER_MAX_SIZE
XFER_MAX_SIZE defines the maximum number of bytes allowed during one transfer.
Definition: weikai.h:34
uint32_t crystal_
crystal value;
Definition: weikai.h:261
size_t free()
returns the number of free positions in the buffer
Definition: weikai.h:111
std::vector< WeikaiChannel * > children_
the list of WeikaiChannel UART children
Definition: weikai.h:264
WeikaiComponent * parent_
our WK2168component parent
Definition: weikai.h:436
std::string name_
name of the entity
Definition: weikai.h:439
This is an helper class that provides a simple ring buffers that works as a FIFO. ...
Definition: weikai.h:61
Helper class to expose a WeiKai family IO pin as an internal GPIO pin.
Definition: weikai.h:271
size_t count()
return the number of item in the ring buffer
Definition: weikai.h:107
int tail_
position of the next element to read
Definition: weikai.h:118
The WeikaiComponent class stores the information global to the WeiKai component and provides methods ...
Definition: weikai.h:197
float get_setup_priority() const override
Get the priority of the component.
Definition: weikai.h:237
void set_flags(gpio::Flags flags)
Definition: weikai.h:276
const float BUS
For communication buses like i2c/spi.
Definition: component.cpp:16
WeikaiRegister & reg(uint8_t reg)
Factory method to create a WeikaiRegister proxy object.
Definition: weikai.h:326
int test_mode_
test mode value (0 -> no tests)
Definition: weikai.h:262
void set_parent(WeikaiComponent *parent)
Definition: weikai.h:273
bool peek(T &item)
return the value of the item at fifo&#39;s head without removing it
Definition: weikai.h:90
void digital_write(bool value) override
Definition: weikai.h:282
The WeikaiChannel class is used to implement all the virtual methods of the ESPHome uart::UARTCompone...
Definition: weikai.h:296
void set_test_mode(int test_mode)
store if the component is in test mode
Definition: weikai.h:208
void set_pin(uint8_t pin)
Definition: weikai.h:274
uint8_t channel_
our Channel number
Definition: weikai.h:437
const uint32_t flags
Definition: stm32flash.h:85
void pin_mode(gpio::Flags flags) override
Definition: weikai.h:280
constexpr size_t RING_BUFFER_SIZE
size of the ring buffer set to size of the FIFO
Definition: weikai.h:43
void set_parent(WeikaiComponent *parent)
We belongs to this WeikaiComponent.
Definition: weikai.h:300
int head_
position of the next element to write
Definition: weikai.h:119
WeikaiRegister objects acts as proxies to access remote register independently of the bus type...
Definition: weikai.h:139
void set_inverted(bool inverted)
Definition: weikai.h:275
bool push(const T item)
pushes an item at the tail of the fifo
Definition: weikai.h:66
bool pop(T &item)
return and remove the item at head of the fifo
Definition: weikai.h:78
uint16_t length
Definition: tt21100.cpp:12
uint8_t register_
address of the register
Definition: weikai.h:186
uint8_t data_
a one byte buffer for register read storage
Definition: weikai.h:438
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool digital_read() override
Definition: weikai.h:281
bool is_full()
test is the ring buffer is full ?
Definition: weikai.h:103
constexpr size_t FIFO_SIZE
size of the internal WeiKai FIFO
Definition: weikai.h:40
const char * get_channel_name()
Get the channel name.
Definition: weikai.h:315
void set_crystal(uint32_t crystal)
store crystal frequency
Definition: weikai.h:204
WeikaiComponent *const comp_
pointer to our parent (aggregation)
Definition: weikai.h:185
std::string name_
name of entity
Definition: weikai.h:265
virtual ~WeikaiComponent()
virtual destructor
Definition: weikai.h:200
uint8_t channel_
channel for this register
Definition: weikai.h:187