ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sm10bit_base.cpp
Go to the documentation of this file.
1#include "sm10bit_base.h"
2
4
5static constexpr uint8_t SM10BIT_MODEL_ID = 0xC0;
6
7static constexpr uint8_t SM10BIT_ADDR_STANDBY = 0x0;
8static constexpr uint8_t SM10BIT_ADDR_START_3CH = 0x8;
9static constexpr uint8_t SM10BIT_ADDR_START_2CH = 0x10;
10static constexpr uint8_t SM10BIT_ADDR_START_5CH = 0x18;
11
12static constexpr uint8_t SM10BIT_DELAY = 2;
13
14// Power current values
15// HEX | Binary | RGB level | White level | Config value
16// 0x0 | 0000 | RGB 10mA | CW 5mA | 0
17// 0x1 | 0001 | RGB 20mA | CW 10mA | 1
18// 0x2 | 0010 | RGB 30mA | CW 15mA | 2 - Default spec color value
19// 0x3 | 0011 | RGB 40mA | CW 20mA | 3
20// 0x4 | 0100 | RGB 50mA | CW 25mA | 4 - Default spec white value
21// 0x5 | 0101 | RGB 60mA | CW 30mA | 5
22// 0x6 | 0110 | RGB 70mA | CW 35mA | 6
23// 0x7 | 0111 | RGB 80mA | CW 40mA | 7
24// 0x8 | 1000 | RGB 90mA | CW 45mA | 8
25// 0x9 | 1001 | RGB 100mA | CW 50mA | 9
26// 0xA | 1010 | RGB 110mA | CW 55mA | 10
27// 0xB | 1011 | RGB 120mA | CW 60mA | 11
28// 0xC | 1100 | RGB 130mA | CW 65mA | 12
29// 0xD | 1101 | RGB 140mA | CW 70mA | 13
30// 0xE | 1110 | RGB 150mA | CW 75mA | 14
31// 0xF | 1111 | RGB 160mA | CW 80mA | 15
32
34 this->data_pin_->setup();
35 this->data_pin_->digital_write(true);
36 this->clock_pin_->setup();
37 this->clock_pin_->digital_write(true);
38}
39
41 this->disable_loop();
42
43 const bool color_on = this->pwm_amounts_[0] > 0 || this->pwm_amounts_[1] > 0 || this->pwm_amounts_[2] > 0;
44 const bool white_on = this->pwm_amounts_[3] > 0 || this->pwm_amounts_[4] > 0;
45 uint8_t addr = SM10BIT_ADDR_START_5CH;
46 if (color_on && !white_on) {
47 addr = SM10BIT_ADDR_START_3CH;
48 } else if (white_on && !color_on) {
49 addr = SM10BIT_ADDR_START_2CH;
50 }
51
52 uint8_t data[12]{};
53 data[0] = SM10BIT_MODEL_ID + addr;
54 // Only the max power of the channel groups being driven is sent
55 data[1] = (color_on ? this->max_power_color_channels_ << 4 : 0) | (white_on ? this->max_power_white_channels_ : 0);
56 for (size_t i = 0; i < this->pwm_amounts_.size(); i++) {
57 data[2 + i * 2] = this->pwm_amounts_[i] >> 8;
58 data[3 + i * 2] = this->pwm_amounts_[i] & 0xFF;
59 }
60 this->write_buffer_(data, sizeof(data));
61
62 if (!color_on && !white_on) {
63 // All channels are off, so put the chip to sleep
64 data[0] = SM10BIT_MODEL_ID + SM10BIT_ADDR_STANDBY;
65 this->write_buffer_(data, sizeof(data));
66 }
67}
68
69void Sm10BitBase::write_channel(uint8_t channel, float state) {
70 auto value = static_cast<uint16_t>(state * 0x3FF);
71 if (this->pwm_amounts_[channel] != value) {
72 this->pwm_amounts_[channel] = value;
73 this->enable_loop();
74 }
75}
76
78 this->data_pin_->digital_write(value);
79 delayMicroseconds(SM10BIT_DELAY);
80 this->clock_pin_->digital_write(true);
81 delayMicroseconds(SM10BIT_DELAY);
82 this->clock_pin_->digital_write(false);
83 delayMicroseconds(SM10BIT_DELAY);
84}
85
86void Sm10BitBase::write_byte_(uint8_t data) {
87 for (uint8_t mask = 0x80; mask; mask >>= 1) {
88 this->write_bit_(data & mask);
89 }
90
91 // ack bit
93 this->clock_pin_->digital_write(true);
94 delayMicroseconds(SM10BIT_DELAY);
95 this->clock_pin_->digital_write(false);
96 delayMicroseconds(SM10BIT_DELAY);
98}
99
100void Sm10BitBase::write_buffer_(const uint8_t *buffer, uint8_t size) {
101 this->data_pin_->digital_write(false);
102 delayMicroseconds(SM10BIT_DELAY);
103 this->clock_pin_->digital_write(false);
104 delayMicroseconds(SM10BIT_DELAY);
105
106 for (uint32_t i = 0; i < size; i++) {
107 this->write_byte_(buffer[i]);
108 }
109
110 this->clock_pin_->digital_write(true);
111 delayMicroseconds(SM10BIT_DELAY);
112 this->data_pin_->digital_write(true);
113 delayMicroseconds(SM10BIT_DELAY);
114}
115
116} // namespace esphome::sm10bit_base
void enable_loop()
Enable this component's loop.
Definition component.h:261
void disable_loop()
Disable this component's loop.
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
void write_channel(uint8_t channel, float state)
void write_buffer_(const uint8_t *buffer, uint8_t size)
void loop() override
Send the new duty values, then sleep until write_channel() changes one.
std::array< uint16_t, 5 > pwm_amounts_
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
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
static void uint32_t