-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSimpleFSM.h
89 lines (68 loc) · 2.48 KB
/
SimpleFSM.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/////////////////////////////////////////////////////////////////
#pragma once
#ifndef SIMPLE_FSM_H
#define SIMPLE_FSM_H
/////////////////////////////////////////////////////////////////
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
#include <functional>
#endif
#include "Arduino.h"
#include "State.h"
#include "Transitions.h"
/////////////////////////////////////////////////////////////////
typedef void (*CallbackFunction)();
typedef bool (*GuardCondition)();
/////////////////////////////////////////////////////////////////
class SimpleFSM {
public:
SimpleFSM();
SimpleFSM(State* initial_state);
~SimpleFSM();
void add(Transition t[], int size);
void add(TimedTransition t[], int size);
void setInitialState(State* state);
void setFinishedHandler(CallbackFunction f);
void setTransitionHandler(CallbackFunction f);
bool trigger(int event_id);
void run(int interval = 1000, CallbackFunction tick_cb = NULL);
void reset();
int getTransitionCount() const;
int getTimedTransitionCount() const;
bool isFinished() const;
State* getState() const;
bool isInState(State* state) const;
State* getPreviousState() const;
unsigned long lastTransitioned() const;
String getDotDefinition();
protected:
int num_timed = 0;
int num_standard = 0;
Transition* transitions = NULL;
TimedTransition* timed = NULL;
bool is_initialized = false;
bool is_finished = false;
unsigned long last_run = 0;
unsigned long last_transition = 0;
State* inital_state = NULL;
State* current_state = NULL;
State* prev_state = NULL;
CallbackFunction on_transition_cb = NULL;
CallbackFunction finished_cb = NULL;
String dot_definition = "";
bool _isDuplicate(const TimedTransition& transition, const TimedTransition* transitionArray, int arraySize) const;
bool _isDuplicate(const Transition& transition, const Transition* transitionArray, int arraySize) const;
bool _isTimeForRun(unsigned long now, int interval);
void _handleTimedEvents(unsigned long now);
bool _initFSM();
bool _transitionTo(AbstractTransition* transition);
bool _changeToState(State* s, unsigned long now);
void _addDotTransition(Transition& t);
void _addDotTransition(TimedTransition& t);
String _dot_transition(String from, String to, String label, String param);
String _dot_inital_state();
String _dot_header();
String _dot_active_node();
};
/////////////////////////////////////////////////////////////////
#endif
/////////////////////////////////////////////////////////////////