ESPHome  2024.3.1
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 
62  protected:
63  uint16_t pin_;
64  bool inverted_;
65 };
66 
68  public:
69  void setup() override;
70  void set_data_pin(GPIOPin *pin) { data_pin_ = pin; }
71  void set_clock_pin(GPIOPin *pin) { clock_pin_ = pin; }
72 
73  protected:
74  void write_gpio() override;
75 
78 };
79 
80 #ifdef USE_SPI
82  public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
83  spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ> {
84  public:
85  void setup() override;
86 
87  protected:
88  void write_gpio() override;
89 };
90 
91 #endif
92 
93 } // namespace sn74hc595
94 } // 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
The SPIDevice is what components using the SPI will create.
Definition: spi.h:408
const uint32_t flags
Definition: stm32flash.h:85
void set_inverted(bool inverted)
Definition: sn74hc595.h:60
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:515