ESPHome  2024.10.2
cd74hc4067.cpp
Go to the documentation of this file.
1 #include "cd74hc4067.h"
2 #include "esphome/core/log.h"
3 #include <cinttypes>
4 
5 namespace esphome {
6 namespace cd74hc4067 {
7 
8 static const char *const TAG = "cd74hc4067";
9 
11 
13  ESP_LOGCONFIG(TAG, "Setting up CD74HC4067...");
14 
15  this->pin_s0_->setup();
16  this->pin_s1_->setup();
17  this->pin_s2_->setup();
18  this->pin_s3_->setup();
19 
20  // set other pin, so that activate_pin will really switch
21  this->active_pin_ = 1;
22  this->activate_pin(0);
23 }
24 
26  ESP_LOGCONFIG(TAG, "CD74HC4067 Multiplexer:");
27  LOG_PIN(" S0 Pin: ", this->pin_s0_);
28  LOG_PIN(" S1 Pin: ", this->pin_s1_);
29  LOG_PIN(" S2 Pin: ", this->pin_s2_);
30  LOG_PIN(" S3 Pin: ", this->pin_s3_);
31  ESP_LOGCONFIG(TAG, "switch delay: %" PRIu32, this->switch_delay_);
32 }
33 
35  if (this->active_pin_ != pin) {
36  ESP_LOGD(TAG, "switch to input %d", pin);
37 
38  static int mux_channel[16][4] = {
39  {0, 0, 0, 0}, // channel 0
40  {1, 0, 0, 0}, // channel 1
41  {0, 1, 0, 0}, // channel 2
42  {1, 1, 0, 0}, // channel 3
43  {0, 0, 1, 0}, // channel 4
44  {1, 0, 1, 0}, // channel 5
45  {0, 1, 1, 0}, // channel 6
46  {1, 1, 1, 0}, // channel 7
47  {0, 0, 0, 1}, // channel 8
48  {1, 0, 0, 1}, // channel 9
49  {0, 1, 0, 1}, // channel 10
50  {1, 1, 0, 1}, // channel 11
51  {0, 0, 1, 1}, // channel 12
52  {1, 0, 1, 1}, // channel 13
53  {0, 1, 1, 1}, // channel 14
54  {1, 1, 1, 1} // channel 15
55  };
56  this->pin_s0_->digital_write(mux_channel[pin][0]);
57  this->pin_s1_->digital_write(mux_channel[pin][1]);
58  this->pin_s2_->digital_write(mux_channel[pin][2]);
59  this->pin_s3_->digital_write(mux_channel[pin][3]);
60  // small delay is needed to let the multiplexer switch
61  delay(this->switch_delay_);
62  this->active_pin_ = pin;
63  }
64 }
65 
67 
69  float value_v = this->sample();
70  this->publish_state(value_v);
71 }
72 
73 float CD74HC4067Sensor::get_setup_priority() const { return this->parent_->get_setup_priority() - 1.0f; }
74 
76  this->parent_->activate_pin(this->pin_);
77  return this->source_->sample();
78 }
79 
81  LOG_SENSOR(TAG, "CD74HC4067 Sensor", this);
82  ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
83  LOG_UPDATE_INTERVAL(this);
84 }
85 
86 } // namespace cd74hc4067
87 } // namespace esphome
virtual void digital_write(bool value)=0
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
virtual void setup()=0
virtual float sample()=0
Get a voltage reading, in V.
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
float get_setup_priority() const override
HARDWARE_LATE setup priority.
Definition: cd74hc4067.cpp:73
void activate_pin(uint8_t pin)
setting pin active by setting the right combination of the four multiplexer input pins ...
Definition: cd74hc4067.cpp:34
voltage_sampler::VoltageSampler * source_
The sampling source to read values from.
Definition: cd74hc4067.h:60
float get_setup_priority() const override
Definition: cd74hc4067.cpp:10
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
CD74HC4067Component * parent_
Definition: cd74hc4067.h:58
void setup() override
Set up the internal sensor array.
Definition: cd74hc4067.cpp:12
CD74HC4067Sensor(CD74HC4067Component *parent)
Definition: cd74hc4067.cpp:66
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26