48 lines
969 B
Rust
48 lines
969 B
Rust
use embedded_hal::digital::v2::OutputPin;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
pub struct Blink<T>
|
|
where
|
|
T: OutputPin,
|
|
{
|
|
state: bool,
|
|
pin: T,
|
|
stopped: bool,
|
|
}
|
|
|
|
impl<T> Blink<T>
|
|
where
|
|
T: OutputPin,
|
|
{
|
|
pub fn new(pin: T) -> Blink<T> {
|
|
return Blink {
|
|
state: false,
|
|
pin,
|
|
stopped: false,
|
|
};
|
|
}
|
|
|
|
pub fn toggle(&mut self) -> Result<(), T::Error> {
|
|
self.state = !self.state;
|
|
if self.state {
|
|
self.pin.set_high()
|
|
} else {
|
|
self.pin.set_low()
|
|
}
|
|
}
|
|
|
|
pub fn play(&mut self) where <T as embedded_hal::digital::v2::OutputPin>::Error: std::fmt::Debug {
|
|
loop {
|
|
if self.stopped {
|
|
break;
|
|
}
|
|
self.toggle().unwrap();
|
|
thread::sleep(Duration::from_millis(10));
|
|
|
|
self.toggle().unwrap();
|
|
thread::sleep(Duration::from_millis(990));
|
|
}
|
|
}
|
|
}
|