-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSMclass.h
executable file
·65 lines (58 loc) · 1.58 KB
/
FSMclass.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
// FSM.h
// Usage:
// 1. Declare a Finite state machine object : (code) FSMClass FSMSample
// 2. Create function as first state: (code) void FSMSampleState1(bool task)
// 2. Create function as second state: (code) void FSMSampleState2(bool task)
// 2.1 Take a look of the function sample
// 2. Initialize FSM in setup() function : (code) FSMSample.init(FSMSampleState1);
// 3. Execute the FSM in run() function : FSMSample.run();
#ifndef _FSM_h
#define _FSM_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include "CDTimerClass.h"
typedef void(*fpState) (void);
class FSMClass
{
protected:
fpState m_fState; //Function pointer
bool m_transited; //Set to true when funtion pointer transited.
bool m_doTask;
CDTimerClass m_timer;
public:
void init(fpState S); // get initizlise function pointer.
void transit(fpState S); // transite state(change function pointer).
void run(); // run the function pointer.
bool isDue(long duration); // check timer with duration.
bool doTask();
long getTime();
};
extern FSMClass FSM;
/*
//2.1 Take a State function sample
FSMClass FSMSample;
void FSMSampleState1(bool task)
{
if (task) // this is required.
{
// Put what you want to do here.
}
//set condition to leave this state
if (FSMSample.getTime() == 1000) // 1000ms = 1second
FSMSample.transit(FSMSampleState2)
}
void FSMSampleState2(bool task)
{
if (task) // this is required.
{
// Put what you want to do here.
}
//set condition to leave this state
if (FSMSample.getTime() == 2000) // 2000ms = 2second
FSMSample.transit(FSMSampleState1)
}
*/
#endif