Bash script to control Powered Up motor

Ben Hughes asked for the code for the demo video “LEGO MINDSTORMS EV3 controlling Powered UP motor” so here it is:

#!/usr/bin/env bash

event_left='*code 105 (KEY_LEFT), value 1*'
event_right='*code 106 (KEY_RIGHT), value 1*'
event_down='*code 108 (KEY_DOWN), value 1*'
event_up='*code 103 (KEY_UP), value 1*'
event_enter='*code 28 (KEY_ENTER), value 1*'

function finish {
    # terminate all subshells
    kill 0
    # reset all motors
    gatttool -i hci0  -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 0800810011510000
    gatttool -i hci0  -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 0800810111510000
}
trap finish EXIT



{
    while :
    do
        gatttool -i hci0  -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 0800810011510000
        sleep 0.35
    done
}&


while :
do
    evtest '/dev/input/event1' | while read line
    do
#        echo $line
        case $line in
            $event_left)
                echo "LEFT"
                gatttool -i hci0 -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 0800810011510060
                sleep 0.1
                ;;
            $event_right)
                echo "RIGHT"
                gatttool -i hci0 -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 08008100115100A0
                sleep 0.1
                ;;
           $event_down)
                echo "DOWN"
                gatttool -i hci0 -b 90:84:2B:06:AB:5D --char-write-req --handle 0x0e --value 0800810011510000
                sleep 0.1
                ;;
           $event_up)
                echo "UP"
                ;;
           $event_enter)
                echo "ENTER"
                ;;
        esac
    done
done