|
|
|
@ -1,21 +1,29 @@
|
|
|
|
|
/*@ 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
|
|
|
|
|
//! Uncomment this to enable the serial debugger to determine the pin mappings
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
|
|
//! Debounce time in milliseconds
|
|
|
|
|
constexpr int debounceTime = 20; |
|
|
|
|
|
|
|
|
|
//! Input button
|
|
|
|
|
struct Input { |
|
|
|
|
int pin; |
|
|
|
|
int keyCode; |
|
|
|
|
bool state; |
|
|
|
|
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 }, |
|
|
|
@ -27,17 +35,20 @@ Input mapping[] = {
|
|
|
|
|
|
|
|
|
|
void setup() |
|
|
|
|
{ |
|
|
|
|
#ifdef DEBUG |
|
|
|
|
Serial.begin(9600); |
|
|
|
|
|
|
|
|
|
#ifdef DEBUG |
|
|
|
|
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.state = HIGH; |
|
|
|
|
i.pressed = false; |
|
|
|
|
i.lastChange = now; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Keyboard.begin(); |
|
|
|
@ -51,18 +62,19 @@ void loop()
|
|
|
|
|
} |
|
|
|
|
Serial.print('\n'); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& i : mapping) { |
|
|
|
|
unsigned long now = millis(); |
|
|
|
|
bool val = !digitalRead(i.pin); |
|
|
|
|
if (val != i.state) { |
|
|
|
|
if (val != i.pressed && (i.lastChange + debounceTime) < now) { |
|
|
|
|
if (val) { |
|
|
|
|
// TODO debounce
|
|
|
|
|
Keyboard.press(i.keyCode); |
|
|
|
|
} else { |
|
|
|
|
Keyboard.release(i.keyCode); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
i.state = val; |
|
|
|
|
i.pressed = val; |
|
|
|
|
i.lastChange = now; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|