ESPHome  2024.3.2
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 setup() override;
43 
44  void dump_config() override;
45 
46  float get_setup_priority() const override { return setup_priority::HARDWARE; }
47 
49  void loop() override;
50 
51  class Channel : public output::FloatOutput {
52  public:
53  void set_parent(SM2135 *parent) { parent_ = parent; }
54  void set_channel(uint8_t channel) { channel_ = channel; }
55 
56  protected:
57  void write_state(float state) override {
58  auto amount = static_cast<uint8_t>(state * 0xff);
59  this->parent_->set_channel_value_(this->channel_, amount);
60  }
61 
63  uint8_t channel_;
64  };
65 
66  protected:
67  void set_channel_value_(uint8_t channel, uint8_t value);
68  void sm2135_set_low_(GPIOPin *pin);
69  void sm2135_set_high_(GPIOPin *pin);
70 
71  void sm2135_start_();
72  void sm2135_stop_();
73  void write_byte_(uint8_t data);
74  void write_buffer_(uint8_t *buffer, uint8_t size);
75 
78  uint8_t current_mask_;
81  uint8_t update_channel_;
82  std::vector<uint8_t> pwm_amounts_;
83  bool update_{true};
84 };
85 
86 } // namespace sm2135
87 } // namespace esphome
void set_rgb_current(SM2135Current rgb_current)
Definition: sm2135.h:32
GPIOPin * data_pin_
Definition: sm2135.h:76
void sm2135_set_low_(GPIOPin *pin)
Definition: sm2135.cpp:132
void sm2135_set_high_(GPIOPin *pin)
Definition: sm2135.cpp:137
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:80
void set_channel_value_(uint8_t channel, uint8_t value)
Definition: sm2135.cpp:124
void set_clock_pin(GPIOPin *clock_pin)
Definition: sm2135.h:30
void set_channel(uint8_t channel)
Definition: sm2135.h:54
float get_setup_priority() const override
Definition: sm2135.h:46
SM2135Current rgb_current_
Definition: sm2135.h:79
uint8_t current_mask_
Definition: sm2135.h:78
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:81
std::vector< uint8_t > pwm_amounts_
Definition: sm2135.h:82
void write_state(float state) override
Definition: sm2135.h:57
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition: sm2135.cpp:81
void set_parent(SM2135 *parent)
Definition: sm2135.h:53
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
GPIOPin * clock_pin_
Definition: sm2135.h:77
void setup() override
Definition: sm2135.cpp:22
bool state
Definition: fan.h:34