ESPHome 2026.1.4
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,
38 "SM2135:\n"
39 " CW Current: %dmA\n"
40 " RGB Current: %dmA",
41 10 + (this->cw_current_ * 5), 10 + (this->rgb_current_ * 5));
42 LOG_PIN(" Data Pin: ", this->data_pin_);
43 LOG_PIN(" Clock Pin: ", this->clock_pin_);
44}
45
46void SM2135::write_byte_(uint8_t data) {
47 for (uint8_t mask = 0x80; mask; mask >>= 1) {
48 if (mask & data) {
49 this->sm2135_set_high_(this->data_pin_);
50 } else {
51 this->sm2135_set_low_(this->data_pin_);
52 }
53
54 this->sm2135_set_high_(this->clock_pin_);
57 }
58
59 this->sm2135_set_high_(this->data_pin_);
60 this->sm2135_set_high_(this->clock_pin_);
62 this->sm2135_set_low_(this->clock_pin_);
64 this->sm2135_set_low_(this->data_pin_);
65}
66
68 this->sm2135_set_low_(this->data_pin_);
70 this->sm2135_set_low_(this->clock_pin_);
71}
72
81
82void SM2135::write_buffer_(uint8_t *buffer, uint8_t size) {
83 this->sm2135_start_();
84
85 this->data_pin_->digital_write(false);
86 for (uint32_t i = 0; i < size; i++) {
87 this->write_byte_(buffer[i]);
88 }
89
90 this->sm2135_stop_();
91}
92
94 if (!this->update_)
95 return;
96
97 this->sm2135_start_();
98 this->write_byte_(SM2135_ADDR_MC);
100
101 if (this->separate_modes_) {
102 if (this->update_channel_ == 3 || this->update_channel_ == 4) {
103 // No color so must be Cold/Warm
104
105 this->write_byte_(SM2135_CW);
106 this->sm2135_stop_();
107 delay(1);
108 this->sm2135_start_();
109 this->write_byte_(SM2135_ADDR_C);
110 this->write_byte_(this->pwm_amounts_[3]);
111 this->write_byte_(this->pwm_amounts_[4]);
112 } else {
113 // Color
114
115 this->write_byte_(SM2135_RGB);
116 this->write_byte_(this->pwm_amounts_[0]);
117 this->write_byte_(this->pwm_amounts_[1]);
118 this->write_byte_(this->pwm_amounts_[2]);
119 }
120 } else {
121 this->write_byte_(SM2135_RGB);
122 this->write_byte_(this->pwm_amounts_[0]);
123 this->write_byte_(this->pwm_amounts_[1]);
124 this->write_byte_(this->pwm_amounts_[2]);
125 this->write_byte_(this->pwm_amounts_[3]);
126 this->write_byte_(this->pwm_amounts_[4]);
127 }
128
129 this->sm2135_stop_();
130
131 this->update_ = false;
132}
133
134void SM2135::set_channel_value_(uint8_t channel, uint8_t value) {
135 if (this->pwm_amounts_[channel] != value) {
136 this->update_ = true;
137 this->update_channel_ = channel;
138 }
139 this->pwm_amounts_[channel] = value;
140}
141
143 pin->digital_write(false);
145}
146
148 pin->digital_write(true);
150}
151
152} // namespace sm2135
153} // 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:46
void loop() override
Send new values if they were updated.
Definition sm2135.cpp:93
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:147
void set_channel_value_(uint8_t channel, uint8_t value)
Definition sm2135.cpp:134
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition sm2135.cpp:82
SM2135Current rgb_current_
Definition sm2135.h:81
GPIOPin * clock_pin_
Definition sm2135.h:79
void sm2135_set_low_(GPIOPin *pin)
Definition sm2135.cpp:142
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:28
@ FLAG_PULLUP
Definition gpio.h:30
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:28
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:26