I am writing an embedded program for an STM32 microcontroller. I would like to be able to store the GPIO pins that represent buttons in a nice, consolidated struct.
Because embedded-hal encodes the state of a "Pin" struct with generics, each GPIO has a different Rust type, however for my case (buttons) they all implement the embedded_hal::digital::v2::InputPin trait.
I could explicitly specify the type for each member, however I also have to write the pin and port for each button when doing configuration, i.e.:
let mut middle = gpioa
.pa6
.into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
This means that specifying the type in the struct would cause redundancy, and if ever I need to change a pin in hardware, I will need to change it in code in more than one place.
What is the canonical way to store these pins? Of course I cannot use trait objects as it is in an embedded context.
At the moment I have the following:
use embedded_hal::digital::v2::InputPin;
struct Buttons<M, R, L, U, D>
where
M: InputPin,
R: InputPin,
L: InputPin,
U: InputPin,
D: InputPin,
{
middle: M,
right: R,
left: L,
up: U,
down: D
}
However this seems like a potential code smell.
Also, if I want to store this button struct inside another struct, the number of generics will most likely be very big in this parent struct unless I explicitly specify (and therefore write twice) the pin type.
Is there a better way to do this which is also efficient?