Skyward boardcore
Loading...
Searching...
No Matches
EventHandler.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2018 Skyward Experimental Rocketry
2 * Author: 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 <utils/Debug.h>
28
29#include "ActiveObject.h"
30
31namespace Boardcore
32{
33
35{
36public:
38
39 virtual ~EventHandlerBase() {};
40
41 virtual void postEvent(const Event& ev) = 0;
42};
43
45{
46public:
47 EventHandler(unsigned int stacksize = miosix::STACK_DEFAULT_FOR_PTHREAD,
48 miosix::Priority priority = miosix::MAIN_PRIORITY);
49
50 virtual ~EventHandler();
51
52 void postEvent(const Event& ev) override;
53
54 void stop() override;
55
56protected:
57 virtual void handleEvent(const Event& ev) = 0;
58
59 void run() override;
60
61 SynchronizedQueue<Event> eventList;
62};
63
64inline EventHandler::EventHandler(unsigned int stacksize,
65 miosix::Priority priority)
66 : ActiveObject(stacksize, priority)
67{
68}
69
71
72inline void EventHandler::postEvent(const Event& ev) { eventList.put(ev); }
73
74inline void EventHandler::stop()
75{
76 if (isRunning())
77 {
78 stopFlag = true;
79
80 // Put empty event in the list to wake the runner thread
82
83 thread->join();
84 }
85}
86
87inline void EventHandler::run()
88{
89 while (!shouldStop())
90 {
91 Event e = eventList.get();
92 handleEvent(e);
93 }
94}
95
96} // namespace Boardcore
std::atomic< bool > stopFlag
bool shouldStop()
Tells whether or not the ActiveObject should stop its execution.
miosix::Thread * thread
Gives access to the thread object.
virtual void postEvent(const Event &ev)=0
EventHandler(unsigned int stacksize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
void run() override
SynchronizedQueue< Event > eventList
virtual void handleEvent(const Event &ev)=0
void stop() override
Signals the runner thread to terminate and joins the thread.
void postEvent(const Event &ev) override
void put(const T &data)
Definition SyncQueue.h:50
This file includes all the types the logdecoder script will decode.
@ EV_EMPTY
Definition Event.h:36
uint8_t Event
Definition Event.h:30