ESPHome  2022.9.2
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 
6 namespace esphome {
7 namespace sm2135 {
8 
9 static const char *const TAG = "sm2135";
10 
11 static const uint8_t SM2135_ADDR_MC = 0xC0; // Max current register
12 static const uint8_t SM2135_ADDR_CH = 0xC1; // RGB or CW channel select register
13 static const uint8_t SM2135_ADDR_R = 0xC2; // Red color
14 static const uint8_t SM2135_ADDR_G = 0xC3; // Green color
15 static const uint8_t SM2135_ADDR_B = 0xC4; // Blue color
16 static const uint8_t SM2135_ADDR_C = 0xC5; // Cold
17 static const uint8_t SM2135_ADDR_W = 0xC6; // Warm
18 
19 static const uint8_t SM2135_RGB = 0x00; // RGB channel
20 static const uint8_t SM2135_CW = 0x80; // CW channel (Chip default)
21 
22 static const uint8_t SM2135_10MA = 0x00;
23 static const uint8_t SM2135_15MA = 0x01;
24 static const uint8_t SM2135_20MA = 0x02; // RGB max current (Chip default)
25 static const uint8_t SM2135_25MA = 0x03;
26 static const uint8_t SM2135_30MA = 0x04; // CW max current (Chip default)
27 static const uint8_t SM2135_35MA = 0x05;
28 static const uint8_t SM2135_40MA = 0x06;
29 static const uint8_t SM2135_45MA = 0x07; // Max value for RGB
30 static const uint8_t SM2135_50MA = 0x08;
31 static const uint8_t SM2135_55MA = 0x09;
32 static const uint8_t SM2135_60MA = 0x0A;
33 
34 static const uint8_t SM2135_CURRENT = (SM2135_20MA << 4) | SM2135_10MA;
35 
36 void SM2135::setup() {
37  ESP_LOGCONFIG(TAG, "Setting up SM2135OutputComponent...");
38  this->data_pin_->setup();
39  this->data_pin_->digital_write(true);
40  this->clock_pin_->setup();
41  this->clock_pin_->digital_write(true);
42  this->pwm_amounts_.resize(5, 0);
43 }
45  ESP_LOGCONFIG(TAG, "SM2135:");
46  LOG_PIN(" Data Pin: ", this->data_pin_);
47  LOG_PIN(" Clock Pin: ", this->clock_pin_);
48 }
49 
50 void SM2135::loop() {
51  if (!this->update_)
52  return;
53 
54  uint8_t data[6];
55  if (this->update_channel_ == 3 || this->update_channel_ == 4) {
56  // No color so must be Cold/Warm
57  data[0] = SM2135_ADDR_MC;
58  data[1] = SM2135_CURRENT;
59  data[2] = SM2135_CW;
60  this->write_buffer_(data, 3);
61  delay(1);
62  data[0] = SM2135_ADDR_C;
63  data[1] = this->pwm_amounts_[4]; // Warm
64  data[2] = this->pwm_amounts_[3]; // Cold
65  this->write_buffer_(data, 3);
66  } else {
67  // Color
68  data[0] = SM2135_ADDR_MC;
69  data[1] = SM2135_CURRENT;
70  data[2] = SM2135_RGB;
71  data[3] = this->pwm_amounts_[1]; // Green
72  data[4] = this->pwm_amounts_[0]; // Red
73  data[5] = this->pwm_amounts_[2]; // Blue
74  this->write_buffer_(data, 6);
75  }
76 
77  this->update_ = false;
78 }
79 
80 } // namespace sm2135
81 } // namespace esphome
virtual void digital_write(bool value)=0
GPIOPin * data_pin_
Definition: sm2135.h:75
virtual void setup()=0
void dump_config() override
Definition: sm2135.cpp:44
uint8_t update_channel_
Definition: sm2135.h:77
std::vector< uint8_t > pwm_amounts_
Definition: sm2135.h:78
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition: sm2135.h:65
void loop() override
Send new values if they were updated.
Definition: sm2135.cpp:50
Definition: a4988.cpp:4
GPIOPin * clock_pin_
Definition: sm2135.h:76
void setup() override
Definition: sm2135.cpp:36
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27