Skip to main content

blinky_without_macro/
main.rs

1use std::time::Duration;
2
3use armature::stator::{Stator, StatorComponent, Response, State};
4use armature::commutator::*;
5use armature::handler::*;
6use armature::sender::*;
7use armature::utils::async_std::timer::Timer;
8use armature_macro::event;
9use async_std::task::block_on;
10
11#[event]
12enum Event {
13    TimerElapsed
14}
15
16#[derive(Clone, Debug, Default)]
17pub struct Led {
18    stator_component: StatorComponent<Self, Event>,
19    handler_component: HandlerComponent<EventSig>,
20    sender_component: SenderComponent<Event>,
21    timer: Timer<Event>,
22    light: bool,
23    counter: usize
24}
25
26impl Led {
27
28    const INIT: State<Self, Event> = Self::on;
29
30    pub fn on(&mut self, event: &Event) -> Response<Self, Event> {
31        match event {
32
33            Event::OnEntry => {
34                self.light = true;
35                self.counter += 1;
36                if self.counter == 10 {
37                    let id = self.get_id().unwrap();
38                    self.publish(Event::Detach(id));
39                    println!("{}: done", &id);
40                }
41                self.timer.start();
42                Response::Handled
43            }
44
45            Event::TimerElapsed => {
46                Response::Transition(Self::off)
47            }
48
49            _ => Response::Handled
50        }
51    }
52
53    fn off(&mut self, event: &Event) -> Response<Self, Event> {
54        match event {
55
56            Event::OnEntry => {
57                self.light = false;
58                self.timer.start();
59                Response::Handled
60            }
61
62            Event::TimerElapsed => {
63                Response::Transition(Self::on)
64            }
65
66            _ => Response::Handled
67        }
68    }
69
70}
71
72impl Led {
73
74    fn on_attach(&mut self) {
75        self.timer.duration = Duration::from_millis(100);
76        self.timer.set_sender_component(self.get_sender_component().clone());
77        self.timer.on_elapsed = |this| { this.post_to_self(Event::TimerElapsed); };
78    }
79
80    fn on_detach(&mut self) {
81        self.timer.clear_sender();
82    }
83
84}
85
86impl Stator for Led {
87
88    const INIT: armature::stator::State<Self, Event> = Self::INIT;
89
90    fn get_stator_component_mut(&mut self) -> &mut StatorComponent<Self, Event> {
91        &mut self.stator_component
92    }
93
94    fn get_stator_component(&self) -> &StatorComponent<Self, Self::Event> {
95        &self.stator_component
96    }
97
98}
99
100impl Handler for Led {
101    type Sig = EventSig;
102
103    fn get_handler_component_mut(&mut self) -> &mut HandlerComponent<Self::Sig> {
104        &mut self.handler_component
105    }
106
107    fn get_handler_component(&self) -> &HandlerComponent<Self::Sig> {
108        &self.handler_component
109    }
110
111    fn on_attach(&mut self) {
112        Led::on_attach(self);
113    }
114
115    fn on_detach(&mut self) {
116        Led::on_detach(self);
117    }
118
119    fn init(&mut self) {
120        Stator::init(self);
121    }
122
123    fn handle(&mut self, event: &Self::Event) {
124        Stator::handle(self, event);
125    }
126
127    fn get_init_subscriptions(&self) -> Vec<Self::Sig>  {
128        Vec::from([Self::Sig::TimerElapsed])
129    }
130}
131
132impl Sender for Led {
133    type Event = Event;
134
135    fn get_sender_component_mut(&mut self) -> &mut SenderComponent<Self::Event> {
136        &mut self.sender_component
137    }
138
139    fn get_sender_component(&self) -> &SenderComponent<Self::Event> {
140        &self.sender_component
141    }
142}
143
144fn main () {
145    let mut commutator = Commutator::new();
146    for _ in 0..10 {
147        let led = Led::default();
148        commutator.attach(Box::new(led.clone()));
149    }
150    block_on(commutator.run());
151}