use crate::BuildError;
use crate::flatten::FlatPart;
use litz_core::{Drive, Level, Part, PartState, PinId, Tick};
const SEGMENT_PINS: [&str; 8] = ["a", "b", "c", "d", "e", "f", "g", "dp"];
pub struct Segments {
segments: [u16; 8],
com: u16,
levels: [Level; 8],
com_level: Level,
}
impl Segments {
pub fn build(part: &FlatPart) -> Result<Box<dyn Part>, BuildError> {
let index = |name: &str| part.pins.iter().position(|p| p == name).map(|i| i as u16);
let mut segments = [0; 8];
for (slot, name) in segments.iter_mut().zip(SEGMENT_PINS) {
*slot = index(name).ok_or_else(|| {
BuildError::Message(format!("part {:?} has no {name:?} pin", part.id))
})?;
}
let com = index("com")
.ok_or_else(|| BuildError::Message(format!("part {:?} has no \"com\" pin", part.id)))?;
Ok(Box::new(Self {
segments,
com,
levels: [Level::HighZ; 8],
com_level: Level::HighZ,
}))
}
fn mask(&self) -> u8 {
let lit = if self.com_level == Level::High {
Level::Low
} else {
Level::High
};
let mut mask = 0;
for (bit, level) in self.levels.iter().enumerate() {
if *level == lit {
mask |= 1 << bit;
}
}
mask
}
}
impl Part for Segments {
fn drive(&self, _pin: PinId) -> Drive {
Drive::HIGH_Z
}
fn set_pin(&mut self, pin: PinId, level: Level, _now: Tick) {
if let Some(i) = self.segments.iter().position(|&p| p == pin.0) {
self.levels[i] = level;
} else if pin.0 == self.com {
self.com_level = level;
}
}
fn state(&self) -> PartState {
PartState::Segments { mask: self.mask() }
}
}
#[cfg(test)]
mod tests {
use super::*;
fn digit() -> Segments {
Segments {
segments: [0, 1, 2, 3, 4, 5, 6, 7],
com: 8,
levels: [Level::HighZ; 8],
com_level: Level::HighZ,
}
}
#[test]
fn common_cathode_lights_a_high_segment() {
let mut d = digit();
d.set_pin(PinId(8), Level::Low, 0);
d.set_pin(PinId(0), Level::High, 0);
assert_eq!(d.state(), PartState::Segments { mask: 0b0000_0001 });
}
#[test]
fn common_anode_inverts_the_drive() {
let mut d = digit();
d.set_pin(PinId(8), Level::High, 0);
d.set_pin(PinId(0), Level::High, 0);
assert_eq!(d.state(), PartState::Segments { mask: 0 });
d.set_pin(PinId(0), Level::Low, 0);
assert_eq!(d.state(), PartState::Segments { mask: 0b0000_0001 });
}
#[test]
fn floating_common_counts_as_ground() {
let mut d = digit();
d.set_pin(PinId(0), Level::High, 0);
assert_eq!(d.state(), PartState::Segments { mask: 0b0000_0001 });
}
}