How to create a Controller Map on Raspberry Pi
I’ve been working on my 2nd eBook and adding a GamePad is a thing I need to explain. You can see them in earlier blog posts:
But I want readers of the book to be able to generate their own controller maps. I cannot assume that everyone has access to Windows.
What I didn’t know back then is that SDL comes with a game controller map generator application called (subtle this!) controllermap that you can run on your Pi or Linux computer.
I had to use the File Manager Find file facility to locate it. It’s in the installed-tests folder for SDL2 or more precisely /usr/lib/aarch64-linux-gnu/installed-tests/SDL2 on my Raspberry Pi running 64-bit Raspberry Pi OS.
On my VM Ubuntu I found it in /usr/lib/x86-64-linux-gnu/installed-tests/SDL2
I did a bit of experimenting with a clean Raspberry Pi OS setup and then I installed libsdl2-dev and saw the installed-tests/SDL2 folder installed, so that’s what you need. So install the dev files as per New tutorial on installing SDL on Linux.
Plug in a gamepad and on your Pi, cd into that folder (the Pi one not the Ubuntu one!) and in a terminal run ./controllermap
It will open with your B button highlighted. Press your B button. In the picture above it’s the circle on the right hand side that is the lowest of the four or the yellow button in this picture of my controller.
It will then go through every possible button. Just press the corresponding button or space key if you haven’t got that. Compared to the main picture, mine lacks the two bottom joysticks, the buttons between the Stop and Start buttons and the two trigger buttons at the top. For each of those just press the space bar. Eventually it will loop back to the B button. Just press it and it will finish and output the gamepad control map.
It outputs two copies of this. You can send one to a text file in your home folder by doing
./controllermap >~/gamepad.txt
but you will have to press all the buttons etc. again. This is what my Pi gave.
030000001008000001e5000010010000,usb gamepad,platform:Linux,a:b2,b:b1,x:b3,y:b0,back:b8,start:b9,leftshoulder:b4,rightshoulder:b5,leftx:a0,lefty:a1,
And that is what you use in SDL2 code.