ESPHome  2022.4.0
sm2135.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 sm2135 {
10 
11 class SM2135 : 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(SM2135 *parent) { parent_ = parent; }
30  void set_channel(uint8_t channel) { channel_ = channel; }
31 
32  protected:
33  void write_state(float state) override {
34  auto amount = static_cast<uint8_t>(state * 0xff);
35  this->parent_->set_channel_value_(this->channel_, amount);
36  }
37 
39  uint8_t channel_;
40  };
41 
42  protected:
43  void set_channel_value_(uint8_t channel, uint8_t value) {
44  if (this->pwm_amounts_[channel] != value) {
45  this->update_ = true;
46  this->update_channel_ = channel;
47  }
48  this->pwm_amounts_[channel] = value;
49  }
50  void write_bit_(bool value) {
51  this->clock_pin_->digital_write(false);
52  this->data_pin_->digital_write(value);
53  this->clock_pin_->digital_write(true);
54  }
55 
56  void write_byte_(uint8_t data) {
57  for (uint8_t mask = 0x80; mask; mask >>= 1) {
58  this->write_bit_(data & mask);
59  }
60  this->clock_pin_->digital_write(false);
61  this->data_pin_->digital_write(true);
62  this->clock_pin_->digital_write(true);
63  }
64 
65  void write_buffer_(uint8_t *buffer, uint8_t size) {
66  this->data_pin_->digital_write(false);
67  for (uint32_t i = 0; i < size; i++) {
68  this->write_byte_(buffer[i]);
69  }
70  this->clock_pin_->digital_write(false);
71  this->clock_pin_->digital_write(true);
72  this->data_pin_->digital_write(true);
73  }
74 
77  uint8_t update_channel_;
78  std::vector<uint8_t> pwm_amounts_;
79  bool update_{true};
80 };
81 
82 } // namespace sm2135
83 } // namespace esphome
virtual void digital_write(bool value)=0
GPIOPin * data_pin_
Definition: sm2135.h:75
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: sm2135.h:43
void set_clock_pin(GPIOPin *clock_pin)
Definition: sm2135.h:16
void set_channel(uint8_t channel)
Definition: sm2135.h:30
float get_setup_priority() const override
Definition: sm2135.h:22
void dump_config() override
Definition: sm2135.cpp:44
void write_byte_(uint8_t data)
Definition: sm2135.h:56
uint8_t update_channel_
Definition: sm2135.h:77
std::vector< uint8_t > pwm_amounts_
Definition: sm2135.h:78
void write_state(float state) override
Definition: sm2135.h:33
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition: sm2135.h:65
void set_parent(SM2135 *parent)
Definition: sm2135.h:29
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void loop() override
Send new values if they were updated.
Definition: sm2135.cpp:50
void set_data_pin(GPIOPin *data_pin)
Definition: sm2135.h:15
Definition: a4988.cpp:4
GPIOPin * clock_pin_
Definition: sm2135.h:76
void write_bit_(bool value)
Definition: sm2135.h:50
void setup() override
Definition: sm2135.cpp:36
bool state
Definition: fan.h:41