diff --git a/esphome/components/weikai/weikai.cpp b/esphome/components/weikai/weikai.cpp index 938b191756d..fac35532c6e 100644 --- a/esphome/components/weikai/weikai.cpp +++ b/esphome/components/weikai/weikai.cpp @@ -5,6 +5,7 @@ #include "weikai.h" #include "esphome/core/helpers.h" +#include "esphome/core/progmem.h" namespace esphome::weikai { @@ -53,19 +54,18 @@ void print_buffer(const uint8_t *data, size_t length) { } } -static const char *const REG_TO_STR_P0[16] = {"GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", - "SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485"}; -static const char *const REG_TO_STR_P1[16] = {"GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL", - "TFTL", "FWTH", "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY"}; +PROGMEM_STRING_TABLE(RegNamesPage0, "GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", "SIFR", "TFCNT", + "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485"); +PROGMEM_STRING_TABLE(RegNamesPage1, "GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL", "TFTL", "FWTH", + "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY"); -// method to print a register value as text: used in the log messages ... -const char *reg_to_str(int reg, bool page1) { +const LogString *reg_to_str(int reg, bool page1) { if (reg == WKREG_GPDAT) { - return "GPDAT"; + return LOG_STR("GPDAT"); } else if (reg == WKREG_GPDIR) { - return "GPDIR"; + return LOG_STR("GPDIR"); } else { - return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F]; + return page1 ? RegNamesPage1::get_log_str(reg & 0x0F, 0) : RegNamesPage0::get_log_str(reg & 0x0F, 0); } } diff --git a/esphome/components/weikai/weikai.h b/esphome/components/weikai/weikai.h index eb8a41ad9d0..a669c4292f5 100644 --- a/esphome/components/weikai/weikai.h +++ b/esphome/components/weikai/weikai.h @@ -40,6 +40,9 @@ constexpr size_t FIFO_SIZE = 256; /// @brief size of the ring buffer set to size of the FIFO constexpr size_t RING_BUFFER_SIZE = FIFO_SIZE; +/// @brief Name of a register for the log messages +const LogString *reg_to_str(int reg, bool page1); + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief This is an helper class that provides a simple ring buffers that works as a FIFO /// @details This ring buffer is used to buffer the bytes received in the FIFO of the Weika device. The best way to read diff --git a/esphome/components/weikai_i2c/weikai_i2c.cpp b/esphome/components/weikai_i2c/weikai_i2c.cpp index bafa6c4e03a..0e71c4c3bbd 100644 --- a/esphome/components/weikai_i2c/weikai_i2c.cpp +++ b/esphome/components/weikai_i2c/weikai_i2c.cpp @@ -25,21 +25,7 @@ void print_buffer(const uint8_t *data, size_t length) { } } -static const char *const REG_TO_STR_P0[16] = {"GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", - "SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485"}; -static const char *const REG_TO_STR_P1[16] = {"GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL", - "TFTL", "FWTH", "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY"}; using namespace weikai; -// method to print a register value as text: used in the log messages ... -const char *reg_to_str(int reg, bool page1) { - if (reg == WKREG_GPDAT) { - return "GPDAT"; - } else if (reg == WKREG_GPDIR) { - return "GPDIR"; - } else { - return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F]; - } -} enum RegType { REG = 0, FIFO = 1 }; ///< Register or FIFO /// @brief Computes the I²C bus's address used to access the component @@ -72,11 +58,11 @@ uint8_t WeikaiRegisterI2C::read_reg() const { if (error == i2c::NO_ERROR) { this->comp_->status_clear_warning(); ESP_LOGVV(TAG, "WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address, - reg_to_str(this->register_, comp_i2c->page1()), this->channel_, (int) error, value); + LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value); } else { // error this->comp_->status_set_warning(); ESP_LOGE(TAG, "WeikaiRegisterI2C::read_reg() @%02X reg=%s ch=%u I2C_code:%d, buf=%02X", address, - reg_to_str(this->register_, comp_i2c->page1()), this->channel_, (int) error, value); + LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value); } return value; } @@ -108,11 +94,11 @@ void WeikaiRegisterI2C::write_reg(uint8_t value) { if (error == i2c::NO_ERROR) { this->comp_->status_clear_warning(); ESP_LOGVV(TAG, "WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%02X", address, - reg_to_str(this->register_, comp_i2c->page1()), this->channel_, (int) error, value); + LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value); } else { // error this->comp_->status_set_warning(); ESP_LOGE(TAG, "WK2168Reg::write_reg() @%02X reg=%s ch=%d I2C_code:%d buf=%d", address, - reg_to_str(this->register_, comp_i2c->page1()), this->channel_, (int) error, value); + LOG_STR_ARG(reg_to_str(this->register_, comp_i2c->page1())), this->channel_, (int) error, value); } } diff --git a/esphome/components/weikai_spi/weikai_spi.cpp b/esphome/components/weikai_spi/weikai_spi.cpp index 6a44fad6f3b..5cbbb7be928 100644 --- a/esphome/components/weikai_spi/weikai_spi.cpp +++ b/esphome/components/weikai_spi/weikai_spi.cpp @@ -52,22 +52,6 @@ void print_buffer(const uint8_t *data, size_t length) { } } -static const char *const REG_TO_STR_P0[16] = {"GENA", "GRST", "GMUT", "SPAGE", "SCR", "LCR", "FCR", "SIER", - "SIFR", "TFCNT", "RFCNT", "FSR", "LSR", "FDAT", "FWCR", "RS485"}; -static const char *const REG_TO_STR_P1[16] = {"GENA", "GRST", "GMUT", "SPAGE", "BAUD1", "BAUD0", "PRES", "RFTL", - "TFTL", "FWTH", "FWTL", "XON1", "XOFF1", "SADR", "SAEN", "RTSDLY"}; - -// method to print a register value as text: used in the log messages ... -const char *reg_to_str(int reg, bool page1) { - if (reg == WKREG_GPDAT) { - return "GPDAT"; - } else if (reg == WKREG_GPDIR) { - return "GPDIR"; - } else { - return page1 ? REG_TO_STR_P1[reg & 0x0F] : REG_TO_STR_P0[reg & 0x0F]; - } -} - enum RegType { REG = 0, FIFO = 1 }; ///< Register or FIFO enum CmdType { WRITE_CMD = 0, READ_CMD = 1 }; ///< Read or Write transfer @@ -101,7 +85,7 @@ uint8_t WeikaiRegisterSPI::read_reg() const { spi_comp->disable(); char bin_buf[9]; ESP_LOGVV(TAG, "WeikaiRegisterSPI::read_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, cmd), cmd, - reg_to_str(this->register_, this->comp_->page1()), this->channel_, val); + LOG_STR_ARG(reg_to_str(this->register_, this->comp_->page1())), this->channel_, val); return val; } @@ -128,7 +112,7 @@ void WeikaiRegisterSPI::write_reg(uint8_t value) { spi_comp->disable(); char bin_buf[9]; ESP_LOGVV(TAG, "WeikaiRegisterSPI::write_reg() cmd=%s(%02X) reg=%s ch=%d buf=%02X", format_bin_to(bin_buf, buf[0]), - buf[0], reg_to_str(this->register_, this->comp_->page1()), this->channel_, buf[1]); + buf[0], LOG_STR_ARG(reg_to_str(this->register_, this->comp_->page1())), this->channel_, buf[1]); } void WeikaiRegisterSPI::write_fifo(uint8_t *data, size_t length) {