-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.cpp
91 lines (81 loc) · 2.2 KB
/
Input.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Input.h"
#include <Arduino.h>
namespace hw
{
Input::Input(void (*buttonPressed)(ButtonId))
:_buttonPressed(buttonPressed)
{
}
void Input::initialize()
{
pinMode(SWITCH_PHASE_PIN, INPUT_PULLUP);
pinMode(SWITCH_VIEW_PIN, INPUT_PULLUP);
pinMode(SWITCH_ON_OFF_PIN, INPUT_PULLUP);
}
void Input::update()
{
checkSwitchPhase();
checkSwitchView();
checkSwitchOnOff();
}
void Input::checkSwitchPhase()
{
int value = digitalRead(SWITCH_PHASE_PIN);
if(_lastSwitchPhasePinValue == 1 && value == 0)
{
switchPhasePressedTs = millis();
}
if(_lastSwitchPhasePinValue == 0 && value == 1)
{
if(millis() - switchPhasePressedTs < LONG_PRESS_THRESHOLD)
{
_buttonPressed(ButtonId::SwitchPhase);
}
else
{
_buttonPressed(ButtonId::SwitchPhaseLong);
}
}
_lastSwitchPhasePinValue = value;
}
void Input::checkSwitchView()
{
int value = digitalRead(SWITCH_VIEW_PIN);
if(_lastSwitchViewPinValue == 1 && value == 0)
{
switchViewPressedTs = millis();
}
if(_lastSwitchViewPinValue == 0 && value == 1)
{
if(millis() - switchViewPressedTs < LONG_PRESS_THRESHOLD)
{
_buttonPressed(ButtonId::SwitchView);
}
else
{
_buttonPressed(ButtonId::SwitchViewLong);
}
}
_lastSwitchViewPinValue = value;
}
void Input::checkSwitchOnOff()
{
int value = digitalRead(SWITCH_ON_OFF_PIN);
if(_lastSwitchOnOffPinValue == 1 && value == 0)
{
switchOnOffPressedTs = millis();
}
if(_lastSwitchOnOffPinValue == 0 && value == 1)
{
if(millis() - switchOnOffPressedTs < LONG_PRESS_THRESHOLD)
{
_buttonPressed(ButtonId::SwitchOnOff);
}
else
{
_buttonPressed(ButtonId::SwitchOnOffLong);
}
}
_lastSwitchOnOffPinValue = value;
}
}