-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.cpp
77 lines (66 loc) · 1.06 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
#include "Input.h"
Input::Input()
{
pinMode(INPUT, INPUT_X_PIN);
pinMode(INPUT, INPUT_Y_PIN);
pinMode(INPUT, INPUT_BTN_PIN);
lastDir = GLB_NONE;
}
Input::~Input()
{
}
uint16_t Input::GetXAxis()
{
return GetAxis(INPUT_X_PIN);
}
uint16_t Input::GetYAxis()
{
return GetAxis(INPUT_Y_PIN);
}
uint16_t Input::GetAxis(uint8_t pin)
{
return analogRead(pin);
}
boolean Input::ButtonClick()
{
return digitalRead(INPUT_BTN_PIN) == LOW;
}
inline uint8_t Input::GetDirection()
{
if (GetXAxis() > 900)
{
return GLB_LEFT;
}
if (GetXAxis() < 80)
{
return GLB_RIGHT;
}
if (GetYAxis() > 900)
{
return GLB_UP;
}
if (GetYAxis() < 80)
{
return GLB_DOWN;
}
// This to reduce flickering between values.
else if (
(GetXAxis() < 600 && GetXAxis() > 500) ||
(GetYAxis() < 600 && GetYAxis() > 500))
return GLB_NONE;
else return lastDir;
}
uint8_t Input::GetDirectionOnce()
{
uint8_t currentDir = GetDirection();
if (currentDir != lastDir)
{
lastDir = currentDir;
return currentDir;
}
else
{
lastDir = currentDir;
return GLB_NONE;
}
}