Skip to content

Commit b781321

Browse files
committed
State Machine implementation finished
1 parent 85b9958 commit b781321

9 files changed

+190
-35
lines changed

Event.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ void Event::Detach(EventListener* listener) {
4444

4545

4646

47-
void Event::Notify() {
47+
void Event::Notify(uint16_t type) {
4848

4949
if (!_listeners.isEmpty()) {
5050

5151
for (_listeners.first(); !_listeners.isDone(); _listeners.next()) {
5252

53-
_listeners.currentItem()->onEvent(this);
53+
_listeners.currentItem()->onEvent(this, type);
5454

5555
}
5656
}

Event.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Event;
77

88
class EventListener {
99
protected:
10-
virtual void onEvent(Event* o) = 0;
10+
virtual void onEvent(Event* o, uint16_t eventType, uint32_t param1 = 0, uint32_t param2 = 0) = 0;
1111

1212
friend class Event;
1313
};
@@ -17,13 +17,15 @@ class Event {
1717
public:
1818

1919
Event();
20-
20+
21+
virtual uint8_t type() = 0;
22+
2123
void Attach(EventListener* listener);
2224
void Detach(EventListener* listener);
2325
byte IsAttached(EventListener* listener);
2426

2527
protected:
26-
virtual void Notify();
28+
virtual void Notify(uint16_t type);
2729

2830
StackArray<EventListener*> _listeners;
2931
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include <Arduino.h>
2+
3+
#include <ctype.h>
4+
#include <ArduinoEssentials.h>
5+
#include <StateMachine.h>
6+
7+
8+
StateMachine stateMachine;
9+
10+
// MODE A
11+
12+
class ModeA : public StateMachineMode {
13+
DECLARE_STATE(ModeA, State1);
14+
DECLARE_STATE(ModeA, EndlessLoop);
15+
16+
BEGIN_STATES_REGISTRATION
17+
ADD_STATE(stateMachine, State1);
18+
ADD_STATE(stateMachine, EndlessLoop);
19+
END_STATES_REGISTRATION
20+
};
21+
22+
23+
SM_COMMAND ModeA::State1(uint16_t eventType, uint32_t param1, uint32_t param2) {
24+
Serial.println("in State1");
25+
26+
return SM_NEXT_STATE;
27+
}
28+
29+
SM_COMMAND ModeA::EndlessLoop(uint16_t eventType, uint32_t param1, uint32_t param2) {
30+
Serial.println("in EndlessLoop");
31+
32+
while(1) ;
33+
34+
return SM_DO_NOTHING;
35+
}
36+
37+
38+
ModeA modeA;
39+
40+
41+
// MODE B
42+
43+
44+
class ModeB : public StateMachineMode {
45+
DECLARE_STATE(ModeB, StateOfB1);
46+
DECLARE_STATE(ModeB, StateOfB2);
47+
48+
BEGIN_STATES_REGISTRATION
49+
ADD_STATE(stateMachine, StateOfB1);
50+
ADD_STATE(stateMachine, StateOfB2);
51+
END_STATES_REGISTRATION
52+
53+
public:
54+
void begin();
55+
void end();
56+
57+
58+
};
59+
60+
61+
SM_COMMAND ModeB::StateOfB1(uint16_t eventType, uint32_t param1, uint32_t param2) {
62+
Serial.println("in StateOfB1");
63+
64+
return SM_NEXT_STATE;
65+
}
66+
67+
SM_COMMAND ModeB::StateOfB2(uint16_t eventType, uint32_t param1, uint32_t param2) {
68+
Serial.println("in StateOfB2");
69+
70+
stateMachine.setMode(modeA);
71+
72+
return SM_DO_NOTHING;
73+
}
74+
75+
void ModeB::begin() {
76+
Serial.println("ModeB::begin");
77+
}
78+
79+
void ModeB::end() {
80+
Serial.println("ModeB::end");
81+
}
82+
83+
84+
ModeB modeB;
85+
86+
87+
88+
void setup() {
89+
90+
Serial.begin(115200);
91+
Serial.println("Initializing State Machine:");
92+
93+
modeA.init(stateMachine);
94+
modeB.init(stateMachine);
95+
96+
RUNTIME.Attach(&stateMachine);
97+
98+
stateMachine.setMode(modeB);
99+
100+
Serial.println("State Machine initialized!");
101+
}
102+
103+
104+
105+
void loop() {
106+
RUNTIME.exec();
107+
}
108+
109+
int main(void) {
110+
111+
init();
112+
setup();
113+
114+
while (true) {
115+
loop();
116+
}
117+
}

Runtime.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
#include <Arduino.h>
1212

13+
const uint8_t EVENT_TYPE_RUNTIME = 10;
1314

1415
Runtime RUNTIME;
1516

1617
void Runtime::exec() {
1718
while (1) {
18-
Notify();
19+
Notify(EVENT_TYPE_RUNTIME);
1920
}
2021
}
2122

Runtime.h

+4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
#include "Event.h"
1212

13+
const extern uint8_t EVENT_TYPE_RUNTIME;
14+
1315
extern class Runtime : public Event {
1416

1517
public:
1618

1719
void exec();
20+
21+
uint8_t type() {return EVENT_TYPE_RUNTIME;}
1822

1923
} RUNTIME;
2024

SoftTimer.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
#include <Arduino.h>
1313

14+
const uint8_t EVENT_TYPE_SOFT_TIMER = 20;
15+
16+
1417
SoftTimer TIMER;
1518

1619
void SoftTimer::init() {
@@ -25,7 +28,7 @@ void SoftTimer::Attach(TimerListener* listener, unsigned long timerPeriod) {
2528
}
2629

2730

28-
void SoftTimer::Notify() {
31+
void SoftTimer::Notify(uint16_t type) {
2932

3033
if (!_listeners.isEmpty()) {
3134
for (_listeners.first(); !_listeners.isDone(); _listeners.next()) {
@@ -38,7 +41,7 @@ void SoftTimer::Notify() {
3841

3942
if (!node->lastTimestamp || now > node->lastTimestamp + node->timerPeriod) {
4043

41-
node->onEvent(this);
44+
node->onEvent(this, type);
4245
node->lastTimestamp = now;
4346

4447
//DEBUG_PRINT(DEBUG_INFO,"Now=");
@@ -48,6 +51,6 @@ void SoftTimer::Notify() {
4851
}
4952
}
5053

51-
void SoftTimer::onEvent(Event* o) {
52-
Notify();
54+
void SoftTimer::onEvent(Event* o, uint16_t eventType, uint32_t param1, uint32_t param2) {
55+
Notify(EVENT_TYPE_SOFT_TIMER);
5356
}

SoftTimer.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ class TimerListener : public Event, public EventListener {
2121
friend class SoftTimer;
2222
};
2323

24+
const extern uint8_t EVENT_TYPE_SOFT_TIMER;
25+
2426
extern class SoftTimer : public Event, public EventListener {
2527

2628
public:
2729
void init();
2830
void Attach(TimerListener* listener, unsigned long timerPeriod);
2931

32+
uint8_t type() {return EVENT_TYPE_SOFT_TIMER;}
33+
3034

3135
protected:
32-
void onEvent(Event* o);
33-
void Notify();
36+
void onEvent(Event* o, uint16_t eventType = 0, uint32_t param1 = 0, uint32_t param2 = 0);
37+
void Notify(uint16_t type);
3438

3539
} TIMER;
3640

StateMachine.cpp

+25-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
#include "StateMachine.h"
99

10+
1011
void StateMachineMode::init(StateMachine& stateMachine) {
1112
stateMachine.addMode(*this);
1213
addStates(stateMachine);
1314
}
1415

1516

1617

17-
StateMachine::StateMachine() : _currMode(0), _totalModes(0) {
18+
StateMachine::StateMachine() : _currMode(UNDEFINED_MODESTATE), _totalModes(0) {
1819

1920
for (uint8_t i = 0; i< SM_TOTAL_MODES; i++) {
2021
_totalStates[i] = 0;
@@ -28,23 +29,27 @@ StateMachine::StateMachine() : _currMode(0), _totalModes(0) {
2829
// // TODO Auto-generated destructor stub
2930
//}
3031

31-
void StateMachine::begin() {
32-
33-
}
34-
3532

3633
uint8_t StateMachine::addMode(StateMachineMode& mode) {
3734

3835
// Store Mode object
3936
_modesMap[_totalModes] = &mode;
4037

38+
Serial.print("Mode added - ");
39+
Serial.println(_totalModes);
40+
4141
return _totalModes++;
4242
}
4343

4444
uint8_t StateMachine::addState(StateMachineMode::STATE_FUNCTION_STATIC_T state) {
45-
_statesMap[_totalModes][_totalStates[_totalModes]] = state;
45+
_statesMap[_totalModes-1][_totalStates[_totalModes-1]] = state;
46+
47+
Serial.print("State added - ");
48+
Serial.print(_totalStates[_totalModes-1]);
49+
Serial.print(" mode - ");
50+
Serial.println(_totalModes-1);
4651

47-
return _totalStates[_totalModes]++;
52+
return _totalStates[_totalModes-1]++;
4853
}
4954

5055

@@ -56,11 +61,16 @@ bool StateMachine::setMode(StateMachineMode& mode, uint8_t state) {
5661
}
5762
}
5863

64+
Serial.println("HERE");
65+
5966
return false;
6067
}
6168

6269
bool StateMachine::setMode(uint8_t mode, uint8_t state) {
6370

71+
Serial.print("Setting mode to - ");
72+
Serial.println(mode);
73+
6474
// Check valid input
6575
if (mode >= _totalModes || (state > _totalStates[mode] && state != UNDEFINED_MODESTATE)) {
6676
return false;
@@ -72,7 +82,7 @@ bool StateMachine::setMode(uint8_t mode, uint8_t state) {
7282
}
7383

7484
// If previously set - call destructor
75-
if (_modesMap[_currMode]) {
85+
if (_currMode != UNDEFINED_MODESTATE) {
7686
_modesMap[_currMode]->end();
7787
}
7888

@@ -89,8 +99,9 @@ bool StateMachine::setMode(uint8_t mode, uint8_t state) {
8999
bool StateMachine::setState(uint8_t state) {
90100

91101

102+
92103
// Valid input check
93-
if (state > _totalStates[_currMode] && state != UNDEFINED_MODESTATE) {
104+
if (_currMode == UNDEFINED_MODESTATE || (state > _totalStates[_currMode] && state != UNDEFINED_MODESTATE)) {
94105
return false;
95106
}
96107

@@ -100,7 +111,11 @@ bool StateMachine::setState(uint8_t state) {
100111
}
101112

102113

103-
void StateMachine::onEvent(uint16_t eventType, uint32_t param1, uint32_t param2) {
114+
void StateMachine::onEvent(Event* o, uint16_t eventType, uint32_t param1, uint32_t param2) {
115+
116+
if (_currMode == UNDEFINED_MODESTATE) {
117+
return;
118+
}
104119

105120
StateMachineMode::STATE_FUNCTION_STATIC_T currentHandler = _statesMap[_currMode][_currState[_currMode]];
106121

0 commit comments

Comments
 (0)