mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[core] Collapse ESP8266 flash string ifdefs onto the progmem helpers (#19578)
This commit is contained in:
@@ -5,16 +5,13 @@
|
||||
#include "esphome/components/noise/noise.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "proto.h"
|
||||
#include <cstring>
|
||||
#include <cinttypes>
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include <pgmspace.h>
|
||||
#endif
|
||||
|
||||
namespace esphome::api {
|
||||
|
||||
using noise::noise_err_to_logstr;
|
||||
@@ -26,11 +23,7 @@ static_assert(MAX_HANDSHAKE_SIZE == noise::MAX_HANDSHAKE_SIZE,
|
||||
"api and noise component handshake size limits must match");
|
||||
|
||||
static const char *const TAG = "api.noise";
|
||||
#ifdef USE_ESP8266
|
||||
static constexpr char PROLOGUE_INIT[] PROGMEM = "NoiseAPIInit";
|
||||
#else
|
||||
static const char *const PROLOGUE_INIT = "NoiseAPIInit";
|
||||
#endif
|
||||
static constexpr size_t PROLOGUE_INIT_LEN = 12; // strlen("NoiseAPIInit")
|
||||
|
||||
// Maximum bytes to log in hex format (168 * 3 = 504, under TX buffer size of 512)
|
||||
@@ -72,11 +65,7 @@ APIError APINoiseFrameHelper::init() {
|
||||
state_ = State::FAILED;
|
||||
return APIError::OUT_OF_MEMORY;
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
memcpy_P(dst, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
#else
|
||||
std::memcpy(dst, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
#endif
|
||||
progmem_memcpy(dst, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
|
||||
state_ = State::CLIENT_HELLO;
|
||||
return APIError::OK;
|
||||
|
||||
@@ -32,9 +32,6 @@ enum LockState : uint8_t {
|
||||
};
|
||||
const LogString *lock_state_to_string(LockState state);
|
||||
|
||||
/// Maximum length of lock state string (including null terminator): "UNLOCKING" = 10
|
||||
static constexpr size_t LOCK_STATE_STR_SIZE = 10;
|
||||
|
||||
class LockTraits {
|
||||
public:
|
||||
LockTraits() = default;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
// On ESP8266 and RP2040 the scheduler-backed MDNS.update() polling window is armed by
|
||||
// IP state listener events on whichever network interface is configured.
|
||||
#if (defined(USE_ESP8266) || defined(USE_RP2)) && \
|
||||
@@ -39,12 +40,7 @@ struct MDNSString;
|
||||
// Macro to cast string literals to MDNSString* (works on all platforms)
|
||||
#define MDNS_STR(name) (reinterpret_cast<const esphome::mdns::MDNSString *>(name))
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include <pgmspace.h>
|
||||
#define MDNS_STR_ARG(s) ((PGM_P) (s))
|
||||
#else
|
||||
#define MDNS_STR_ARG(s) (reinterpret_cast<const char *>(s))
|
||||
#endif
|
||||
#define MDNS_STR_ARG(s) (reinterpret_cast<ESPHOME_PGM_P>(s))
|
||||
|
||||
// Service count is calculated at compile time by Python codegen
|
||||
// MDNS_SERVICE_COUNT will always be defined
|
||||
|
||||
@@ -312,11 +312,7 @@ bool MQTTComponent::send_discovery_() {
|
||||
// Buffer sized for format string expansion: ~4 bytes net growth from format specifier to 8 hex digits, plus
|
||||
// safety margin
|
||||
char version_buf[sizeof(ver_fmt) + 8];
|
||||
#ifdef USE_ESP8266
|
||||
snprintf_P(version_buf, sizeof(version_buf), ver_fmt, App.get_config_hash());
|
||||
#else
|
||||
snprintf(version_buf, sizeof(version_buf), ver_fmt, App.get_config_hash());
|
||||
#endif
|
||||
ESPHOME_snprintf_P(version_buf, sizeof(version_buf), ver_fmt, App.get_config_hash());
|
||||
device_info[MQTT_DEVICE_SW_VERSION] = version_buf;
|
||||
device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD;
|
||||
#if defined(USE_ESP8266) || defined(USE_ESP32)
|
||||
|
||||
@@ -50,14 +50,8 @@ bool MQTTLockComponent::send_initial_state() { return this->publish_state(); }
|
||||
|
||||
bool MQTTLockComponent::publish_state() {
|
||||
char topic_buf[MQTT_DEFAULT_TOPIC_MAX_LEN];
|
||||
#ifdef USE_ESP8266
|
||||
char buf[LOCK_STATE_STR_SIZE];
|
||||
strncpy_P(buf, (PGM_P) lock_state_to_string(this->lock_->state), sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = '\0';
|
||||
return this->publish(this->get_state_topic_to_(topic_buf), buf);
|
||||
#else
|
||||
return this->publish(this->get_state_topic_to_(topic_buf), LOG_STR_ARG(lock_state_to_string(this->lock_->state)));
|
||||
#endif
|
||||
return this->publish(this->get_state_topic_to_(topic_buf),
|
||||
reinterpret_cast<ProgmemStr>(lock_state_to_string(this->lock_->state)));
|
||||
}
|
||||
|
||||
} // namespace esphome::mqtt
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <cinttypes>
|
||||
#include "mqtt_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
#include "mqtt_const.h"
|
||||
|
||||
@@ -59,11 +60,7 @@ void MQTTSensorComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon
|
||||
root[MQTT_FORCE_UPDATE] = true;
|
||||
|
||||
if (this->sensor_->get_state_class() != STATE_CLASS_NONE) {
|
||||
#ifdef USE_ESP8266
|
||||
root[MQTT_STATE_CLASS] = (const __FlashStringHelper *) state_class_to_string(this->sensor_->get_state_class());
|
||||
#else
|
||||
root[MQTT_STATE_CLASS] = LOG_STR_ARG(state_class_to_string(this->sensor_->get_state_class()));
|
||||
#endif
|
||||
root[MQTT_STATE_CLASS] = reinterpret_cast<ProgmemStr>(state_class_to_string(this->sensor_->get_state_class()));
|
||||
}
|
||||
|
||||
config.command_topic = false;
|
||||
|
||||
@@ -144,14 +144,8 @@ void PrometheusHandler::add_friendly_name_label_(AsyncResponseStream *stream, st
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
void PrometheusHandler::print_metric_labels_(AsyncResponseStream *stream, const __FlashStringHelper *metric_name,
|
||||
EntityBase *obj, std::string &area, std::string &node,
|
||||
std::string &friendly_name) {
|
||||
#else
|
||||
void PrometheusHandler::print_metric_labels_(AsyncResponseStream *stream, const char *metric_name, EntityBase *obj,
|
||||
void PrometheusHandler::print_metric_labels_(AsyncResponseStream *stream, ProgmemStr metric_name, EntityBase *obj,
|
||||
std::string &area, std::string &node, std::string &friendly_name) {
|
||||
#endif
|
||||
stream->print(metric_name);
|
||||
stream->print(ESPHOME_F("{id=\""));
|
||||
stream->print(relabel_id_(obj).c_str());
|
||||
@@ -903,11 +897,7 @@ void PrometheusHandler::valve_row_(AsyncResponseStream *stream, valve::Valve *ob
|
||||
stream->print(ESPHOME_F("\",name=\""));
|
||||
stream->print(relabel_name_(obj).c_str());
|
||||
stream->print(ESPHOME_F("\",operation=\""));
|
||||
#ifdef USE_ESP8266
|
||||
stream->print((const __FlashStringHelper *) valve::valve_operation_to_str(obj->current_operation));
|
||||
#else
|
||||
stream->print((const char *) valve::valve_operation_to_str(obj->current_operation));
|
||||
#endif
|
||||
stream->print(reinterpret_cast<ProgmemStr>(valve::valve_operation_to_str(obj->current_operation)));
|
||||
stream->print(ESPHOME_F("\"} "));
|
||||
stream->print(ESPHOME_F("1.0"));
|
||||
stream->print(ESPHOME_F("\n"));
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/controller.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
#ifdef USE_CLIMATE
|
||||
#include "esphome/core/log.h"
|
||||
#endif
|
||||
@@ -68,13 +69,8 @@ class PrometheusHandler final : public AsyncWebHandler, public Component {
|
||||
void add_node_label_(AsyncResponseStream *stream, std::string &node);
|
||||
void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
|
||||
/// Print metric name and common labels (id, area, node, friendly_name, name)
|
||||
#ifdef USE_ESP8266
|
||||
void print_metric_labels_(AsyncResponseStream *stream, const __FlashStringHelper *metric_name, EntityBase *obj,
|
||||
std::string &area, std::string &node, std::string &friendly_name);
|
||||
#else
|
||||
void print_metric_labels_(AsyncResponseStream *stream, const char *metric_name, EntityBase *obj, std::string &area,
|
||||
void print_metric_labels_(AsyncResponseStream *stream, ProgmemStr metric_name, EntityBase *obj, std::string &area,
|
||||
std::string &node, std::string &friendly_name);
|
||||
#endif
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
/// Return the type for prometheus
|
||||
|
||||
@@ -5,14 +5,8 @@ namespace esphome::script {
|
||||
|
||||
static const char *const TAG = "script";
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
void ScriptLogger::esp_log_(int level, int line, const __FlashStringHelper *format, const char *param) {
|
||||
void ScriptLogger::esp_log_(int level, int line, ProgmemStr format, const char *param) {
|
||||
esp_log_printf_(level, TAG, line, format, param);
|
||||
}
|
||||
#else
|
||||
void ScriptLogger::esp_log_(int level, int line, const char *format, const char *param) {
|
||||
esp_log_printf_(level, TAG, line, format, param);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace esphome::script
|
||||
|
||||
@@ -8,28 +8,19 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
namespace esphome::script {
|
||||
|
||||
class ScriptLogger {
|
||||
protected:
|
||||
#ifdef USE_ESP8266
|
||||
void esp_logw_(int line, const __FlashStringHelper *format, const char *param) {
|
||||
void esp_logw_(int line, ProgmemStr format, const char *param) {
|
||||
esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
|
||||
}
|
||||
void esp_logd_(int line, const __FlashStringHelper *format, const char *param) {
|
||||
void esp_logd_(int line, ProgmemStr format, const char *param) {
|
||||
esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
|
||||
}
|
||||
void esp_log_(int level, int line, const __FlashStringHelper *format, const char *param);
|
||||
#else
|
||||
void esp_logw_(int line, const char *format, const char *param) {
|
||||
esp_log_(ESPHOME_LOG_LEVEL_WARN, line, format, param);
|
||||
}
|
||||
void esp_logd_(int line, const char *format, const char *param) {
|
||||
esp_log_(ESPHOME_LOG_LEVEL_DEBUG, line, format, param);
|
||||
}
|
||||
void esp_log_(int level, int line, const char *format, const char *param);
|
||||
#endif
|
||||
void esp_log_(int level, int line, ProgmemStr format, const char *param);
|
||||
};
|
||||
|
||||
/// The abstract base class for all script types.
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/controller.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
#ifdef USE_LOGGER
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#endif
|
||||
@@ -36,12 +37,8 @@ extern const size_t ESPHOME_WEBSERVER_JS_INCLUDE_SIZE;
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
// Type for parameter names that can be stored in flash on ESP8266
|
||||
#ifdef USE_ESP8266
|
||||
using ParamNameType = const __FlashStringHelper *;
|
||||
#else
|
||||
using ParamNameType = const char *;
|
||||
#endif
|
||||
// Parameter names live in flash on ESP8266
|
||||
using ParamNameType = ProgmemStr;
|
||||
|
||||
// All platforms need to defer actions to main loop thread.
|
||||
// Multi-core platforms need this for thread safety.
|
||||
|
||||
Reference in New Issue
Block a user