mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Implement a slow sigma-delta modulation based output (#4132)
This commit is contained in:
		| @@ -213,6 +213,7 @@ esphome/components/sgp4x/* @SenexCrenshaw @martgras | |||||||
| esphome/components/shelly_dimmer/* @edge90 @rnauber | esphome/components/shelly_dimmer/* @edge90 @rnauber | ||||||
| esphome/components/sht4x/* @sjtrny | esphome/components/sht4x/* @sjtrny | ||||||
| esphome/components/shutdown/* @esphome/core @jsuanet | esphome/components/shutdown/* @esphome/core @jsuanet | ||||||
|  | esphome/components/sigma_delta_output/* @Cat-Ion | ||||||
| esphome/components/sim800l/* @glmnet | esphome/components/sim800l/* @glmnet | ||||||
| esphome/components/sm10bit_base/* @Cossid | esphome/components/sm10bit_base/* @Cossid | ||||||
| esphome/components/sm2135/* @BoukeHaarsma23 | esphome/components/sm2135/* @BoukeHaarsma23 | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								esphome/components/sigma_delta_output/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/sigma_delta_output/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | CODEOWNERS = ["@Cat-Ion"] | ||||||
							
								
								
									
										66
									
								
								esphome/components/sigma_delta_output/output.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								esphome/components/sigma_delta_output/output.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | from esphome import automation, pins | ||||||
|  | from esphome.components import output | ||||||
|  | import esphome.config_validation as cv | ||||||
|  | import esphome.codegen as cg | ||||||
|  | from esphome.const import ( | ||||||
|  |     CONF_ID, | ||||||
|  |     CONF_PIN, | ||||||
|  |     CONF_TURN_ON_ACTION, | ||||||
|  |     CONF_TURN_OFF_ACTION, | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | DEPENDENCIES = [] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | sigma_delta_output_ns = cg.esphome_ns.namespace("sigma_delta_output") | ||||||
|  | SigmaDeltaOutput = sigma_delta_output_ns.class_( | ||||||
|  |     "SigmaDeltaOutput", output.FloatOutput, cg.PollingComponent | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | CONF_STATE_CHANGE_ACTION = "state_change_action" | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA = cv.All( | ||||||
|  |     output.FLOAT_OUTPUT_SCHEMA.extend(cv.polling_component_schema("60s")).extend( | ||||||
|  |         { | ||||||
|  |             cv.Required(CONF_ID): cv.declare_id(SigmaDeltaOutput), | ||||||
|  |             cv.Optional(CONF_PIN): pins.gpio_output_pin_schema, | ||||||
|  |             cv.Optional(CONF_STATE_CHANGE_ACTION): automation.validate_automation( | ||||||
|  |                 single=True | ||||||
|  |             ), | ||||||
|  |             cv.Inclusive( | ||||||
|  |                 CONF_TURN_ON_ACTION, | ||||||
|  |                 "on_off", | ||||||
|  |                 f"{CONF_TURN_ON_ACTION} and {CONF_TURN_OFF_ACTION} must both be defined", | ||||||
|  |             ): automation.validate_automation(single=True), | ||||||
|  |             cv.Inclusive( | ||||||
|  |                 CONF_TURN_OFF_ACTION, | ||||||
|  |                 "on_off", | ||||||
|  |                 f"{CONF_TURN_ON_ACTION} and {CONF_TURN_OFF_ACTION} must both be defined", | ||||||
|  |             ): automation.validate_automation(single=True), | ||||||
|  |         } | ||||||
|  |     ), | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | async def to_code(config): | ||||||
|  |     var = cg.new_Pvariable(config[CONF_ID]) | ||||||
|  |     await cg.register_component(var, config) | ||||||
|  |     await output.register_output(var, config) | ||||||
|  |  | ||||||
|  |     if CONF_PIN in config: | ||||||
|  |         pin = await cg.gpio_pin_expression(config[CONF_PIN]) | ||||||
|  |         cg.add(var.set_pin(pin)) | ||||||
|  |  | ||||||
|  |     if CONF_STATE_CHANGE_ACTION in config: | ||||||
|  |         await automation.build_automation( | ||||||
|  |             var.get_state_change_trigger(), | ||||||
|  |             [(bool, "state")], | ||||||
|  |             config[CONF_STATE_CHANGE_ACTION], | ||||||
|  |         ) | ||||||
|  |     if CONF_TURN_ON_ACTION in config: | ||||||
|  |         await automation.build_automation( | ||||||
|  |             var.get_turn_on_trigger(), [], config[CONF_TURN_ON_ACTION] | ||||||
|  |         ) | ||||||
|  |         await automation.build_automation( | ||||||
|  |             var.get_turn_off_trigger(), [], config[CONF_TURN_OFF_ACTION] | ||||||
|  |         ) | ||||||
							
								
								
									
										66
									
								
								esphome/components/sigma_delta_output/sigma_delta_output.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								esphome/components/sigma_delta_output/sigma_delta_output.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | #pragma once | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/components/output/float_output.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace sigma_delta_output { | ||||||
|  | class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput { | ||||||
|  |  public: | ||||||
|  |   Trigger<> *get_turn_on_trigger() { | ||||||
|  |     if (!this->turn_on_trigger_) | ||||||
|  |       this->turn_on_trigger_ = make_unique<Trigger<>>(); | ||||||
|  |     return this->turn_on_trigger_.get(); | ||||||
|  |   } | ||||||
|  |   Trigger<> *get_turn_off_trigger() { | ||||||
|  |     if (!this->turn_off_trigger_) | ||||||
|  |       this->turn_off_trigger_ = make_unique<Trigger<>>(); | ||||||
|  |     return this->turn_off_trigger_.get(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   Trigger<bool> *get_state_change_trigger() { | ||||||
|  |     if (!this->state_change_trigger_) | ||||||
|  |       this->state_change_trigger_ = make_unique<Trigger<bool>>(); | ||||||
|  |     return this->state_change_trigger_.get(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   void set_pin(GPIOPin *pin) { this->pin_ = pin; }; | ||||||
|  |   void write_state(float state) override { this->state_ = state; } | ||||||
|  |   void update() override { | ||||||
|  |     this->accum_ += this->state_; | ||||||
|  |     const bool next_value = this->accum_ > 0; | ||||||
|  |  | ||||||
|  |     if (next_value) { | ||||||
|  |       this->accum_ -= 1.; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (next_value != this->value_) { | ||||||
|  |       this->value_ = next_value; | ||||||
|  |       if (this->pin_) { | ||||||
|  |         this->pin_->digital_write(next_value); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (this->state_change_trigger_) { | ||||||
|  |         this->state_change_trigger_->trigger(next_value); | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       if (next_value && this->turn_on_trigger_) { | ||||||
|  |         this->turn_on_trigger_->trigger(); | ||||||
|  |       } else if (!next_value && this->turn_off_trigger_) { | ||||||
|  |         this->turn_off_trigger_->trigger(); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   GPIOPin *pin_{nullptr}; | ||||||
|  |  | ||||||
|  |   std::unique_ptr<Trigger<>> turn_on_trigger_{nullptr}; | ||||||
|  |   std::unique_ptr<Trigger<>> turn_off_trigger_{nullptr}; | ||||||
|  |   std::unique_ptr<Trigger<bool>> state_change_trigger_{nullptr}; | ||||||
|  |  | ||||||
|  |   float accum_{0}; | ||||||
|  |   float state_{0.}; | ||||||
|  |   bool value_{false}; | ||||||
|  | }; | ||||||
|  | }  // namespace sigma_delta_output | ||||||
|  | }  // namespace esphome | ||||||
| @@ -1313,6 +1313,21 @@ output: | |||||||
|       return {s}; |       return {s}; | ||||||
|     outputs: |     outputs: | ||||||
|       - id: custom_binary |       - id: custom_binary | ||||||
|  |   - platform: sigma_delta_output | ||||||
|  |     id: sddac | ||||||
|  |     update_interval: 60s | ||||||
|  |     pin: D4 | ||||||
|  |     turn_on_action: | ||||||
|  |       then: | ||||||
|  |         - logger.log: "Turned on" | ||||||
|  |     turn_off_action: | ||||||
|  |       then: | ||||||
|  |         - logger.log: "Turned off" | ||||||
|  |     state_change_action: | ||||||
|  |       then: | ||||||
|  |         - logger.log: | ||||||
|  |             format: "Changed state: %d" | ||||||
|  |             args: [ 'state' ] | ||||||
|   - platform: custom |   - platform: custom | ||||||
|     type: float |     type: float | ||||||
|     lambda: |- |     lambda: |- | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user