ref: move static constants to flash memory to reduce RAM usage

This commit is contained in:
GrKoR
2026-01-22 01:02:03 -08:00
parent bdb187cbfe
commit 24aa6dc8fd
2 changed files with 44 additions and 79 deletions

View File

@@ -92,57 +92,37 @@ namespace esphome
class Constants
{
public:
static const std::string AC_FIRMWARE_VERSION;
// AUX_AC_FIRMWARE_VERSION is defined by the ESPHome code generator at compile time
static constexpr const char* AC_FIRMWARE_VERSION = AUX_AC_FIRMWARE_VERSION;
static const std::string MUTE;
static const std::string TURBO;
static const std::string CLEAN;
static const std::string HEALTH;
static const std::string ANTIFUNGUS;
// custom fan modes
static constexpr const char* MUTE = "mute";
static constexpr const char* TURBO = "turbo";
// custom presets
static constexpr const char* CLEAN = "Clean";
static constexpr const char* HEALTH = "Health";
static constexpr const char* ANTIFUNGUS = "Antifungus";
/// минимальная и максимальная температура в градусах Цельсия, ограничения самого кондиционера
static const float AC_MIN_TEMPERATURE;
static const float AC_MAX_TEMPERATURE;
/// шаг изменения целевой температуры, градусы Цельсия
static const float AC_TEMPERATURE_STEP;
static constexpr const float AC_MIN_TEMPERATURE = 16.0;
static constexpr const float AC_MAX_TEMPERATURE = 32.0;
/// Target temperature step, Celsius degrees
static constexpr const float AC_TEMPERATURE_STEP = 0.5;
/// минимальное и максимальное значение мощности инвертора при установке ограничений
static const uint8_t AC_MIN_INVERTER_POWER_LIMIT;
static const uint8_t AC_MAX_INVERTER_POWER_LIMIT;
/// Minimal and maximal values of invertor power
// AUX_AC_MIN_INVERTER_POWER_LIMIT and AUX_AC_MAX_INVERTER_POWER_LIMIT are defined by the ESPHome code generator at compile time
static constexpr const uint8_t AC_MIN_INVERTER_POWER_LIMIT = AUX_AC_MIN_INVERTER_POWER_LIMIT;
static constexpr const uint8_t AC_MAX_INVERTER_POWER_LIMIT = AUX_AC_MAX_INVERTER_POWER_LIMIT;
// периодичность опроса кондиционера на предмет изменения состояния
// изменение параметров с пульта не сообщается в UART, поэтому надо запрашивать состояние, чтобы быть в курсе
// значение в миллисекундах
static const uint32_t AC_STATES_REQUEST_INTERVAL;
static constexpr const uint32_t AC_STATES_REQUEST_INTERVAL = 7000;
// границы допустимого диапазона таймаута загрузки пакета
// таймаут загрузки - через такое количиство миллисекунд конечный автомат перейдет из
// состояния ACSM_RECEIVING_PACKET в ACSM_IDLE, если пакет не будет загружен
static const uint32_t AC_PACKET_TIMEOUT_MAX;
static const uint32_t AC_PACKET_TIMEOUT_MIN;
};
// AUX_AC_FIRMWARE_VERSION will be defined by the ESPHome code generator at compile time
const std::string Constants::AC_FIRMWARE_VERSION = AUX_AC_FIRMWARE_VERSION;
// custom fan modes
const std::string Constants::MUTE = "mute";
const std::string Constants::TURBO = "turbo";
// custom presets
const std::string Constants::CLEAN = "Clean";
const std::string Constants::HEALTH = "Health";
const std::string Constants::ANTIFUNGUS = "Antifungus";
// params
const float Constants::AC_MIN_TEMPERATURE = 16.0;
const float Constants::AC_MAX_TEMPERATURE = 32.0;
const float Constants::AC_TEMPERATURE_STEP = 0.5;
// AUX_AC_MIN_INVERTER_POWER_LIMIT and AUX_AC_MAX_INVERTER_POWER_LIMIT will be defined by the ESPHome code generator at compile time
const uint8_t Constants::AC_MIN_INVERTER_POWER_LIMIT = AUX_AC_MIN_INVERTER_POWER_LIMIT;
const uint8_t Constants::AC_MAX_INVERTER_POWER_LIMIT = AUX_AC_MAX_INVERTER_POWER_LIMIT;
const uint32_t Constants::AC_STATES_REQUEST_INTERVAL = 7000;
// таймаут загрузки пакета
// По расчетам выходит:
// - получение и обработка посимвольно не должна длиться дольше 600 мсек.
// - получение и обработка пакетов целиком не должна длиться дольше 150 мсек.
@@ -152,9 +132,10 @@ namespace esphome
// команды будут теряться. От такой коллизии мы не защищены в любом случае. Но чем меньше таймаут,
// тем меньше шансов на коллизию.
// Из этих соображений выбраны границы диапазона (_MIN и _MAX значения).
// AUX_AC_PACKET_TIMEOUT_MAX and AUX_AC_PACKET_TIMEOUT_MIN will be defined by the ESPHome code generator at compile time
const uint32_t Constants::AC_PACKET_TIMEOUT_MAX = AUX_AC_PACKET_TIMEOUT_MAX;
const uint32_t Constants::AC_PACKET_TIMEOUT_MIN = AUX_AC_PACKET_TIMEOUT_MIN;
// AUX_AC_PACKET_TIMEOUT_MAX and AUX_AC_PACKET_TIMEOUT_MIN are defined by the ESPHome code generator at compile time
static constexpr const uint32_t AC_PACKET_TIMEOUT_MAX = AUX_AC_PACKET_TIMEOUT_MAX;
static constexpr const uint32_t AC_PACKET_TIMEOUT_MIN = AUX_AC_PACKET_TIMEOUT_MIN;
};
//****************************************************************************************************************************************************
//********************************************************* ОСНОВНЫЕ СТРУКТУРЫ ***********************************************************************
@@ -2638,7 +2619,7 @@ namespace esphome
switch (_current_ac_state.fanTurbo)
{
case AC_FANTURBO_ON:
this->set_custom_fan_mode_(Constants::TURBO.c_str());
this->set_custom_fan_mode_(Constants::TURBO);
break;
case AC_FANTURBO_OFF:
@@ -2656,7 +2637,7 @@ namespace esphome
switch (_current_ac_state.fanMute)
{
case AC_FANMUTE_ON:
this->set_custom_fan_mode_(Constants::MUTE.c_str());
this->set_custom_fan_mode_(Constants::MUTE);
break;
case AC_FANMUTE_OFF:
@@ -2674,7 +2655,7 @@ namespace esphome
if (_current_ac_state.health == AC_HEALTH_ON &&
_current_ac_state.power == AC_POWER_ON)
{
this->set_custom_preset_(Constants::HEALTH.c_str());
this->set_custom_preset_(Constants::HEALTH);
}
// AC_HEALTH_OFF
// только в том случае, если до этого пресет был установлен
@@ -2709,7 +2690,7 @@ namespace esphome
if (_current_ac_state.clean == AC_CLEAN_ON &&
_current_ac_state.power == AC_POWER_OFF)
{
this->set_custom_preset_(Constants::CLEAN.c_str());
this->set_custom_preset_(Constants::CLEAN);
}
// AC_CLEAN_OFF
// только в том случае, если до этого пресет был установлен
@@ -2739,7 +2720,7 @@ namespace esphome
switch (_current_ac_state.mildew)
{
case AC_MILDEW_ON:
this->set_custom_preset_(Constants::ANTIFUNGUS.c_str());
this->set_custom_preset_(Constants::ANTIFUNGUS);
break;
case AC_MILDEW_OFF:
@@ -2853,7 +2834,7 @@ namespace esphome
void dump_config()
{
ESP_LOGCONFIG(TAG, "AUX HVAC:");
ESP_LOGCONFIG(TAG, " [x] Firmware version: %s", Constants::AC_FIRMWARE_VERSION.c_str());
ESP_LOGCONFIG(TAG, " [x] Firmware version: %s", Constants::AC_FIRMWARE_VERSION);
ESP_LOGCONFIG(TAG, " [x] Period: %" PRIu32 "ms", this->get_period());
ESP_LOGCONFIG(TAG, " [x] Show action: %s", TRUEFALSE(this->get_show_action()));
ESP_LOGCONFIG(TAG, " [x] Display inverted: %s", TRUEFALSE(this->get_display_inverted()));
@@ -3108,7 +3089,7 @@ namespace esphome
hasCommand = true;
cmd.clean = AC_CLEAN_ON;
cmd.mildew = AC_MILDEW_OFF;
this->set_custom_preset_(Constants::CLEAN.c_str());
this->set_custom_preset_(Constants::CLEAN);
}
else
{
@@ -3140,7 +3121,7 @@ namespace esphome
{
cmd.fanSpeed = AC_FANSPEED_MEDIUM; // зависимость от health
}
this->set_custom_preset_(Constants::HEALTH.c_str());
this->set_custom_preset_(Constants::HEALTH);
}
else
{
@@ -3163,7 +3144,7 @@ namespace esphome
cmd.clean = AC_CLEAN_OFF; // для логики пресетов
hasCommand = true;
this->set_custom_preset_(Constants::ANTIFUNGUS.c_str());
this->set_custom_preset_(Constants::ANTIFUNGUS);
}
}

View File

@@ -128,12 +128,6 @@ AirConPowerLimitationOnAction = aux_ac_ns.class_(
)
def use_new_api():
esphome_current_version = tuple(map(int, __version__.split('.')))
esphome_bc_version = tuple(map(int, "2025.11.0".split('.')))
return esphome_current_version >= esphome_bc_version
def validate_packet_timeout(value):
minV = AC_PACKET_TIMEOUT_MIN
maxV = AC_PACKET_TIMEOUT_MAX
@@ -433,16 +427,6 @@ async def to_code(config):
cg.add(var.set_supported_swing_modes(config[CONF_SUPPORTED_SWING_MODES]))
if CONF_SUPPORTED_PRESETS in config:
cg.add(var.set_supported_presets(config[CONF_SUPPORTED_PRESETS]))
if use_new_api():
if CONF_CUSTOM_PRESETS in config:
presets = config[CONF_CUSTOM_PRESETS]
c_str_presets = [cg.RawExpression(f"aux_ac::Constants::{p}.c_str()") for p in presets]
cg.add(var.set_custom_presets(c_str_presets))
if CONF_CUSTOM_FAN_MODES in config:
fan_modes = config[CONF_CUSTOM_FAN_MODES]
c_str_fan_modes = [cg.RawExpression(f"aux_ac::Constants::{p}.c_str()") for p in fan_modes]
cg.add(var.set_custom_fan_modes(c_str_fan_modes))
else:
if CONF_CUSTOM_PRESETS in config:
cg.add(var.set_custom_presets(config[CONF_CUSTOM_PRESETS]))
if CONF_CUSTOM_FAN_MODES in config: