add power pin

This commit is contained in:
Alexandr Pyslar 2024-10-08 02:54:37 +00:00 committed by Chelios
parent 87312d445d
commit 1edeadbbf4
3 changed files with 14 additions and 11 deletions

View file

@ -49,7 +49,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.Optional(CONF_POWER_PIN): pins.internal_gpio_output_pin_number,
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,
cv.Optional(CONF_APN, default="internet"): cv.string,
@ -72,12 +72,10 @@ async def to_code(config):
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_power_pin(config[CONF_POWER_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))
cg.add(var.set_tx_pin(config[CONF_TX_PIN]))
cg.add(var.set_rx_pin(config[CONF_RX_PIN]))
cg.add(var.set_apn(config[CONF_APN]))

View file

@ -88,6 +88,12 @@ void ModemComponent::setup() {
esp_netif_flags_t flags = esp_netif_get_flags(this->modem_netif_);
this->started_ = true;
// power pin on
if (this->power_pin_) {
this->power_pin_->setup();
this->power_pin_->digital_write(false);
}
}
void ModemComponent::loop() {
@ -114,7 +120,7 @@ void ModemComponent::loop() {
break;
case ModemComponentState::CONNECTED:
if (time_info_print < now) {
ESP_LOGI(TAG, "voltage %dV.", get_modem_voltage()/1000);
ESP_LOGI(TAG, "voltage %dV.", get_modem_voltage() / 1000);
if (esp_netif_is_netif_up(this->modem_netif_)) {
ESP_LOGD(TAG, "esp_netif_is_netif_UP");
} else {
@ -129,7 +135,7 @@ void ModemComponent::loop() {
void ModemComponent::dump_config() {
this->dump_connect_params();
ESP_LOGCONFIG(TAG, "Modem:");
ESP_LOGCONFIG(TAG, " Power Pin: %d", this->power_pin_);
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, " APN: %s", this->apn_.c_str());
@ -251,8 +257,7 @@ void ModemComponent::start_connect_() {
}
bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; }
void ModemComponent::set_power_pin(std::unique_ptr<GPIOPin> power_pin) { this->power_pin_ = &power_pin; }
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_apn(const std::string &apn) { this->apn_ = apn; }

View file

@ -8,7 +8,7 @@
#ifdef USE_ESP32
using esphome::esp_log_printf_;
using esphome::esp_log_printf_;
#include "esp_netif.h"
#include "cxx_include/esp_modem_api.hpp"
@ -40,7 +40,7 @@ class ModemComponent : public Component {
float get_setup_priority() const override;
bool can_proceed() override;
bool is_connected();
void set_power_pin(std::unique_ptr<GPIOPin> power_pin);
void set_power_pin(InternalGPIOPin *power_pin);
void set_type(ModemType type);
void set_reset_pin(int reset_pin);
void set_apn(const std::string &apn);
@ -69,7 +69,7 @@ class ModemComponent : public Component {
std::unique_ptr<esp_modem::DCE> dce{nullptr};
ModemType type_{MODEM_TYPE_UNKNOWN};
int reset_pin_{-1};
std::unique_ptr<GPIOPin> power_pin_{nullptr};
InternalGPIOPin *power_pin_{nullptr};
int tx_pin_{-1};
int rx_pin_{-1};
std::string apn_{""};