Jan 23, 2013

Setup PlayStation Joysticks on Raspberry Pi



My friends and I are big fans of KOF. We all know the latency between PC and USB converter of PS joysticks is too long to enjoy our fight. However, the PS joysticks to PC via parallel port is a nice choice for low latency. (It you want to know how the converter works, please refer another Chinese site : http://stevie.myweb.hinet.net/adaptor2.htm )

PlayStation joysticks converter via Parallel Port.

Although it's nice but my computer with parallel port is broken. Why don't we play NeoGeo or MAME on Raspberry Pi? Thus, there is a plan in my mind to turn Raspberry Pi as a console game platform to play many kind of emulators, and RetroPie is a such great project supports SNES, NES, GBA. GB, SEGA, and MAME. In this part, I only focus on how to modify the joysticks and won't mention how to setup an emulator.

I should have a set of very handful joysticks for my game platform. The legacy PlayStation joysticks via GPIO is the best choice because it got the benefits of 12 buttons and 4-axis analog control, and the most important one is with very lower latency via GPIO than USB. If you are a fighting gamer (KOF, Street Fighter) as us, you will know why the latency is such important.


So, let's start to modify the converter for RaspberryPi.

gamecon_gpio_rpi is the driver for NES, SNES, GameCube, and PS Joystick via GPIO, and it's included in RetroPie. In this instruction, you will see how to connect a PS joys to Raspberry Pi. If you need to know the pin descriptions of PS joy, this site gives you how PS joysticks work. The connection method is some different between rev 1.0 and rev 2.0 of RPi. My RPi is rev 2.0, so I summarized the pin table. 


I start to make a simple connector for the P1 of RPi and DB25-Male. Here are some materials I prepared and the connector that I finished:




After a little work, connect with PS joysticks, converters, and RPi together. Start to test it!!!!


Before playing games, use RetroPie-Setup to install the emulator platform and gamecon_gpio_rpi driver for joysticks. The detail instruction can be found here. (The driver should be compiled and installed separately without RetroPie installed, but I haven't tried that yet. :P).

If the kernel module of gamecon_gpio_rpi is built correctly, you can install the module by the command:
pi@raspberrypi ~ $ sudo modprobe gamecon_gpio_rpi map=0,0,0,0,7,7 psx_delay=14

The parameter "map=0,0,0,0,7,7" is indicated using Pad5&6 as both PlayStation joysticks.
The parameter "psx_delay=14" configures the access delay for psx pads to 14ms. The default is 10ms, but it doesn't work correctly. 14ms is more save for me. 
After modprobe, there should be two joystick devices in /dev/input/jsX. (X indicated 0 & 1.) Then use the command to test:
pi@raspberrypi ~ $ jstest /dev/input/js0 

Driver version is 2.1.0.
Joystick (PSX controller) has 6 axes (X, Y, Rx, Ry, Hat0X, Hat0Y)
and 12 buttons (BtnX, BtnY, BtnTL, BtnTR, BtnTR2, BtnSelect, BtnStart, BtnMode, BtnThumbL, BtnThumbR, ?, ?).
Testing ... (interrupt to exit)
Axes:  0:-32767  1:-32767  2:-32767  3:-32767  4:-32767  5:-32767 Buttons:  0:off  1:off  2:off  3:off  4:off  5:off  6:off  7:off  8:off  9:off 10:off 11:off 


If all buttons and axis work well, congratulation that the joysticks setup correctly on RPi!!

There will be another article soon to share how to setup the emulators on RPi.

編譯Nethack for Android筆記


一直以來都對NETHACK很有興趣,發現有ANDROID的版本時,更是好奇如何將以C語言寫成的程式移植到以JAVA為主的ANDROID平台。
關於移植上的重點,我想應該非JNI莫屬,JNI提供一個界面,提供由NDK編譯出Shared Object有機會可以直接在ANDROID APP中使用。
這次的目的是要紀錄如何成功編譯NETHACK for ANDROID。

專案的網站為

因此先將原始碼checkout下來:
svn checkout http://nethack-android.googlecode.com/svn/trunk/ nethack-android-read-only

直接閱讀編譯ANDROID版本的README
vim ./nethack-3.4.3/sys/android/README

以下節錄編譯時的重要步驟:
1. Unpack the source tree somewhere.
# 這部份已經完成了

2. Open 'sys/android/setup.sh' in a text editor, and change ANDROID_NDK_DIR to indicate the path where you have installed the Android NDK.
# 由於需要用到c/c++ compiler編譯nehack的原始碼,在這裡要設定NDK的目錄
ANDROID_NDK_DIR=/opt/android-ndk-r8b

3. Verify options in 'include/config.h' and elsewhere, if you want anything different than the standard configuration.
# 在這裡基本上不需要特別針對config.h做特別的修改,文件中雖然#define ANDROID是註解,但makefile中會自動加上。

4. Run Eclipse and create a new project, importing the source code from 'sys/android/NetHackApp'.
# 在Eclipse中匯入APP的專案:File -> Import… -> Android -> Existing Android Code Into Workspace

5. Make 'sys/android' your current directory, and run 'setup.sh'. This should create some symbolic links and do other preparations to make your source tree ready to build the Android port.
# 在執行setup.sh前,請先在NDK的目錄中新增apps目錄:mkdir ${ANDROID_NDK_DIR}/apps
# 如果發生設定的錯誤時,可以執行sh ./setup_clean.sh && sh ./clean.sh清空產生的檔案

6. Go to the 'dat' folder, and type 'make'. This builds the data files, using tools built for the host computer.
# 照步驟作即可

7. Go to your NDK directory (as specified as ANDROID_NDK_DIR).
# 照步驟作即可

8. Type 'make APP=NetHackNative' to build the native code.
# 在編譯的最後要進行shared library的link時,遇到以下錯誤:
SharedLibrary  : libnethack.so
./out/apps/NetHackNative/armeabi/objs/nethack/__/sys/share/unixtty.o: In function `init_linux_cons':
/Users/leeweilun/Desktop/VBShare/src/android_app_src/nethack-android-read-only/nethack-3.4.3/sys/android/NetHackNative/nethack/../sys/share/unixtty.c:429: undefined reference to `has_colors'
collect2: ld returned 1 exit status
make: *** [out/apps/NetHackNative/armeabi/libnethack.so] Error 1
# 發現是在./sys/shared/unixtty.c中,有用到curses library的has_colors(),
# 我自己的解法是在unixtty.c中加入#undef TEXTCOLOR,使得這段code不會被編譯到
# 重新在執行一次make就成功了,也會將libnethack.so複製到APP的project中。

9. Go back to Eclipse, build and launch the application.
# 接下來應該可以正常編譯並將APP(APK)安裝到Android系統上了。

以上就是這次編譯的過程。