Idea: Bluetooth Controller Setup
-
Hi everyone,
I've recently purchased a Raspberry Pi 3 Model B to put Retropie (4.1 I think it is) on it as a retro gaming console.
Also I tried to set up two Bluetooth controllers, one of which is a Sony DS4 controller. I found out that the DS4 controller is somewhat special, as there is a ds4 driver required for it and then an additional joypad input device device is created for it. Unfortunately when starting up the PI and using two controllers via bluetooth, the RetroArch (auto) configuration completely breaks, as it highly depends on whether if one, the other or both controllers are enabled, and the order in which they are turned on after starting up the pi also messes with the controller configuration (as the joypad index always changes, depending on the situation).
Since I got the Pi to give it away, I had to find a solution that "always" works, which I came pretty close to with a few drawbacks.
(I'll post a download link to the script below the explanation of how it works).
So my solution is: Check, which Bluetooth devices are connected to the PI and get the order in which they would show up. Switch out the RetroArch config, whenever something changes (or at least five seconds after that).
For this purpose I prepared a few retroarch.cfg files. In my case there are 5 config files with a very distinctive naming scheme:
- No bluetooth device connected: retroarch.cfg.keyboard.config
- BT Controller A connected only: retroarch.cfg.XY_YZ_ZA_YX_ZY_ZX.config
- BT Controller B connected only: retroarch.cfg.AB_BC_CA_BA_CB_AC.config
- Both connected: retroarch.cfg.XY_YZ_ZA_YX_ZY_ZX-AB_BC_CA_BA_CB_AC.config
- Both connected (swapped): retroarch.cfg.AB_BC_CA_BA_CB_AC-XY_YZ_ZA_YX_ZY_ZX.config
The AB_BC_CA_BA_CB_AC and XY_YZ_ZA_YX_ZY_ZX represent the MAC-Addresses of the controllers, with underscores instead of colons inbetween.
As you can see, there are two "both connected" configs. The reason for that is, that the script sets the "Player 2" device as "Player 1" when "Player 1" device is turned off and is turned on again (it will then be new "Player 2").
I call this behavior "controller rotation" and I think it's a neat way of switching roles if everyone brings his/her "own" joypad along.Okay, now how is it possible to switch configs on the fly?
Well, I utilize the crontab for this purpose. I set up a few entries to be loaded every minute, except that every row waits longer until it executes the "rotation" script.
The script itself saves the current controller rotation status into an own file to keep track, of which controller was first and which was second player.Cool thing about this method is, that you can't really break anything in terms of config, as it self repairs every time, whenever you just turn off the devices and wait for five seconds.
Drawback with this method (at this current state): Changing the config is a bit complicated, as you probably have to do the changes in multiple, if not all of the config files, that I posted above.
Another problem with that is, that I'm not sure, how it behaves, if other bluetooth devices are connected, that are not controllers.
I haven't tested out using more than two bluetooth controllers with it, but I don't think the Pi can handle that many bluetooth controllers at the same time without lag, anyway.IMPORTANT: Before you install the script, back up your retroarch.cfg file in the config directory (/opt/retropie/configs/all/). Also you should read the full setup guide below, first.
Download link: https://download.cloudatcost.com/download/2u1dqfg1ax2o8is19kxq7rjjj
Please also keep in mind: In the current build of the script the order of which controller is connected first, right after booting up the PI still does matter! This is something, I plan on improving in the future (along with some other things).
My plan is to make this script to work with the original retroarch.cfg in the future without the need to switch it out (I also already know how, but I need to figure out, how the retropie sets controller indices.How to install?
- Back up your retroarch.cfg (even if you didn't configure it, yet)!
- Extract the contents of the zip folder into the pi home folder.
- Add +x permission to the "controller-autoconfig.sh" file
- Don't execute the script, yet!
- Pair your bluetooth controllers with the PI
- Turn off the bluetooth devices, Restart your PI and leave the bluetooth devices off!
- Set up your retroarch.cfg config, so it works with your keyboard (and/or your wired controllers)
- copy the "retroarch.cfg" file to "/opt/retropie/configs/all/controller-autoconfig.data/retroarch.cfg.keyboard.config"
- make sure to use the correct MAC-Address for each controller and replace the colon with an underscore, instead of the "AB_BC_..." and "XY_YZ_..." shown above.
- Turn on your first bluetooth controller and wait for it to connect.
- Configure the controller in RetroArch and save the config.
- Copy the config into the "controller-autoconfig.data" folder and rename it "retroarch.cfg.<CONTROLLER_MAC_ADDRESS>.config" (replace colons with underscores)
- Turn off the first controller and turn the second one on.
- Do the same as for the first (configure, save and copy to the correct place)
Now the controllers are configured and should work on their own with the script (which is not active, yet).
Set up both controllers to work simultaneously:
- Restart your PI.
- Enable the first controller, wait for it to connect and then enable the second one.
- Execute the script now and see, what is written into the newly created file: "/home/pi/controller_rotation.log"
- In it you will find the MAC-Addresses of the two connected controllers. It shows the "turned on" order. With that information you will know, which file to create next within the "/opt/retropie/configs/all/controller-autoconfig.data/" directory.
- To make things easier, go ahead and copy the .config file which contains the first MAC-Address and add the second MAC address from the "controller_rotation.log" into the name, devided by a dash. It should be named like this: retroarch.cfg.<MAC1>-<MAC2>.config
- Make sure to use "_" instead of ":" in the names when entering MAC-Addresses.
- Configure the files to the buttons of the second controller. Also make sure to set the correct joypad_index lines for the two players correctly.
- Now copy the just created file and swap the MAC-Addresses in the file name.
- Edit the file and create the button mappings there.
- To try out if everything is working, just run the script (with sudo priviliges).
Okay the final step is to set up crontab.
I've included my modified crontab file, but only the last 12 lines are required for the script to be executed every 5 seconds. See "crontab-mod" in the zip file.- Open the file /etc/crontab with sudo priviliges and add those lines
Okay, finally done.
As you may find, this is VERY complicated right now (at leas the setup).
My plans for the future are:- Simplifying the first installation (to become almost "plug 'n play" like)
- Make changes easier by working with the original retroarch.cfg file
- Getting rid of all of those config files, using the retroarch autoconfigs.
- Getting joypad indices right, so it doesn't matter which controller is turned on first after reboot
- Addressing bluetooth devices by name instead of MAC-Addresses
- Being able to distinguish between bluetooth controllers and other bluetooth hardware, and only taking bt. controllers into account
- Make the script do the right thing without bothering about which bluetooth device is connected, first, after booting up the Pi.
If you have any questions, feedback or any suggestions, or if you need help with this, just contact me.
Greetings,
Noxoreos -
@Noxoreos I've been crawling through the forums for the past few days looking for something like this. Thank you!
-
@Noxoreos Quick question regarding this instruction...
copy the "retroarch.cfg" file to "/opt/retropie/configs/all/controller-autoconfig.data/retroarch.cfg.keyboard.config"
Is controller-autoconfig.data a directory? -
@Noxoreos, never mind on that question regarding the directory (it just looked weird to me; I've never seen a folder with a ".data" at the end). Another question, however, whenever I try and run the shell script, I get an error about not having the permission to access data in the /var/lib/bluetooth/ directory. Any idea what's going on there? and how I might fix it?
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.