ESPHome  2024.12.2
bp5758d.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 bp5758d {
10 
11 class BP5758D : 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 
18  void setup() override;
19 
20  void dump_config() override;
21 
22  float get_setup_priority() const override { return setup_priority::HARDWARE; }
23 
25  void loop() override;
26 
27  class Channel : public output::FloatOutput {
28  public:
29  void set_parent(BP5758D *parent) { parent_ = parent; }
30  void set_channel(uint8_t channel) { channel_ = channel; }
31  void set_current(uint8_t current) { current_ = current; }
32 
33  protected:
34  void write_state(float state) override {
35  auto amount = static_cast<uint16_t>(state * 0x3FF);
36  // We're enforcing channels start at 1 to mach OUT1-OUT5, we must adjust
37  // to our 0-based array internally here by subtracting 1.
38  this->parent_->set_channel_value_(this->channel_ - 1, amount);
39  this->parent_->set_channel_current_(this->channel_ - 1, this->current_);
40  }
41 
43  uint8_t channel_;
44  uint8_t current_;
45  };
46 
47  protected:
48  uint8_t correct_current_level_bits_(uint8_t current);
49  void set_channel_value_(uint8_t channel, uint16_t value);
50  void set_channel_current_(uint8_t channel, uint8_t current);
51  void write_bit_(bool value);
52  void write_byte_(uint8_t data);
53  void write_buffer_(uint8_t *buffer, uint8_t size);
54 
57  uint8_t update_channel_;
58  std::vector<uint8_t> channel_current_;
59  std::vector<uint16_t> pwm_amounts_;
60  bool update_{true};
61 };
62 
63 } // namespace bp5758d
64 } // namespace esphome
void write_state(float state) override
Definition: bp5758d.h:34
void set_data_pin(GPIOPin *data_pin)
Definition: bp5758d.h:15
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void set_channel_current_(uint8_t channel, uint8_t current)
Definition: bp5758d.cpp:127
void set_clock_pin(GPIOPin *clock_pin)
Definition: bp5758d.h:16
void set_channel(uint8_t channel)
Definition: bp5758d.h:30
void dump_config() override
Definition: bp5758d.cpp:33
std::vector< uint16_t > pwm_amounts_
Definition: bp5758d.h:59
void setup() override
Definition: bp5758d.cpp:22
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition: bp5758d.cpp:152
void set_channel_value_(uint8_t channel, uint16_t value)
Definition: bp5758d.cpp:119
void loop() override
Send new values if they were updated.
Definition: bp5758d.cpp:39
void set_parent(BP5758D *parent)
Definition: bp5758d.h:29
float get_setup_priority() const override
Definition: bp5758d.h:22
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void set_current(uint8_t current)
Definition: bp5758d.h:31
std::vector< uint8_t > channel_current_
Definition: bp5758d.h:58
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_bit_(bool value)
Definition: bp5758d.cpp:129
void write_byte_(uint8_t data)
Definition: bp5758d.cpp:138
bool state
Definition: fan.h:34
uint8_t correct_current_level_bits_(uint8_t current)
Definition: bp5758d.cpp:104