forked from MobiFlight/MobiFlight-FirmwareSource
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMFButton.cpp
More file actions
68 lines (58 loc) · 1.21 KB
/
MFButton.cpp
File metadata and controls
68 lines (58 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// MFButton.cpp
//
// (C) MobiFlight Project 2022
//
#include "MFButton.h"
buttonEvent MFButton::_inputHandler = NULL;
MFButton::MFButton()
{
_initialized = false;
}
void MFButton::attach(uint8_t pin, const char *name)
{
_pin = pin;
_name = name;
pinMode(_pin, INPUT_PULLUP); // set pin to input
_state = digitalRead(_pin); // initialize on actual status
_initialized = true;
}
void MFButton::detach()
{
_initialized = false;
}
void MFButton::update()
{
if (!_initialized)
return;
uint8_t newState = (uint8_t)digitalRead(_pin);
if (newState != _state) {
_state = newState;
trigger(_state);
}
}
void MFButton::trigger(uint8_t state)
{
(state == LOW) ? triggerOnPress() : triggerOnRelease();
}
void MFButton::triggerOnPress()
{
if (!_initialized)
return;
if (_inputHandler && _state == LOW) {
(*_inputHandler)(btnOnPress, _name);
}
}
void MFButton::triggerOnRelease()
{
if (!_initialized)
return;
if (_inputHandler && _state == HIGH) {
(*_inputHandler)(btnOnRelease, _name);
}
}
void MFButton::attachHandler(buttonEvent newHandler)
{
_inputHandler = newHandler;
}
// MFButton.cpp