48 lines
901 B
Rust
48 lines
901 B
Rust
use embedded_hal::digital::blocking::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) {
|
|
loop {
|
|
if self.stopped {
|
|
break;
|
|
}
|
|
self.toggle().unwrap();
|
|
thread::sleep(Duration::from_millis(50));
|
|
|
|
self.toggle().unwrap();
|
|
thread::sleep(Duration::from_millis(950));
|
|
}
|
|
}
|
|
}
|