A Platform Agnostic Driver For The Cd74hc4067

CD74HC4067 Embedded-HAL driver crate

A Rust driver crate for the CD74HC4067 16-channel bidirectional analog multiplexer. This driver requires just a couple embedded-hal GPIO pins. It ensures that the multiplexer is disabled during the changing of the channel.

Why do this?

It is a simple driver for very simple hardware, yes. But any code saved is code saved!

Additionally, this serves as a good demonstration of testing using embedded-hal-mock and measuring test coverage using tarpaulin. And it serves as an example of type-state programming, it’s advantages, and also ergonomic shortfalls.

Line Coverage and PhantomData

After increasing the test coverage, I noticed that some lines could not be covered - for a really good reason! PhantomData markers are used in the code to separate disabled/enabled states. These markers are a compile-time-only construct. Hence, line coverage cannot ever include them. Kinda neat (IMO). See here: coverage.pdf.

Ok(Self {
    pin_0,
    pin_1,
    pin_2,
    pin_3,
    pin_enable,
    state: PhantomData::<DisabledState>, // exactly this line is not covered
})

Cumbersome usage due to Typestate Programming

Written on March 21, 2021