Elma
An event loop manager for embedded systems
Public Member Functions | Protected Attributes | List of all members
elma::StateMachine Class Reference

A finite state machine class. More...

#include <state_machine.h>

+ Inheritance diagram for elma::StateMachine:
+ Collaboration diagram for elma::StateMachine:

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)
 
vector< Transitiontransitions ()
 Get the list of transitions.
 
Statecurrent ()
 
void init ()
 
void start ()
 
void update ()
 
void stop ()
 
- Public Member Functions inherited from elma::Process
 Process (int n=0)
 Default constructor. Names process "no name".
 
 Process (std::string name, int n=0)
 Constructor that takes a name for the process. More...
 
string name ()
 
void set_name (std::string str)
 
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)
 
void halt ()
 
void set_manager (Manager *m_ptr)
 

Protected Attributes

vector< Transition_transitions
 
State_initial
 
State_current
 
bool _propagate
 
- Protected Attributes inherited from elma::Process
Manager_manager_ptr
 

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() {
toggle_switch_example::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

◆ add_transition()

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 12 of file state_machine.cc.

◆ current()

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

Definition at line 44 of file state_machine.h.

◆ init()

void elma::StateMachine::init ( )
virtual

If you override init() in your state machine class, you should call StateMachine::init() at the end of you init method.

Implements elma::Process.

Definition at line 19 of file state_machine.cc.

◆ set_initial()

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 7 of file state_machine.cc.

◆ set_propagate()

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.

◆ start()

void elma::StateMachine::start ( )
virtual

If you override start() in your state machine class, you should call StateMachine::start() at the end of you start method.

Implements elma::Process.

Definition at line 34 of file state_machine.cc.

◆ stop()

void elma::StateMachine::stop ( )
virtual

If you override stop() in your state machine class, you should call StateMachine::stop() at the end of you stop method.

Implements elma::Process.

Definition at line 49 of file state_machine.cc.

◆ update()

void elma::StateMachine::update ( )
virtual

If you override update() in your state machine class, you should call StateMachine::update() at the end of you update method.

Implements elma::Process.

Definition at line 42 of file state_machine.cc.


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