Elma
An event loop manager for embedded systems
microwave.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <chrono>
3 #include "gtest/gtest.h"
4 #include "elma.h"
5 
7 
8 using namespace std::chrono;
9 using namespace elma;
10 
11 namespace microwave_example {
12 
14  class Power : public Process {
15 
16  public:
17 
19  Power() : Process("power") {}
20 
23  void init() {
24  watch("on", [this](Event& e) {
25  running = true;
26  });
27  watch("off", [this](Event &e) {
28  running = false;
29  });
30  watch("set power", [this](Event& e) {
31  power_level = e.value();
32  });
33  }
34 
36  void start() {
37  running = false;
38  power_level = 0;
39  }
40 
42  void update() {}
43 
45  void stop() {}
46 
48  bool running;
49 
51  double power_level;
52 
53  };
54 
56  class DoorClosedOff : public State {
57 
58  public:
60  DoorClosedOff() : State("Door closed off") {}
61 
66  void entry(const Event& e) {
67  if ( e.name() == "power level set" ) {
68  emit(Event("set power", e.value()));
69  }
70  }
71 
73  void during() {}
74 
78  void exit(const Event& e) {
79  if ( e.name() == "on button pressed" ) {
80  emit(Event("on"));
81  }
82  }
83 
84  };
85 
87  class DoorClosedOn : public State {
88  public:
90  DoorClosedOn() : State("Door closed on") {}
91 
93  void entry(const Event& e) {}
94 
96  void during() {}
97 
100  void exit(const Event& e) {
101  emit(Event("off"));
102  }
103  };
104 
106  class DoorOpen : public State {
107  public:
109  DoorOpen() : State("Door open") {}
110 
112  void entry(const Event& e) {
113  if ( e.name() == "power level set" ) {
114  emit(Event("set power", e.value()));
115  }
116  }
118  void during() {}
119 
121  void exit(const Event& e) {}
122  };
123 
124 }
125 
126 TEST(Microwave, Safety) {
127 
128  Manager m;
129 
131 
135 
136  StateMachine microwave;
137 
138  microwave
139  .set_initial(closed_off)
140  .add_transition("on button pressed", closed_off, closed_on)
141  .add_transition("door opened", closed_off, open)
142  .add_transition("power level set", closed_off, closed_off)
143 
144  .add_transition("off button pressed", closed_on, closed_off)
145  .add_transition("door opened", closed_on, open)
146 
147  .add_transition("door closed", open, closed_off)
148  .add_transition("power level set", open, open);
149 
150  m.schedule(power, 10_ms)
151  .schedule(microwave, 10_ms)
152  .init();
153 
154  m.start();
155  m.emit(Event("on button pressed"));
156  ASSERT_EQ(true, power.running);
157 
158  m.start();
159  m.emit(Event("door opened"));
160  m.emit(Event("on button pressed"));
161  ASSERT_EQ(false, power.running);
162 
163  // INSERT GRADING TESTS HERE
164 
165 }
166 
167 GTEST_API_ int main(int argc, char **argv) {
168  testing::InitGoogleTest(&argc, argv);
169  return RUN_ALL_TESTS();
170 }
void entry(const Event &e)
Nothing to do upon entry.
Definition: microwave.cc:93
The Process Manager class.
Definition: manager.h:27
void exit(const Event &e)
Nothing to do upon exiting this state.
Definition: microwave.cc:121
Manager & schedule(Process &process, high_resolution_clock::duration period)
Definition: manager.cc:13
The state in which the oven&#39;s door is open and the oven is off. See examples/microwave.cc.
Definition: microwave.cc:106
double power_level
Keep track of the power level.
Definition: microwave.cc:51
json value() const
Definition: event.h:34
StateMachine & add_transition(std::string event_name, State &from, State &to)
void start()
Set running to false and power_level to zero.
Definition: microwave.cc:36
The state in which the oven&#39;s door is closed and the oven is on. See examples/microwave.cc.
Definition: microwave.cc:87
StateMachine & set_initial(State &s)
Definition: state_machine.cc:7
The state in which the oven&#39;s door is closed and the oven is off. See examples/microwave.cc.
Definition: microwave.cc:56
bool running
Keep track of whether the power is on.
Definition: microwave.cc:48
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
void during()
Nothing to do during this state.
Definition: microwave.cc:118
States for the StateMachine class.
Definition: state.h:14
A class the models the power component of a microwave oven. See examples/microwave.cc.
Definition: microwave.cc:14
std::string name() const
Definition: event.h:41
Manager & init()
Definition: manager.cc:109
DoorOpen()
Make a new state.
Definition: microwave.cc:109
void stop()
Nothing to do to stop.
Definition: microwave.cc:45
void exit(const Event &e)
Definition: microwave.cc:100
Manager & start()
Definition: manager.cc:116
void entry(const Event &e)
Upon entry,.
Definition: microwave.cc:112
void during()
Nothing to do in during.
Definition: microwave.cc:73
void during()
Nothing to do in during.
Definition: microwave.cc:96
void exit(const Event &e)
Definition: microwave.cc:78
DoorClosedOff()
Make a new state.
Definition: microwave.cc:60
void entry(const Event &e)
Definition: microwave.cc:66
Manager & emit(const Event &event)
Definition: manager.cc:85
A finite state machine class.
Definition: state_machine.h:13
void update()
Nothing to do to update.
Definition: microwave.cc:42
Power()
Create a new power component.
Definition: microwave.cc:19
Definition: channel.cc:5
DoorClosedOn()
Make a new state.
Definition: microwave.cc:90
An abstract base class for processes.
Definition: process.h:24