Skip to content

Instantly share code, notes, and snippets.

@DaneGardner
Created May 27, 2017 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DaneGardner/a3ff6124e1ffaac34a6823b3d5235a26 to your computer and use it in GitHub Desktop.
Save DaneGardner/a3ff6124e1ffaac34a6823b3d5235a26 to your computer and use it in GitHub Desktop.
Automatic connecting to Bluetooth joystick for Raspberry Pi
#!/bin/bash
###
### NOTE: add cron job that runs every minute using `crontab -e`:
### * * * * * ~/joystick-connect.sh
###
# using a device ID is more secure, but using \t uses first available device
DEVICE='\t'
#DEVICE='XX:XX:XX:XX:XX:XX'
JS='/dev/input/js0'
BUTTON_MAP="8,0,1,2,5,9,10,16,17,16,315,304,305,314,307,308,310,311,316,317,318,319,306,309,312,313"
BUTTON_CAL="8,1,0,127,127,-4194176,-4227201,1,0,127,127,4227201,4194176,1,0,127,127,2147483647,2147483647,1,0,127,127,2147483647,2147483647,1,0,0,0,2147483647,2147483647,1,0,0,0,2147483647,2147483647,1,0,0,0,2147483647,2147483647,1,0,0,0,2147483647,2147483647"
# if the joystick isn't connected, try to connect it
if [ ! -e ${JS} ]; then
echo -e "power on\nconnect ${DEVICE}\nquit\n" | bluetoothctl
# wait for joystick driver to kick in before next step
sleep 10
fi
# if the joystick is connected, ensure we're using the right calibration settings
if [ -e ${JS} ]; then
jscal -q ${JS} | grep "${BUTTON_MAP}" &>/dev/null
if [ $? -ne 0 ]; then
echo "calibrating ${JS}"
jscal -u ${BUTTON_MAP} ${JS}
jscal -s ${BUTTON_CAL} ${JS}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment