PERIOD=5000000

getDuty() {
    echo $(((PERIOD/10000) * ${1}))
}

getStick() {
    stdbuf -i0 -o0 -e0  jstest --select /dev/input/js0 | stdbuf -i0 -o0 -e0 grep 'number 3' | stdbuf -i0 -o0 -e0 sed 's/^.*value //' | stdbuf -i0 -o0 -e0 tail -n +2
}

getStick | while read pos
do
    value=$(( (-${pos}+32767)/35 + 2320 ))
    echo pos: $pos
    echo value: $value
    echo duty: $(getDuty $value)
done

