Skyward boardcore
Loading...
Searching...
No Matches
PinObserver.cpp
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#include "PinObserver.h"
24
26
27#include <functional>
28
29namespace Boardcore
30{
31
32bool PinObserver::registerPinCallback(miosix::GpioPin pin, PinCallback callback,
33 uint32_t detectionThreshold,
34 bool reverted)
35{
36 // Try to insert the callback
37 auto result = callbacks.insert({pin,
38 {callback, detectionThreshold, 0, 0,
39 pin.value() != reverted, 0, reverted}});
40
41 // Check if the insertion took place
42 if (result.second)
43 {
44 if (scheduler.addTask(
45 std::bind(&PinObserver::periodicPinValueCheck, this, pin),
46 pollInterval, TaskScheduler::Policy::RECOVER))
47 {
48 return true;
49 }
50 else
51 {
52 callbacks.erase(pin);
53 }
54 }
55
56 return false;
57}
58
59PinData PinObserver::getPinData(miosix::GpioPin pin) { return callbacks[pin]; }
60
61void PinObserver::resetPinChangesCount(miosix::GpioPin pin)
62{
63 callbacks[pin].changesCount = 0;
64}
65
66void PinObserver::periodicPinValueCheck(miosix::GpioPin pin)
67{
68 // Make sure the pin informations are still present
69 if (callbacks.find(pin) == callbacks.end())
70 return;
71
72 auto& pinData = callbacks[pin];
73
74 // Retrieve the pin information
75 uint32_t& count = pinData.periodCount;
76
77 // Read the current pin status
78 const bool newState = pin.value() != pinData.reverted;
79
80 // Are we in a transition?
81 if (pinData.lastState != newState)
82 {
83 // Register the current state change
84 count++;
85
86 // If the count reaches the threshold, then the change is confirmed and
87 // we can trigger the event
88 if (count > pinData.threshold)
89 {
90 count = 0; // Reset the counter
91 pinData.changesCount++; // Register the change
92
93 // Update the state
94 pinData.lastStateTimestamp = TimestampTimer::getTimestamp();
95 pinData.lastState = newState;
96
97 // Execute the callback
98 pinData.callback(newState ? PinTransition::RISING_EDGE
100 }
101 }
102 else
103 {
104 // If the value is still the same as before, the counter is reset to 0
105 count = 0;
106 }
107}
108
109} // namespace Boardcore
std::function< void(PinTransition)> PinCallback
Definition PinObserver.h:69
void resetPinChangesCount(miosix::GpioPin pin)
Resets the changes counter for the specified pin.
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.
@ RECOVER
Prioritize the number of executions over the period.
size_t addTask(function_t function, uint32_t periodMs, Policy policy=Policy::RECOVER, int64_t startTick=Kernel::getOldTick())
Add a millisecond-period task function to the scheduler with an auto generated ID.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
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