ESPHome 2025.6.3
Loading...
Searching...
No Matches
sn74hc595.cpp
Go to the documentation of this file.
1#include "sn74hc595.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace sn74hc595 {
6
7static const char *const TAG = "sn74hc595";
8
10 ESP_LOGCONFIG(TAG, "Running setup");
11 if (this->have_oe_pin_) { // disable output
12 this->oe_pin_->setup();
13 this->oe_pin_->digital_write(true);
14 }
15}
17 this->latch_pin_->setup();
18 this->latch_pin_->digital_write(false);
19
20 // send state to shift register
21 this->write_gpio();
22}
23
25 this->pre_setup_();
26 this->clock_pin_->setup();
27 this->data_pin_->setup();
28 this->clock_pin_->digital_write(false);
29 this->data_pin_->digital_write(false);
30 this->post_setup_();
31}
32
33#ifdef USE_SPI
35 this->pre_setup_();
36 this->spi_setup();
37 this->post_setup_();
38}
39#endif
40
41void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); }
42
43void SN74HC595Component::digital_write_(uint16_t pin, bool value) {
44 if (pin >= this->sr_count_ * 8) {
45 ESP_LOGE(TAG, "Pin %u is out of range! Maximum pin number with %u chips in series is %u", pin, this->sr_count_,
46 (this->sr_count_ * 8) - 1);
47 return;
48 }
49 if (value) {
50 this->output_bytes_[pin / 8] |= (1 << (pin % 8));
51 } else {
52 this->output_bytes_[pin / 8] &= ~(1 << (pin % 8));
53 }
54 this->write_gpio();
55}
56
58 for (auto byte = this->output_bytes_.rbegin(); byte != this->output_bytes_.rend(); byte++) {
59 for (int8_t i = 7; i >= 0; i--) {
60 bool bit = (*byte >> i) & 1;
61 this->data_pin_->digital_write(bit);
62 this->clock_pin_->digital_write(true);
63 this->clock_pin_->digital_write(false);
64 }
65 }
67}
68
69#ifdef USE_SPI
71 for (auto byte = this->output_bytes_.rbegin(); byte != this->output_bytes_.rend(); byte++) {
72 this->enable();
73 this->transfer_byte(*byte);
74 this->disable();
75 }
77}
78#endif
79
81 // pulse latch to activate new values
82 this->latch_pin_->digital_write(true);
83 this->latch_pin_->digital_write(false);
84
85 // enable output if configured
86 if (this->have_oe_pin_) {
87 this->oe_pin_->digital_write(false);
88 }
89}
90
92
94 this->parent_->digital_write_(this->pin_, value != this->inverted_);
95}
96std::string SN74HC595GPIOPin::dump_summary() const { return str_snprintf("%u via SN74HC595", 18, pin_); }
97
98} // namespace sn74hc595
99} // namespace esphome
virtual void setup()=0
virtual void digital_write(bool value)=0
void digital_write_(uint16_t pin, bool value)
Definition sn74hc595.cpp:43
float get_setup_priority() const override
Definition sn74hc595.cpp:91
std::vector< uint8_t > output_bytes_
Definition sn74hc595.h:47
std::string dump_summary() const override
Definition sn74hc595.cpp:96
void digital_write(bool value) override
Definition sn74hc595.cpp:93
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:309