ESPHome  2024.11.1
sm2135.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
6 #include "esphome/core/hal.h"
7 
8 namespace esphome {
9 namespace sm2135 {
10 
11 enum SM2135Current : uint8_t {
19  SM2135_CURRENT_45MA = 0x07, // Max value for RGB
23 };
24 
25 class SM2135 : public Component {
26  public:
27  class Channel;
28 
29  void set_data_pin(GPIOPin *data_pin) { this->data_pin_ = data_pin; }
30  void set_clock_pin(GPIOPin *clock_pin) { this->clock_pin_ = clock_pin; }
31 
32  void set_rgb_current(SM2135Current rgb_current) {
33  this->rgb_current_ = rgb_current;
34  this->current_mask_ = (this->rgb_current_ << 4) | this->cw_current_;
35  }
36 
37  void set_cw_current(SM2135Current cw_current) {
38  this->cw_current_ = cw_current;
39  this->current_mask_ = (this->rgb_current_ << 4) | this->cw_current_;
40  }
41 
42  void set_separate_modes(bool separate_modes) { this->separate_modes_ = separate_modes; }
43 
44  void setup() override;
45 
46  void dump_config() override;
47 
48  float get_setup_priority() const override { return setup_priority::HARDWARE; }
49 
51  void loop() override;
52 
53  class Channel : public output::FloatOutput {
54  public:
55  void set_parent(SM2135 *parent) { parent_ = parent; }
56  void set_channel(uint8_t channel) { channel_ = channel; }
57 
58  protected:
59  void write_state(float state) override {
60  auto amount = static_cast<uint8_t>(state * 0xff);
61  this->parent_->set_channel_value_(this->channel_, amount);
62  }
63 
65  uint8_t channel_;
66  };
67 
68  protected:
69  void set_channel_value_(uint8_t channel, uint8_t value);
70  void sm2135_set_low_(GPIOPin *pin);
71  void sm2135_set_high_(GPIOPin *pin);
72 
73  void sm2135_start_();
74  void sm2135_stop_();
75  void write_byte_(uint8_t data);
76  void write_buffer_(uint8_t *buffer, uint8_t size);
77 
80  uint8_t current_mask_;
84  uint8_t update_channel_;
85  std::vector<uint8_t> pwm_amounts_;
86  bool update_{true};
87 };
88 
89 } // namespace sm2135
90 } // namespace esphome
void set_rgb_current(SM2135Current rgb_current)
Definition: sm2135.h:32
GPIOPin * data_pin_
Definition: sm2135.h:78
void set_separate_modes(bool separate_modes)
Definition: sm2135.h:42
void sm2135_set_low_(GPIOPin *pin)
Definition: sm2135.cpp:141
void sm2135_set_high_(GPIOPin *pin)
Definition: sm2135.cpp:146
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
SM2135Current cw_current_
Definition: sm2135.h:82
void set_channel_value_(uint8_t channel, uint8_t value)
Definition: sm2135.cpp:133
void set_clock_pin(GPIOPin *clock_pin)
Definition: sm2135.h:30
void set_channel(uint8_t channel)
Definition: sm2135.h:56
float get_setup_priority() const override
Definition: sm2135.h:48
SM2135Current rgb_current_
Definition: sm2135.h:81
uint8_t current_mask_
Definition: sm2135.h:80
void dump_config() override
Definition: sm2135.cpp:37
void write_byte_(uint8_t data)
Definition: sm2135.cpp:45
uint8_t update_channel_
Definition: sm2135.h:84
std::vector< uint8_t > pwm_amounts_
Definition: sm2135.h:85
void write_state(float state) override
Definition: sm2135.h:59
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition: sm2135.cpp:81
void set_parent(SM2135 *parent)
Definition: sm2135.h:55
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void loop() override
Send new values if they were updated.
Definition: sm2135.cpp:92
void set_data_pin(GPIOPin *data_pin)
Definition: sm2135.h:29
void set_cw_current(SM2135Current cw_current)
Definition: sm2135.h:37
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
GPIOPin * clock_pin_
Definition: sm2135.h:79
void setup() override
Definition: sm2135.cpp:22
bool state
Definition: fan.h:34