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