Elma
An event loop manager for embedded systems
toggle-switch.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <chrono>
3 #include "elma.h"
4 
6 
7 using namespace std::chrono;
8 using namespace elma;
9 
10 namespace toggle_switch_example {
11 
12 
14  class Trigger : public Process {
15  public:
16 
18  Trigger() : Process("trigger") {}
19 
21  void init() {}
22 
24  void start() {}
25 
27  void update() {
28  std::cout << "switch at " << milli_time() << "\n";
29  emit(Event("switch"));
30  }
31 
33  void stop() {}
34 
35  };
36 
38  class Mode : public State {
39 
40  public:
42  Mode(std::string name) : State(name) {}
43 
45  void entry(const Event& e) {
46  std::cout << "entering " + name() << "\n";
47  }
48 
50  void during() {}
51 
53  void exit(const Event&) {}
54  };
55 
56 }
57 
58 int main() {
59 
60  Manager m;
62  toggle_switch_example::Mode off("off"), on("on");
63  StateMachine fsm("toggle switch");
64 
65  fsm.set_initial(off)
66  .set_propagate(false)
67  .add_transition("switch", off, on)
68  .add_transition("switch", on, off);
69 
70  m.schedule(trigger, 1_ms)
71  .schedule(fsm, 5_ms) // Doesn't matter since mode has empty update()
72  .init()
73  .run(11_ms);
74 
75 }
The Process Manager class.
Definition: manager.h:27
A generic state class with nothing more than a name. See examples/toggle_switch.cc.
Manager & schedule(Process &process, high_resolution_clock::duration period)
Definition: manager.cc:13
void entry(const Event &e)
Print something when the state is entered.
StateMachine & add_transition(std::string event_name, State &from, State &to)
void during()
Do nothing while the state is active.
void init()
Nothing to do for init.
Manager & run(high_resolution_clock::duration runtime)
Definition: manager.cc:190
StateMachine & set_initial(State &s)
Definition: state_machine.cc:7
Trigger()
Wrap the base process class.
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
void exit(const Event &)
Do nothing upon exiting the state.
States for the StateMachine class.
Definition: state.h:14
A process class that sends switch events. See examples/toggle_switch.cc.
void stop()
Nothing to do to stop.
void update()
Emit a switch event each time called.
Manager & init()
Definition: manager.cc:109
Mode(std::string name)
Wrap the base state class.
StateMachine & set_propagate(bool val)
Definition: state_machine.h:38
void start()
Nothing to do upon starting.
A finite state machine class.
Definition: state_machine.h:13
Definition: channel.cc:5
An abstract base class for processes.
Definition: process.h:24