pzem(build): flash strings clean-up, remove FlashStringHelper

This commit is contained in:
Maxim Prokhorov
2025-09-08 22:41:42 +03:00
parent c8c1b7a4d2
commit 7fffcbda4f
2 changed files with 39 additions and 27 deletions

View File

@@ -287,16 +287,26 @@ public:
static constexpr size_t DevicesMax { PZEM004T_DEVICES_MAX };
static IPAddress defaultAddress(size_t device) {
const __FlashStringHelper* ptr {
(0 == device) ? F(PZEM004T_ADDRESS_1) :
(1 == device) ? F(PZEM004T_ADDRESS_2) :
(2 == device) ? F(PZEM004T_ADDRESS_3) :
(3 == device) ? F(PZEM004T_ADDRESS_4) :
nullptr
static const espurna::StringView addresses[] PROGMEM {
#define PZEM004T_MAKE_ADDRESS(X)\
(__extension__({\
STRING_VIEW_INLINE(Address, X);\
Address;}))
PZEM004T_MAKE_ADDRESS(PZEM004T_ADDRESS_1),
PZEM004T_MAKE_ADDRESS(PZEM004T_ADDRESS_2),
PZEM004T_MAKE_ADDRESS(PZEM004T_ADDRESS_3),
PZEM004T_MAKE_ADDRESS(PZEM004T_ADDRESS_4),
#undef PZEM004T_MAKE_ADDRESS
};
IPAddress out;
out.fromString(String(ptr));
if (device < std::size(addresses)) {
out.fromString(addresses[device].toString());
} else {
out.fromString(nullptr);
}
return out;
}

View File

@@ -176,9 +176,10 @@ public:
bool locked { false };
};
void modbusDebugBuffer(const String& message, buffer_type& buffer, size_t size) {
void modbusDebugBuffer(espurna::StringView message, buffer_type& buffer, size_t size) {
hexEncode(buffer.data(), size, _debug_buffer, sizeof(_debug_buffer));
PZEM_DEBUG_MSG_P(PSTR("[PZEM004TV3] %s: %s (%u bytes)\n"), message.c_str(), _debug_buffer, size);
PZEM_DEBUG_MSG_P(PSTR("[PZEM004TV3] %.*s: %s (%u bytes)\n"),
message.length(), message.data(), _debug_buffer, size);
}
static size_t modbusExpect(const adu_builder& builder) {
@@ -250,7 +251,7 @@ public:
}
if (bytes && _debug) {
modbusDebugBuffer(F("Received"), buffer, bytes);
modbusDebugBuffer(STRING_VIEW("Received"), buffer, bytes);
}
if (bytes != expect) {
@@ -269,7 +270,7 @@ public:
if (buffer[1] & ErrorMask) {
PZEM_DEBUG_MSG_P(PSTR("[PZEM004TV3] ERROR: %s (0x%02X)\n"),
errorToString(buffer[2]).c_str(), buffer[2]);
errorToString(buffer[2]).toString().c_str(), buffer[2]);
return;
}
@@ -312,36 +313,37 @@ public:
}
// For more, see MODBUS application protocol specification, 7 MODBUS Exception Responses
String errorToString(uint8_t error) {
const __FlashStringHelper *ptr = nullptr;
espurna::StringView errorToString(uint8_t error) {
espurna::StringView out;
switch (error) {
case 0x01:
ptr = F("Illegal function");
out = STRING_VIEW("Illegal function");
break;
case 0x02:
ptr = F("Illegal data address");
out = STRING_VIEW("Illegal data address");
break;
case 0x03:
ptr = F("Illegal data value");
out = STRING_VIEW("Illegal data value");
break;
case 0x04:
ptr = F("Device failure");
out = STRING_VIEW("Device failure");
break;
case 0x05:
ptr = F("Acknowledged");
out = STRING_VIEW("Acknowledged");
break;
case 0x06:
ptr = F("Busy");
out = STRING_VIEW("Busy");
break;
case 0x08:
ptr = F("Memory parity error");
out = STRING_VIEW("Memory parity error");
break;
default:
ptr = F("Unknown");
out = STRING_VIEW("Unknown");
break;
}
return ptr;
return out;
}
// Quoting the README.md of the original library repo and datasheet, we have:
@@ -510,8 +512,8 @@ public:
}
String description() const override {
static const String base(F("PZEM004TV30"));
return base + " @ 0x" + String(_address, 16);
STRING_VIEW_INLINE(Base, "PZEM004TV30 @ 0x");
return Base + String(_address, 16);
}
String address(unsigned char) const override {
@@ -603,11 +605,11 @@ constexpr espurna::duration::Milliseconds PZEM004TV30Sensor::DefaultUpdateInterv
PZEM004TV30Sensor::Instance PZEM004TV30Sensor::_instance{};
PROGMEM_STRING(PzemV3Address, "PZ.ADDRESS");
STRING_VIEW_INLINE(PzemV3Address, "PZ.ADDRESS");
void PZEM004TV30Sensor::command_address(::terminal::CommandContext&& ctx) {
if (ctx.argv.size() != 2) {
terminalError(ctx, F("PZ.ADDRESS <ADDRESS>"));
terminalError(ctx, STRING_VIEW("PZ.ADDRESS <ADDRESS>"));
return;
}
@@ -621,7 +623,7 @@ void PZEM004TV30Sensor::command_address(::terminal::CommandContext&& ctx) {
return;
}
terminalError(ctx, F("Could not change the address"));
terminalError(ctx, STRING_VIEW("Could not change the address"));
}
static constexpr ::terminal::Command PzemV3Commands[] PROGMEM {