ESPHome 2026.11.0b6
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::sm2135 {
7
8ESPHOME_LOG_TAG(TAG, "sm2135");
9
10static const uint8_t SM2135_ADDR_MC = 0xC0; // Max current register
11static const uint8_t SM2135_ADDR_CH = 0xC1; // RGB or CW channel select register
12static const uint8_t SM2135_ADDR_R = 0xC2; // Red color
13static const uint8_t SM2135_ADDR_G = 0xC3; // Green color
14static const uint8_t SM2135_ADDR_B = 0xC4; // Blue color
15static const uint8_t SM2135_ADDR_C = 0xC5; // Cold
16static const uint8_t SM2135_ADDR_W = 0xC6; // Warm
17
18static const uint8_t SM2135_RGB = 0x00; // RGB channel
19static const uint8_t SM2135_CW = 0x80; // CW channel (Chip default)
20
22 this->data_pin_->setup();
23 this->data_pin_->digital_write(false);
25 this->clock_pin_->setup();
26 this->clock_pin_->digital_write(false);
28
31
32 this->pwm_amounts_.resize(5, 0);
33}
34
36 ESP_LOGCONFIG(TAG,
37 "SM2135:\n"
38 " CW Current: %dmA\n"
39 " RGB Current: %dmA",
40 10 + (this->cw_current_ * 5), 10 + (this->rgb_current_ * 5));
41 LOG_PIN(" Data Pin: ", this->data_pin_);
42 LOG_PIN(" Clock Pin: ", this->clock_pin_);
43}
44
45void SM2135::write_byte_(uint8_t data) {
46 for (uint8_t mask = 0x80; mask; mask >>= 1) {
47 if (mask & data) {
48 this->sm2135_set_high_(this->data_pin_);
49 } else {
50 this->sm2135_set_low_(this->data_pin_);
51 }
52
53 this->sm2135_set_high_(this->clock_pin_);
56 }
57
58 this->sm2135_set_high_(this->data_pin_);
59 this->sm2135_set_high_(this->clock_pin_);
61 this->sm2135_set_low_(this->clock_pin_);
63 this->sm2135_set_low_(this->data_pin_);
64}
65
67 this->sm2135_set_low_(this->data_pin_);
69 this->sm2135_set_low_(this->clock_pin_);
70}
71
80
81void SM2135::write_buffer_(uint8_t *buffer, uint8_t size) {
82 this->sm2135_start_();
83
84 this->data_pin_->digital_write(false);
85 for (uint32_t i = 0; i < size; i++) {
86 this->write_byte_(buffer[i]);
87 }
88
89 this->sm2135_stop_();
90}
91
93 if (!this->update_)
94 return;
95
96 this->sm2135_start_();
97 this->write_byte_(SM2135_ADDR_MC);
99
100 if (this->separate_modes_) {
101 const bool color_on = this->pwm_amounts_[0] || this->pwm_amounts_[1] || this->pwm_amounts_[2];
102 const bool white_on = this->pwm_amounts_[3] || this->pwm_amounts_[4];
103 // Both groups only hold a value without color_interlock; the last updated channel then decides
104 if (!color_on || (white_on && this->update_channel_ >= 3)) {
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 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::write_channel(uint8_t channel, float state) {
133 auto value = static_cast<uint8_t>(state * 0xff);
134 if (this->pwm_amounts_[channel] != value) {
135 this->update_ = true;
136 this->update_channel_ = channel;
137 }
138 this->pwm_amounts_[channel] = value;
139}
140
142 pin->digital_write(false);
144}
145
147 pin->digital_write(true);
149}
150
151} // namespace esphome::sm2135
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:45
void loop() override
Send new values if they were updated.
Definition sm2135.cpp:92
uint8_t update_channel_
Definition sm2135.h:66
SM2135Current cw_current_
Definition sm2135.h:64
std::vector< uint8_t > pwm_amounts_
Definition sm2135.h:67
void write_channel(uint8_t channel, float state)
Definition sm2135.cpp:132
void sm2135_set_high_(GPIOPin *pin)
Definition sm2135.cpp:146
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition sm2135.cpp:81
SM2135Current rgb_current_
Definition sm2135.h:63
GPIOPin * clock_pin_
Definition sm2135.h:61
void sm2135_set_low_(GPIOPin *pin)
Definition sm2135.cpp:141
void dump_config() override
Definition sm2135.cpp:35
GPIOPin * data_pin_
Definition sm2135.h:60
void setup() override
Definition sm2135.cpp:21
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "sm2135")
uint32_t value
Definition hal.cpp:41
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint16_t size
Definition helpers.cpp:34
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t