forked from cgwood/ArdustationMega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.ino
201 lines (171 loc) · 4.13 KB
/
buttons.ino
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ASM Keypad class
// Created 2012 By Colin G http://www.diydrones.com/profile/ColinG
void Buttons::expanderWrite(byte _data) {
Wire.beginTransmission(expander);
Wire.write(_data);
Wire.endTransmission();
}
byte Buttons::expanderRead() {
byte _data;
Wire.requestFrom(expander, 1);
if (Wire.available()) {
_data = Wire.read();
}
return _data;
}
uint8_t Buttons::pressed(void) {
uint8_t newButton;
// find what is currently being pressed
newButton = _scanDebounced();
// If the new state differs from the previous state, the
// new state is interesting and we will report it.
if (newButton != _currentButton) {
_currentButton = newButton;
// if we are reporting an actual button press, give
// audio feedback
if (0 != newButton)
beep.play(BEEP_KEY);
return (newButton);
}
// State has not changed, report "no button press"
/// @note implement auto-repeat here
return (0);
}
// Scan the buttons
uint8_t Buttons::_scan(void) {
uint8_t id;
int16_t orientation;
uint8_t scanCode;
// expanderWrite(B11111101); // Turns the red LED off
// expanderWrite(B11111100); // Turns the red LED on
id = KEYPAD_ORIENTATION;
nvram.load_setting(&id, &orientation);
uint8_t code;
code = expanderRead();
scanCode = 0;
switch (orientation) {
default:
case KP_CONN_RIGHT:
if (!(code & 128))
scanCode = B_RIGHT;
if (!(code & 4))
scanCode = B_DOWN;
if (!(code & 8))
scanCode = B_LEFT;
if (!(code & 32))
scanCode = B_UP;
if (!(code & 16))
scanCode = B_OK;
if (!(code & 64))
scanCode = B_CANCEL;
break;
case KP_CONN_DOWN:
if (!(code & 128))
scanCode = B_DOWN;
if (!(code & 4))
scanCode = B_LEFT;
if (!(code & 8))
scanCode = B_UP;
if (!(code & 32))
scanCode = B_RIGHT;
if (!(code & 16))
scanCode = B_OK;
if (!(code & 64))
scanCode = B_CANCEL;
break;
case KP_CONN_LEFT:
if (!(code & 128))
scanCode = B_LEFT;
if (!(code & 4))
scanCode = B_UP;
if (!(code & 8))
scanCode = B_RIGHT;
if (!(code & 32))
scanCode = B_DOWN;
if (!(code & 16))
scanCode = B_OK;
if (!(code & 64))
scanCode = B_CANCEL;
break;
case KP_CONN_UP:
if (!(code & 128))
scanCode = B_UP;
if (!(code & 4))
scanCode = B_RIGHT;
if (!(code & 8))
scanCode = B_DOWN;
if (!(code & 32))
scanCode = B_LEFT;
if (!(code & 16))
scanCode = B_OK;
if (!(code & 64))
scanCode = B_CANCEL;
break;
}
return (scanCode);
}
uint8_t Buttons::_scanDebounced(void) {
uint8_t scanCode;
// get initial state, bail immediately if no buttons pressed
if (0 == (scanCode = _scan())) {
// _scanCode = 0;
_scanStart = millis();
_scanCodeLast = 0;
_buttonHolding = 0;
return (0);
}
// if the state changed, save and exit
if (scanCode != _scanCode) {
_scanCode = scanCode;
_scanStart = millis();
_lastPress = millis() - nvram.nv.keypadRepeatDelay + BUTTON_DEBOUNCE_TIMER;
return (0);
}
// Check if we've registered this button press already
if (scanCode == _scanCodeLast) {
// Check to see if we're holding the button down
if (_buttonHolding) {
// If it's been long enough since the last repeat of the button, repeat again
// But only perform repeats on left, right, up and down
if (scanCode <= B_RIGHT && millis() - _lastPress >= nvram.nv.keypadRepeatDelay) {
_lastPress = millis();
return (scanCode);
}
else {
return (0);
}
}
else {
// If we've held it down long enough, flag the _buttonHolding as true
if (millis() - _lastPress >= nvram.nv.keypadHoldDelay) {
_lastPress = millis();
_buttonHolding = 1;
if (scanCode > B_RIGHT) {
// Not a directional code, invoke hold behaviour
if (scanCode == B_OK)
return (B_OK_HOLD);
else if (scanCode == B_CANCEL)
return (B_CANCEL_HOLD);
else
return 0;
}
else
return (scanCode);
}
else {
return (0);
}
}
}
// if the state has remained the same for long enough return the button code
if ((millis() - _scanStart) >= BUTTON_DEBOUNCE_TIMER) {
_scanStart = millis();
_lastPress = millis();
// Save the scan code
_scanCodeLast = _scanCode;
// Return what was pressed
return (scanCode);
}
// None of the above
return (0);
}