-
Notifications
You must be signed in to change notification settings - Fork 1
/
DDRuino.ino
80 lines (67 loc) · 1.67 KB
/
DDRuino.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
/*@ file DDRunino.ino
*
* Simple Arduino sketch to run a DDR pad as a keyboard
*
* https://git.beesbuzz.biz/fluffy/DDRuino/
*/
#include <Keyboard.h>
//! Uncomment this to enable the serial debugger to determine the pin mappings
//#define DEBUG
//! Debounce time in milliseconds
constexpr int debounceTime = 10;
//! Input button
struct Input {
int pin; //!< Input pin
int keyCode; //!< Mapped key
bool pressed; //!< Last read state
unsigned long lastChange; //!< Last update time
};
/*! Map the input pins to a button on the pad; use debug mode to determine
* your button layout if you're not sure
*/
Input mapping[] = {
{ 4, KEY_RETURN },
{ 5, KEY_ESC },
{ 6, KEY_UP_ARROW },
{ 7, KEY_DOWN_ARROW },
{ 8, KEY_RIGHT_ARROW },
{ 9, KEY_LEFT_ARROW },
};
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
for (int i = 0; i < 12; i++) {
pinMode(i, INPUT_PULLUP);
}
#endif
unsigned long now = millis();
for (auto i : mapping) {
pinMode(i.pin, INPUT_PULLUP);
i.pressed = false;
i.lastChange = now;
}
Keyboard.begin();
}
void loop()
{
#ifdef DEBUG
for (int i = 0; i < 12; i++) {
Serial.print(!digitalRead(i) ? "0123456789abcdef"[i] : ' ');
}
Serial.print('\n');
#endif
unsigned long now = millis();
for (auto& i : mapping) {
bool val = !digitalRead(i.pin);
if (val != i.pressed && (i.lastChange + debounceTime) < now) {
if (val) {
Keyboard.press(i.keyCode);
} else {
Keyboard.release(i.keyCode);
}
i.pressed = val;
i.lastChange = now;
}
}
}