From bfe6187e14141bd860d1a83c91bcbaf84b5ec11d Mon Sep 17 00:00:00 2001 From: Chelios Date: Tue, 8 Oct 2024 08:11:49 +0300 Subject: [PATCH] using reset pi as GPIOPin entity --- esphome/components/modem/__init__.py | 7 +++++-- esphome/components/modem/modem_component.cpp | 13 ++++++------- esphome/components/modem/modem_component.h | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/esphome/components/modem/__init__.py b/esphome/components/modem/__init__.py index 83ec5b1566..ade8b66d59 100644 --- a/esphome/components/modem/__init__.py +++ b/esphome/components/modem/__init__.py @@ -48,7 +48,7 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(ModemComponent), cv.Required(CONF_TYPE): cv.enum(MODEM_TYPES, upper=True), - cv.Required(CONF_RESET_PIN): pins.internal_gpio_output_pin_number, + cv.Required(CONF_RESET_PIN): pins.internal_gpio_output_pin_schema, cv.Optional(CONF_POWER_PIN): pins.internal_gpio_output_pin_schema, cv.Required(CONF_TX_PIN): pins.internal_gpio_output_pin_number, cv.Required(CONF_RX_PIN): pins.internal_gpio_output_pin_number, @@ -71,7 +71,10 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) cg.add(var.set_type(config[CONF_TYPE])) - cg.add(var.set_reset_pin(config[CONF_RESET_PIN])) + # cg.add(var.set_reset_pin(config[CONF_RESET_PIN])) + if reset_pin := config.get(CONF_RESET_PIN, None): + pin = await cg.gpio_pin_expression(reset_pin) + cg.add(var.set_reset_pin(pin)) if power_pin := config.get(CONF_POWER_PIN, None): pin = await cg.gpio_pin_expression(power_pin) cg.add(var.set_power_pin(pin)) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index c7496b91bc..3c04ff82cf 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -93,6 +93,7 @@ void ModemComponent::setup() { if (this->power_pin_) { this->power_pin_->setup(); this->power_pin_->digital_write(false); + this->reset_pin_->setup(); } } @@ -137,7 +138,7 @@ void ModemComponent::dump_config() { ESP_LOGCONFIG(TAG, "Modem:"); ESP_LOGCONFIG(TAG, " Power Pin: %d", this->power_pin_->get_pin()); ESP_LOGCONFIG(TAG, " Type: %d", this->type_); - ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_); + ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_->get_pin()); ESP_LOGCONFIG(TAG, " APN: %s", this->apn_.c_str()); ESP_LOGCONFIG(TAG, " TX Pin: %d", this->tx_pin_); ESP_LOGCONFIG(TAG, " RX Pin: %d", this->rx_pin_); @@ -162,12 +163,10 @@ void ModemComponent::dump_connect_params() { } void ModemComponent::esp_modem_hard_reset() { - gpio_set_direction(gpio_num_t(this->reset_pin_), GPIO_MODE_OUTPUT); - // gpio_pullup_en(reset_pin_); - gpio_set_level(gpio_num_t(this->reset_pin_), 0); + this->reset_pin_->digital_write(false); ESP_LOGD(TAG, "reset_pin_ 0"); - vTaskDelay(pdMS_TO_TICKS(50)); // NOLINT - gpio_set_level(gpio_num_t(this->reset_pin_), 1); + vTaskDelay(pdMS_TO_TICKS(110)); // NOLINT + this->reset_pin_->digital_write(true); ESP_LOGD(TAG, "reset_pin_ 1"); time_hard_reset_modem = millis(); } @@ -259,7 +258,7 @@ void ModemComponent::start_connect_() { bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; } void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; } void ModemComponent::set_type(ModemType type) { this->type_ = type; } -void ModemComponent::set_reset_pin(int reset_pin) { this->reset_pin_ = reset_pin; } +void ModemComponent::set_reset_pin(InternalGPIOPin *reset_pin) { this->reset_pin_ = reset_pin; } void ModemComponent::set_apn(const std::string &apn) { this->apn_ = apn; } void ModemComponent::set_tx_pin(int tx_pin) { this->tx_pin_ = tx_pin; } void ModemComponent::set_rx_pin(int rx_pin) { this->rx_pin_ = rx_pin; } diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index d256974c3d..1f1fce7be8 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -42,7 +42,7 @@ class ModemComponent : public Component { bool is_connected(); void set_power_pin(InternalGPIOPin *power_pin); void set_type(ModemType type); - void set_reset_pin(int reset_pin); + void set_reset_pin(InternalGPIOPin *reset_pin); void set_apn(const std::string &apn); void set_tx_pin(int tx_pin); void set_rx_pin(int rx_pin); @@ -68,7 +68,7 @@ class ModemComponent : public Component { std::shared_ptr dte{nullptr}; std::unique_ptr dce{nullptr}; ModemType type_{MODEM_TYPE_UNKNOWN}; - int reset_pin_{-1}; + InternalGPIOPin *reset_pin_{nullptr}; InternalGPIOPin *power_pin_{nullptr}; int tx_pin_{-1}; int rx_pin_{-1};