mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	seperated the code for different functions
This commit is contained in:
		| @@ -130,30 +130,23 @@ void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) { | |||||||
|   uint16_t tempWord; |   uint16_t tempWord; | ||||||
|   uint8_t tempByte; |   uint8_t tempByte; | ||||||
|  |  | ||||||
|   // Disable input buffer |  | ||||||
|   // Writing a 1 to the pin bit will disable that pins input buffer |  | ||||||
|   this->read_byte_16(REG_INPUT_DISABLE_B, &tempWord); |   this->read_byte_16(REG_INPUT_DISABLE_B, &tempWord); | ||||||
|   tempWord |= (1 << pin); |   tempWord |= (1 << pin); | ||||||
|   this->write_byte_16(REG_INPUT_DISABLE_B, tempWord); |   this->write_byte_16(REG_INPUT_DISABLE_B, tempWord); | ||||||
|  |  | ||||||
|   // Disable pull-up |  | ||||||
|   // Writing a 0 to the pin bit will disable that pull-up resistor |  | ||||||
|   this->read_byte_16(REG_PULL_UP_B, &tempWord); |   this->read_byte_16(REG_PULL_UP_B, &tempWord); | ||||||
|   tempWord &= ~(1 << pin); |   tempWord &= ~(1 << pin); | ||||||
|   this->write_byte_16(REG_PULL_UP_B, tempWord); |   this->write_byte_16(REG_PULL_UP_B, tempWord); | ||||||
|  |  | ||||||
|   // Set direction to output (REG_DIR_B) |  | ||||||
|   this->read_byte_16(REG_DIR_B, &tempWord); |   this->read_byte_16(REG_DIR_B, &tempWord); | ||||||
|   tempWord &= ~(1 << pin);  // 0=output |   tempWord &= ~(1 << pin);  // 0=output | ||||||
|   this->write_byte_16(REG_DIR_B, tempWord); |   this->write_byte_16(REG_DIR_B, tempWord); | ||||||
|  |  | ||||||
|   // Enable oscillator (REG_CLOCK) |  | ||||||
|   this->read_byte(REG_CLOCK, &tempByte); |   this->read_byte(REG_CLOCK, &tempByte); | ||||||
|   tempByte |= (1 << 6);   // Internal 2MHz oscillator part 1 (set bit 6) |   tempByte |= (1 << 6);   // Internal 2MHz oscillator part 1 (set bit 6) | ||||||
|   tempByte &= ~(1 << 5);  // Internal 2MHz oscillator part 2 (clear bit 5) |   tempByte &= ~(1 << 5);  // Internal 2MHz oscillator part 2 (clear bit 5) | ||||||
|   this->write_byte(REG_CLOCK, tempByte); |   this->write_byte(REG_CLOCK, tempByte); | ||||||
|  |  | ||||||
|   // Configure LED driver clock and mode (REG_MISC) |  | ||||||
|   this->read_byte(REG_MISC, &tempByte); |   this->read_byte(REG_MISC, &tempByte); | ||||||
|   if (log) { |   if (log) { | ||||||
|     tempByte |= (1 << 7);  // set logarithmic mode bank B |     tempByte |= (1 << 7);  // set logarithmic mode bank B | ||||||
| @@ -163,7 +156,6 @@ void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) { | |||||||
|     tempByte &= ~(1 << 3);  // set linear mode bank A |     tempByte &= ~(1 << 3);  // set linear mode bank A | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   // Use configClock to setup the clock divder |  | ||||||
|   if (_clkX == 0)  // Make clckX non-zero |   if (_clkX == 0)  // Make clckX non-zero | ||||||
|   { |   { | ||||||
|     _clkX = 2000000.0 / (1 << (1 - 1));  // Update private clock variable |     _clkX = 2000000.0 / (1 << (1 - 1));  // Update private clock variable | ||||||
| @@ -173,12 +165,10 @@ void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) { | |||||||
|   } |   } | ||||||
|   this->write_byte(REG_MISC, tempByte); |   this->write_byte(REG_MISC, tempByte); | ||||||
|  |  | ||||||
|   // Enable LED driver operation (REG_LED_DRIVER_ENABLE) |  | ||||||
|   this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &tempWord); |   this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &tempWord); | ||||||
|   tempWord |= (1 << pin); |   tempWord |= (1 << pin); | ||||||
|   this->write_byte_16(REG_LED_DRIVER_ENABLE_B, tempWord); |   this->write_byte_16(REG_LED_DRIVER_ENABLE_B, tempWord); | ||||||
|  |  | ||||||
|   // Set REG_DATA bit low ~ LED driver started |  | ||||||
|   this->read_byte_16(REG_DATA_B, &tempWord); |   this->read_byte_16(REG_DATA_B, &tempWord); | ||||||
|   tempWord &= ~(1 << pin); |   tempWord &= ~(1 << pin); | ||||||
|   this->write_byte_16(REG_DATA_B, tempWord); |   this->write_byte_16(REG_DATA_B, tempWord); | ||||||
| @@ -290,22 +280,12 @@ uint8_t SX1509Component::calculate_slope_register(uint16_t ms, uint8_t onIntensi | |||||||
| 		return regSlope2;   | 		return regSlope2;   | ||||||
| } | } | ||||||
|  |  | ||||||
| void SX1509FloatOutputChannel::write_state(float state) { | // SX1509GPIOPin::SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted) | ||||||
|   ESP_LOGD(TAG, "write_state %f", state); | //     : GPIOPin(pin, mode, inverted), parent_(parent) {} | ||||||
|   const uint16_t max_duty = 255; | // void SX1509GPIOPin::setup() { this->pin_mode(this->mode_); } | ||||||
|   const float duty_rounded = roundf(state * max_duty); | // void SX1509GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); } | ||||||
|   auto duty = static_cast<uint16_t>(duty_rounded); | // bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } | ||||||
|   this->parent_->set_pin_value_(this->pin_, duty); | // void SX1509GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } | ||||||
| } |  | ||||||
|  |  | ||||||
| void SX1509FloatOutputChannel::setup_channel() { this->parent_->pin_mode(this->pin_, ANALOG_OUTPUT); } |  | ||||||
|  |  | ||||||
| SX1509GPIOPin::SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted) |  | ||||||
|     : GPIOPin(pin, mode, inverted), parent_(parent) {} |  | ||||||
| void SX1509GPIOPin::setup() { this->pin_mode(this->mode_); } |  | ||||||
| void SX1509GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); } |  | ||||||
| bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } |  | ||||||
| void SX1509GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } |  | ||||||
|  |  | ||||||
| }  // namespace sx1509 | }  // namespace sx1509 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -1,9 +1,10 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include "esphome/components/i2c/i2c.h" | #include "esphome/components/i2c/i2c.h" | ||||||
| #include "esphome/components/output/float_output.h" |  | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "sx1509_registers.h" | #include "sx1509_registers.h" | ||||||
|  | #include "sx1509_float_output.h" | ||||||
|  | #include "sx1509_gpio_pin.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace sx1509 { | namespace sx1509 { | ||||||
| @@ -20,42 +21,31 @@ namespace sx1509 { | |||||||
| #define HARDWARE_RESET 1 | #define HARDWARE_RESET 1 | ||||||
|  |  | ||||||
| #define ANALOG_OUTPUT 0x03  // To set a pin mode for PWM output | #define ANALOG_OUTPUT 0x03  // To set a pin mode for PWM output | ||||||
| #define BREATHE_OUTPUT 0x04 | // #define BREATHE_OUTPUT 0x04 | ||||||
|  |  | ||||||
| /// Modes for MCP23017 pins | // /// Modes for SX1509 pins | ||||||
| enum SX1509GPIOMode : uint8_t { | // enum SX1509GPIOMode : uint8_t { | ||||||
|   SX1509_INPUT = INPUT,                   // 0x00 | //   SX1509_INPUT = INPUT,                   // 0x00 | ||||||
|   SX1509_INPUT_PULLUP = INPUT_PULLUP,     // 0x02 | //   SX1509_INPUT_PULLUP = INPUT_PULLUP,     // 0x02 | ||||||
|   SX1509_OUTPUT = OUTPUT,                 // 0x01 | //   SX1509_OUTPUT = OUTPUT,                 // 0x01 | ||||||
|   SX1509_BREATHE_OUTPUT = BREATHE_OUTPUT  // 0x04 | //   SX1509_BREATHE_OUTPUT = BREATHE_OUTPUT  // 0x04 | ||||||
| }; | // }; | ||||||
|  |  | ||||||
| class SX1509Component; | // class SX1509Component; | ||||||
|  | // class SX1509FloatOutputChannel; | ||||||
|  |  | ||||||
| class SX1509FloatOutputChannel : public output::FloatOutput { | // class SX1509GPIOPin : public GPIOPin { | ||||||
|  public: | //  public: | ||||||
|   SX1509FloatOutputChannel(SX1509Component *parent, uint8_t pin) : parent_(parent), pin_(pin) {} | //   SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false); | ||||||
|   void setup_channel(); |  | ||||||
|  |  | ||||||
|  protected: | //   void setup() override; | ||||||
|   void write_state(float state) override; | //   void pin_mode(uint8_t mode) override; | ||||||
|  | //   bool digital_read() override; | ||||||
|  | //   void digital_write(bool value) override; | ||||||
|  |  | ||||||
|   SX1509Component *parent_; | //  protected: | ||||||
|   uint8_t pin_; | //   SX1509Component *parent_; | ||||||
| }; | // }; | ||||||
|  |  | ||||||
| class SX1509GPIOPin : public GPIOPin { |  | ||||||
|  public: |  | ||||||
|   SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false); |  | ||||||
|  |  | ||||||
|   void setup() override; |  | ||||||
|   void pin_mode(uint8_t mode) override; |  | ||||||
|   bool digital_read() override; |  | ||||||
|   void digital_write(bool value) override; |  | ||||||
|  |  | ||||||
|  protected: |  | ||||||
|   SX1509Component *parent_; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| /// SX1509 float output component. | /// SX1509 float output component. | ||||||
| class SX1509Component : public Component, public i2c::I2CDevice { | class SX1509Component : public Component, public i2c::I2CDevice { | ||||||
|   | |||||||
							
								
								
									
										21
									
								
								esphome/components/sx1509/sx1509_float_output.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esphome/components/sx1509/sx1509_float_output.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | #include "sx1509_float_output.h" | ||||||
|  | #include "esphome/core/helpers.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace sx1509 { | ||||||
|  |  | ||||||
|  | static const char *TAG = "sx1509"; | ||||||
|  |  | ||||||
|  | void SX1509FloatOutputChannel::write_state(float state) { | ||||||
|  |   ESP_LOGD(TAG, "write_state %f", state); | ||||||
|  |   const uint16_t max_duty = 255; | ||||||
|  |   const float duty_rounded = roundf(state * max_duty); | ||||||
|  |   auto duty = static_cast<uint16_t>(duty_rounded); | ||||||
|  |   this->parent_->set_pin_value_(this->pin_, duty); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void SX1509FloatOutputChannel::setup_channel() { this->parent_->pin_mode(this->pin_, ANALOG_OUTPUT); } | ||||||
|  |  | ||||||
|  | }  // namespace sx1509 | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										23
									
								
								esphome/components/sx1509/sx1509_float_output.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								esphome/components/sx1509/sx1509_float_output.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "sx1509.h" | ||||||
|  | #include "esphome/components/output/float_output.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace sx1509 { | ||||||
|  |  | ||||||
|  | class SX1509Component; | ||||||
|  |  | ||||||
|  | class SX1509FloatOutputChannel : public output::FloatOutput { | ||||||
|  |  public: | ||||||
|  |   SX1509FloatOutputChannel(SX1509Component *parent, uint8_t pin) : parent_(parent), pin_(pin) {} | ||||||
|  |   void setup_channel(); | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void write_state(float state) override; | ||||||
|  |  | ||||||
|  |   SX1509Component *parent_; | ||||||
|  |   uint8_t pin_; | ||||||
|  | }; | ||||||
|  | }  // namespace sx1509 | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										17
									
								
								esphome/components/sx1509/sx1509_gpio_pin.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								esphome/components/sx1509/sx1509_gpio_pin.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | #include "esphome/core/helpers.h" | ||||||
|  | #include "esphome/core/log.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace sx1509 { | ||||||
|  |  | ||||||
|  | static const char *TAG = "sx1509_gpio_pin"; | ||||||
|  |  | ||||||
|  | SX1509GPIOPin::SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted) | ||||||
|  |     : GPIOPin(pin, mode, inverted), parent_(parent) {} | ||||||
|  | void SX1509GPIOPin::setup() { this->pin_mode(this->mode_); } | ||||||
|  | void SX1509GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); } | ||||||
|  | bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } | ||||||
|  | void SX1509GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } | ||||||
|  |  | ||||||
|  | }  // namespace sx1509 | ||||||
|  | }  // namespace esphome | ||||||
							
								
								
									
										33
									
								
								esphome/components/sx1509/sx1509_gpio_pin.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								esphome/components/sx1509/sx1509_gpio_pin.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "sx1509.h" | ||||||
|  |  | ||||||
|  | #define BREATHE_OUTPUT 0x04 | ||||||
|  |  | ||||||
|  | /// Modes for SX1509 pins | ||||||
|  | enum SX1509GPIOMode : uint8_t { | ||||||
|  |   SX1509_INPUT = INPUT,                   // 0x00 | ||||||
|  |   SX1509_INPUT_PULLUP = INPUT_PULLUP,     // 0x02 | ||||||
|  |   SX1509_OUTPUT = OUTPUT,                 // 0x01 | ||||||
|  |   SX1509_BREATHE_OUTPUT = BREATHE_OUTPUT  // 0x04 | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace sx1509 { | ||||||
|  |  | ||||||
|  | class SX1509Component; | ||||||
|  |  | ||||||
|  | class SX1509GPIOPin : public GPIOPin { | ||||||
|  |  public: | ||||||
|  |   SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false); | ||||||
|  |  | ||||||
|  |   void setup() override; | ||||||
|  |   void pin_mode(uint8_t mode) override; | ||||||
|  |   bool digital_read() override; | ||||||
|  |   void digital_write(bool value) override; | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   SX1509Component *parent_; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }} | ||||||
		Reference in New Issue
	
	Block a user