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
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 <map>
29
30namespace Boardcore
31{
32
36enum class PinTransition : uint8_t
37{
38 FALLING_EDGE = 0,
40};
41
45struct PinData
46{
47 std::function<void(PinTransition)> callback;
48 uint32_t threshold;
49 uint32_t periodCount;
51 bool lastState;
52 uint32_t changesCount;
53 bool reverted;
54};
55
67{
68public:
69 using PinCallback = std::function<void(PinTransition)>;
70
77 explicit PinObserver(TaskScheduler& scheduler, uint32_t pollInterval = 20)
78 : scheduler{scheduler}, pollInterval{pollInterval}
79 {
80 }
81
93 bool registerPinCallback(miosix::GpioPin pin, PinCallback callback,
94 uint32_t detectionThreshold = 1,
95 bool reverted = false);
96
100 PinData getPinData(miosix::GpioPin pin);
101
105 void resetPinChangesCount(miosix::GpioPin pin);
106
107private:
114 void periodicPinValueCheck(miosix::GpioPin pin);
115
116 TaskScheduler& scheduler;
117 uint32_t pollInterval;
118
120 std::map<miosix::GpioPin, PinData, GpioPinCompare> callbacks;
121};
122
123} // namespace Boardcore
std::function< void(PinTransition)> PinCallback
Definition PinObserver.h:69
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.
Definition PinObserver.h:77
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...
This file includes all the types the logdecoder script will decode.
PinTransition
Pin transition.
Definition PinObserver.h:37
@ FALLING_EDGE
The pin goes from high to low.
@ RISING_EDGE
The pin goes from low to high.
Pin informations.
Definition PinObserver.h:46
uint64_t lastStateTimestamp
Timestamp of the last measurement.
Definition PinObserver.h:50
uint32_t changesCount
Incremental count of the pin changes.
Definition PinObserver.h:52
uint32_t threshold
Number of periods to trigger an event.
Definition PinObserver.h:48
bool lastState
The last measured pin state.
Definition PinObserver.h:51
bool reverted
Whether to revert the pin state.
Definition PinObserver.h:53
std::function< void(PinTransition)> callback
The callback function.
Definition PinObserver.h:47
uint32_t periodCount
Number of periods the value was the same.
Definition PinObserver.h:49