[Tutorial] How to compile and upload code to the keypad

I decided to change the buttons layout of my cpi, from XY AB to AB XY because I felt more confortable that way and the thumb is in a better resting position, also I am really used to a Gameboy Color position
image

And found out that there is a bit of lack of information at plain sight (There is information, but you need to check the schematics) so I decided to make this guide to save you time for such a straightforward task that took me longer than I expected.

First things first.

If we don´t have it already, we need arduino ide. There is a portable version but I recommend you to install it.

As many arduino clones, they don´t come with the same usb chip as the genuine arduino uno, they come with its chinese counterpart CH340 we need to install the driver in order to use USB to serial and get to see the COM port on Arduino IDE.

We can download it from the manufacturer website: http://www.wch.cn/download/CH341SER_EXE.html

Direct links:

Almost there

Now we need to download the UsbKeyboard library from github and the code of the keypad from here

After we got the UsbKeyboard folder, we need to zip it and include it to our Arduino IDE from Sketch -> Include library -> Add .zip library

image

Note: You can also install the library by pasting the uncompressed folder in Documents/Arduino/libraries but for the sake of simplicity I explained the other way around.
image

Now we are ready for the fun part

Connect the keypad via usb to your computer and check on Arduino IDE if it recognises the COM port.

image

Now that we are here lets configure our board.

Board: Arduino Nano
Processor: ATmega168
Port: Chose the arduino comp port, it should be set automatically when recognised.

Programmer: leave it default, should be AVRISP mkII

Now we are set

You can open now the code on the IDE and change what we want. We can hit Upload and watch the flashy lights on our board while we wait.

image

A succesful upload should look like this:
image

Happy hacking :man_mechanic:

12 Likes

Ok, how would the changed code look like for the Nintendo Layout (A and X button on the right)?

Changing these lines:

#define KEYPAD_Y      KEY_K
#define KEYPAD_X      KEY_J
#define KEYPAD_A      KEY_U
#define KEYPAD_B      KEY_I

Take a look at this image

The original position of buttons is

X= KEY_U Y= KEY_I A = KEY_J B =KEY_K

The code above is for how I have it, i inverted the buttons, my AB are on top and XY on bottom.

So I guess what you want to archieve is like this:

The code for that would be:

    #define KEYPAD_Y      KEY_Y
    #define KEYPAD_X      KEY_I
    #define KEYPAD_A      KEY_K
    #define KEYPAD_B      KEY_J
3 Likes

You can easily make this change in Settings->Buttons Layout and set it to SNES Compatible. Don’t forget to select “UpdateRetroArch” to make the changes to RetroArch’s configuration file.

1 Like

My understanding is that what Petrais/Pixelpunker want to achieve is that changing the key layout on whole system level, not only in Retroarch.

Yes I changed the layout on a hardware level

Noob here. I am unclear on how to do this:

I have 2 questions:
Where is the code that I need to start with?
How do I know what code my keypad is already running?

Thanks in advance.

EDIT:

I just found it, but to install the library, I had to pull the “UsbKeyboard” folder out of the zip and place it in the Documents/arduino/libraries folder. I then loaded the .ino file from another copy of the contents of the zip file.

Has anyone tried doing this do change the update loop from 1 second to a lower number? Did it work?

Yes, another user in another topic changed it to and his gameshell worked like before.

EDIT:

There is a bug in the original code making the delay in the loop irelevant.
More info here: https://github.com/clockworkpi/Keypad/issues/1#issuecomment-495069870

In my fork I fixed that and also updated to the latest v-usb library

1 Like

I installed UsbKeyboard and tried to upload the code, but this is all I get:
Arduino: 1.8.13 (Mac OS X), Board: “Arduino Nano, ATmega168”

clockworkpi_keypad:1:10: fatal error: oddebug.h: No such file or directory
#include <oddebug.h>
^~~~~~~~~~~
compilation terminated.
exit status 1
oddebug.h: No such file or directory

This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences. (oof)

It also won’t recognize my GameShell’s keypad as connected. I guess ill have to try this again on Windows?

(edit: The reason it didnt recognize my GameShell’s keypad was because I didnt install the drivers :man_facepalming:

So I installed the driver, and now it’ll recognize the board. However, when I install the library, this
happens: Arduino: 1.8.13 (Mac OS X), Board: “Arduino Nano, ATmega168”

Specified folder/zip file does not contain a valid library

Guess ill try to install it manually.

(edit: I found out why the whole thing wasn’t working: I was installing the wrong library, and I didnt install the driver for the Keypad. Classic PEBKAC issue. :expressionless:)

(edit: Arduino: 1.8.13 (Mac OS X), Board: “Arduino Nano, ATmega168”

In file included from /Users/kids/Downloads/Keypad-master/clockworkpi_keypad/clockworkpi_keypad.ino:4:0:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h: In constructor ‘UsbKeyboardDevice::UsbKeyboardDevice()’:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:152:5: error: ‘usbSetInterrupt’ was not declared in this scope
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
^~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:152:5: note: suggested alternative: ‘detachInterrupt’
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
^~~~~~~~~~~~~~~
detachInterrupt
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h: In member function ‘void UsbKeyboardDevice::sendKeyStroke(byte, byte)’:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:165:13: error: ‘usbInterruptIsReady’ was not declared in this scope
while (!usbInterruptIsReady()) {
^~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:165:13: note: suggested alternative: ‘noInterrupts’
while (!usbInterruptIsReady()) {
^~~~~~~~~~~~~~~~~~~
noInterrupts
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:176:5: error: ‘usbSetInterrupt’ was not declared in this scope
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
^~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:176:5: note: suggested alternative: ‘detachInterrupt’
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
^~~~~~~~~~~~~~~
detachInterrupt
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:178:13: error: ‘usbInterruptIsReady’ was not declared in this scope
while (!usbInterruptIsReady()) {
^~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:178:13: note: suggested alternative: ‘noInterrupts’
while (!usbInterruptIsReady()) {
^~~~~~~~~~~~~~~~~~~
noInterrupts
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h: In member function ‘size_t UsbKeyboardDevice::press(uint8_t)’:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:213:2: error: ‘usbSetInterrupt’ was not declared in this scope
usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
^~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:213:2: note: suggested alternative: ‘detachInterrupt’
usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
^~~~~~~~~~~~~~~
detachInterrupt
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h: In member function ‘size_t UsbKeyboardDevice::release(uint8_t)’:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:230:2: error: ‘usbSetInterrupt’ was not declared in this scope
usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
^~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:230:2: note: suggested alternative: ‘detachInterrupt’
usbSetInterrupt((uint8_t*)&_keyReport, sizeof(_keyReport));
^~~~~~~~~~~~~~~
detachInterrupt
In file included from /Users/kids/Downloads/Keypad-master/clockworkpi_keypad/clockworkpi_keypad.ino:8:0:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/asciimap.h: At global scope:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/asciimap.h:147:54: error: ‘keyboard_report_t’ has not been declared
ASCIIMAPPUBLIC uint8_t asciitokeyreport(uint8_t key, keyboard_report_t *report);
^~~~~~~~~~~~~~~~~
In file included from /Users/kids/Downloads/Keypad-master/clockworkpi_keypad/clockworkpi_keypad.ino:16:0:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:123:3: error: conflicting declaration ‘typedef struct KeyReport KeyReport’
} KeyReport;
^~~~~~~~~
In file included from /Users/kids/Downloads/Keypad-master/clockworkpi_keypad/clockworkpi_keypad.ino:4:0:
/Users/kids/Documents/Arduino/libraries/UsbKeyboard/UsbKeyboard.h:62:3: note: previous declaration as ‘typedef struct KeyReport KeyReport’
} KeyReport;
^~~~~~~~~
In file included from /Users/kids/Downloads/Keypad-master/clockworkpi_keypad/clockworkpi_keypad.ino:16:0:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:158:22: error: ‘keyboard_report_t’ has not been declared
void sendKeyReport(keyboard_report_t *report) {
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:236:3: error: ‘keyboard_report_t’ does not name a type
keyboard_report_t getKeyReportFromChar(byte symbol) {
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:257:24: error: ‘mouse_report_t’ has not been declared
void sendMouseReport(mouse_report_t *report) {
^~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:267:3: error: ‘mouse_report_t’ does not name a type
mouse_report_t getMouseReport(int8_t deltaX, int8_t deltaY, uint8_t buttons) {
^~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In constructor ‘VUSBHIDKeyboardMouseDevice::VUSBHIDKeyboardMouseDevice()’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:130:5: error: ‘hidInit’ was not declared in this scope
hidInit();
^~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:130:5: note: suggested alternative: ‘usbInit’
hidInit();
^~~~~~~
usbInit
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::update(uint8_t)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:150:5: error: ‘hidPoll’ was not declared in this scope
hidPoll(&time_since_lastcall_in_four_ms_units);
^~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:150:5: note: suggested alternative: ‘usbPoll’
hidPoll(&time_since_lastcall_in_four_ms_units);
^~~~~~~
usbPoll
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendKeyReport(int*)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:159:12: error: ‘keyboard_report_dirty’ was not declared in this scope
while (keyboard_report_dirty) {
^~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:163:13: error: ‘current_keyboard_report’ was not declared in this scope
memcpy(&current_keyboard_report, report, sizeof(keyboard_report_t));
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:163:53: error: ‘keyboard_report_t’ was not declared in this scope
memcpy(&current_keyboard_report, report, sizeof(keyboard_report_t));
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:164:5: error: ‘keyboard_report_dirty’ was not declared in this scope
keyboard_report_dirty|=_BV(1);
^~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendKeyRelease()’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:168:5: error: ‘keyboard_report_t’ was not declared in this scope
keyboard_report_t report;
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:169:28: error: ‘report’ was not declared in this scope
keyboard_report_clear(&report);
^~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:169:28: note: suggested alternative: ‘remove’
keyboard_report_clear(&report);
^~~~~~
remove
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:169:5: error: ‘keyboard_report_clear’ was not declared in this scope
keyboard_report_clear(&report);
^~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:170:5: error: ‘keyboard_report_setNoEvent’ was not declared in this scope
keyboard_report_setNoEvent(&report);
^~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendKeyStroke(byte, byte)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:174:5: error: ‘keyboard_report_t’ was not declared in this scope
keyboard_report_t report;
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:175:28: error: ‘report’ was not declared in this scope
keyboard_report_clear(&report);
^~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:175:28: note: suggested alternative: ‘remove’
keyboard_report_clear(&report);
^~~~~~
remove
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:175:5: error: ‘keyboard_report_clear’ was not declared in this scope
keyboard_report_clear(&report);
^~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘size_t VUSBHIDKeyboardMouseDevice::press(uint8_t)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:193:27: error: ‘current_keyboard_report’ was not declared in this scope
for(j = 0; j < sizeof(current_keyboard_report.keycode); j++) {
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:198:21: error: ‘current_keyboard_report’ was not declared in this scope
if (j == sizeof(current_keyboard_report.keycode)) {
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:210:20: error: ‘current_keyboard_report’ was not declared in this scope
sendKeyReport(&current_keyboard_report);
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘size_t VUSBHIDKeyboardMouseDevice::release(uint8_t)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:222:28: error: ‘current_keyboard_report’ was not declared in this scope
for (i = 0; i < sizeof(current_keyboard_report.keycode); i++) {
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:227:20: error: ‘current_keyboard_report’ was not declared in this scope
sendKeyReport(&current_keyboard_report);
^~~~~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendKey(byte)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:243:5: error: ‘keyboard_report_t’ was not declared in this scope
keyboard_report_t report = getKeyReportFromChar(symbol);
^~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:244:20: error: ‘report’ was not declared in this scope
sendKeyReport(&report);
^~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:244:20: note: suggested alternative: ‘remove’
sendKeyReport(&report);
^~~~~~
remove
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendMouseReport(int*)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:258:12: error: ‘mouse_report_dirty’ was not declared in this scope
while (mouse_report_dirty) {
^~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:262:13: error: ‘current_mouse_report’ was not declared in this scope
memcpy(&current_mouse_report, report, sizeof(mouse_report_t));
^~~~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:262:13: note: suggested alternative: ‘sendMouseReport’
memcpy(&current_mouse_report, report, sizeof(mouse_report_t));
^~~~~~~~~~~~~~~~~~~~
sendMouseReport
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:262:50: error: ‘mouse_report_t’ was not declared in this scope
memcpy(&current_mouse_report, report, sizeof(mouse_report_t));
^~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:263:5: error: ‘mouse_report_dirty’ was not declared in this scope
mouse_report_dirty|=_BV(1);
^~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendMouseIdle()’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:276:5: error: ‘mouse_report_t’ was not declared in this scope
mouse_report_t report;
^~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:277:25: error: ‘report’ was not declared in this scope
mouse_report_clear(&report);
^~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:277:25: note: suggested alternative: ‘remove’
mouse_report_clear(&report);
^~~~~~
remove
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:277:5: error: ‘mouse_report_clear’ was not declared in this scope
mouse_report_clear(&report);
^~~~~~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h: In member function ‘void VUSBHIDKeyboardMouseDevice::sendMouseEvent(int8_t, int8_t, uint8_t)’:
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:282:7: error: ‘mouse_report_t’ was not declared in this scope
mouse_report_t report = getMouseReport(deltaX, deltaY, buttons);
^~~~~~~~~~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:283:24: error: ‘report’ was not declared in this scope
sendMouseReport(&report);
^~~~~~
/Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse/VUSBHIDKeyboardMouse.h:283:24: note: suggested alternative: ‘remove’
sendMouseReport(&report);
^~~~~~
remove
Multiple libraries were found for “oddebug.h”
Used: /Users/kids/Documents/Arduino/libraries/UsbKeyboard
Not used: /Users/kids/Documents/Arduino/libraries/VUSBHIDKeyboardMouse
exit status 1
Error compiling for board Arduino Nano.)