Skyward boardcore
Loading...
Searching...
No Matches
ButtonHandler.h
Go to the documentation of this file.
1/* Copyright (c) 2015-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
25#include <Singleton.h>
28
29#include <map>
30
31namespace Boardcore
32{
33
34enum class ButtonEvent
35{
36 PRESSED,
40};
41
51class ButtonHandler : public Singleton<ButtonHandler>
52{
53 friend Singleton<ButtonHandler>;
54
55 static constexpr uint32_t SAMPLE_PERIOD = 100; // 10Hz
56 static constexpr int LONG_PRESS_TICKS = 10; // 1s
57 static constexpr int VERY_LONG_PRESS_TICKS = 50; // 5s
58
59public:
60 using ButtonCallback = std::function<void(ButtonEvent)>;
61
69 bool registerButtonCallback(miosix::GpioPin pin, ButtonCallback callback);
70
78 bool unregisterButtonCallback(miosix::GpioPin pin);
79
88 bool start();
89
93 void stop();
94
95private:
97
104 void periodicButtonValueCheck(miosix::GpioPin pin);
105
106 TaskScheduler scheduler;
107
116 std::map<miosix::GpioPin, std::tuple<ButtonCallback, bool, unsigned int>,
118 callbacks;
119};
120
121} // namespace Boardcore
Utility to detects if buttons are pressed, long pressed or long-long pressed and calls a callback in ...
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
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.
@ 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.
Comparison operator between GpioPins used for std::map.