Skyward boardcore
Loading...
Searching...
No Matches
ButtonHandler.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: 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 "ButtonHandler.h"
24
25#include <functional>
26
27namespace Boardcore
28{
29
31 ButtonCallback callback)
32{
33 // Try to insert the callback
34 auto result = callbacks.insert({pin, {callback, false, 0}});
35
36 // Check if the insertion took place
37 if (result.second)
38 {
39 return scheduler.addTask(
40 std::bind(&ButtonHandler::periodicButtonValueCheck, this, pin),
41 SAMPLE_PERIOD, TaskScheduler::Policy::SKIP);
42 }
43
44 return false;
45}
46
48{
49 return callbacks.erase(pin) != 0;
50}
51
52bool ButtonHandler::start() { return scheduler.start(); }
53
54void ButtonHandler::stop() { scheduler.stop(); }
55
56ButtonHandler::ButtonHandler()
57{
58 // Start the scheduler immediately
59 scheduler.start();
60}
61
62void ButtonHandler::periodicButtonValueCheck(miosix::GpioPin pin)
63{
64 // Make sure the pin informations are still present
65 if (callbacks.find(pin) == callbacks.end())
66 return;
67
68 // Retrieve the pin information
69 const ButtonCallback& callback = std::get<0>(callbacks[pin]);
70 bool& wasPressed = std::get<1>(callbacks[pin]);
71 unsigned int& pressedTicks = std::get<2>(callbacks[pin]);
72
73 // Read the current button status
74 // Note: The button is assumed to be pressed if the pin value is low
75 // (pulldown)
76 const bool isNowPressed = !pin.value();
77
78 if (isNowPressed)
79 {
80 // If the pin was not pressed before it has just been pressed
81 if (!wasPressed && callback)
82 callback(ButtonEvent::PRESSED);
83
84 // Increment the tick
85 pressedTicks++;
86 }
87 // If the button was pressed before and just released
88 else if (wasPressed)
89 {
90 if (pressedTicks >= VERY_LONG_PRESS_TICKS)
92 else if (pressedTicks >= LONG_PRESS_TICKS)
94 else
96
97 // Reset the ticks
98 pressedTicks = 0;
99 }
100
101 // Save the current button status
102 wasPressed = isNowPressed;
103}
104
105} // namespace Boardcore
void stop()
Stops the ButtonHandler's task scheduler.
bool start()
Starts the ButtonHandler's task scheduler.
bool registerButtonCallback(miosix::GpioPin pin, ButtonCallback callback)
Registers a callback on the specified pin.
bool unregisterButtonCallback(miosix::GpioPin pin)
Unregisters the callback associated with the specified pin, if any.
std::function< void(ButtonEvent)> ButtonCallback
bool start() override
Start the thread associated with this active object.
void stop() override
Signals the runner thread to terminate and joins the thread.
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.
This file includes all the types the logdecoder script will decode.
@ PRESSED
The button is pressed.
@ LONG_PRESS
The button is released before VERY_LONG_PRESS_TICKS.
@ SHORT_PRESS
The button is released before LONG_PRESS_TICKS.
@ VERY_LONG_PRESS
The button is released after VERY_LONG_PRESS_TICKS.