211 lines
3.8 KiB
C++
211 lines
3.8 KiB
C++
/*
|
|
|
|
Convert DDR control panel to buttons.
|
|
DDR control panel uses a JST YLR-12V connector
|
|
Pins 1-6 are used for buttons, pin 7 and 8 are GRD
|
|
Hold down L1+R1+S1 for 5 seconds (hold_time) for different key
|
|
|
|
Button to keyboard mappings
|
|
Left 1 : A
|
|
Left 2 : J
|
|
Right 1 : D
|
|
Right 2 : L
|
|
Start 1 : S
|
|
Start 2 : K
|
|
L1+R1+S1 (hold_time) : Q
|
|
L2+R2+S2 (hold_time) : P
|
|
|
|
*/
|
|
|
|
// Button to pin definitions
|
|
#define BT_L1 1
|
|
#define BT_L2 2
|
|
#define BT_R1 3
|
|
#define BT_R2 4
|
|
#define BT_S1 5
|
|
#define BT_S2 6
|
|
|
|
// Timer preperation for ESC (back)
|
|
bool L1_held = false, L2_held = false, R1_held = false, R2_held = false, S1_held = false, S2_held = false, E1_held = false, E2_held = false;
|
|
bool P1_held = false, P2_held = false;
|
|
unsigned long P1_time = 0, P2_time = 0;
|
|
const long hold_time = 5000; // how long to hold for
|
|
|
|
#include <Keyboard.h>
|
|
|
|
void setup()
|
|
{
|
|
// Setup pins
|
|
pinMode (BT_L1, INPUT);
|
|
pinMode (BT_L2, INPUT);
|
|
pinMode (BT_R1, INPUT);
|
|
pinMode (BT_R2, INPUT);
|
|
pinMode (BT_S1, INPUT);
|
|
pinMode (BT_S2, INPUT);
|
|
|
|
// Set pins to high
|
|
digitalWrite(BT_L1, HIGH);
|
|
digitalWrite(BT_L2, HIGH);
|
|
digitalWrite(BT_R1, HIGH);
|
|
digitalWrite(BT_R2, HIGH);
|
|
digitalWrite(BT_S1, HIGH);
|
|
digitalWrite(BT_S2, HIGH);
|
|
|
|
Keyboard.begin();
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
// Check button inputs
|
|
if(digitalRead(BT_L1) == LOW)
|
|
{
|
|
if(!E1_held) // make sure we're not trying to use back
|
|
{
|
|
Keyboard.press('a');
|
|
L1_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('a');
|
|
L1_held = false;
|
|
}
|
|
|
|
if(digitalRead(BT_L2) == LOW)
|
|
{
|
|
if(!E2_held)
|
|
{
|
|
Keyboard.press('j');
|
|
L2_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('j');
|
|
L2_held = false;
|
|
}
|
|
|
|
if(digitalRead(BT_R1) == LOW)
|
|
{
|
|
if(!E1_held)
|
|
{
|
|
Keyboard.press('d');
|
|
R1_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('d');
|
|
R1_held = false;
|
|
}
|
|
|
|
if(digitalRead(BT_R2) == LOW)
|
|
{
|
|
if(!E2_held)
|
|
{
|
|
Keyboard.press('l');
|
|
R2_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('l');
|
|
R2_held = false;
|
|
}
|
|
|
|
if(digitalRead(BT_S1) == LOW)
|
|
{
|
|
if(!E1_held)
|
|
{
|
|
Keyboard.press('s');
|
|
S1_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('s');
|
|
S1_held = false;
|
|
}
|
|
|
|
if(digitalRead(BT_S2) == LOW)
|
|
{
|
|
if(!E2_held)
|
|
{
|
|
Keyboard.press('k');
|
|
S2_held = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('k');
|
|
S2_held = false;
|
|
}
|
|
|
|
// check hold status to determine ESC keys
|
|
if(!L1_held || !R1_held || !S1_held)
|
|
{
|
|
E1_held = false;
|
|
P1_held = false;
|
|
P1_time = 0;
|
|
}
|
|
else
|
|
{
|
|
// determine time held
|
|
if(!P1_held)
|
|
{
|
|
P1_held = true;
|
|
P1_time = millis(); // start timer
|
|
}
|
|
else
|
|
{
|
|
if(millis() - P1_time >= hold_time)
|
|
{
|
|
E1_held = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if(!L2_held || !R2_held || !S2_held)
|
|
{
|
|
E2_held = false;
|
|
P2_held = false;
|
|
P2_time = 0;
|
|
}
|
|
else
|
|
{
|
|
// determine time held
|
|
if(!P2_held)
|
|
{
|
|
P2_held = true;
|
|
P2_time = millis(); // start timer
|
|
}
|
|
else
|
|
{
|
|
if(millis() - P2_time >= hold_time)
|
|
{
|
|
E2_held = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ESC (back) keys
|
|
if(E1_held)
|
|
{
|
|
Keyboard.press('q');
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('q');
|
|
}
|
|
|
|
if(E2_held)
|
|
{
|
|
Keyboard.press('p');
|
|
}
|
|
else
|
|
{
|
|
Keyboard.release('p');
|
|
}
|
|
}
|