ESPHome  2025.2.0
sn74hc595.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/hal.h"
6 #include "esphome/core/helpers.h"
7 
8 #ifdef USE_SPI
10 #endif
11 
12 #include <vector>
13 
14 namespace esphome {
15 namespace sn74hc595 {
16 
17 class SN74HC595Component : public Component {
18  public:
19  SN74HC595Component() = default;
20 
21  void setup() override = 0;
22  float get_setup_priority() const override;
23  void dump_config() override;
24 
25  void set_latch_pin(GPIOPin *pin) { this->latch_pin_ = pin; }
26  void set_oe_pin(GPIOPin *pin) {
27  this->oe_pin_ = pin;
28  this->have_oe_pin_ = true;
29  }
30  void set_sr_count(uint8_t count) {
31  this->sr_count_ = count;
32  this->output_bytes_.resize(count);
33  }
34 
35  protected:
36  friend class SN74HC595GPIOPin;
37  void digital_write_(uint16_t pin, bool value);
38  virtual void write_gpio();
39 
40  void pre_setup_();
41  void post_setup_();
42 
45  uint8_t sr_count_;
46  bool have_oe_pin_{false};
47  std::vector<uint8_t> output_bytes_;
48 };
49 
51 class SN74HC595GPIOPin : public GPIOPin, public Parented<SN74HC595Component> {
52  public:
53  void setup() override {}
54  void pin_mode(gpio::Flags flags) override {}
55  bool digital_read() override { return false; }
56  void digital_write(bool value) override;
57  std::string dump_summary() const override;
58 
59  void set_pin(uint16_t pin) { pin_ = pin; }
60  void set_inverted(bool inverted) { inverted_ = inverted; }
61 
63  gpio::Flags get_flags() const override { return gpio::Flags::FLAG_OUTPUT; }
64 
65  protected:
66  uint16_t pin_;
67  bool inverted_;
68 };
69 
71  public:
72  void setup() override;
73  void set_data_pin(GPIOPin *pin) { data_pin_ = pin; }
74  void set_clock_pin(GPIOPin *pin) { clock_pin_ = pin; }
75 
76  protected:
77  void write_gpio() override;
78 
81 };
82 
83 #ifdef USE_SPI
85  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
86  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ> {
87  public:
88  void setup() override;
89 
90  protected:
91  void write_gpio() override;
92 };
93 
94 #endif
95 
96 } // namespace sn74hc595
97 } // namespace esphome
Helper class to expose a SC74HC595 pin as an internal output GPIO pin.
Definition: sn74hc595.h:51
void pin_mode(gpio::Flags flags) override
Definition: sn74hc595.h:54
std::vector< uint8_t > output_bytes_
Definition: sn74hc595.h:47
float get_setup_priority() const override
Definition: sn74hc595.cpp:92
gpio::Flags get_flags() const override
Always returns gpio::Flags::FLAG_OUTPUT.
Definition: sn74hc595.h:63
The SPIDevice is what components using the SPI will create.
Definition: spi.h:410
const uint32_t flags
Definition: stm32flash.h:85
void set_inverted(bool inverted)
Definition: sn74hc595.h:60
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_sr_count(uint8_t count)
Definition: sn74hc595.h:30
void digital_write_(uint16_t pin, bool value)
Definition: sn74hc595.cpp:44
Helper class to easily give an object a parent of type T.
Definition: helpers.h:538