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