Skip to content

Commit d8105e6

Browse files
committed
Use int for transition event instead of string
Using a string for the event identifier is costly as it requires a lot more space than an int.
1 parent 0887ff7 commit d8105e6

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Fsm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Fsm::~Fsm()
3838
}
3939

4040

41-
void Fsm::add_transition(State* state_from, State* state_to, String event,
41+
void Fsm::add_transition(State* state_from, State* state_to, int event,
4242
void (*on_transition)())
4343
{
4444
if (state_from == NULL || state_to == NULL)
@@ -57,7 +57,7 @@ void Fsm::add_transition(State* state_from, State* state_to, String event,
5757
}
5858

5959

60-
void Fsm::trigger(String event)
60+
void Fsm::trigger(int event)
6161
{
6262
// Find the transition with the current state and given event.
6363
Transition transition;

Fsm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ class Fsm
3939
Fsm(State* initial_state);
4040
~Fsm();
4141

42-
void add_transition(State* state_from, State* state_to, String event,
42+
void add_transition(State* state_from, State* state_to, int event,
4343
void (*on_transition)());
44-
void trigger(String event);
44+
void trigger(int event);
4545

4646
private:
4747
struct Transition
4848
{
4949
State* state_from;
5050
State* state_to;
51-
String event;
51+
int event;
5252
void (*on_transition)();
5353
};
5454

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ feature branch.
2020
* Add library.properties
2121
* Add keywords.txt
2222
* Remove name attribute from state
23+
* Use int for transition event instead of string
2324

2425
**1.0.0 - 24/12/2013**
2526

examples/light_switch/light_switch.ino

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#include <Fsm.h>
22

3+
// State machine variables
4+
#define FLIP_LIGHT_SWITCH 1
5+
6+
State state_light_on(on_light_on_enter, &on_light_on_exit);
7+
State state_light_off(on_light_off_enter, &on_light_off_exit);
8+
Fsm fsm(&state_light_off);
9+
10+
// Transition callback functions
311
void on_light_on_enter()
412
{
513
Serial.println("Entering LIGHT_ON");
@@ -30,24 +38,23 @@ void on_trans_light_off_light_on()
3038
Serial.println("Transitioning from LIGHT_OFF to LIGHT_ON");
3139
}
3240

33-
State state_light_on(on_light_on_enter, &on_light_on_exit);
34-
State state_light_off(on_light_off_enter, &on_light_off_exit);
35-
Fsm fsm(&state_light_off);
36-
41+
// standard arduino functions
3742
void setup()
3843
{
3944
Serial.begin(9600);
4045

41-
fsm.add_transition(&state_light_on, &state_light_off, "flip_switch",
46+
fsm.add_transition(&state_light_on, &state_light_off,
47+
FLIP_LIGHT_SWITCH,
4248
&on_trans_light_on_light_off);
43-
fsm.add_transition(&state_light_off, &state_light_on, "flip_switch",
49+
fsm.add_transition(&state_light_off, &state_light_on,
50+
FLIP_LIGHT_SWITCH,
4451
&on_trans_light_off_light_on);
4552
}
4653

4754
void loop()
4855
{
4956
delay(2000);
50-
fsm.trigger("flip_switch");
57+
fsm.trigger(FLIP_LIGHT_SWITCH);
5158
delay(2000);
52-
fsm.trigger("flip_switch");
59+
fsm.trigger(FLIP_LIGHT_SWITCH);
5360
}

0 commit comments

Comments
 (0)