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