mirror of
https://github.com/esphome/esphome.git
synced 2025-09-09 23:02:23 +01:00
[pca9554] Reduce I2C bus usage with lazy input caching
This commit is contained in:
@@ -37,10 +37,9 @@ void PCA9554Component::setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PCA9554Component::loop() {
|
void PCA9554Component::loop() {
|
||||||
// The read_inputs_() method will cache the input values from the chip.
|
// Invalidate the cache at the start of each loop.
|
||||||
this->read_inputs_();
|
// The actual read will happen on demand in digital_read()
|
||||||
// Clear all the previously read flags.
|
this->cache_valid_ = false;
|
||||||
this->was_previously_read_ = 0x00;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PCA9554Component::dump_config() {
|
void PCA9554Component::dump_config() {
|
||||||
@@ -55,16 +54,10 @@ void PCA9554Component::dump_config() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool PCA9554Component::digital_read(uint8_t pin) {
|
bool PCA9554Component::digital_read(uint8_t pin) {
|
||||||
// Note: We want to try and avoid doing any I2C bus read transactions here
|
// Read the inputs once per loop on demand and cache the result
|
||||||
// to conserve I2C bus bandwidth. So what we do is check to see if we
|
if (!this->cache_valid_ && this->read_inputs_()) {
|
||||||
// have seen a read during the time esphome is running this loop. If we have,
|
this->cache_valid_ = true;
|
||||||
// we do an I2C bus transaction to get the latest value. If we haven't
|
}
|
||||||
// we return a cached value which was read at the time loop() was called.
|
|
||||||
if (this->was_previously_read_ & (1 << pin))
|
|
||||||
this->read_inputs_(); // Force a read of a new value
|
|
||||||
// Indicate we saw a read request for this pin in case a
|
|
||||||
// read happens later in the same loop.
|
|
||||||
this->was_previously_read_ |= (1 << pin);
|
|
||||||
return this->input_mask_ & (1 << pin);
|
return this->input_mask_ & (1 << pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -45,8 +45,8 @@ class PCA9554Component : public Component, public i2c::I2CDevice {
|
|||||||
uint16_t output_mask_{0x00};
|
uint16_t output_mask_{0x00};
|
||||||
/// The state of the actual input pin states - 1 means HIGH, 0 means LOW
|
/// The state of the actual input pin states - 1 means HIGH, 0 means LOW
|
||||||
uint16_t input_mask_{0x00};
|
uint16_t input_mask_{0x00};
|
||||||
/// Flags to check if read previously during this loop
|
/// Cache validity flag - true if we've read inputs this loop cycle
|
||||||
uint16_t was_previously_read_ = {0x00};
|
bool cache_valid_{false};
|
||||||
/// Storage for last I2C error seen
|
/// Storage for last I2C error seen
|
||||||
esphome::i2c::ErrorCode last_error_;
|
esphome::i2c::ErrorCode last_error_;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user