Skyward boardcore
Loading...
Searching...
No Matches
FSM.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2018 Skyward Experimental Rocketry
2 * Authors: Matteo Piazzolla, Alain Carlucci, Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <events/Event.h>
26#include <events/EventHandler.h>
28
29#include "ActiveObject.h"
30
31namespace Boardcore
32{
33
34template <class T>
35class FSM : public EventHandler
36{
37public:
38 FSM(void (T::*initialState)(const Event&),
39 unsigned int stacksize = miosix::STACK_DEFAULT_FOR_PTHREAD,
40 miosix::Priority priority = miosix::MAIN_PRIORITY);
41
42 virtual ~FSM();
43
44 void transition(void (T::*nextState)(const Event&));
45
51 bool testState(void (T::*testState)(const Event&));
52
53protected:
54 void handleEvent(const Event& e) override;
55
56private:
57 void (T::*state)(const Event&);
58 Event specialEvent;
59};
60
61template <class T>
62FSM<T>::FSM(void (T::*initialState)(const Event&), unsigned int stacksize,
63 miosix::Priority priority)
64 : EventHandler(stacksize, priority)
65{
66 state = initialState;
67 specialEvent = EV_ENTRY;
68 postEvent(specialEvent);
69}
70
71template <class T>
73
74template <class T>
75void FSM<T>::transition(void (T::*nextState)(const Event&))
76{
77 specialEvent = EV_EXIT;
78 (static_cast<T*>(this)->*state)(specialEvent);
79 state = nextState;
80 specialEvent = EV_ENTRY;
81 (static_cast<T*>(this)->*state)(specialEvent);
82}
83
84template <class T>
85bool FSM<T>::testState(void (T::*testState)(const Event&))
86{
87 return (this->state == testState);
88}
89
90template <class T>
92{
93 (static_cast<T*>(this)->*state)(e);
94}
95
96} // namespace Boardcore
void postEvent(const Event &ev) override
void transition(void(T::*nextState)(const Event &))
Definition FSM.h:75
FSM(void(T::*initialState)(const Event &), unsigned int stacksize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
Definition FSM.h:62
virtual ~FSM()
Definition FSM.h:72
bool testState(void(T::*testState)(const Event &))
Test if the FSM is in the given state.
Definition FSM.h:85
void handleEvent(const Event &e) override
Definition FSM.h:91
This file includes all the types the logdecoder script will decode.
@ EV_ENTRY
Definition Event.h:34
@ EV_EXIT
Definition Event.h:35
uint8_t Event
Definition Event.h:30