Skip to content

Commit

Permalink
Use int for transition event instead of string
Browse files Browse the repository at this point in the history
Using a string for the event identifier is costly as it requires
a lot more space than an int.
  • Loading branch information
jonblack committed Sep 3, 2015
1 parent 0887ff7 commit d8105e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Fsm.cpp
Expand Up @@ -38,7 +38,7 @@ Fsm::~Fsm()
}


void Fsm::add_transition(State* state_from, State* state_to, String event,
void Fsm::add_transition(State* state_from, State* state_to, int event,
void (*on_transition)())
{
if (state_from == NULL || state_to == NULL)
Expand All @@ -57,7 +57,7 @@ void Fsm::add_transition(State* state_from, State* state_to, String event,
}


void Fsm::trigger(String event)
void Fsm::trigger(int event)
{
// Find the transition with the current state and given event.
Transition transition;
Expand Down
6 changes: 3 additions & 3 deletions Fsm.h
Expand Up @@ -39,16 +39,16 @@ class Fsm
Fsm(State* initial_state);
~Fsm();

void add_transition(State* state_from, State* state_to, String event,
void add_transition(State* state_from, State* state_to, int event,
void (*on_transition)());
void trigger(String event);
void trigger(int event);

private:
struct Transition
{
State* state_from;
State* state_to;
String event;
int event;
void (*on_transition)();
};

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ feature branch.
* Add library.properties
* Add keywords.txt
* Remove name attribute from state
* Use int for transition event instead of string

**1.0.0 - 24/12/2013**

Expand Down
23 changes: 15 additions & 8 deletions examples/light_switch/light_switch.ino
@@ -1,5 +1,13 @@
#include <Fsm.h>

// State machine variables
#define FLIP_LIGHT_SWITCH 1

State state_light_on(on_light_on_enter, &on_light_on_exit);
State state_light_off(on_light_off_enter, &on_light_off_exit);
Fsm fsm(&state_light_off);

// Transition callback functions
void on_light_on_enter()
{
Serial.println("Entering LIGHT_ON");
Expand Down Expand Up @@ -30,24 +38,23 @@ void on_trans_light_off_light_on()
Serial.println("Transitioning from LIGHT_OFF to LIGHT_ON");
}

State state_light_on(on_light_on_enter, &on_light_on_exit);
State state_light_off(on_light_off_enter, &on_light_off_exit);
Fsm fsm(&state_light_off);

// standard arduino functions
void setup()
{
Serial.begin(9600);

fsm.add_transition(&state_light_on, &state_light_off, "flip_switch",
fsm.add_transition(&state_light_on, &state_light_off,
FLIP_LIGHT_SWITCH,
&on_trans_light_on_light_off);
fsm.add_transition(&state_light_off, &state_light_on, "flip_switch",
fsm.add_transition(&state_light_off, &state_light_on,
FLIP_LIGHT_SWITCH,
&on_trans_light_off_light_on);
}

void loop()
{
delay(2000);
fsm.trigger("flip_switch");
fsm.trigger(FLIP_LIGHT_SWITCH);
delay(2000);
fsm.trigger("flip_switch");
fsm.trigger(FLIP_LIGHT_SWITCH);
}

0 comments on commit d8105e6

Please sign in to comment.