Skyward boardcore
Loading...
Searching...
No Matches
EventCounter.h
Go to the documentation of this file.
1/* Copyright (c) 2019 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/EventBroker.h>
26#include <events/EventHandler.h>
27
28namespace Boardcore
29{
30
39{
40public:
46 EventCounter(EventBroker& broker) : broker(broker) {}
47
48 ~EventCounter() { broker.unsubscribe(this); }
49
55 void subscribe(uint8_t topic) { broker.subscribe(this, topic); }
56
57 // Override postEvent not to put events in a queue, just count the events it
58 // receives.
59 void postEvent(const Event& ev) override
60 {
61 Lock<FastMutex> l(mutex);
62
63 ++mapCounter[ev];
64 ++totalCount;
65
66 lastEvent = ev;
67 }
68
72 unsigned int getCount(const Event& ev)
73 {
74 Lock<FastMutex> l(mutex);
75
76 if (mapCounter.count(ev) == 1)
77 return mapCounter.at(ev);
78
79 return 0;
80 }
81
85 unsigned int getTotalCount() { return totalCount; }
86
90 uint8_t getLastEvent() { return lastEvent; }
91
92protected:
93 // Do nothing
94 void handleEvent(const Event& ev __attribute__((unused))) override {};
95
96private:
97 EventBroker& broker;
98
99 FastMutex mutex;
100 // Count how many times we have received each event
101 map<uint8_t, unsigned int> mapCounter;
102
103 // How many events we have received in total
104 unsigned int totalCount = 0;
105 uint8_t lastEvent;
106};
107
108} // namespace Boardcore
void __attribute__((naked)) CAN1_RX0_IRQHandler()
void subscribe(EventHandlerBase *subscriber, uint8_t topic)
void unsubscribe(EventHandlerBase *subscriber, uint8_t topic)
Unsubscribe an EventHandler from a specific topic This function should be used only for testing purpo...
Helper class to count how many events are sent to the topic(s) it is registered to.
unsigned int getTotalCount()
Returns how many events have been received in total.
EventCounter(EventBroker &broker)
Construct a new Event Counter object.
uint8_t getLastEvent()
Returns the signature of the last event received (ev)
void subscribe(uint8_t topic)
Subscribes to a topic in the EventBroker.
void postEvent(const Event &ev) override
unsigned int getCount(const Event &ev)
Returns the number of times a specific event has been received.
void handleEvent(const Event &ev __attribute__((unused))) override
This file includes all the types the logdecoder script will decode.
uint8_t Event
Definition Event.h:30