mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	refactor mcp4728 component (#5609)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
		| @@ -10,6 +10,7 @@ CONF_STORE_IN_EEPROM = "store_in_eeprom" | |||||||
|  |  | ||||||
| mcp4728_ns = cg.esphome_ns.namespace("mcp4728") | mcp4728_ns = cg.esphome_ns.namespace("mcp4728") | ||||||
| MCP4728Component = mcp4728_ns.class_("MCP4728Component", cg.Component, i2c.I2CDevice) | MCP4728Component = mcp4728_ns.class_("MCP4728Component", cg.Component, i2c.I2CDevice) | ||||||
|  | CONF_MCP4728_ID = "mcp4728_id" | ||||||
|  |  | ||||||
| CONFIG_SCHEMA = ( | CONFIG_SCHEMA = ( | ||||||
|     cv.Schema( |     cv.Schema( | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| #include "mcp4728_output.h" | #include "mcp4728.h" | ||||||
| 
 | 
 | ||||||
| #include "esphome/core/helpers.h" | #include "esphome/core/helpers.h" | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
| @@ -110,12 +110,5 @@ void MCP4728Component::select_gain_(MCP4728ChannelIdx channel, MCP4728Gain gain) | |||||||
|   this->update_ = true; |   this->update_ = true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void MCP4728Channel::write_state(float state) { |  | ||||||
|   const uint16_t max_duty = 4095; |  | ||||||
|   const float duty_rounded = roundf(state * max_duty); |  | ||||||
|   auto duty = static_cast<uint16_t>(duty_rounded); |  | ||||||
|   this->parent_->set_channel_value_(this->channel_, duty); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| }  // namespace mcp4728
 | }  // namespace mcp4728
 | ||||||
| }  // namespace esphome
 | }  // namespace esphome
 | ||||||
| @@ -1,7 +1,6 @@ | |||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "esphome/components/output/float_output.h" |  | ||||||
| #include "esphome/components/i2c/i2c.h" | #include "esphome/components/i2c/i2c.h" | ||||||
| 
 | 
 | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -64,28 +63,5 @@ class MCP4728Component : public Component, public i2c::I2CDevice { | |||||||
|   bool update_ = false; |   bool update_ = false; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class MCP4728Channel : public output::FloatOutput { |  | ||||||
|  public: |  | ||||||
|   MCP4728Channel(MCP4728Component *parent, MCP4728ChannelIdx channel, MCP4728Vref vref, MCP4728Gain gain, |  | ||||||
|                  MCP4728PwrDown pwrdown) |  | ||||||
|       : parent_(parent), channel_(channel), vref_(vref), gain_(gain), pwrdown_(pwrdown) { |  | ||||||
|     // update VREF
 |  | ||||||
|     parent->select_vref_(channel, vref_); |  | ||||||
|     // update PD
 |  | ||||||
|     parent->select_power_down_(channel, pwrdown_); |  | ||||||
|     // update GAIN
 |  | ||||||
|     parent->select_gain_(channel, gain_); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|  protected: |  | ||||||
|   void write_state(float state) override; |  | ||||||
| 
 |  | ||||||
|   MCP4728Component *parent_; |  | ||||||
|   MCP4728ChannelIdx channel_; |  | ||||||
|   MCP4728Vref vref_; |  | ||||||
|   MCP4728Gain gain_; |  | ||||||
|   MCP4728PwrDown pwrdown_; |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| }  // namespace mcp4728
 | }  // namespace mcp4728
 | ||||||
| }  // namespace esphome
 | }  // namespace esphome
 | ||||||
| @@ -2,12 +2,11 @@ import esphome.codegen as cg | |||||||
| import esphome.config_validation as cv | import esphome.config_validation as cv | ||||||
| from esphome.components import output | from esphome.components import output | ||||||
| from esphome.const import CONF_CHANNEL, CONF_ID, CONF_GAIN | from esphome.const import CONF_CHANNEL, CONF_ID, CONF_GAIN | ||||||
| from . import MCP4728Component, mcp4728_ns | from .. import MCP4728Component, CONF_MCP4728_ID, mcp4728_ns | ||||||
| 
 | 
 | ||||||
| DEPENDENCIES = ["mcp4728"] | DEPENDENCIES = ["mcp4728"] | ||||||
| 
 | 
 | ||||||
| MCP4728Channel = mcp4728_ns.class_("MCP4728Channel", output.FloatOutput) | MCP4728Channel = mcp4728_ns.class_("MCP4728Channel", output.FloatOutput) | ||||||
| CONF_MCP4728_ID = "mcp4728_id" |  | ||||||
| CONF_VREF = "vref" | CONF_VREF = "vref" | ||||||
| CONF_POWER_DOWN = "power_down" | CONF_POWER_DOWN = "power_down" | ||||||
| 
 | 
 | ||||||
							
								
								
									
										17
									
								
								esphome/components/mcp4728/output/mcp4728_output.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								esphome/components/mcp4728/output/mcp4728_output.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | #include "mcp4728_output.h" | ||||||
|  |  | ||||||
|  | #include "esphome/core/helpers.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace mcp4728 { | ||||||
|  |  | ||||||
|  | void MCP4728Channel::write_state(float state) { | ||||||
|  |   const uint16_t max_duty = 4095; | ||||||
|  |   const float duty_rounded = roundf(state * max_duty); | ||||||
|  |   auto duty = static_cast<uint16_t>(duty_rounded); | ||||||
|  |   this->parent_->set_channel_value_(this->channel_, duty); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace mcp4728 | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										32
									
								
								esphome/components/mcp4728/output/mcp4728_output.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								esphome/components/mcp4728/output/mcp4728_output.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "../mcp4728.h" | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/components/output/float_output.h" | ||||||
|  | #include "esphome/components/i2c/i2c.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace mcp4728 { | ||||||
|  |  | ||||||
|  | class MCP4728Channel : public output::FloatOutput { | ||||||
|  |  public: | ||||||
|  |   MCP4728Channel(MCP4728Component *parent, MCP4728ChannelIdx channel, MCP4728Vref vref, MCP4728Gain gain, | ||||||
|  |                  MCP4728PwrDown pwrdown) | ||||||
|  |       : parent_(parent), channel_(channel) { | ||||||
|  |     // update VREF | ||||||
|  |     parent->select_vref_(channel, vref); | ||||||
|  |     // update PD | ||||||
|  |     parent->select_power_down_(channel, pwrdown); | ||||||
|  |     // update GAIN | ||||||
|  |     parent->select_gain_(channel, gain); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void write_state(float state) override; | ||||||
|  |  | ||||||
|  |   MCP4728Component *parent_; | ||||||
|  |   MCP4728ChannelIdx channel_; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace mcp4728 | ||||||
|  | }  // namespace esphome | ||||||
		Reference in New Issue
	
	Block a user