From d1981f84f6f7d524539428e287fddd39a76de0a7 Mon Sep 17 00:00:00 2001 From: Aaron Angert Date: Wed, 1 Feb 2017 07:20:49 +0000 Subject: [PATCH] Add support for more than 8 controllers One main problem with not being able to use more than 8 controllers at once was the fact that each adapter can only handle about 6-7 controllers. The move.pair() function seemed to always just pair controllers to the default adapter, thus filling up a single hci device in /var/lib/bluetooth This change loops through all of the connected adapters as seen by hciconfig, and pairs the move controller with the adapter with the smallest connected controller count using the move.pair_custom() function. Thus allowing for an even spread among the different bluetooth adapters. also removed the dev in scan_enable, since hciconfig dev, doesn't seem to list out all of the connected adapters, while hciconfig by itself does --- oust.py | 22 +++++++++++++++++++++- scan_enable.sh | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/oust.py b/oust.py index 339e0b7..2a68d01 100644 --- a/oust.py +++ b/oust.py @@ -3,6 +3,7 @@ import time import psmove import subprocess +import os from collections import defaultdict def disconnect_move(move): @@ -53,6 +54,25 @@ def sleep_controllers(sleep=0.5, leds=(255,255,255), rumble=0, moves=[]): othermove.update_leds() +#returns the adapter with the fewest connections +#this allows for an even spread of controllers to adapters +def get_smallest_adapter(): + BT_PATH = '/var/lib/bluetooth/' + hci_addresses = os.popen("hciconfig | grep Address | awk '{print $3}'").read().split('\n')[:-1] + hci_device_count = {} + for hci in hci_addresses: + device_path = os.path.join(BT_PATH, hci) + if os.path.exists(device_path): + hci_device_count[hci] = len([con for con in os.listdir(device_path) if ':' in con]) + if (hci_device_count): + smallest_hci = hci_addresses[0] + smallest_count = hci_device_count[smallest_hci] + for hci, count in hci_device_count.items(): + if (count < smallest_count): + smallest_hci = hci + smallest_count = count + return smallest_hci + paired_controllers = [] controllers_alive = {} usb_paired_controllers = [] @@ -73,7 +93,7 @@ def sleep_controllers(sleep=0.5, leds=(255,255,255), rumble=0, moves=[]): if move.connection_type == psmove.Conn_USB: #make sure the serial is not None. this might happen if you pull the usb while still in this loop if move.get_serial() not in usb_paired_controllers and move.get_serial() != None: - move.pair() + move.pair_custom(get_smallest_adapter()) usb_paired_controllers.append(move.get_serial()) print(move.get_serial() + " connected over USB") move.set_leds(255,255,255) diff --git a/scan_enable.sh b/scan_enable.sh index e997543..e8fb507 100755 --- a/scan_enable.sh +++ b/scan_enable.sh @@ -4,7 +4,7 @@ set -eo pipefail echo "Watching hci devices..." while true; do - devices=$(hciconfig dev | grep hci | awk '{print $1}' | sed -e 's/://') + devices=$(hciconfig | grep hci | awk '{print $1}' | sed -e 's/://') for device in $devices do