1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 14:43:51 +00:00

handle 16 pins

This commit is contained in:
J. Nick Koston
2025-09-04 12:28:23 -05:00
parent 4885819881
commit e866ae0f50
3 changed files with 17 additions and 8 deletions

View File

@@ -16,12 +16,12 @@ namespace esphome::gpio_expander {
/// T - Type which represents internal register. Could be uint8_t or uint16_t. Adjust to
/// match size of your internal GPIO bank register.
/// N - Number of pins
template<typename T, T N> class CachedGpioExpander {
template<typename T, uint8_t N> class CachedGpioExpander {
public:
/// @brief Read the state of the given pin. This will invalidate the cache for the given pin number.
/// @param pin Pin number to read
/// @return Pin state
bool digital_read(T pin) {
bool digital_read(uint8_t pin) {
const uint8_t bank = pin / BANK_SIZE;
const T pin_mask = (1 << (pin % BANK_SIZE));
// Check if specific pin cache is valid
@@ -38,15 +38,15 @@ template<typename T, T N> class CachedGpioExpander {
return this->digital_read_cache(pin);
}
void digital_write(T pin, bool value) { this->digital_write_hw(pin, value); }
void digital_write(uint8_t pin, bool value) { this->digital_write_hw(pin, value); }
protected:
/// @brief Call component low level function to read GPIO state from device
virtual bool digital_read_hw(T pin) = 0;
virtual bool digital_read_hw(uint8_t pin) = 0;
/// @brief Call component read function from internal cache.
virtual bool digital_read_cache(T pin) = 0;
virtual bool digital_read_cache(uint8_t pin) = 0;
/// @brief Call component low level function to write GPIO state to device
virtual void digital_write_hw(T pin, bool value) = 0;
virtual void digital_write_hw(uint8_t pin, bool value) = 0;
/// @brief Invalidate cache. This function should be called in component loop().
void reset_pin_cache_() { memset(this->read_cache_valid_, 0x00, CACHE_SIZE_BYTES); }

View File

@@ -28,14 +28,21 @@ void PCF8574Component::dump_config() {
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
}
}
bool PCF8574Component::digital_read(uint8_t pin) { return this->get_pin_value_(pin); }
bool PCF8574Component::digital_read(uint8_t pin) {
// Call the base class method
return this->CachedGpioExpander::digital_read(pin);
}
bool PCF8574Component::digital_read_hw(uint8_t pin) {
return this->read_gpio_() ? (this->input_mask_ & (1 << pin)) : false;
}
bool PCF8574Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
void PCF8574Component::digital_write(uint8_t pin, bool value) { this->set_pin_value_(pin, value); }
void PCF8574Component::digital_write(uint8_t pin, bool value) {
// Call the base class method
this->CachedGpioExpander::digital_write(pin, value);
}
void PCF8574Component::digital_write_hw(uint8_t pin, bool value) {
if (value) {

View File

@@ -22,6 +22,8 @@ class PCF8574Component : public Component,
void setup() override;
/// Invalidate cache at start of each loop
void loop() override;
/// Helper function to read the value of a pin.
bool digital_read(uint8_t pin);
/// Helper function to write the value of a pin.
void digital_write(uint8_t pin, bool value);
/// Helper function to set the pin mode of a pin.