Elma
An event loop manager for embedded systems
 All Classes Files Functions Enumerations
Public Member Functions | List of all members
elma::StateMachine Class Reference

A finite state machine class. More...

#include <state_machine.h>

Inheritance diagram for elma::StateMachine:
Inheritance graph
[legend]
Collaboration diagram for elma::StateMachine:
Collaboration graph
[legend]

Public Member Functions

 StateMachine (std::string name)
 Construct a new StateMachine with the given name.
 
 StateMachine ()
 Construct an unnamed StateMachine.
 
StateMachineset_initial (State &s)
 
StateMachineadd_transition (std::string event_name, State &from, State &to)
 
StateMachineset_propagate (bool val)
 
Statecurrent ()
 
void init ()
 Do not override init() for a state machine.
 
void start ()
 Do not override init() for a state machine.
 
void update ()
 Do not override init() for a state machine.
 
void stop ()
 Do not override init() for a state machine.
 
- Public Member Functions inherited from elma::Process
 Process ()
 Default constructor. Names process "no name".
 
 Process (std::string name)
 Constructor that takes a name for the process. More...
 
string name ()
 
status_type status ()
 
high_resolution_clock::duration period ()
 
int num_updates ()
 
time_point< high_resolution_clock > start_time ()
 
high_resolution_clock::duration last_update ()
 
high_resolution_clock::duration previous_update ()
 
Channelchannel (string name)
 Access a channel with the given name. More...
 
double milli_time ()
 The time since the last update in millisconds, as a double. More...
 
double delta ()
 The most recent amount of time between updates. More...
 
void watch (string event_name, std::function< void(Event &)> handler)
 
void emit (const Event &event)
 
void http_get (std::string url, std::function< void(json &)> handler)
 

Additional Inherited Members

- Public Types inherited from elma::Process
enum  status_type { UNINITIALIZED, STOPPED, RUNNING }
 

Detailed Description

A finite state machine class.

Together with the State and Transition classes, this class is used to make a finite state machine process. For example, here is a toggle switch machine

#include <iostream>
#include <chrono>
#include "elma.h"
using namespace std::chrono;
using namespace elma;
class Trigger : public Process {
public:
Trigger() : Process("trigger") {}
void init() {}
void start() {}
void update() {
std::cout << "switch at " << milli_time() << "\n";
emit(Event("switch"));
}
void stop() {}
};
class Mode : public State {
public:
Mode(std::string name) : State(name) {}
void entry(const Event& e) {
std::cout << "entering " + name() << "\n";
}
void during() {}
void exit(const Event&) {}
};
int main() {
Trigger trigger;
Mode off("off"), on("on");
StateMachine fsm("toggle switch");
fsm.set_initial(off)
.set_propagate(false)
.add_transition("switch", off, on)
.add_transition("switch", on, off);
m.schedule(trigger, 1_ms)
.schedule(fsm, 5_ms) // Doesn't matter since mode has empty update()
.init()
.run(11_ms);
}

Definition at line 13 of file state_machine.h.

Member Function Documentation

StateMachine & elma::StateMachine::add_transition ( std::string  event_name,
State from,
State to 
)

Add a transition to the state machine

Parameters
event_nameEvents with this name will trigger the transition
fromThe state the machine must be in to take the transition
toThe state the machine will go to upon taking the transition
Returns
A reference to the state machine, for chaining

Definition at line 13 of file state_machine.cc.

State& elma::StateMachine::current ( )
inline
Returns
The current state

Definition at line 41 of file state_machine.h.

StateMachine & elma::StateMachine::set_initial ( State s)

Set the initial state of the state machine

Parameters
sAn instantiation of a class derived from State
Returns
A reference to the state machine, for chaining

Definition at line 8 of file state_machine.cc.

StateMachine& elma::StateMachine::set_propagate ( bool  val)
inline

Set whether the state machine should propagate transitions (default = false)

Parameters
valTrue or false
Returns
A reference to the state machine, for chaining

Definition at line 38 of file state_machine.h.


The documentation for this class was generated from the following files: