forked from FRC2994/TestProg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGamepad.cpp
148 lines (126 loc) · 3.15 KB
/
EGamepad.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "EGamepad.h"
EGamepad::EGamepad(UINT32 port):
Gamepad(port)
{
Initialize();
};
EGamepad::~EGamepad()
{
};
EventType EGamepad::GetEvent(int buttonNumber)
{
if (buttonNumber < 1 || buttonNumber > EGAMEPAD_NUM_BUTTONS)
{
return kEventErr;
}
// Disabled buttons are initialized to kEventErr (just sayin')
return m_buttonTable[buttonNumber].event;
}
StateType EGamepad::GetState(int buttonNumber)
{
if (buttonNumber < 1 || buttonNumber > EGAMEPAD_NUM_BUTTONS)
{
return kStateErr;
}
// Disabled buttons are initialized to kStateErr (just sayin' again)
return m_buttonTable[buttonNumber].state;
}
EventType EGamepad::GetDPadEvent(Gamepad::DPadDirection direction)
{
return m_dpadTable[direction].event;
}
StateType EGamepad::GetDPadState(Gamepad::DPadDirection direction)
{
return m_dpadTable[direction].state;
}
void EGamepad::Update()
{
StateType newState;
for (int i=1; i<=EGAMEPAD_NUM_BUTTONS; i++)
{
if (true == m_buttonTable[i].enabled)
{
newState = GetRawButton(i) ? kStateClosed : kStateOpen;
if (newState == m_buttonTable[i].state)
{
m_buttonTable[i].event = kEventNone;
}
else
{
if (kStateClosed == newState)
{
m_buttonTable[i].event = kEventClosed;
}
else
{
m_buttonTable[i].event = kEventOpened;
}
m_buttonTable[i].state = newState;
}
}
}
Gamepad::DPadDirection dpadDirection = GetDPad();
// By definition, only one contact on the dpad, at most, can be pressed at
// any one time. So, we loop through the table using a newState of "open"
// for every entry except the one (if any) that is being pressed. Note that
// a kCenter direction is equivalent to nothing pressed.
for (int i=Gamepad::kCenter; i<=Gamepad::kUpRight; i++)
{
if (i == dpadDirection)
{
newState = kStateClosed;
}
else
{
newState = kStateOpen;
}
if (newState == m_dpadTable[i].state)
{
m_dpadTable[i].event = kEventNone;
}
else
{
if (kStateClosed == newState)
{
m_dpadTable[i].event = kEventClosed;
}
else
{
m_dpadTable[i].event = kEventOpened;
}
m_dpadTable[i].state = newState;
}
}
}
bool EGamepad::EnableButton(int buttonNumber)
{
if (buttonNumber < 1 || buttonNumber > EGAMEPAD_NUM_BUTTONS)
{
return false;
}
m_buttonTable[buttonNumber].enabled = true;
return true;
}
void EGamepad::Initialize()
{
m_buttonTable[0].enabled = false;
m_buttonTable[0].event = kEventErr;
m_buttonTable[0].state = kStateErr;
for (int i=1; i<=EGAMEPAD_NUM_BUTTONS; i++)
{
m_buttonTable[i].enabled = false;
m_buttonTable[i].event = kEventErr;
m_buttonTable[i].state = GetRawButton(i) ? kStateClosed : kStateOpen;
}
// Only one DPad direction can be closed at any one time (kCenter is returned
// when no direction is closed). We initialize all the directions to open then
// read the DPad and set closed the direction read (which will be, unless someone
// is messing with the DPad at init time, kCentre)
for (int i=Gamepad::kCenter; i<=Gamepad::kUpRight; i++)
{
m_dpadTable[i].enabled = false; // We never check this
m_dpadTable[i].event = kEventNone;
m_dpadTable[i].state = kStateOpen;
}
m_dpadTable[GetDPad()].state = kStateClosed;
}