mirror of
https://github.com/esphome/esphome.git
synced 2026-03-24 06:53:07 +08:00
[gpio] Compile out interlock fields when unused (#15111)
This commit is contained in:
@@ -32,6 +32,7 @@ async def to_code(config):
|
||||
cg.add(var.set_pin(pin))
|
||||
|
||||
if CONF_INTERLOCK in config:
|
||||
cg.add_define("USE_GPIO_SWITCH_INTERLOCK")
|
||||
interlock = []
|
||||
for it in config[CONF_INTERLOCK]:
|
||||
lock = await cg.get_variable(it)
|
||||
|
||||
@@ -5,7 +5,9 @@ namespace esphome {
|
||||
namespace gpio {
|
||||
|
||||
static const char *const TAG = "switch.gpio";
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
static constexpr uint32_t INTERLOCK_TIMEOUT_ID = 0;
|
||||
#endif
|
||||
|
||||
float GPIOSwitch::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
void GPIOSwitch::setup() {
|
||||
@@ -28,6 +30,7 @@ void GPIOSwitch::setup() {
|
||||
void GPIOSwitch::dump_config() {
|
||||
LOG_SWITCH("", "GPIO Switch", this);
|
||||
LOG_PIN(" Pin: ", this->pin_);
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
if (!this->interlock_.empty()) {
|
||||
ESP_LOGCONFIG(TAG, " Interlocks:");
|
||||
for (auto *lock : this->interlock_) {
|
||||
@@ -36,8 +39,10 @@ void GPIOSwitch::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " %s", lock->get_name().c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void GPIOSwitch::write_state(bool state) {
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
if (state != this->inverted_) {
|
||||
// Turning ON, check interlocking
|
||||
|
||||
@@ -64,11 +69,15 @@ void GPIOSwitch::write_state(bool state) {
|
||||
// re-activations
|
||||
this->cancel_timeout(INTERLOCK_TIMEOUT_ID);
|
||||
}
|
||||
#endif
|
||||
|
||||
this->pin_->digital_write(state);
|
||||
this->publish_state(state);
|
||||
}
|
||||
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
void GPIOSwitch::set_interlock(const std::initializer_list<Switch *> &interlock) { this->interlock_ = interlock; }
|
||||
#endif
|
||||
|
||||
} // namespace gpio
|
||||
} // namespace esphome
|
||||
|
||||
@@ -18,15 +18,19 @@ class GPIOSwitch final : public switch_::Switch, public Component {
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
void set_interlock(const std::initializer_list<Switch *> &interlock);
|
||||
void set_interlock_wait_time(uint32_t interlock_wait_time) { interlock_wait_time_ = interlock_wait_time; }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
void write_state(bool state) override;
|
||||
|
||||
GPIOPin *pin_;
|
||||
#ifdef USE_GPIO_SWITCH_INTERLOCK
|
||||
FixedVector<Switch *> interlock_;
|
||||
uint32_t interlock_wait_time_{0};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace gpio
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
#define USE_ESP32_IMPROV_STATE_CALLBACK
|
||||
#define USE_EVENT
|
||||
#define USE_FAN
|
||||
#define USE_GPIO_SWITCH_INTERLOCK
|
||||
#define USE_GRAPH
|
||||
#define USE_GRAPHICAL_DISPLAY_MENU
|
||||
#define USE_HOMEASSISTANT_TIME
|
||||
|
||||
Reference in New Issue
Block a user