use crate::DIGITS;
use embedded_hal::digital::{Error, PinState};
pub trait LED5011ASDriver {
type Error: Error;
fn set_segment(&mut self, segment: u8, state: PinState) -> Result<(), Self::Error>;
fn set_digit(&mut self, digit: u8, dp: bool) -> Result<(), Self::Error> {
assert!((0..=9).contains(&digit), "digit must be between 0 and 9");
let digit = DIGITS[digit as usize] | (dp as u8) << 7;
self.write_byte(digit)?;
Ok(())
}
fn clear(&mut self) -> Result<(), Self::Error> {
self.write_byte(0b00000000)
}
fn write_byte(&mut self, data: u8) -> Result<(), Self::Error> {
for i in 0..8 {
let bit = (data & 1 << i) >> i;
let state = match bit {
0 => PinState::Low,
1 => PinState::High,
_ => unreachable!(
"bit can only be 0 or 1 because the bit operations checks whether a bit of the data is set or not"
),
};
self.set_segment(i, state)?;
}
Ok(())
}
}