mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 17:05:36 +00:00
15cb2c0580
The min_power / max_power / zero_means_zero scaling support on FloatOutput costs 12 bytes per instance (max_power_, min_power_, zero_means_zero_ + alignment padding) on every PWM channel, DAC channel, LEDC output, and dimmer-chip channel — even on configs that never touch the feature. Repo-wide usage is ~17 YAML lines, mostly in test fixtures and a couple of LED-driver chip tests; the runtime set_min_power / set_max_power actions added in #8934 have no usage outside the action's own test. Add USE_OUTPUT_FLOAT_POWER_SCALING and gate the fields and scaling math in FloatOutput::set_level() behind it, mirroring the USE_POWER_SUPPLY pattern already used in BinaryOutput. Python codegen flips the define on whenever: - a min_power / max_power / zero_means_zero key is set on any output, or - a non-default zero_means_zero value is provided, or - an output.set_min_power / output.set_max_power action is registered The action class templates (SetMinPowerAction, SetMaxPowerAction) are also gated on the same define so their non-dependent member access on FloatOutput::set_min_power doesn't fail to parse when the methods aren't compiled in. zero_means_zero_ now has a default initializer (was UB before — it was always written from setup, but only because the schema default forced it). For configs without scaling: 12 B .bss saved per FloatOutput instance, plus a small flash saving from the elided multiply/subtract in set_level(). For configs with scaling: behavior is unchanged. Verified on tests/components/esp8266_pwm (no scaling): pstorage 0x28 → 0x1c per output (40 B → 28 B). Verified on tests/components/output (uses set_min_power/set_max_power actions): builds correctly with the define on.
45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
#include "float_output.h"
|
|
#include "esphome/core/helpers.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace output {
|
|
|
|
static const char *const TAG = "output.float";
|
|
|
|
#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
|
|
void FloatOutput::set_max_power(float max_power) {
|
|
this->max_power_ = clamp(max_power, this->min_power_, 1.0f); // Clamp to MIN>=MAX>=1.0
|
|
}
|
|
|
|
void FloatOutput::set_min_power(float min_power) {
|
|
this->min_power_ = clamp(min_power, 0.0f, this->max_power_); // Clamp to 0.0>=MIN>=MAX
|
|
}
|
|
#endif
|
|
|
|
void FloatOutput::set_level(float state) {
|
|
state = clamp(state, 0.0f, 1.0f);
|
|
|
|
#ifdef USE_POWER_SUPPLY
|
|
if (state > 0.0f) { // ON
|
|
this->power_.request();
|
|
} else { // OFF
|
|
this->power_.unrequest();
|
|
}
|
|
#endif
|
|
|
|
#ifdef USE_OUTPUT_FLOAT_POWER_SCALING
|
|
if (state != 0.0f || !this->zero_means_zero_) // regardless of min_power_, 0.0 means off
|
|
state = (state * (this->max_power_ - this->min_power_)) + this->min_power_;
|
|
#endif
|
|
|
|
if (this->is_inverted())
|
|
state = 1.0f - state;
|
|
this->write_state(state);
|
|
}
|
|
|
|
void FloatOutput::write_state(bool state) { this->set_level(state != this->inverted_ ? 1.0f : 0.0f); }
|
|
|
|
} // namespace output
|
|
} // namespace esphome
|