ESPHome  2022.6.2
my9231.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 my9231 {
10 
13  public:
14  class Channel;
15  void set_pin_di(GPIOPin *pin_di) { pin_di_ = pin_di; }
16  void set_pin_dcki(GPIOPin *pin_dcki) { pin_dcki_ = pin_dcki; }
17 
18  void set_num_channels(uint16_t num_channels) { this->num_channels_ = num_channels; }
19  void set_num_chips(uint8_t num_chips) { this->num_chips_ = num_chips; }
20  void set_bit_depth(uint8_t bit_depth) { this->bit_depth_ = bit_depth; }
21 
23  void setup() override;
24  void dump_config() override;
26  float get_setup_priority() const override { return setup_priority::HARDWARE; }
28  void loop() override;
29 
30  class Channel : public output::FloatOutput {
31  public:
32  void set_parent(MY9231OutputComponent *parent) { parent_ = parent; }
33  void set_channel(uint8_t channel) { channel_ = channel; }
34 
35  protected:
36  void write_state(float state) override {
37  auto amount = uint16_t(state * this->parent_->get_max_amount_());
38  this->parent_->set_channel_value_(this->channel_, amount);
39  }
40 
42  uint8_t channel_;
43  };
44 
45  protected:
46  uint16_t get_max_amount_() const { return (uint32_t(1) << this->bit_depth_) - 1; }
47 
48  void set_channel_value_(uint8_t channel, uint16_t value);
49  void init_chips_(uint8_t command);
50  void write_word_(uint16_t value, uint8_t bits);
51  void send_di_pulses_(uint8_t count);
52 
55  uint8_t bit_depth_;
56  uint16_t num_channels_;
57  uint8_t num_chips_;
58  std::vector<uint16_t> pwm_amounts_;
59  bool update_{true};
60 };
61 
62 } // namespace my9231
63 } // namespace esphome
void set_channel_value_(uint8_t channel, uint16_t value)
Definition: my9231.cpp:76
void write_state(float state) override
Definition: my9231.h:36
float get_setup_priority() const override
HARDWARE setup_priority.
Definition: my9231.h:26
void set_bit_depth(uint8_t bit_depth)
Definition: my9231.h:20
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void set_num_channels(uint16_t num_channels)
Definition: my9231.h:18
void set_parent(MY9231OutputComponent *parent)
Definition: my9231.h:32
void set_pin_dcki(GPIOPin *pin_dcki)
Definition: my9231.h:16
MY9231 float output component.
Definition: my9231.h:12
void setup() override
Setup the MY9231.
Definition: my9231.cpp:29
void set_pin_di(GPIOPin *pin_di)
Definition: my9231.h:15
void send_di_pulses_(uint8_t count)
Definition: my9231.cpp:102
std::vector< uint16_t > pwm_amounts_
Definition: my9231.h:58
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void set_num_chips(uint8_t num_chips)
Definition: my9231.h:19
void write_word_(uint16_t value, uint8_t bits)
Definition: my9231.cpp:96
Definition: a4988.cpp:4
void loop() override
Send new values if they were updated.
Definition: my9231.cpp:65
void init_chips_(uint8_t command)
Definition: my9231.cpp:84
uint16_t get_max_amount_() const
Definition: my9231.h:46
bool state
Definition: fan.h:34