Skip to content

Commit

Permalink
SX1509 output debounce fix (resolves esphome/issues#4402) (esphome#4672)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracestep authored Oct 26, 2023
1 parent 8b9a760 commit 6b8a75d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions esphome/components/sx1509/sx1509.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@ void SX1509Component::digital_write(uint8_t pin, bool bit_value) {
uint16_t temp_reg_data = 0;
this->read_byte_16(REG_DATA_B, &temp_reg_data);
if (bit_value) {
temp_reg_data |= (1 << pin);
output_state_ |= (1 << pin); // set bit in shadow register
} else {
temp_reg_data &= ~(1 << pin);
output_state_ &= ~(1 << pin); // reset bit shadow register
}
for (uint16_t b = 0x8000; b; b >>= 1) {
if ((~ddr_mask_) & b) { // transfer bits of outputs, but don't mess with inputs
if (output_state_ & b) {
temp_reg_data |= b;
} else {
temp_reg_data &= ~b;
}
}
}
this->write_byte_16(REG_DATA_B, temp_reg_data);
}
Expand Down Expand Up @@ -134,6 +143,7 @@ void SX1509Component::setup_led_driver(uint8_t pin) {

this->read_byte_16(REG_DATA_B, &temp_word);
temp_word &= ~(1 << pin);
output_state_ &= ~(1 << pin);
this->write_byte_16(REG_DATA_B, temp_word);
}

Expand Down
1 change: 1 addition & 0 deletions esphome/components/sx1509/sx1509.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class SX1509Component : public Component, public i2c::I2CDevice {
uint16_t ddr_mask_ = 0x00;
uint16_t input_mask_ = 0x00;
uint16_t port_mask_ = 0x00;
uint16_t output_state_ = 0x00;
bool has_keypad_ = false;
uint8_t rows_ = 0;
uint8_t cols_ = 0;
Expand Down

0 comments on commit 6b8a75d

Please sign in to comment.