From 32e1e03141eda6244a5bc6421da532d25e08f25f Mon Sep 17 00:00:00 2001 From: mvturnho Date: Sun, 26 May 2019 10:50:34 +0200 Subject: [PATCH] seperated the code for different functions --- esphome/components/sx1509/sx1509.cpp | 32 +++-------- esphome/components/sx1509/sx1509.h | 54 ++++++++----------- .../components/sx1509/sx1509_float_output.cpp | 21 ++++++++ .../components/sx1509/sx1509_float_output.h | 23 ++++++++ esphome/components/sx1509/sx1509_gpio_pin.cpp | 17 ++++++ esphome/components/sx1509/sx1509_gpio_pin.h | 33 ++++++++++++ 6 files changed, 122 insertions(+), 58 deletions(-) create mode 100644 esphome/components/sx1509/sx1509_float_output.cpp create mode 100644 esphome/components/sx1509/sx1509_float_output.h create mode 100644 esphome/components/sx1509/sx1509_gpio_pin.cpp create mode 100644 esphome/components/sx1509/sx1509_gpio_pin.h diff --git a/esphome/components/sx1509/sx1509.cpp b/esphome/components/sx1509/sx1509.cpp index 8588418fce..1dc34ea8a0 100644 --- a/esphome/components/sx1509/sx1509.cpp +++ b/esphome/components/sx1509/sx1509.cpp @@ -130,30 +130,23 @@ void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) { uint16_t tempWord; 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); tempWord |= (1 << pin); 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); tempWord &= ~(1 << pin); this->write_byte_16(REG_PULL_UP_B, tempWord); - // Set direction to output (REG_DIR_B) this->read_byte_16(REG_DIR_B, &tempWord); tempWord &= ~(1 << pin); // 0=output this->write_byte_16(REG_DIR_B, tempWord); - // Enable oscillator (REG_CLOCK) this->read_byte(REG_CLOCK, &tempByte); tempByte |= (1 << 6); // Internal 2MHz oscillator part 1 (set bit 6) tempByte &= ~(1 << 5); // Internal 2MHz oscillator part 2 (clear bit 5) this->write_byte(REG_CLOCK, tempByte); - // Configure LED driver clock and mode (REG_MISC) this->read_byte(REG_MISC, &tempByte); if (log) { 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 } - // Use configClock to setup the clock divder if (_clkX == 0) // Make clckX non-zero { _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); - // Enable LED driver operation (REG_LED_DRIVER_ENABLE) this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &tempWord); tempWord |= (1 << pin); 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); tempWord &= ~(1 << pin); 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; } -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(duty_rounded); - this->parent_->set_pin_value_(this->pin_, duty); -} - -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_); } +// 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 diff --git a/esphome/components/sx1509/sx1509.h b/esphome/components/sx1509/sx1509.h index ad1365ed7a..01aa8f6055 100644 --- a/esphome/components/sx1509/sx1509.h +++ b/esphome/components/sx1509/sx1509.h @@ -1,9 +1,10 @@ #pragma once #include "esphome/components/i2c/i2c.h" -#include "esphome/components/output/float_output.h" #include "esphome/core/component.h" #include "sx1509_registers.h" +#include "sx1509_float_output.h" +#include "sx1509_gpio_pin.h" namespace esphome { namespace sx1509 { @@ -20,42 +21,31 @@ namespace sx1509 { #define HARDWARE_RESET 1 #define ANALOG_OUTPUT 0x03 // To set a pin mode for PWM output -#define BREATHE_OUTPUT 0x04 +// #define BREATHE_OUTPUT 0x04 -/// Modes for MCP23017 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 -}; +// /// 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 +// }; -class SX1509Component; +// class SX1509Component; +// class SX1509FloatOutputChannel; -class SX1509FloatOutputChannel : public output::FloatOutput { - public: - SX1509FloatOutputChannel(SX1509Component *parent, uint8_t pin) : parent_(parent), pin_(pin) {} - void setup_channel(); +// class SX1509GPIOPin : public GPIOPin { +// public: +// SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false); - protected: - void write_state(float state) override; +// void setup() override; +// void pin_mode(uint8_t mode) override; +// bool digital_read() override; +// void digital_write(bool value) override; - SX1509Component *parent_; - uint8_t pin_; -}; - -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_; -}; +// protected: +// SX1509Component *parent_; +// }; /// SX1509 float output component. class SX1509Component : public Component, public i2c::I2CDevice { diff --git a/esphome/components/sx1509/sx1509_float_output.cpp b/esphome/components/sx1509/sx1509_float_output.cpp new file mode 100644 index 0000000000..b71e20b3c6 --- /dev/null +++ b/esphome/components/sx1509/sx1509_float_output.cpp @@ -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(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 \ No newline at end of file diff --git a/esphome/components/sx1509/sx1509_float_output.h b/esphome/components/sx1509/sx1509_float_output.h new file mode 100644 index 0000000000..2df74528b9 --- /dev/null +++ b/esphome/components/sx1509/sx1509_float_output.h @@ -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 \ No newline at end of file diff --git a/esphome/components/sx1509/sx1509_gpio_pin.cpp b/esphome/components/sx1509/sx1509_gpio_pin.cpp new file mode 100644 index 0000000000..c97a110093 --- /dev/null +++ b/esphome/components/sx1509/sx1509_gpio_pin.cpp @@ -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 \ No newline at end of file diff --git a/esphome/components/sx1509/sx1509_gpio_pin.h b/esphome/components/sx1509/sx1509_gpio_pin.h new file mode 100644 index 0000000000..7bf3025917 --- /dev/null +++ b/esphome/components/sx1509/sx1509_gpio_pin.h @@ -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_; +}; + +}} \ No newline at end of file