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