ESPHome  2024.4.0
sm16716.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 #include <vector>
7 
8 namespace esphome {
9 namespace sm16716 {
10 
11 class SM16716 : public Component {
12  public:
13  class Channel;
14 
15  void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
16  void set_clock_pin(GPIOPin *clock_pin) { clock_pin_ = clock_pin; }
17  void set_num_channels(uint8_t num_channels) { num_channels_ = num_channels; }
18  void set_num_chips(uint8_t num_chips) { num_chips_ = num_chips; }
19 
20  void setup() override;
21 
22  void dump_config() override;
23 
24  float get_setup_priority() const override { return setup_priority::HARDWARE; }
25 
27  void loop() override;
28 
29  class Channel : public output::FloatOutput {
30  public:
31  void set_parent(SM16716 *parent) { parent_ = parent; }
32  void set_channel(uint8_t channel) { channel_ = channel; }
33 
34  protected:
35  void write_state(float state) override {
36  auto amount = uint8_t(state * 0xFF);
37  this->parent_->set_channel_value_(this->channel_, amount);
38  }
39 
41  uint8_t channel_;
42  };
43 
44  protected:
45  void set_channel_value_(uint8_t channel, uint8_t value) {
46  uint8_t index = this->num_channels_ - channel - 1;
47  if (this->pwm_amounts_[index] != value) {
48  this->update_ = true;
49  }
50  this->pwm_amounts_[index] = value;
51  }
52  void write_bit_(bool value) {
53  this->data_pin_->digital_write(value);
54  this->clock_pin_->digital_write(true);
55  this->clock_pin_->digital_write(false);
56  }
57  void write_byte_(uint8_t data) {
58  for (uint8_t mask = 0x80; mask; mask >>= 1) {
59  this->write_bit_(data & mask);
60  }
61  }
62 
65  uint8_t num_channels_;
66  uint8_t num_chips_;
67  std::vector<uint8_t> pwm_amounts_;
68  bool update_{true};
69 };
70 
71 } // namespace sm16716
72 } // namespace esphome
virtual void digital_write(bool value)=0
void set_clock_pin(GPIOPin *clock_pin)
Definition: sm16716.h:16
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void set_channel_value_(uint8_t channel, uint8_t value)
Definition: sm16716.h:45
void write_state(float state) override
Definition: sm16716.h:35
void setup() override
Definition: sm16716.cpp:9
float get_setup_priority() const override
Definition: sm16716.h:24
void set_num_chips(uint8_t num_chips)
Definition: sm16716.h:18
std::vector< uint8_t > pwm_amounts_
Definition: sm16716.h:67
void set_num_channels(uint8_t num_channels)
Definition: sm16716.h:17
void dump_config() override
Definition: sm16716.cpp:17
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void set_data_pin(GPIOPin *data_pin)
Definition: sm16716.h:15
void write_bit_(bool value)
Definition: sm16716.h:52
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_channel(uint8_t channel)
Definition: sm16716.h:32
void loop() override
Send new values if they were updated.
Definition: sm16716.cpp:24
void set_parent(SM16716 *parent)
Definition: sm16716.h:31
void write_byte_(uint8_t data)
Definition: sm16716.h:57
bool state
Definition: fan.h:34