virt_ic/chip/
generators.rs

1use std::time::Duration;
2
3use crate::State;
4
5use super::{Chip, ChipBuilder, ChipRunner, ChipSet, Pin, PinId, PinType};
6
7#[derive(Debug, Clone)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Generator {
10    state: State,
11    pub pin: Pin,
12}
13
14impl Generator {
15    pub const OUT: PinId = 1;
16
17    pub fn with_state(mut self, state: State) -> Self {
18        self.state = state;
19        self.pin.state = state;
20        self
21    }
22}
23
24impl ChipBuilder<Generator> for Generator {
25    fn build() -> Generator {
26        Generator {
27            state: State::High,
28            pin: Pin {
29                pin_type: PinType::Output,
30                state: State::High,
31            },
32        }
33    }
34}
35
36impl From<Generator> for ChipSet {
37    fn from(value: Generator) -> Self {
38        ChipSet::Generator(value)
39    }
40}
41
42impl Chip for Generator {
43    fn list_pins(&self) -> Vec<(super::PinId, &Pin)> {
44        vec![(Generator::OUT, &self.pin)]
45    }
46
47    fn get_pin(&self, _pin: super::PinId) -> Option<&Pin> {
48        Some(&self.pin)
49    }
50
51    fn get_pin_mut(&mut self, _pin: super::PinId) -> Option<&mut Pin> {
52        Some(&mut self.pin)
53    }
54}
55
56impl ChipRunner for Generator {
57    fn run(&mut self, _: Duration) {
58        self.pin.state = self.state
59    }
60}