DDRuino/DDRuino.ino

81 lines
1.7 KiB
Arduino
Raw Permalink Normal View History

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