mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Merge branch 'mcp_23016_gpio_cache' into integration
This commit is contained in:
		| @@ -11,6 +11,7 @@ from esphome.const import ( | ||||
|     CONF_OUTPUT, | ||||
| ) | ||||
|  | ||||
| AUTO_LOAD = ["gpio_expander"] | ||||
| DEPENDENCIES = ["i2c"] | ||||
| MULTI_CONF = True | ||||
|  | ||||
|   | ||||
| @@ -22,14 +22,29 @@ void MCP23016::setup() { | ||||
|   this->write_reg_(MCP23016_IODIR0, 0xFF); | ||||
|   this->write_reg_(MCP23016_IODIR1, 0xFF); | ||||
| } | ||||
| bool MCP23016::digital_read(uint8_t pin) { | ||||
|   uint8_t bit = pin % 8; | ||||
|  | ||||
| void MCP23016::loop() { | ||||
|   // Invalidate cache at the start of each loop | ||||
|   this->reset_pin_cache_(); | ||||
| } | ||||
| bool MCP23016::digital_read_hw(uint8_t pin) { | ||||
|   uint8_t reg_addr = pin < 8 ? MCP23016_GP0 : MCP23016_GP1; | ||||
|   uint8_t value = 0; | ||||
|   this->read_reg_(reg_addr, &value); | ||||
|   return value & (1 << bit); | ||||
|   if (!this->read_reg_(reg_addr, &value)) { | ||||
|     return false; | ||||
|   } | ||||
| void MCP23016::digital_write(uint8_t pin, bool value) { | ||||
|  | ||||
|   // Update the appropriate part of input_mask_ | ||||
|   if (pin < 8) { | ||||
|     this->input_mask_ = (this->input_mask_ & 0xFF00) | value; | ||||
|   } else { | ||||
|     this->input_mask_ = (this->input_mask_ & 0x00FF) | (uint16_t(value) << 8); | ||||
|   } | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| bool MCP23016::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); } | ||||
| void MCP23016::digital_write_hw(uint8_t pin, bool value) { | ||||
|   uint8_t reg_addr = pin < 8 ? MCP23016_OLAT0 : MCP23016_OLAT1; | ||||
|   this->update_reg_(pin, value, reg_addr); | ||||
| } | ||||
| @@ -41,7 +56,7 @@ void MCP23016::pin_mode(uint8_t pin, gpio::Flags flags) { | ||||
|     this->update_reg_(pin, false, iodir); | ||||
|   } | ||||
| } | ||||
| float MCP23016::get_setup_priority() const { return setup_priority::HARDWARE; } | ||||
| float MCP23016::get_setup_priority() const { return setup_priority::IO; } | ||||
| bool MCP23016::read_reg_(uint8_t reg, uint8_t *value) { | ||||
|   if (this->is_failed()) | ||||
|     return false; | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/core/hal.h" | ||||
| #include "esphome/components/i2c/i2c.h" | ||||
| #include "esphome/components/gpio_expander/cached_gpio.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace mcp23016 { | ||||
| @@ -24,19 +25,22 @@ enum MCP23016GPIORegisters { | ||||
|   MCP23016_IOCON1 = 0x0B, | ||||
| }; | ||||
|  | ||||
| class MCP23016 : public Component, public i2c::I2CDevice { | ||||
| class MCP23016 : public Component, public i2c::I2CDevice, public gpio_expander::CachedGpioExpander<uint8_t, 16> { | ||||
|  public: | ||||
|   MCP23016() = default; | ||||
|  | ||||
|   void setup() override; | ||||
|  | ||||
|   bool digital_read(uint8_t pin); | ||||
|   void digital_write(uint8_t pin, bool value); | ||||
|   void loop() override; | ||||
|   void pin_mode(uint8_t pin, gpio::Flags flags); | ||||
|  | ||||
|   float get_setup_priority() const override; | ||||
|  | ||||
|  protected: | ||||
|   // Virtual methods from CachedGpioExpander | ||||
|   bool digital_read_hw(uint8_t pin) override; | ||||
|   bool digital_read_cache(uint8_t pin) override; | ||||
|   void digital_write_hw(uint8_t pin, bool value) override; | ||||
|  | ||||
|   // read a given register | ||||
|   bool read_reg_(uint8_t reg, uint8_t *value); | ||||
|   // write a value to a given register | ||||
| @@ -46,6 +50,8 @@ class MCP23016 : public Component, public i2c::I2CDevice { | ||||
|  | ||||
|   uint8_t olat_0_{0x00}; | ||||
|   uint8_t olat_1_{0x00}; | ||||
|   // Cache for input values (16-bit combined for both banks) | ||||
|   uint16_t input_mask_{0x00}; | ||||
| }; | ||||
|  | ||||
| class MCP23016GPIOPin : public GPIOPin { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user