Skip to main content
RKTK API Docs RKTK Home Repo

rktk_drivers_common/magnetic/mux/
sn74lv4051.rs

1use embedded_hal::digital::OutputPin;
2use rktk::drivers::interface::magnetic::Multiplexer;
3
4/// SN74LV4051 8-channel analog multiplexer.
5pub struct Sn74lv4051<S0: OutputPin, S1: OutputPin, S2: OutputPin> {
6    s0: S0,
7    s1: S1,
8    s2: S2,
9}
10
11impl<S0: OutputPin, S1: OutputPin, S2: OutputPin> Sn74lv4051<S0, S1, S2> {
12    pub fn new(s0: S0, s1: S1, s2: S2) -> Self {
13        Self { s0, s1, s2 }
14    }
15}
16
17impl<
18    E: core::fmt::Debug + rktk_log::MaybeFormat,
19    S0: OutputPin<Error = E>,
20    S1: OutputPin<Error = E>,
21    S2: OutputPin<Error = E>,
22> Multiplexer for Sn74lv4051<S0, S1, S2>
23{
24    type Error = E;
25
26    async fn select(&mut self, channel: u8) -> Result<(), Self::Error> {
27        if channel & 0b001 != 0 {
28            self.s0.set_high()?;
29        } else {
30            self.s0.set_low()?;
31        }
32
33        if channel & 0b010 != 0 {
34            self.s1.set_high()?;
35        } else {
36            self.s1.set_low()?;
37        }
38
39        if channel & 0b100 != 0 {
40            self.s2.set_high()?;
41        } else {
42            self.s2.set_low()?;
43        }
44
45        Ok(())
46    }
47}