Created
February 2, 2012 22:38
-
-
Save smarthall/1726243 to your computer and use it in GitHub Desktop.
ArcadeMachineControls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Settings | |
#define PLAYER_NUM 1 | |
#define PIN_SQUELCH 5 | |
// Constants | |
#define USB_PKT_SIZE 8 | |
#define USB_KEY_START 2 | |
#if PLAYER_NUM == 1 | |
#define PIN_COUNT 15 | |
int pinmap[PIN_COUNT] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, A0, A1, A2, A3, A4 }; | |
int keymap[PIN_COUNT] = { 0x08, 0x1a, 0x14, 0x07, 0x16, 0x04, 0x1d, 0x06, 0x19, 0x1b, 0x29, 0x09, 0x0b, 0x0a, 0x17 }; | |
#else | |
#define PIN_COUNT 10 | |
int pinmap[PIN_COUNT] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | |
int keymap[PIN_COUNT] = { 0x12, 0x0c, 0x18, 0x0f, 0x0e, 0x0d, 0x05, 0x10, 0x36, 0x11 }; | |
#endif | |
int buttonstate[PIN_COUNT]; | |
int newbuttonstate[PIN_COUNT]; | |
unsigned long buttonsquelch[PIN_COUNT]; | |
void setup() { | |
Serial.begin(9600); | |
// Set all input pins to input and enable pullups | |
for (int i = 0; i < PIN_COUNT; i++) { | |
buttonstate[i] = HIGH; | |
pinMode(pinmap[i], INPUT); | |
digitalWrite(pinmap[i], HIGH); | |
} | |
} | |
void loop() { | |
int keychange = 0; | |
// Scan all our buttons for changes that arent squelched | |
for (int i = 0; i < PIN_COUNT; i++) { | |
unsigned long ctime = millis(); | |
newbuttonstate[i] = digitalRead(pinmap[i]); | |
// Have key states changed | |
if ((newbuttonstate[i] != buttonstate[i]) && (buttonsquelch[i] < ctime)) { | |
buttonstate[i] = newbuttonstate[i]; | |
keychange = 1; | |
buttonsquelch[i] = ctime + PIN_SQUELCH; | |
} | |
} | |
// Send a packet if there were changes. | |
if (keychange == 1) { | |
uint8_t kbdbuf[USB_PKT_SIZE]; | |
// Keys start at byte 2 | |
int currentbyte = USB_KEY_START; | |
// Set whole packet to 0 | |
for (int i = 0; i < USB_PKT_SIZE; i++) kbdbuf[i] = 0; | |
// Put first 6 pressed keys in packet | |
for (int i = 0; i < PIN_COUNT; i++) { | |
if (buttonstate[i] == LOW) { | |
kbdbuf[currentbyte++] = keymap[i]; | |
if (currentbyte == USB_PKT_SIZE) break; | |
} | |
} | |
// Send to PC | |
Serial.write(kbdbuf, USB_PKT_SIZE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment