Skyward boardcore
Loading...
Searching...
No Matches
PinObserver.h
Go to the documentation of this file.
1/* Copyright (c) 2018-2022 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Alberto Nidasio, Niccolò Betto
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
27
28#include <chrono>
29#include <functional>
30#include <map>
31
32namespace Boardcore
33{
34
38enum class PinTransition : uint8_t
39{
40 FALLING_EDGE = 0,
42};
43
55{
56public:
57 using Clock = std::chrono::steady_clock;
58 using TimePoint = Clock::time_point;
59
63 struct PinData
64 {
65 // Number of periods the value was the same.
66 uint32_t periodCount;
67 // Last time the pin transitioned to a different state
69 // Time when the last state change was detected
71 bool lastState;
72 uint32_t changesCount;
73
74 std::chrono::milliseconds getLastDetectionDelay() const
75 {
76 return std::chrono::duration_cast<std::chrono::milliseconds>(
78 }
79 };
80
89 std::function<void(PinTransition transition, const PinData& data)>;
90
100
107 PinObserver(TaskScheduler& scheduler, uint32_t pollInterval = 20)
108 : scheduler{scheduler}, pollInterval{pollInterval}
109 {
110 }
111
124 bool registerPinCallback(miosix::GpioPin pin, PinCallback callback,
125 uint32_t detectionThreshold = 1,
126 bool reverted = false);
127
131 PinData getPinData(miosix::GpioPin pin);
132
136 void resetPinChangesCount(miosix::GpioPin pin);
137
138private:
145 void periodicPinValueCheck(miosix::GpioPin pin);
146
147 TaskScheduler& scheduler;
148 uint32_t pollInterval;
149
153 struct PinEntry
154 {
155 PinConfig config;
156 PinData data;
157 };
158
160 std::map<miosix::GpioPin, PinEntry, GpioPinCompare> callbacks;
161};
162
163// Re-export the PinData type, was moved inside PinObserver for convenience
165
166} // namespace Boardcore
std::chrono::steady_clock Clock
Definition PinObserver.h:57
std::function< void(PinTransition transition, const PinData &data)> PinCallback
Callback function type for pin transitions.
Definition PinObserver.h:89
Clock::time_point TimePoint
Definition PinObserver.h:58
void resetPinChangesCount(miosix::GpioPin pin)
Resets the changes counter for the specified pin.
PinObserver(TaskScheduler &scheduler, uint32_t pollInterval=20)
Construct a new PinObserver object.
bool registerPinCallback(miosix::GpioPin pin, PinCallback callback, uint32_t detectionThreshold=1, bool reverted=false)
PinData getPinData(miosix::GpioPin pin)
Returns the information for the specified pin.
The Task Scheduler allow to manage simple tasks with a single thread. All the task added must not tak...
Driver for the VN100S IMU.
PinTransition
Pin transition.
Definition PinObserver.h:39
@ FALLING_EDGE
The pin goes from high to low.
@ RISING_EDGE
The pin goes from low to high.
PinObserver::PinCallback callback
The callback function.
Definition PinObserver.h:96
uint32_t threshold
Number of periods to trigger an event.
Definition PinObserver.h:97
bool reverted
Whether to revert the pin state.
Definition PinObserver.h:98
bool lastState
The last detected pin state.
Definition PinObserver.h:71
std::chrono::milliseconds getLastDetectionDelay() const
Definition PinObserver.h:74
PinObserver::TimePoint lastTransitionTs
Definition PinObserver.h:68
uint32_t changesCount
Incremental count of the pin changes.
Definition PinObserver.h:72
PinObserver::TimePoint lastStateChangeTs
Definition PinObserver.h:70