1use async_std::task::block_on;
2use armature::Commutator;
3
4extern crate armature_macro;
5use armature_macro::{event, stator};
6
7#[event]
8pub enum Event {
9 TimerElapsed
10}
11
12#[stator]
13pub mod led {
14
15 use super::*;
16 use std::time::Duration;
17 use armature::utils::async_std::timer::Timer;
18
19 #[stator_struct]
20 #[derive(Clone, Default, Debug)]
21 pub struct Led {
22 #[sender]
23 pub timer: Timer<Event>,
24 pub light: bool,
25 pub counter: usize
26 }
27
28 #[stator_states]
29 impl Led {
30
31 const INIT: State<Self, Event> = Self::on;
32
33 pub fn on(&mut self, event: &Event) -> Response<Self, Event> {
34 match event {
35
36 Event::OnEntry => {
37 self.light = true;
38 self.counter += 1;
39 if self.counter == 10 {
40 let id = self.get_id().unwrap();
41 self.publish(Event::Detach(id));
42 println!("{}: done", &id);
43 }
44 self.timer.start();
45 Response::Handled
46 }
47
48 Event::TimerElapsed => {
49 Response::Transition(Self::off)
50 }
51
52 _ => Response::Handled
53
54 }
55 }
56
57 fn off(&mut self, event: &Event) -> Response<Self, Event> {
58 match event {
59
60 Event::OnEntry => {
61 self.light = false;
62 self.timer.start();
63 Response::Handled
64 }
65
66 Event::TimerElapsed => {
67 Response::Transition(Self::on)
68 }
69
70 _ => Response::Handled
71
72 }
73 }
74
75 }
76
77 #[stator_lifecycle]
78 impl Led {
79
80 fn on_attach(&mut self) {
81 self.timer.on_elapsed = |this| { this.post_to_self(Event::TimerElapsed) };
82 self.timer.duration = Duration::from_millis(100);
83 }
84
85 }
86
87}
88
89fn main () {
90 let mut commutator = Commutator::new();
91 for _ in 0..10 {
92 let led = led::Led::default();
93 commutator.attach(Box::new(led.clone()));
94 }
95 block_on(commutator.run());
96}