ESPHome  2021.11.3
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 bool SN74HC595Component::digital_read_(uint8_t pin) { return this->output_bits_ >> pin; }
32 
33 void SN74HC595Component::digital_write_(uint8_t pin, bool value) {
34  uint32_t mask = 1UL << pin;
35  this->output_bits_ &= ~mask;
36  if (value)
37  this->output_bits_ |= mask;
38  this->write_gpio_();
39 }
40 
42  for (int i = this->sr_count_ - 1; i >= 0; i--) {
43  uint8_t data = (uint8_t)(this->output_bits_ >> (8 * i) & 0xff);
44  for (int j = 0; j < 8; j++) {
45  this->data_pin_->digital_write(data & (1 << (7 - j)));
46  this->clock_pin_->digital_write(true);
47  this->clock_pin_->digital_write(false);
48  }
49  }
50 
51  // pulse latch to activate new values
52  this->latch_pin_->digital_write(true);
53  this->latch_pin_->digital_write(false);
54 
55  // enable output if configured
56  if (this->have_oe_pin_) {
57  this->oe_pin_->digital_write(false);
58  }
59 
60  return true;
61 }
62 
64 
66  this->parent_->digital_write_(this->pin_, value != this->inverted_);
67 }
68 std::string SN74HC595GPIOPin::dump_summary() const {
69  char buffer[32];
70  snprintf(buffer, sizeof(buffer), "%u via SN74HC595", pin_);
71  return buffer;
72 }
73 
74 } // namespace sn74hc595
75 } // namespace esphome
virtual void digital_write(bool value)=0
float get_setup_priority() const override
Definition: sn74hc595.cpp:63
virtual void setup()=0
void digital_write_(uint8_t pin, bool value)
Definition: sn74hc595.cpp:33
std::string dump_summary() const override
Definition: sn74hc595.cpp:68
void digital_write(bool value) override
Definition: sn74hc595.cpp:65
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:16
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4