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