ESPHome  2023.5.5
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 
17  // initialize output pins
18  this->clock_pin_->setup();
19  this->data_pin_->setup();
20  this->latch_pin_->setup();
21  this->clock_pin_->digital_write(false);
22  this->data_pin_->digital_write(false);
23  this->latch_pin_->digital_write(false);
24 
25  // send state to shift register
26  this->write_gpio_();
27 }
28 
29 void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); }
30 
31 void SN74HC595Component::digital_write_(uint16_t pin, bool value) {
32  if (pin >= this->sr_count_ * 8) {
33  ESP_LOGE(TAG, "Pin %u is out of range! Maximum pin number with %u chips in series is %u", pin, this->sr_count_,
34  (this->sr_count_ * 8) - 1);
35  return;
36  }
37  this->output_bits_[pin] = value;
38  this->write_gpio_();
39 }
40 
42  for (auto bit = this->output_bits_.rbegin(); bit != this->output_bits_.rend(); bit++) {
43  this->data_pin_->digital_write(*bit);
44  this->clock_pin_->digital_write(true);
45  this->clock_pin_->digital_write(false);
46  }
47 
48  // pulse latch to activate new values
49  this->latch_pin_->digital_write(true);
50  this->latch_pin_->digital_write(false);
51 
52  // enable output if configured
53  if (this->have_oe_pin_) {
54  this->oe_pin_->digital_write(false);
55  }
56 }
57 
59 
61  this->parent_->digital_write_(this->pin_, value != this->inverted_);
62 }
63 std::string SN74HC595GPIOPin::dump_summary() const {
64  char buffer[32];
65  snprintf(buffer, sizeof(buffer), "%u via SN74HC595", pin_);
66  return buffer;
67 }
68 
69 } // namespace sn74hc595
70 } // namespace esphome
virtual void digital_write(bool value)=0
float get_setup_priority() const override
Definition: sn74hc595.cpp:58
virtual void setup()=0
std::string dump_summary() const override
Definition: sn74hc595.cpp:63
void digital_write(bool value) override
Definition: sn74hc595.cpp:60
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:16
Definition: a4988.cpp:4
std::vector< bool > output_bits_
Definition: sn74hc595.h:43
void digital_write_(uint16_t pin, bool value)
Definition: sn74hc595.cpp:31