Wednesday, October 5, 2016

COIN-OP: mini arcade cabinet build log 2/3

This is a 3 part post, in which i cover:

INTRODUCTION Arduino Uno Keyboard emulator - dfu mode / programming
SKETCH Arduino Uno keyboard emulator - ino sketch
COINS Coin acceptor (3 coins)

In this post is covered for convenience the Arduino sketch part.

The sketch uses a simple Serial.write(buf) to send a keystroke / keyrelease for each switch, like
Serial.write(buf, 8); // Send keypress

The HID message sent in the serial part is a byte array:
uint8_t buf[8] = { 0 };

Byte 0 is a modifier bye. If you set a bit with another keypress, you will have a combination like CTRL-X.

Modifier keys:
Bit 0 - Left CTRL
Bit 1 - Left SHIFT
Bit 2 - Left ALT
Bit 3 - Left GUI
Bit 4 - Right CTRL
Bit 5 - Right SHIFT
Bit 6 - Right ALT
Bit 7 - Right GUI

Byte 1 - Not used

The useful part starts from byte 2 up to byte 7, where are mapped the HID active key usage codes. This accepts up to 6 keys being pressed at the same time.

Because i have more than 6 keys, i had to join more than one key in each byte. In this situation the same byte is used to send KEY_UP or KEY_DOWN: this works because the switches are inside a joystick and cannot be sent at the same time.

This is my mapping, modify as you require your situation (S1 means Switch 1):
#define PIN_UP 6
#define PIN_DOWN 4 
...
My joins are:
Byte 2: PIN_UP & PIN_DOWN
Byte 3: PIN_LEFT & PIN_RIGHT
Byte 4: PIN_S1 & PIN_S5
etc...
Byte 7: PIN_START & PIN_ESC & PIN_COIN

This mapping helps to send joystick on upper left (PIN_UP + PIN_LEFT) while pressing a fire switch (PIN_S1) and jump switch . Less attention needs the Start/Select and Exit switch.

Each time the loop function finds a specific pin press, in the buffer is wrote the key code. 
To send the keypress "Enter" write 0x28 into the buf, using this code:
buf[2] = 0x28; //Or define a variable for convenience

To emulate a key release: buf[2] = 0;

More HID Keyboard codes could be found at this link

Debounce: I carefully tested the sketch on debounce matter. With simple timing check, a debounce routine is not needed at all.

In the next part i'll cover the coin acceptor part.

No comments:

Post a Comment