mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 08:41:59 +00:00
tweaks
This commit is contained in:
@@ -15,11 +15,12 @@ PROGMEM_STRING_TABLE(ColorModeStrings, "onoff", "brightness", "white", "color_te
|
||||
"rgbww");
|
||||
|
||||
// Get JSON string for color mode. Returns nullptr for UNKNOWN (bit 0).
|
||||
static const char *get_color_mode_json_str(ColorMode mode) {
|
||||
// Returns ProgmemStr so ArduinoJson knows to handle PROGMEM strings on ESP8266.
|
||||
static ProgmemStr get_color_mode_json_str(ColorMode mode) {
|
||||
unsigned bit = ColorModeBitPolicy::to_bit(mode);
|
||||
if (bit == 0)
|
||||
return nullptr;
|
||||
return ColorModeStrings::get(bit - 1);
|
||||
return ColorModeStrings::get_progmem_str(bit - 1);
|
||||
}
|
||||
|
||||
void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
|
||||
|
||||
@@ -300,7 +300,8 @@ void Logger::dump_config() {
|
||||
"Logger:\n"
|
||||
" Max Level: %s\n"
|
||||
" Initial Level: %s",
|
||||
LogLevelStrings::get(ESPHOME_LOG_LEVEL), LogLevelStrings::get(this->current_level_));
|
||||
LOG_STR_ARG(LogLevelStrings::get_log_str(ESPHOME_LOG_LEVEL)),
|
||||
LOG_STR_ARG(LogLevelStrings::get_log_str(this->current_level_)));
|
||||
#ifndef USE_HOST
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Log Baud Rate: %" PRIu32 "\n"
|
||||
@@ -319,7 +320,7 @@ void Logger::dump_config() {
|
||||
|
||||
#ifdef USE_LOGGER_RUNTIME_TAG_LEVELS
|
||||
for (auto &it : this->log_levels_) {
|
||||
ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.first, LogLevelStrings::get(it.second));
|
||||
ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.first, LOG_STR_ARG(LogLevelStrings::get_log_str(it.second)));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -327,7 +328,8 @@ void Logger::dump_config() {
|
||||
void Logger::set_log_level(uint8_t level) {
|
||||
if (level > ESPHOME_LOG_LEVEL) {
|
||||
level = ESPHOME_LOG_LEVEL;
|
||||
ESP_LOGW(TAG, "Cannot set log level higher than pre-compiled %s", LogLevelStrings::get(ESPHOME_LOG_LEVEL));
|
||||
ESP_LOGW(TAG, "Cannot set log level higher than pre-compiled %s",
|
||||
LOG_STR_ARG(LogLevelStrings::get_log_str(ESPHOME_LOG_LEVEL)));
|
||||
}
|
||||
this->current_level_ = level;
|
||||
#ifdef USE_LOGGER_LEVEL_LISTENERS
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#define ESPHOME_strcasecmp_P strcasecmp_P
|
||||
#define ESPHOME_strncmp_P strncmp_P
|
||||
#define ESPHOME_strncasecmp_P strncasecmp_P
|
||||
#define progmem_read_byte(addr) pgm_read_byte(addr)
|
||||
// Type for pointers to PROGMEM strings (for use with ESPHOME_F return values)
|
||||
using ProgmemStr = const __FlashStringHelper *;
|
||||
#else
|
||||
@@ -34,7 +33,6 @@ using ProgmemStr = const __FlashStringHelper *;
|
||||
#define ESPHOME_strcasecmp_P strcasecmp
|
||||
#define ESPHOME_strncmp_P strncmp
|
||||
#define ESPHOME_strncasecmp_P strncasecmp
|
||||
#define progmem_read_byte(addr) (*(addr))
|
||||
// Type for pointers to strings (no PROGMEM on non-ESP8266 platforms)
|
||||
using ProgmemStr = const char *;
|
||||
#endif
|
||||
@@ -56,7 +54,8 @@ template<size_t N> struct FixedString {
|
||||
///
|
||||
/// Example:
|
||||
/// PROGMEM_STRING_TABLE(MyStrings, "foo", "bar", "baz");
|
||||
/// const char *str = MyStrings::get(index); // 0-based index
|
||||
/// ProgmemStr str = MyStrings::get_progmem_str(index); // For ArduinoJson
|
||||
/// const LogString *log_str = MyStrings::get_log_str(index); // For ESP_LOG*
|
||||
///
|
||||
template<FixedString... Strs> struct ProgmemStringTable {
|
||||
static constexpr size_t COUNT = sizeof...(Strs);
|
||||
@@ -83,8 +82,11 @@ template<FixedString... Strs> struct ProgmemStringTable {
|
||||
}
|
||||
};
|
||||
|
||||
// Forward declaration for LogString (defined in log.h)
|
||||
struct LogString;
|
||||
|
||||
/// Instantiate a ProgmemStringTable with PROGMEM storage.
|
||||
/// Creates: Name::get(index), Name::COUNT, Name::BLOB_SIZE
|
||||
/// Creates: Name::get_progmem_str(index), Name::get_log_str(index)
|
||||
#define PROGMEM_STRING_TABLE(Name, ...) \
|
||||
struct Name { \
|
||||
using Table = ProgmemStringTable<__VA_ARGS__>; \
|
||||
@@ -92,10 +94,15 @@ template<FixedString... Strs> struct ProgmemStringTable {
|
||||
static constexpr size_t BLOB_SIZE = Table::BLOB_SIZE; \
|
||||
static constexpr auto BLOB PROGMEM = Table::make_blob(); \
|
||||
static constexpr auto OFFSETS PROGMEM = Table::make_offsets(); \
|
||||
static const char *get(uint8_t index) { \
|
||||
static ProgmemStr get_progmem_str(uint8_t index) { \
|
||||
if (index >= COUNT) \
|
||||
return nullptr; \
|
||||
return &BLOB[progmem_read_byte(&OFFSETS[index])]; \
|
||||
return reinterpret_cast<ProgmemStr>(&BLOB[progmem_read_byte(&OFFSETS[index])]); \
|
||||
} \
|
||||
static const LogString *get_log_str(uint8_t index) { \
|
||||
if (index >= COUNT) \
|
||||
return nullptr; \
|
||||
return reinterpret_cast<const LogString *>(&BLOB[progmem_read_byte(&OFFSETS[index])]); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user