SM10BIT_BASE - Add delays and ACKs, clear all channels before sleeping. (#5526)

This commit is contained in:
Cossid 2023-10-12 18:51:19 -05:00 committed by Jesse Hills
parent 969f6dbe13
commit 8c1ad1e9a6
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A

View file

@ -11,6 +11,8 @@ static const uint8_t SM10BIT_ADDR_START_3CH = 0x8;
static const uint8_t SM10BIT_ADDR_START_2CH = 0x10; static const uint8_t SM10BIT_ADDR_START_2CH = 0x10;
static const uint8_t SM10BIT_ADDR_START_5CH = 0x18; static const uint8_t SM10BIT_ADDR_START_5CH = 0x18;
static const uint8_t SM10BIT_DELAY = 2;
// Power current values // Power current values
// HEX | Binary | RGB level | White level | Config value // HEX | Binary | RGB level | White level | Config value
// 0x0 | 0000 | RGB 10mA | CW 5mA | 0 // 0x0 | 0000 | RGB 10mA | CW 5mA | 0
@ -37,10 +39,13 @@ void Sm10BitBase::loop() {
uint8_t data[12]; uint8_t data[12];
if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 && if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) { this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) {
// Off / Sleep
data[0] = this->model_id_ + SM10BIT_ADDR_STANDBY;
for (int i = 1; i < 12; i++) for (int i = 1; i < 12; i++)
data[i] = 0; data[i] = 0;
// First turn all channels off
data[0] = this->model_id_ + SM10BIT_ADDR_START_5CH;
this->write_buffer_(data, 12);
// Then sleep
data[0] = this->model_id_ + SM10BIT_ADDR_STANDBY;
this->write_buffer_(data, 12); this->write_buffer_(data, 12);
} else if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 && } else if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
(this->pwm_amounts_[3] > 0 || this->pwm_amounts_[4] > 0)) { (this->pwm_amounts_[3] > 0 || this->pwm_amounts_[4] > 0)) {
@ -84,28 +89,42 @@ void Sm10BitBase::set_channel_value_(uint8_t channel, uint16_t value) {
this->pwm_amounts_[channel] = value; this->pwm_amounts_[channel] = value;
} }
void Sm10BitBase::write_bit_(bool value) { void Sm10BitBase::write_bit_(bool value) {
this->clock_pin_->digital_write(false);
this->data_pin_->digital_write(value); this->data_pin_->digital_write(value);
delayMicroseconds(SM10BIT_DELAY);
this->clock_pin_->digital_write(true); this->clock_pin_->digital_write(true);
delayMicroseconds(SM10BIT_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(SM10BIT_DELAY);
} }
void Sm10BitBase::write_byte_(uint8_t data) { void Sm10BitBase::write_byte_(uint8_t data) {
for (uint8_t mask = 0x80; mask; mask >>= 1) { for (uint8_t mask = 0x80; mask; mask >>= 1) {
this->write_bit_(data & mask); this->write_bit_(data & mask);
} }
this->clock_pin_->digital_write(false);
this->data_pin_->digital_write(true); // ack bit
this->data_pin_->pin_mode(gpio::FLAG_INPUT);
this->clock_pin_->digital_write(true); this->clock_pin_->digital_write(true);
delayMicroseconds(SM10BIT_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(SM10BIT_DELAY);
this->data_pin_->pin_mode(gpio::FLAG_OUTPUT);
} }
void Sm10BitBase::write_buffer_(uint8_t *buffer, uint8_t size) { void Sm10BitBase::write_buffer_(uint8_t *buffer, uint8_t size) {
this->data_pin_->digital_write(false); this->data_pin_->digital_write(false);
delayMicroseconds(SM10BIT_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(SM10BIT_DELAY);
for (uint32_t i = 0; i < size; i++) { for (uint32_t i = 0; i < size; i++) {
this->write_byte_(buffer[i]); this->write_byte_(buffer[i]);
} }
this->clock_pin_->digital_write(false);
this->clock_pin_->digital_write(true); this->clock_pin_->digital_write(true);
delayMicroseconds(SM10BIT_DELAY);
this->data_pin_->digital_write(true); this->data_pin_->digital_write(true);
delayMicroseconds(SM10BIT_DELAY);
} }
} // namespace sm10bit_base } // namespace sm10bit_base