Update tca6408.h

This commit is contained in:
Dave Williams | DitroniX | G8PUO
2026-06-13 08:34:45 +01:00
committed by GitHub
parent 456f8eeca4
commit 8a6ea593db
+54 -40
View File
@@ -1,51 +1,65 @@
#include "tca6408.h" #pragma once
#include "esphome/core/log.h"
#include "esphome/components/gpio_expander/cached_gpio.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome { namespace esphome {
namespace tca6408 { namespace tca6408 {
static const char *const TAG = "tca6408"; class TCA6408Component : public Component,
public i2c::I2CDevice,
public gpio_expander::CachedGpioExpander<uint8_t, 8> {
public:
void setup() override;
void loop() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::IO; }
static const uint8_t TCA6408_INPUT_PORT = 0x00; void set_address(uint8_t address) { this->address_ = address; }
static const uint8_t TCA6408_OUTPUT_PORT = 0x01; void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
static const uint8_t TCA6408_POLARITY_PORT = 0x02;
static const uint8_t TCA6408_CONFIG_PORT = 0x03;
void TCA6408Component::setup() { void set_polarity_inversion(uint8_t pin, bool inverted);
ESP_LOGCONFIG(TAG, "Setting up TCA6408...");
// Initialize all registers protected:
if (this->write_register(TCA6408_POLARITY_PORT, 0x00) != i2c::ERROR_OK || static void IRAM_ATTR gpio_intr(TCA6408Component *arg);
this->write_register(TCA6408_CONFIG_PORT, this->mode_mask_) != i2c::ERROR_OK ||
!this->read_gpio_outputs_()) {
this->mark_failed();
return;
}
if (this->interrupt_pin_ != nullptr) { bool digital_read_hw(uint8_t pin) override;
this->interrupt_pin_->setup(); bool digital_read_cache(uint8_t pin) override;
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); void digital_write_hw(uint8_t pin, bool value) override;
this->interrupt_pin_->attach_interrupt(&TCA6408Component::gpio_intr, this, gpio::INTERRUPT_FALLING_EDGE); void pin_mode(uint8_t pin, gpio::Flags flags) override;
this->set_invalidate_on_read_(false);
ESP_LOGCONFIG(TAG, " Interrupt mode enabled (active-low)");
} else {
this->enable_loop();
ESP_LOGCONFIG(TAG, " Polling mode enabled");
}
}
// ... (gpio_intr, loop, dump_config stay the same) uint8_t mode_mask_{0xFF}; // 1 = input, 0 = output
uint8_t output_mask_{0x00};
uint8_t input_mask_{0x00};
uint8_t polarity_mask_{0x00}; // 1 = inverted (for inputs)
bool TCA6408Component::read_gpio_outputs_() { InternalGPIOPin *interrupt_pin_{nullptr};
if (this->is_failed()) return false; bool interrupt_triggered_{false};
uint8_t data;
if (this->read_register(TCA6408_OUTPUT_PORT, &data) != i2c::ERROR_OK) {
this->status_set_warning();
return false;
}
this->output_mask_ = data;
this->status_clear_warning();
return true;
}
// Rest of the file remains the same as you have it... private:
bool read_gpio_outputs_();
};
class TCA6408GPIOPin : public gpio::GPIOPin, public Parented<TCA6408Component> {
public:
void setup() override;
void pin_mode(gpio::Flags flags) override;
bool digital_read() override;
void digital_write(bool value) override;
size_t dump_summary(char *buffer, size_t len) const override;
void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted);
void set_flags(gpio::Flags flags) { flags_ = flags; }
gpio::Flags get_flags() const override { return flags_; }
protected:
uint8_t pin_{0};
bool inverted_{false};
gpio::Flags flags_{0};
};
} // namespace tca6408
} // namespace esphome