ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
sm2135.cpp
Go to the documentation of this file.
1#include "sm2135.h"
2#include "esphome/core/log.h"
3
4// Tnx to the work of https://github.com/arendst (Tasmota) for making the initial version of the driver
5
6namespace esphome {
7namespace sm2135 {
8
9static const char *const TAG = "sm2135";
10
11static const uint8_t SM2135_ADDR_MC = 0xC0; // Max current register
12static const uint8_t SM2135_ADDR_CH = 0xC1; // RGB or CW channel select register
13static const uint8_t SM2135_ADDR_R = 0xC2; // Red color
14static const uint8_t SM2135_ADDR_G = 0xC3; // Green color
15static const uint8_t SM2135_ADDR_B = 0xC4; // Blue color
16static const uint8_t SM2135_ADDR_C = 0xC5; // Cold
17static const uint8_t SM2135_ADDR_W = 0xC6; // Warm
18
19static const uint8_t SM2135_RGB = 0x00; // RGB channel
20static const uint8_t SM2135_CW = 0x80; // CW channel (Chip default)
21
23 this->data_pin_->setup();
24 this->data_pin_->digital_write(false);
26 this->clock_pin_->setup();
27 this->clock_pin_->digital_write(false);
29
32
33 this->pwm_amounts_.resize(5, 0);
34}
35
37 ESP_LOGCONFIG(TAG, "SM2135:");
38 LOG_PIN(" Data Pin: ", this->data_pin_);
39 LOG_PIN(" Clock Pin: ", this->clock_pin_);
40 ESP_LOGCONFIG(TAG, " CW Current: %dmA", 10 + (this->cw_current_ * 5));
41 ESP_LOGCONFIG(TAG, " RGB Current: %dmA", 10 + (this->rgb_current_ * 5));
42}
43
44void SM2135::write_byte_(uint8_t data) {
45 for (uint8_t mask = 0x80; mask; mask >>= 1) {
46 if (mask & data) {
47 this->sm2135_set_high_(this->data_pin_);
48 } else {
49 this->sm2135_set_low_(this->data_pin_);
50 }
51
52 this->sm2135_set_high_(this->clock_pin_);
55 }
56
57 this->sm2135_set_high_(this->data_pin_);
58 this->sm2135_set_high_(this->clock_pin_);
60 this->sm2135_set_low_(this->clock_pin_);
62 this->sm2135_set_low_(this->data_pin_);
63}
64
66 this->sm2135_set_low_(this->data_pin_);
68 this->sm2135_set_low_(this->clock_pin_);
69}
70
79
80void SM2135::write_buffer_(uint8_t *buffer, uint8_t size) {
81 this->sm2135_start_();
82
83 this->data_pin_->digital_write(false);
84 for (uint32_t i = 0; i < size; i++) {
85 this->write_byte_(buffer[i]);
86 }
87
88 this->sm2135_stop_();
89}
90
92 if (!this->update_)
93 return;
94
95 this->sm2135_start_();
96 this->write_byte_(SM2135_ADDR_MC);
98
99 if (this->separate_modes_) {
100 if (this->update_channel_ == 3 || this->update_channel_ == 4) {
101 // No color so must be Cold/Warm
102
103 this->write_byte_(SM2135_CW);
104 this->sm2135_stop_();
105 delay(1);
106 this->sm2135_start_();
107 this->write_byte_(SM2135_ADDR_C);
108 this->write_byte_(this->pwm_amounts_[3]);
109 this->write_byte_(this->pwm_amounts_[4]);
110 } else {
111 // Color
112
113 this->write_byte_(SM2135_RGB);
114 this->write_byte_(this->pwm_amounts_[0]);
115 this->write_byte_(this->pwm_amounts_[1]);
116 this->write_byte_(this->pwm_amounts_[2]);
117 }
118 } else {
119 this->write_byte_(SM2135_RGB);
120 this->write_byte_(this->pwm_amounts_[0]);
121 this->write_byte_(this->pwm_amounts_[1]);
122 this->write_byte_(this->pwm_amounts_[2]);
123 this->write_byte_(this->pwm_amounts_[3]);
124 this->write_byte_(this->pwm_amounts_[4]);
125 }
126
127 this->sm2135_stop_();
128
129 this->update_ = false;
130}
131
132void SM2135::set_channel_value_(uint8_t channel, uint8_t value) {
133 if (this->pwm_amounts_[channel] != value) {
134 this->update_ = true;
135 this->update_channel_ = channel;
136 }
137 this->pwm_amounts_[channel] = value;
138}
139
141 pin->digital_write(false);
143}
144
146 pin->digital_write(true);
148}
149
150} // namespace sm2135
151} // namespace esphome
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
void write_byte_(uint8_t data)
Definition sm2135.cpp:44
void loop() override
Send new values if they were updated.
Definition sm2135.cpp:91
uint8_t update_channel_
Definition sm2135.h:84
SM2135Current cw_current_
Definition sm2135.h:82
std::vector< uint8_t > pwm_amounts_
Definition sm2135.h:85
void sm2135_set_high_(GPIOPin *pin)
Definition sm2135.cpp:145
void set_channel_value_(uint8_t channel, uint8_t value)
Definition sm2135.cpp:132
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition sm2135.cpp:80
SM2135Current rgb_current_
Definition sm2135.h:81
GPIOPin * clock_pin_
Definition sm2135.h:79
void sm2135_set_low_(GPIOPin *pin)
Definition sm2135.cpp:140
void dump_config() override
Definition sm2135.cpp:36
GPIOPin * data_pin_
Definition sm2135.h:78
void setup() override
Definition sm2135.cpp:22
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_PULLUP
Definition gpio.h:21
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29