Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Add logic in Output for minimum power setting (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtl010957 authored and OttoWinter committed Feb 26, 2019
1 parent 3017c3c commit f3fa254
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/esphome/output/float_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ ESPHOME_NAMESPACE_BEGIN
namespace output {

void FloatOutput::set_max_power(float max_power) {
this->max_power_ = clamp(0.0f, 1.0f, max_power);
this->max_power_ = clamp(this->min_power_, 1.0f, max_power); // Clamp to MIN>=MAX>=1.0
}

float FloatOutput::get_max_power() const {
return this->max_power_;
}

void FloatOutput::set_min_power(float min_power) {
this->min_power_ = clamp(0.0f, this->max_power_, min_power); // Clamp to 0.0>=MIN>=MAX
}

float FloatOutput::get_min_power() const {
return this->min_power_;
}

void FloatOutput::set_level(float state) {
state = clamp(0.0f, 1.0f, state);

Expand All @@ -35,7 +43,7 @@ void FloatOutput::set_level(float state) {
}
}

float adjusted_value = state * this->max_power_;
float adjusted_value = (state * (this->max_power_ - this->min_power_)) + this->min_power_;
if (this->is_inverted())
adjusted_value = 1.0f - adjusted_value;
this->write_state(adjusted_value);
Expand Down
23 changes: 18 additions & 5 deletions src/esphome/output/float_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class SetLevelAction;

#define LOG_FLOAT_OUTPUT(this) \
LOG_BINARY_OUTPUT(this) \
if (this->max_power_ != 1.0f) { ESP_LOGCONFIG(TAG, " Max Power: %.1f%%", this->max_power_ * 100.0f); }
if (this->max_power_ != 1.0f) { ESP_LOGCONFIG(TAG, " Max Power: %.1f%%", this->max_power_ * 100.0f); } \
if (this->min_power_ != 0.0f) { ESP_LOGCONFIG(TAG, " Min Power: %.1f%%", this->min_power_ * 100.0f); }

/** Base class for all output components that can output a variable level, like PWM.
*
Expand All @@ -26,22 +27,30 @@ class SetLevelAction;
* makes using maths much easier and (in theory) supports all possible bit depths.
*
* If you want to create a FloatOutput yourself, you essentially just have to override write_state(float).
* That method will be called for you with inversion and max power already applied. It is
* That method will be called for you with inversion and max-min power and offset to min power already applied.
*
* This interface is compatible with BinaryOutput (and will automatically convert the binary states to floating
* point states for you). Additionally, this class provides a way for users to set a maximum power
* point states for you). Additionally, this class provides a way for users to set a minimum and/or maximum power
* output
*/
class FloatOutput : public BinaryOutput {
public:
/** Set the maximum power output of this component.
*
* All values are multiplied by this float to get the adjusted value.
* All values are multiplied by max_power - min_power and offset to min_power to get the adjusted value.
*
* @param max_power Automatically clamped from 0 to 1.
* @param max_power Automatically clamped from 0 or min_power to 1.
*/
void set_max_power(float max_power);

/** Set the minimum power output of this component.
*
* All values are multiplied by max_power - min_power and offset by min_power to get the adjusted value.
*
* @param min_power Automatically clamped from 0 to max_power or 1.
*/
void set_min_power(float min_power);

/// Set the level of this float output, this is called from the front-end.
void set_level(float state);

Expand All @@ -51,6 +60,9 @@ class FloatOutput : public BinaryOutput {
/// Get the maximum power output.
float get_max_power() const;

/// Get the minimum power output.
float get_min_power() const;

/// Implement BinarySensor's write_enabled; this should never be called.
void write_state(bool value) override;

Expand All @@ -61,6 +73,7 @@ class FloatOutput : public BinaryOutput {
virtual void write_state(float state) = 0;

float max_power_{1.0f};
float min_power_{0.0f};
};

template<typename T>
Expand Down

0 comments on commit f3fa254

Please sign in to comment.