My Elma Project
An event loop manager for embedded systems
 All Classes Functions
stopwatch.cc
1 #include <chrono>
2 #include <vector>
3 
4 #include "stopwatch.h"
5 
6 using namespace std::chrono;
7 using namespace elma;
8 using namespace stopwatch;
9 
10 StopWatch::StopWatch() : StateMachine("stopwatch") {
11 
12  // Define state machine initial states and transitions here
13  set_initial(off);
14  set_propagate(false);
15  add_transition("start/stop", off, on);
16  add_transition("reset", off, off);
17  add_transition("start/stop", on, off);
18  add_transition("lap", on, on);
19 
20  // Make sure we start in the right condition
21  reset();
22 }
23 
24 high_resolution_clock::duration StopWatch::value() {
25  if ( current().name() == "on" ) {
26  return high_resolution_clock::now() - _start_time + _elapsed;
27  } else {
28  return _elapsed;
29  }
30 }
31 
33  _start_time = high_resolution_clock::now();
34 }
35 
37  _elapsed = high_resolution_clock::duration::zero();
38  _laps.clear();
39 }
40 
42  _elapsed += high_resolution_clock::now() - _start_time;
43 }
44 
46  _laps.insert(_laps.begin(), value());
47 }
void stop()
Stop the stopwatch.
Definition: stopwatch.cc:41
Definition: off.h:6
void begin()
Start the stopwatch.
Definition: stopwatch.cc:32
void lap()
Add a lap time to the list of lap times.
Definition: stopwatch.cc:45
high_resolution_clock::duration value()
Get the time stored by the stopwatch.
Definition: stopwatch.cc:24
void reset()
Reset the stopwatch to zero and erase laps.
Definition: stopwatch.cc:36