mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
handle 16 pins
This commit is contained in:
@@ -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); }
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user