Change 4 chip limit on sn74hc595 to 256 (#4108)

This commit is contained in:
Jesse Hills 2022-12-07 07:23:07 +13:00 committed by GitHub
parent 30a2fc1273
commit f34e797a0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 34 deletions

View file

@ -17,7 +17,9 @@ MULTI_CONF = True
sn74hc595_ns = cg.esphome_ns.namespace("sn74hc595") sn74hc595_ns = cg.esphome_ns.namespace("sn74hc595")
SN74HC595Component = sn74hc595_ns.class_("SN74HC595Component", cg.Component) SN74HC595Component = sn74hc595_ns.class_("SN74HC595Component", cg.Component)
SN74HC595GPIOPin = sn74hc595_ns.class_("SN74HC595GPIOPin", cg.GPIOPin) SN74HC595GPIOPin = sn74hc595_ns.class_(
"SN74HC595GPIOPin", cg.GPIOPin, cg.Parented.template(SN74HC595Component)
)
CONF_SN74HC595 = "sn74hc595" CONF_SN74HC595 = "sn74hc595"
CONF_LATCH_PIN = "latch_pin" CONF_LATCH_PIN = "latch_pin"
@ -30,7 +32,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_LATCH_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_LATCH_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_OE_PIN): pins.gpio_output_pin_schema, cv.Optional(CONF_OE_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_SR_COUNT, default=1): cv.int_range(1, 4), cv.Optional(CONF_SR_COUNT, default=1): cv.int_range(min=1, max=256),
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -60,7 +62,7 @@ SN74HC595_PIN_SCHEMA = cv.All(
{ {
cv.GenerateID(): cv.declare_id(SN74HC595GPIOPin), cv.GenerateID(): cv.declare_id(SN74HC595GPIOPin),
cv.Required(CONF_SN74HC595): cv.use_id(SN74HC595Component), cv.Required(CONF_SN74HC595): cv.use_id(SN74HC595Component),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=31), cv.Required(CONF_NUMBER): cv.int_range(min=0, max=2048, max_included=False),
cv.Optional(CONF_MODE, default={}): cv.All( cv.Optional(CONF_MODE, default={}): cv.All(
{ {
cv.Optional(CONF_OUTPUT, default=True): cv.All( cv.Optional(CONF_OUTPUT, default=True): cv.All(
@ -76,10 +78,8 @@ SN74HC595_PIN_SCHEMA = cv.All(
@pins.PIN_SCHEMA_REGISTRY.register(CONF_SN74HC595, SN74HC595_PIN_SCHEMA) @pins.PIN_SCHEMA_REGISTRY.register(CONF_SN74HC595, SN74HC595_PIN_SCHEMA)
async def sn74hc595_pin_to_code(config): async def sn74hc595_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_SN74HC595]) await cg.register_parented(var, config[CONF_SN74HC595])
cg.add(var.set_parent(parent))
num = config[CONF_NUMBER] cg.add(var.set_pin(config[CONF_NUMBER]))
cg.add(var.set_pin(num))
cg.add(var.set_inverted(config[CONF_INVERTED])) cg.add(var.set_inverted(config[CONF_INVERTED]))
return var return var

View file

@ -28,25 +28,22 @@ void SN74HC595Component::setup() {
void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); } void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); }
bool SN74HC595Component::digital_read_(uint8_t pin) { return this->output_bits_ >> pin; } void SN74HC595Component::digital_write_(uint16_t pin, bool value) {
if (pin >= this->sr_count_ * 8) {
void SN74HC595Component::digital_write_(uint8_t pin, bool value) { ESP_LOGE(TAG, "Pin %u is out of range! Maximum pin number with %u chips in series is %u", pin, this->sr_count_,
uint32_t mask = 1UL << pin; (this->sr_count_ * 8) - 1);
this->output_bits_ &= ~mask; return;
if (value) }
this->output_bits_ |= mask; this->output_bits_[pin] = value;
this->write_gpio_(); this->write_gpio_();
} }
bool SN74HC595Component::write_gpio_() { void SN74HC595Component::write_gpio_() {
for (int i = this->sr_count_ - 1; i >= 0; i--) { for (auto bit = this->output_bits_.rbegin(); bit != this->output_bits_.rend(); bit++) {
uint8_t data = (uint8_t)(this->output_bits_ >> (8 * i) & 0xff); this->data_pin_->digital_write(*bit);
for (int j = 0; j < 8; j++) {
this->data_pin_->digital_write(data & (1 << (7 - j)));
this->clock_pin_->digital_write(true); this->clock_pin_->digital_write(true);
this->clock_pin_->digital_write(false); this->clock_pin_->digital_write(false);
} }
}
// pulse latch to activate new values // pulse latch to activate new values
this->latch_pin_->digital_write(true); this->latch_pin_->digital_write(true);
@ -56,8 +53,6 @@ bool SN74HC595Component::write_gpio_() {
if (this->have_oe_pin_) { if (this->have_oe_pin_) {
this->oe_pin_->digital_write(false); this->oe_pin_->digital_write(false);
} }
return true;
} }
float SN74HC595Component::get_setup_priority() const { return setup_priority::IO; } float SN74HC595Component::get_setup_priority() const { return setup_priority::IO; }

View file

@ -2,6 +2,9 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include <vector>
namespace esphome { namespace esphome {
namespace sn74hc595 { namespace sn74hc595 {
@ -21,13 +24,15 @@ class SN74HC595Component : public Component {
oe_pin_ = pin; oe_pin_ = pin;
have_oe_pin_ = true; have_oe_pin_ = true;
} }
void set_sr_count(uint8_t count) { sr_count_ = count; } void set_sr_count(uint8_t count) {
sr_count_ = count;
this->output_bits_.resize(count * 8);
}
protected: protected:
friend class SN74HC595GPIOPin; friend class SN74HC595GPIOPin;
bool digital_read_(uint8_t pin); void digital_write_(uint16_t pin, bool value);
void digital_write_(uint8_t pin, bool value); void write_gpio_();
bool write_gpio_();
GPIOPin *data_pin_; GPIOPin *data_pin_;
GPIOPin *clock_pin_; GPIOPin *clock_pin_;
@ -35,11 +40,11 @@ class SN74HC595Component : public Component {
GPIOPin *oe_pin_; GPIOPin *oe_pin_;
uint8_t sr_count_; uint8_t sr_count_;
bool have_oe_pin_{false}; bool have_oe_pin_{false};
uint32_t output_bits_{0x00}; std::vector<bool> output_bits_;
}; };
/// Helper class to expose a SC74HC595 pin as an internal output GPIO pin. /// Helper class to expose a SC74HC595 pin as an internal output GPIO pin.
class SN74HC595GPIOPin : public GPIOPin { class SN74HC595GPIOPin : public GPIOPin, public Parented<SN74HC595Component> {
public: public:
void setup() override {} void setup() override {}
void pin_mode(gpio::Flags flags) override {} void pin_mode(gpio::Flags flags) override {}
@ -47,13 +52,11 @@ class SN74HC595GPIOPin : public GPIOPin {
void digital_write(bool value) override; void digital_write(bool value) override;
std::string dump_summary() const override; std::string dump_summary() const override;
void set_parent(SN74HC595Component *parent) { parent_ = parent; } void set_pin(uint16_t pin) { pin_ = pin; }
void set_pin(uint8_t pin) { pin_ = pin; }
void set_inverted(bool inverted) { inverted_ = inverted; } void set_inverted(bool inverted) { inverted_ = inverted; }
protected: protected:
SN74HC595Component *parent_; uint16_t pin_;
uint8_t pin_;
bool inverted_; bool inverted_;
}; };