ESPHome  2024.10.2
sn74hc165.cpp
Go to the documentation of this file.
1 #include "sn74hc165.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace sn74hc165 {
6 
7 static const char *const TAG = "sn74hc165";
8 
10  ESP_LOGCONFIG(TAG, "Setting up SN74HC165...");
11 
12  // initialize pins
13  this->clock_pin_->setup();
14  this->data_pin_->setup();
15  this->load_pin_->setup();
16  this->clock_pin_->digital_write(false);
17  this->load_pin_->digital_write(false);
18 
19  if (this->clock_inhibit_pin_ != nullptr) {
20  this->clock_inhibit_pin_->setup();
21  this->clock_inhibit_pin_->digital_write(true);
22  }
23 
24  // read state from shift register
25  this->read_gpio_();
26 }
27 
29 
30 void SN74HC165Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC165:"); }
31 
33  if (pin >= this->sr_count_ * 8) {
34  ESP_LOGE(TAG, "Pin %u is out of range! Maximum pin number with %u chips in series is %u", pin, this->sr_count_,
35  (this->sr_count_ * 8) - 1);
36  return false;
37  }
38  return this->input_bits_[pin];
39 }
40 
42  this->load_pin_->digital_write(false);
44  this->load_pin_->digital_write(true);
46 
47  if (this->clock_inhibit_pin_ != nullptr)
48  this->clock_inhibit_pin_->digital_write(false);
49 
50  for (uint8_t i = 0; i < this->sr_count_; i++) {
51  for (uint8_t j = 0; j < 8; j++) {
52  this->input_bits_[(i * 8) + (7 - j)] = this->data_pin_->digital_read();
53 
54  this->clock_pin_->digital_write(true);
56  this->clock_pin_->digital_write(false);
58  }
59  }
60 
61  if (this->clock_inhibit_pin_ != nullptr)
62  this->clock_inhibit_pin_->digital_write(true);
63 }
64 
66 
67 bool SN74HC165GPIOPin::digital_read() { return this->parent_->digital_read_(this->pin_) != this->inverted_; }
68 
69 std::string SN74HC165GPIOPin::dump_summary() const { return str_snprintf("%u via SN74HC165", 18, pin_); }
70 
71 } // namespace sn74hc165
72 } // namespace esphome
virtual void digital_write(bool value)=0
float get_setup_priority() const override
Definition: sn74hc165.cpp:65
std::string dump_summary() const override
Definition: sn74hc165.cpp:69
virtual void setup()=0
virtual bool digital_read()=0
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:17
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:296