ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bp1658cj.cpp
Go to the documentation of this file.
1#include "bp1658cj.h"
2#include "esphome/core/log.h"
3
5
6ESPHOME_LOG_TAG(TAG, "bp1658cj");
7
8static const uint8_t BP1658CJ_MODEL_ID = 0x80;
9static const uint8_t BP1658CJ_ADDR_STANDBY = 0x0;
10static const uint8_t BP1658CJ_ADDR_START_3CH = 0x10;
11static const uint8_t BP1658CJ_ADDR_START_2CH = 0x20;
12static const uint8_t BP1658CJ_ADDR_START_5CH = 0x30;
13
14static const uint8_t BP1658CJ_DELAY = 2;
15
17 this->data_pin_->setup();
18 this->data_pin_->digital_write(false);
19 this->clock_pin_->setup();
20 this->clock_pin_->digital_write(false);
21 this->pwm_amounts_.resize(5, 0);
22}
24 ESP_LOGCONFIG(TAG,
25 "BP1658CJ:\n"
26 " Color Channels Max Power: %u\n"
27 " White Channels Max Power: %u",
29 LOG_PIN(" Data Pin: ", this->data_pin_);
30 LOG_PIN(" Clock Pin: ", this->clock_pin_);
31}
32
34 if (!this->update_)
35 return;
36
37 uint8_t data[12];
38 if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
39 this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) {
40 for (int i = 1; i < 12; i++)
41 data[i] = 0;
42
43 // First turn all channels off
44 data[0] = BP1658CJ_MODEL_ID + BP1658CJ_ADDR_START_5CH;
45 this->write_buffer_(data, 12);
46 // Then sleep
47 data[0] = BP1658CJ_MODEL_ID + BP1658CJ_ADDR_STANDBY;
48 this->write_buffer_(data, 12);
49 } else if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
50 (this->pwm_amounts_[3] > 0 || this->pwm_amounts_[4] > 0)) {
51 // Only data on white channels
52 data[0] = BP1658CJ_MODEL_ID + BP1658CJ_ADDR_START_2CH;
53 data[1] = 0 << 4 | this->max_power_white_channels_;
54 for (int i = 2, j = 0; i < 12; i += 2, j++) {
55 data[i] = this->pwm_amounts_[j] & 0x1F;
56 data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
57 }
58 this->write_buffer_(data, 12);
59 } else if ((this->pwm_amounts_[0] > 0 || this->pwm_amounts_[1] > 0 || this->pwm_amounts_[2] > 0) &&
60 this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) {
61 // Only data on RGB channels
62 data[0] = BP1658CJ_MODEL_ID + BP1658CJ_ADDR_START_3CH;
63 data[1] = this->max_power_color_channels_ << 4 | 0;
64 for (int i = 2, j = 0; i < 12; i += 2, j++) {
65 data[i] = this->pwm_amounts_[j] & 0x1F;
66 data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
67 }
68 this->write_buffer_(data, 12);
69 } else {
70 // All channels
71 data[0] = BP1658CJ_MODEL_ID + BP1658CJ_ADDR_START_5CH;
72 data[1] = this->max_power_color_channels_ << 4 | this->max_power_white_channels_;
73 for (int i = 2, j = 0; i < 12; i += 2, j++) {
74 data[i] = this->pwm_amounts_[j] & 0x1F;
75 data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
76 }
77 this->write_buffer_(data, 12);
78 }
79
80 this->update_ = false;
81}
82
83void BP1658CJ::write_channel(uint8_t channel, float state) {
84 auto value = static_cast<uint16_t>(state * 0x3FF);
85 if (this->pwm_amounts_[channel] != value) {
86 this->update_ = true;
87 this->update_channel_ = channel;
88 }
89 this->pwm_amounts_[channel] = value;
90}
91
93 this->data_pin_->digital_write(value);
94 delayMicroseconds(BP1658CJ_DELAY);
95 this->clock_pin_->digital_write(true);
96 delayMicroseconds(BP1658CJ_DELAY);
97 this->clock_pin_->digital_write(false);
98 delayMicroseconds(BP1658CJ_DELAY);
99}
100
101void BP1658CJ::write_byte_(uint8_t data) {
102 for (uint8_t mask = 0x80; mask; mask >>= 1) {
103 this->write_bit_(data & mask);
104 }
105
106 // ack bit
108 this->clock_pin_->digital_write(true);
109 delayMicroseconds(BP1658CJ_DELAY);
110 this->clock_pin_->digital_write(false);
111 delayMicroseconds(BP1658CJ_DELAY);
113}
114
115void BP1658CJ::write_buffer_(uint8_t *buffer, uint8_t size) {
116 this->data_pin_->digital_write(false);
117 delayMicroseconds(BP1658CJ_DELAY);
118 this->clock_pin_->digital_write(false);
119 delayMicroseconds(BP1658CJ_DELAY);
120
121 for (uint32_t i = 0; i < size; i++) {
122 this->write_byte_(buffer[i]);
123 }
124
125 this->clock_pin_->digital_write(true);
126 delayMicroseconds(BP1658CJ_DELAY);
127 this->data_pin_->digital_write(true);
128 delayMicroseconds(BP1658CJ_DELAY);
129}
130
131} // namespace esphome::bp1658cj
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
void dump_config() override
Definition bp1658cj.cpp:23
std::vector< uint16_t > pwm_amounts_
Definition bp1658cj.h:41
void write_buffer_(uint8_t *buffer, uint8_t size)
Definition bp1658cj.cpp:115
void loop() override
Send new values if they were updated.
Definition bp1658cj.cpp:33
void write_bit_(bool value)
Definition bp1658cj.cpp:92
void write_channel(uint8_t channel, float state)
Definition bp1658cj.cpp:83
void write_byte_(uint8_t data)
Definition bp1658cj.cpp:101
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "bp1658cj")
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