Klassiska joysticken till USB adapter (3 / 5 steg)
Steg 3: programvara
Följande Arduino skiss filen bör upprättas och sänds in till Arduino Leonardo eller Arduino Micro: JoystickToKeyboard.ino
ColecoVision / ADAM Joystick till PC tangentbord omvandlare
för Arduino Leonardo
2014-08-24
//-----------------------------------------------------------------------------
Joystick Pins
CONST byte gcFirePin = 2;
CONST byte gcUpPin = 3;
CONST byte gcDownPin = 4;
CONST byte gcLeftPin = 5;
CONST byte gcRightPin = 6;
CONST byte gcModeAPin = 7.
CONST byte gcModeBPin = 8;
CONST byte gcFireMonitorPin = 13.
CONST byte gcBit0Pin = 3;
CONST byte gcBit2Pin = 4;
CONST byte gcBit3Pin = 5;
CONST byte gcBit1Pin = 6;
Tangentbordet för
CONST char gcUpKey = KEY_UP_ARROW;
CONST char gcDownKey = KEY_DOWN_ARROW;
CONST char gcLeftKey = KEY_LEFT_ARROW;
CONST char gcRightKey = KEY_RIGHT_ARROW;
CONST char gcLeftFireKey = KEY_LEFT_ALT;
CONST char gcRightFireKey = KEY_LEFT_CTRL;
CONST char gcPurpleButtonKey = KEY_LEFT_SHIFT;
CONST char gcBlueButtonKey = "z";
CONST char gcAsteriskKey = '-';
CONST char gcPoundKey = '=';
Aktuell bildruta Joystick Status
byte gLeftFireButton = 0;
byte gRightFireButton = 0;
byte gUp = 0;
byte gDown = 0;
byte gLeft = 0;
byte gRight = 0;
char gNumPadValue = "";
byte gPurpleButton = 0;
byte gBlueButton = 0;
byte gBit0 = 0;
byte gBit1 = 0;
byte gBit2 = 0;
byte gBit3 = 0;
Sista bildrutan Joystick Status
byte gLastLeftFireButton = 0;
byte gLastRightFireButton = 0;
byte gLastUp = 0;
byte gLastDown = 0;
byte gLastLeft = 0;
byte gLastRight = 0;
char gLastNumPadValue = "";
byte gLastPurpleButton = 0;
byte gLastBlueButton = 0;
Linje Läs variabler
CONST unsigned int gcThreashold = 4;
unsigned int gLeftFireCount = 0;
unsigned int gRightFireCount = 0;
unsigned int gUpCount = 0;
unsigned int gDownCount = 0;
unsigned int gLeftCount = 0;
unsigned int gRightCount = 0;
unsigned int gBit0Count = 0;
unsigned int gBit1Count = 0;
unsigned int gBit2Count = 0;
unsigned int gBit3Count = 0;
Frame variabler
CONST int gcFrameLength = 10;
unsigned int gLoopsPerFrame = 0;
osignerade långa gLastFrameStart = 0;
Visar status för styrspaken.
void ShowJoystickStatus()
{
Debug Information
Serial.println(gLoopsPerFrame);
digitalWrite (gcFireMonitorPin, gLeftFireButton | gRightFireButton);
}
void SendKeyboardStateToPc()
{
om ((gLastNumPadValue! = "") & & (gLastNumPadValue! = gNumPadValue))
{
Keyboard.release(gLastNumPadValue);
}
om ((gNumPadValue! = "") & & (gLastNumPadValue! = gNumPadValue))
{
Keyboard.Press(gNumPadValue);
}
SendLineStateToPc (gLastLeftFireButton, gLeftFireButton, gcLeftFireKey);
SendLineStateToPc (gLastRightFireButton, gRightFireButton, gcRightFireKey);
SendLineStateToPc (gLastUp, gUp, gcUpKey);
SendLineStateToPc (gLastDown, gDown, gcDownKey);
SendLineStateToPc (gLastLeft, gLeft, gcLeftKey);
SendLineStateToPc (gLastRight, gRight, gcRightKey);
SendLineStateToPc (gLastPurpleButton, gPurpleButton, gcPurpleButtonKey);
SendLineStateToPc (gLastBlueButton, gBlueButton, gcBlueButtonKey);
}
void SendLineStateToPc (byte lastState, byte currentState, char keyUsedForLine)
{
om ((lastState == 1) & & (currentState == 0))
{
Keyboard.release(keyUsedForLine);
}
om ((lastState == 0) & & (currentState == 1))
{
Keyboard.Press(keyUsedForLine);
}
}
unsigned int CheckJoystickLine(byte pin)
{
Return (digitalRead(pin) == låg);
}
void CheckJoystickLines()
{
CONST int cLineDelay = 50;
Lägga Joystick i riktning läge
digitalWrite (gcModeAPin, hög);
digitalWrite (gcModeBPin, låg);
delayMicroseconds(cLineDelay);
gLeftFireCount += CheckJoystickLine(gcFirePin);
gUpCount += CheckJoystickLine(gcUpPin);
gDownCount += CheckJoystickLine(gcDownPin);
gLeftCount += CheckJoystickLine(gcLeftPin);
gRightCount += CheckJoystickLine(gcRightPin);
Lägga Joystick i knappsatsen läge
digitalWrite (gcModeAPin, låg);
digitalWrite (gcModeBPin, hög);
delayMicroseconds(cLineDelay);
gRightFireCount += CheckJoystickLine(gcFirePin);
gBit0Count += CheckJoystickLine(gcBit0Pin);
gBit1Count += CheckJoystickLine(gcBit1Pin);
gBit2Count += CheckJoystickLine(gcBit2Pin);
gBit3Count += CheckJoystickLine(gcBit3Pin);
}
void ResetFrameVariables()
{
Kopiera aktuell bildruta för att hålla ram
gLastNumPadValue = gNumPadValue;
gLastLeftFireButton = gLeftFireButton;
gLastRightFireButton = gRightFireButton;
gLastUp = gUp;
gLastDown = gDown;
gLastLeft = gLeft;
gLastRight = gRight;
gLastPurpleButton = gPurpleButton;
gLastBlueButton = gBlueButton;
Återställa bildräkneverket Loop
gLoopsPerFrame = 0;
Återställ Joysick staten
gLeftFireCount = 0;
gLeftFireButton = 0;
gRightFireCount = 0;
gRightFireButton = 0;
gUpCount = 0;
gUp = 0;
gDownCount = 0;
gDown = 0;
gLeftCount = 0;
gLeft = 0;
gRightCount = 0;
gRight = 0;
gBit0Count = 0;
gBit0 = 0;
gBit1Count = 0;
gBit1 = 0;
gBit2Count = 0;
gBit2 = 0;
gBit3Count = 0;
gBit3 = 0;
gNumPadValue = "";
gPurpleButton = 0;
gBlueButton = 0;
}
byte DetermineJoystickLineValue (unsigned int pressCount)
{
Return (pressCount > = gcThreashold);
}
void DetermineJoystickValues()
{
CONST char cKeypadValueLookup [16] = {
' ', '6', '1', "3',"9', '0', gcAsteriskKey,"",
"2', gcPoundKey,"7"," ","5","4","8"," "};
gLeftFireButton = DetermineJoystickLineValue(gLeftFireCount);
gRightFireButton = DetermineJoystickLineValue(gRightFireCount);
gUp = DetermineJoystickLineValue(gUpCount);
gDown = DetermineJoystickLineValue(gDownCount);
gLeft = DetermineJoystickLineValue(gLeftCount);
gRight = DetermineJoystickLineValue(gRightCount);
gBit0 = DetermineJoystickLineValue(gBit0Count);
gBit1 = DetermineJoystickLineValue(gBit1Count);
gBit2 = DetermineJoystickLineValue(gBit2Count);
gBit3 = DetermineJoystickLineValue(gBit3Count);
int keypadCode = (gBit3 << 3) + (gBit2 << 2) + (gBit1 << 1) + gBit0;
gNumPadValue = cKeypadValueLookup [keypadCode];
Kontrollera om SuperAction Controller knappar.
om (keypadCode == 7)
{
gPurpleButton = 1;
}
om (keypadCode == 11)
{
gBlueButton = 1;
}
}
void setup()
{
Setup Serial Monitor
Serial.BEGIN(19200);
Setup Joystick Pins
pinMode (gcFirePin, INPUT_PULLUP);
pinMode (gcUpPin, INPUT_PULLUP);
pinMode (gcDownPin, INPUT_PULLUP);
pinMode (gcLeftPin, INPUT_PULLUP);
pinMode (gcRightPin, INPUT_PULLUP);
pinMode (gcModeAPin, produktionen);
pinMode (gcModeBPin, produktionen);
pinMode (gcFireMonitorPin, produktionen);
}
void loop()
{
osignerade långa currentTime;
currentTime = millis();
om (currentTime > = (gLastFrameStart + gcFrameLength))
{
Commit Joystick värde logik
DetermineJoystickValues();
Skicka värden till Monitor
ShowJoystickStatus();
Skicka Keyboad staten till PC
SendKeyboardStateToPc();
Återställ bildruta variabler
ResetFrameVariables();
Dags att börja nästa bildruta
gLastFrameStart = currentTime;
}
annat
{
Kontrollera värdet på de ingående linjerna och anteckna dem.
gLoopsPerFrame ++;
CheckJoystickLines();
}
}