#!/bin/bash stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -icanon min 1 time 1 # This script depends on the program xdotool to be installed. # On Debian and probably ubuntu, it can be installed with: # sudo apt install xdotool USB_DEV="/dev/ttyUSB0" JOY_BUTTON=1 JOY_UP=1 JOY_DOWN=1 JOY_LEFT=1 JOY_RIGHT=1 PREV_BUTTON=1 PREV_UP=1 PREV_DOWN=1 PREV_LEFT=1 PREV_RIGHT=1 KEY_BUTTON=q KEY_UP=w KEY_DOWN=s KEY_LEFT=a KEY_RIGHT=d # All input in this script is read from $USB_DEV (defined above) ( while :; do read LINE # If we have received 5 chars in a line, it is assumed correct if [ ${#LINE} = 5 ]; then # Split the line up into the different directions and button JOY_BUTTON=${LINE:0:1} JOY_UP=${LINE:4:1} JOY_DOWN=${LINE:3:1} JOY_LEFT=${LINE:2:1} JOY_RIGHT=${LINE:1:1} # Only change keypress if read value is different than last time if [ $JOY_BUTTON -ne $PREV_BUTTON ]; then PREV_BUTTON=$JOY_BUTTON if [ $JOY_BUTTON = 0 ]; then xdotool keydown $KEY_BUTTON else xdotool keyup $KEY_BUTTON fi fi if [ $JOY_UP -ne $PREV_UP ]; then PREV_UP=$JOY_UP if [ $JOY_UP = 0 ]; then xdotool keydown $KEY_UP else xdotool keyup $KEY_UP fi fi if [ $JOY_DOWN -ne $PREV_DOWN ]; then PREV_DOWN=$JOY_DOWN if [ $JOY_DOWN = 0 ]; then xdotool keydown $KEY_DOWN else xdotool keyup $KEY_DOWN fi fi if [ $JOY_LEFT -ne $PREV_LEFT ]; then PREV_LEFT=$JOY_LEFT if [ $JOY_LEFT = 0 ]; then xdotool keydown $KEY_LEFT else xdotool keyup $KEY_LEFT fi fi if [ $JOY_RIGHT -ne $PREV_RIGHT ]; then PREV_RIGHT=$JOY_RIGHT if [ $JOY_RIGHT = 0 ]; then xdotool keydown $KEY_RIGHT else xdotool keyup $KEY_RIGHT fi fi fi done ) < $USB_DEV