mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[gpio] Avoid heap allocation in dump_summary (#12760)
This commit is contained in:
@@ -128,7 +128,9 @@ void CH422GGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->
|
||||
bool CH422GGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) ^ this->inverted_; }
|
||||
|
||||
void CH422GGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value ^ this->inverted_); }
|
||||
std::string CH422GGPIOPin::dump_summary() const { return str_sprintf("EXIO%u via CH422G", pin_); }
|
||||
size_t CH422GGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "EXIO%u via CH422G", this->pin_);
|
||||
}
|
||||
void CH422GGPIOPin::set_flags(gpio::Flags flags) {
|
||||
flags_ = flags;
|
||||
this->parent_->pin_mode(this->pin_, flags);
|
||||
|
||||
@@ -50,7 +50,7 @@ class CH422GGPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(CH422GComponent *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -97,10 +97,8 @@ void ESP32InternalGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpi
|
||||
gpio_isr_handler_add(this->get_pin_num(), func, arg);
|
||||
}
|
||||
|
||||
std::string ESP32InternalGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "GPIO%" PRIu32, static_cast<uint32_t>(this->pin_));
|
||||
return buffer;
|
||||
size_t ESP32InternalGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "GPIO%" PRIu32, static_cast<uint32_t>(this->pin_));
|
||||
}
|
||||
|
||||
void ESP32InternalGPIOPin::setup() {
|
||||
|
||||
@@ -24,7 +24,7 @@ class ESP32InternalGPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return this->pin_; }
|
||||
|
||||
@@ -98,10 +98,8 @@ void ESP8266GPIOPin::pin_mode(gpio::Flags flags) {
|
||||
pinMode(pin_, flags_to_mode(flags, pin_)); // NOLINT
|
||||
}
|
||||
|
||||
std::string ESP8266GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "GPIO%u", pin_);
|
||||
return buffer;
|
||||
size_t ESP8266GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "GPIO%u", this->pin_);
|
||||
}
|
||||
|
||||
bool ESP8266GPIOPin::digital_read() {
|
||||
|
||||
@@ -17,7 +17,7 @@ class ESP8266GPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return pin_; }
|
||||
|
||||
@@ -34,9 +34,9 @@ void HLW8012Component::setup() {
|
||||
}
|
||||
void HLW8012Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "HLW8012:");
|
||||
LOG_PIN(" SEL Pin: ", this->sel_pin_)
|
||||
LOG_PIN(" CF Pin: ", this->cf_pin_)
|
||||
LOG_PIN(" CF1 Pin: ", this->cf1_pin_)
|
||||
LOG_PIN(" SEL Pin: ", this->sel_pin_);
|
||||
LOG_PIN(" CF Pin: ", this->cf_pin_);
|
||||
LOG_PIN(" CF1 Pin: ", this->cf1_pin_);
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Change measurement mode every %" PRIu32 "\n"
|
||||
" Current resistor: %.1f mΩ\n"
|
||||
|
||||
@@ -25,11 +25,7 @@ void HostGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::Interr
|
||||
}
|
||||
void HostGPIOPin::pin_mode(gpio::Flags flags) { ESP_LOGD(TAG, "Setting pin %d mode to %02X", pin_, (uint32_t) flags); }
|
||||
|
||||
std::string HostGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "GPIO%u", pin_);
|
||||
return buffer;
|
||||
}
|
||||
size_t HostGPIOPin::dump_summary(char *buffer, size_t len) const { return snprintf(buffer, len, "GPIO%u", this->pin_); }
|
||||
|
||||
bool HostGPIOPin::digital_read() { return inverted_; }
|
||||
void HostGPIOPin::digital_write(bool value) {
|
||||
|
||||
@@ -17,7 +17,7 @@ class HostGPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return pin_; }
|
||||
|
||||
@@ -63,10 +63,8 @@ void ArduinoInternalGPIOPin::pin_mode(gpio::Flags flags) {
|
||||
pinMode(pin_, flags_to_mode(flags)); // NOLINT
|
||||
}
|
||||
|
||||
std::string ArduinoInternalGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u", pin_);
|
||||
return buffer;
|
||||
size_t ArduinoInternalGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u", this->pin_);
|
||||
}
|
||||
|
||||
bool ArduinoInternalGPIOPin::digital_read() {
|
||||
|
||||
@@ -16,7 +16,7 @@ class ArduinoInternalGPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return pin_; }
|
||||
|
||||
@@ -161,10 +161,8 @@ void MAX6956GPIOPin::setup() { pin_mode(flags_); }
|
||||
void MAX6956GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool MAX6956GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void MAX6956GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string MAX6956GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via Max6956", pin_);
|
||||
return buffer;
|
||||
size_t MAX6956GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via Max6956", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace max6956
|
||||
|
||||
@@ -76,7 +76,7 @@ class MAX6956GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(MAX6956 *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -99,10 +99,8 @@ void MCP23016GPIOPin::setup() { pin_mode(flags_); }
|
||||
void MCP23016GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool MCP23016GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void MCP23016GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string MCP23016GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via MCP23016", pin_);
|
||||
return buffer;
|
||||
size_t MCP23016GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via MCP23016", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace mcp23016
|
||||
|
||||
@@ -60,7 +60,7 @@ class MCP23016GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(MCP23016 *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -16,8 +16,8 @@ template<uint8_t N> bool MCP23XXXGPIOPin<N>::digital_read() {
|
||||
template<uint8_t N> void MCP23XXXGPIOPin<N>::digital_write(bool value) {
|
||||
this->parent_->digital_write(this->pin_, value != this->inverted_);
|
||||
}
|
||||
template<uint8_t N> std::string MCP23XXXGPIOPin<N>::dump_summary() const {
|
||||
return str_snprintf("%u via MCP23XXX", 15, pin_);
|
||||
template<uint8_t N> size_t MCP23XXXGPIOPin<N>::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via MCP23XXX", this->pin_);
|
||||
}
|
||||
|
||||
template class MCP23XXXGPIOPin<8>;
|
||||
|
||||
@@ -36,7 +36,7 @@ template<uint8_t N> class MCP23XXXGPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(MCP23XXXBase<N> *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -153,10 +153,8 @@ void MPR121GPIOPin::digital_write(bool value) {
|
||||
this->parent_->digital_write(this->pin_ - 4, value != this->inverted_);
|
||||
}
|
||||
|
||||
std::string MPR121GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "ELE%u on MPR121", this->pin_);
|
||||
return buffer;
|
||||
size_t MPR121GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "ELE%u on MPR121", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace mpr121
|
||||
|
||||
@@ -109,7 +109,7 @@ class MPR121GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(MPR121Component *parent) { this->parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { this->pin_ = pin; }
|
||||
|
||||
@@ -180,10 +180,8 @@ void PCA6416AGPIOPin::setup() { pin_mode(flags_); }
|
||||
void PCA6416AGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool PCA6416AGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void PCA6416AGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string PCA6416AGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via PCA6416A", pin_);
|
||||
return buffer;
|
||||
size_t PCA6416AGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via PCA6416A", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace pca6416a
|
||||
|
||||
@@ -52,7 +52,7 @@ class PCA6416AGPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(PCA6416AComponent *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -129,10 +129,8 @@ void PCA9554GPIOPin::setup() { pin_mode(flags_); }
|
||||
void PCA9554GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool PCA9554GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void PCA9554GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string PCA9554GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via PCA9554", pin_);
|
||||
return buffer;
|
||||
size_t PCA9554GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via PCA9554", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace pca9554
|
||||
|
||||
@@ -59,7 +59,7 @@ class PCA9554GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(PCA9554Component *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -104,10 +104,8 @@ void PCF8574GPIOPin::setup() { pin_mode(flags_); }
|
||||
void PCF8574GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool PCF8574GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void PCF8574GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string PCF8574GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via PCF8574", pin_);
|
||||
return buffer;
|
||||
size_t PCF8574GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via PCF8574", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace pcf8574
|
||||
|
||||
@@ -54,7 +54,7 @@ class PCF8574GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(PCF8574Component *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
|
||||
@@ -164,7 +164,9 @@ bool PI4IOE5V6408GPIOPin::digital_read() { return this->parent_->digital_read(th
|
||||
void PI4IOE5V6408GPIOPin::digital_write(bool value) {
|
||||
this->parent_->digital_write(this->pin_, value != this->inverted_);
|
||||
}
|
||||
std::string PI4IOE5V6408GPIOPin::dump_summary() const { return str_sprintf("%u via PI4IOE5V6408", this->pin_); }
|
||||
size_t PI4IOE5V6408GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via PI4IOE5V6408", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace pi4ioe5v6408
|
||||
} // namespace esphome
|
||||
|
||||
@@ -52,7 +52,7 @@ class PI4IOE5V6408GPIOPin : public GPIOPin, public Parented<PI4IOE5V6408Componen
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_pin(uint8_t pin) { this->pin_ = pin; }
|
||||
void set_inverted(bool inverted) { this->inverted_ = inverted; }
|
||||
|
||||
@@ -64,10 +64,8 @@ void RP2040GPIOPin::pin_mode(gpio::Flags flags) {
|
||||
pinMode(pin_, flags_to_mode(flags, pin_)); // NOLINT
|
||||
}
|
||||
|
||||
std::string RP2040GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "GPIO%u", pin_);
|
||||
return buffer;
|
||||
size_t RP2040GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "GPIO%u", this->pin_);
|
||||
}
|
||||
|
||||
bool RP2040GPIOPin::digital_read() {
|
||||
|
||||
@@ -18,7 +18,7 @@ class RP2040GPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return pin_; }
|
||||
|
||||
@@ -64,7 +64,9 @@ float SN74HC165Component::get_setup_priority() const { return setup_priority::IO
|
||||
|
||||
bool SN74HC165GPIOPin::digital_read() { return this->parent_->digital_read_(this->pin_) != this->inverted_; }
|
||||
|
||||
std::string SN74HC165GPIOPin::dump_summary() const { return str_snprintf("%u via SN74HC165", 18, pin_); }
|
||||
size_t SN74HC165GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via SN74HC165", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace sn74hc165
|
||||
} // namespace esphome
|
||||
|
||||
@@ -47,7 +47,7 @@ class SN74HC165GPIOPin : public GPIOPin, public Parented<SN74HC165Component> {
|
||||
void pin_mode(gpio::Flags flags) override {}
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override{};
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_pin(uint16_t pin) { pin_ = pin; }
|
||||
void set_inverted(bool inverted) { inverted_ = inverted; }
|
||||
|
||||
@@ -93,7 +93,9 @@ float SN74HC595Component::get_setup_priority() const { return setup_priority::IO
|
||||
void SN74HC595GPIOPin::digital_write(bool value) {
|
||||
this->parent_->digital_write_(this->pin_, value != this->inverted_);
|
||||
}
|
||||
std::string SN74HC595GPIOPin::dump_summary() const { return str_snprintf("%u via SN74HC595", 18, pin_); }
|
||||
size_t SN74HC595GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via SN74HC595", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace sn74hc595
|
||||
} // namespace esphome
|
||||
|
||||
@@ -54,7 +54,7 @@ class SN74HC595GPIOPin : public GPIOPin, public Parented<SN74HC595Component> {
|
||||
void pin_mode(gpio::Flags flags) override {}
|
||||
bool digital_read() override { return false; }
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_pin(uint16_t pin) { pin_ = pin; }
|
||||
void set_inverted(bool inverted) { inverted_ = inverted; }
|
||||
|
||||
@@ -64,9 +64,9 @@ void SPIComponent::setup() {
|
||||
|
||||
void SPIComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "SPI bus:");
|
||||
LOG_PIN(" CLK Pin: ", this->clk_pin_)
|
||||
LOG_PIN(" SDI Pin: ", this->sdi_pin_)
|
||||
LOG_PIN(" SDO Pin: ", this->sdo_pin_)
|
||||
LOG_PIN(" CLK Pin: ", this->clk_pin_);
|
||||
LOG_PIN(" SDI Pin: ", this->sdi_pin_);
|
||||
LOG_PIN(" SDO Pin: ", this->sdo_pin_);
|
||||
for (size_t i = 0; i != this->data_pins_.size(); i++) {
|
||||
ESP_LOGCONFIG(TAG, " Data pin %u: GPIO%d", i, this->data_pins_[i]);
|
||||
}
|
||||
|
||||
@@ -120,7 +120,11 @@ class NullPin : public GPIOPin {
|
||||
|
||||
void digital_write(bool value) override {}
|
||||
|
||||
std::string dump_summary() const override { return std::string(); }
|
||||
size_t dump_summary(char *buffer, size_t len) const override {
|
||||
if (len > 0)
|
||||
buffer[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
static GPIOPin *const NULL_PIN; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
@@ -12,10 +12,8 @@ void SX1509GPIOPin::setup() { pin_mode(flags_); }
|
||||
void SX1509GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void SX1509GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string SX1509GPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via sx1509", this->pin_);
|
||||
return buffer;
|
||||
size_t SX1509GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via sx1509", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace sx1509
|
||||
|
||||
@@ -13,7 +13,7 @@ class SX1509GPIOPin : public GPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_parent(SX1509Component *parent) { this->parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { this->pin_ = pin; }
|
||||
|
||||
@@ -138,7 +138,9 @@ void TCA9555GPIOPin::setup() { this->pin_mode(this->flags_); }
|
||||
void TCA9555GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool TCA9555GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void TCA9555GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
std::string TCA9555GPIOPin::dump_summary() const { return str_sprintf("%u via TCA9555", this->pin_); }
|
||||
size_t TCA9555GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via TCA9555", this->pin_);
|
||||
}
|
||||
|
||||
} // namespace tca9555
|
||||
} // namespace esphome
|
||||
|
||||
@@ -48,7 +48,7 @@ class TCA9555GPIOPin : public GPIOPin, public Parented<TCA9555Component> {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
|
||||
void set_pin(uint8_t pin) { this->pin_ = pin; }
|
||||
void set_inverted(bool inverted) { this->inverted_ = inverted; }
|
||||
|
||||
@@ -177,10 +177,10 @@ uint32_t WaveshareEPaper2P13InV3::idle_timeout_() { return 5000; }
|
||||
void WaveshareEPaper2P13InV3::dump_config() {
|
||||
LOG_DISPLAY("", "Waveshare E-Paper", this)
|
||||
ESP_LOGCONFIG(TAG, " Model: 2.13inV3");
|
||||
LOG_PIN(" CS Pin: ", this->cs_)
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_)
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_)
|
||||
LOG_PIN(" Busy Pin: ", this->busy_pin_)
|
||||
LOG_PIN(" CS Pin: ", this->cs_);
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_);
|
||||
LOG_PIN(" Busy Pin: ", this->busy_pin_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -245,10 +245,8 @@ void WeikaiGPIOPin::setup() {
|
||||
this->pin_mode(this->flags_);
|
||||
}
|
||||
|
||||
std::string WeikaiGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "%u via WeiKai %s", this->pin_, this->parent_->get_name());
|
||||
return buffer;
|
||||
size_t WeikaiGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via WeiKai %s", this->pin_, this->parent_->get_name());
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -278,7 +278,7 @@ class WeikaiGPIOPin : public GPIOPin {
|
||||
gpio::Flags get_flags() const override { return this->flags_; }
|
||||
|
||||
void setup() override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void pin_mode(gpio::Flags flags) override { this->parent_->set_pin_direction_(this->pin_, flags); }
|
||||
bool digital_read() override { return this->parent_->read_pin_val_(this->pin_) != this->inverted_; }
|
||||
void digital_write(bool value) override { this->parent_->write_pin_val_(this->pin_, value != this->inverted_); }
|
||||
|
||||
@@ -110,7 +110,9 @@ void XL9535Component::pin_mode(uint8_t pin, gpio::Flags mode) {
|
||||
|
||||
void XL9535GPIOPin::setup() { this->pin_mode(this->flags_); }
|
||||
|
||||
std::string XL9535GPIOPin::dump_summary() const { return str_snprintf("%u via XL9535", 15, this->pin_); }
|
||||
size_t XL9535GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "%u via XL9535", this->pin_);
|
||||
}
|
||||
|
||||
void XL9535GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
|
||||
bool XL9535GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
|
||||
@@ -39,7 +39,7 @@ class XL9535GPIOPin : public GPIOPin {
|
||||
gpio::Flags get_flags() const override { return this->flags_; }
|
||||
|
||||
void setup() override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
|
||||
@@ -85,10 +85,8 @@ void ZephyrGPIOPin::pin_mode(gpio::Flags flags) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string ZephyrGPIOPin::dump_summary() const {
|
||||
char buffer[32];
|
||||
snprintf(buffer, sizeof(buffer), "GPIO%u, P%u.%u", this->pin_, this->pin_ / 32, this->pin_ % 32);
|
||||
return buffer;
|
||||
size_t ZephyrGPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
return snprintf(buffer, len, "GPIO%u, P%u.%u", this->pin_, this->pin_ / 32, this->pin_ % 32);
|
||||
}
|
||||
|
||||
bool ZephyrGPIOPin::digital_read() {
|
||||
|
||||
@@ -16,7 +16,7 @@ class ZephyrGPIOPin : public InternalGPIOPin {
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
size_t dump_summary(char *buffer, size_t len) const override;
|
||||
void detach_interrupt() const override;
|
||||
ISRInternalGPIOPin to_isr() const override;
|
||||
uint8_t get_pin() const override { return this->pin_; }
|
||||
|
||||
24
esphome/core/gpio.cpp
Normal file
24
esphome/core/gpio.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "esphome/core/gpio.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin) {
|
||||
if (pin == nullptr)
|
||||
return;
|
||||
static constexpr size_t LOG_PIN_PREFIX_MAX_LEN = 32;
|
||||
char prefix_buf[LOG_PIN_PREFIX_MAX_LEN];
|
||||
strncpy_P(prefix_buf, reinterpret_cast<const char *>(prefix), sizeof(prefix_buf) - 1);
|
||||
prefix_buf[sizeof(prefix_buf) - 1] = '\0';
|
||||
log_pin_with_prefix(tag, prefix_buf, pin);
|
||||
}
|
||||
#else
|
||||
void log_pin(const char *tag, const char *prefix, GPIOPin *pin) {
|
||||
if (pin == nullptr)
|
||||
return;
|
||||
log_pin_with_prefix(tag, prefix, pin);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace esphome
|
||||
@@ -1,13 +1,22 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
#define LOG_PIN(prefix, pin) \
|
||||
if ((pin) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, prefix "%s", (pin)->dump_summary().c_str()); \
|
||||
}
|
||||
/// Maximum buffer size for dump_summary output
|
||||
inline constexpr size_t GPIO_SUMMARY_MAX_LEN = 48;
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#define LOG_PIN(prefix, pin) log_pin(TAG, F(prefix), pin)
|
||||
#else
|
||||
#define LOG_PIN(prefix, pin) log_pin(TAG, prefix, pin)
|
||||
#endif
|
||||
|
||||
// put GPIO flags in a namespace to not pollute esphome namespace
|
||||
namespace gpio {
|
||||
@@ -64,7 +73,17 @@ class GPIOPin {
|
||||
|
||||
virtual void digital_write(bool value) = 0;
|
||||
|
||||
virtual std::string dump_summary() const = 0;
|
||||
/// Write a summary of this pin to the provided buffer.
|
||||
/// @param buffer The buffer to write to
|
||||
/// @param len The size of the buffer (must be > 0)
|
||||
/// @return The number of characters that would be written (excluding null terminator),
|
||||
/// which may exceed len-1 if truncation occurred (snprintf semantics)
|
||||
virtual size_t dump_summary(char *buffer, size_t len) const;
|
||||
|
||||
/// Get a summary of this pin as a string.
|
||||
/// @deprecated Use dump_summary(char*, size_t) instead. Will be removed in 2026.7.0.
|
||||
ESPDEPRECATED("Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0.", "2026.1.0")
|
||||
virtual std::string dump_summary() const;
|
||||
|
||||
virtual bool is_internal() { return false; }
|
||||
};
|
||||
@@ -103,4 +122,41 @@ class InternalGPIOPin : public GPIOPin {
|
||||
virtual void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const = 0;
|
||||
};
|
||||
|
||||
// Inline default implementations for GPIOPin virtual methods.
|
||||
// These provide bridge functionality for backwards compatibility with external components.
|
||||
|
||||
// Default implementation bridges to old std::string method for backwards compatibility.
|
||||
inline size_t GPIOPin::dump_summary(char *buffer, size_t len) const {
|
||||
if (len == 0)
|
||||
return 0;
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
std::string s = this->dump_summary();
|
||||
#pragma GCC diagnostic pop
|
||||
size_t copy_len = std::min(s.size(), len - 1);
|
||||
memcpy(buffer, s.c_str(), copy_len);
|
||||
buffer[copy_len] = '\0';
|
||||
return s.size(); // Return would-be length (snprintf semantics)
|
||||
}
|
||||
|
||||
// Default implementation returns empty string.
|
||||
// External components should override this if they haven't migrated to buffer-based version.
|
||||
// Remove before 2026.7.0
|
||||
inline std::string GPIOPin::dump_summary() const { return {}; }
|
||||
|
||||
// Inline helper for log_pin - allows compiler to inline into log_pin in gpio.cpp
|
||||
inline void log_pin_with_prefix(const char *tag, const char *prefix, GPIOPin *pin) {
|
||||
char buffer[GPIO_SUMMARY_MAX_LEN];
|
||||
size_t len = pin->dump_summary(buffer, sizeof(buffer));
|
||||
len = std::min(len, sizeof(buffer) - 1);
|
||||
esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, "%s%.*s", prefix, (int) len, buffer);
|
||||
}
|
||||
|
||||
// log_pin function declarations - implementation in gpio.cpp
|
||||
#ifdef USE_ESP8266
|
||||
void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin);
|
||||
#else
|
||||
void log_pin(const char *tag, const char *prefix, GPIOPin *pin);
|
||||
#endif
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
Reference in New Issue
Block a user