Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 6 of 6
  1. #1

    Volume Knob - mouse scroll hack

    Here is a shell script that takes input from the mouse scroll wheel to change the volume of the named local player. I wrote it with a view to hacking a Poundland mouse plus rotary encoder to put a real volume knob on my LMS / Squeezebox computer. I would welcome any comments or suggestions.
    Code:
    #!/bin/bash
    
    #use command - xinput - to find id of mouse who's scroll wheel you want to use
    mouseID=10 
    player='Dining Room'
    IP="127.0.0.1 9090"
    
    
    
    
      #Need to convert player name to mac address
      #find number of players
     
      nplayers=$(echo "player count ?" | nc -q1 $IP | tail -c 2 )
    
    
      n=0
    	#loop through players to determine mac address of player
    
    	while [ $n -lt $nplayers ]
    	do
    		pmac=$(echo "player id $n ?" | nc -q1 $IP | tail -c 28 | sed -r 's/%3A/:/g' )
    		pname=$(echo "player name $n ?" | nc -q1 $IP | cut -c 15- | sed -r 's/%20/ /g')
    			if [ "$pname" = "$player" ]; then
    				playerID=$pmac
    				echo
    				echo "$pname"   "$pmac"
    			fi
    		n=$(($n + 1))
    	done
    
    echo
    OldScroll=$(xinput --query-state $mouseID | grep 'valuator\[2\]\=' | cut -c 14-)
    echo "Initial wheel position: $OldScroll"
    playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 3 | tr -dc '0-9')
    echo "Initial Volume: $playerVOL"
    echo
    
    # Infinite loop checks to see if mouse scroll wheel has turned and in which direction - Ctrl C to quit
    
    while :
    do
    
    scroll=$(xinput --query-state $mouseID | grep 'valuator\[2\]\=' | cut -c 14-)
    
    if [ $OldScroll -gt $scroll ]; then
    	playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 3 | tr -dc '0-9')
    	setVOL=`expr $playerVOL + 5`
    	printf "$playerID mixer volume $setVOL\nexit\n" | nc $IP
    fi
    
    if [ $scroll -gt $OldScroll ]; then
    	playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 3 | tr -dc '0-9')
    	setVOL=`expr $playerVOL - 5`
    	printf  "$playerID mixer volume $setVOL\nexit\n" | nc $IP
    fi
      
    OldScroll=$scroll  
    sleep 0.2
    
    done
    Last edited by Wiredcharlie; 2020-01-05 at 13:08.

  2. #2
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    963
    Quote Originally Posted by Wiredcharlie View Post
    Here is a shell script that takes input from the mouse scroll wheel to change the volume of the named local player. I wrote it with a view to hacking a Poundland mouse plus rotary encoder to put a real volume knob on my LMS / Squeezebox computer. I would welcome any comments or suggestions.
    This might be of interest.
    Various SW: Web Interface | Playlist Editor / Generator | Music Classification | Similar Music | Announce | EventTrigger | LMSlib2go | ...
    Various HowTos: build a self-contained LMS | Bluetooth/ALSA | Control LMS with any device | ...

  3. #3
    Quote Originally Posted by Roland0 View Post
    This might be of interest.
    From what I can see that is button/keypress only. The point (for me) is to have the rotation of a traditional volume knob that properly interfaces with the LMS volume setting. Most of the Human Interface Device implementations I could find used a programmed microcontroller to provide a USB interface. I figured that looked like hard work when a mouse scroll wheel already does that! Then I stumbled across this https://19max63.wordpress.com/2016/0...-knob-for-sdr/
    where someone had connected a standard rotary encoder to a mouse PCB.

    My server is a headless MiniATX box which also provides a local player output, so I figured if I added a volume knob on the front it would look like a piece of HiFi!

    Tony
    Last edited by Wiredcharlie; 2019-11-17 at 02:11.

  4. #4
    Senior Member
    Join Date
    Aug 2012
    Location
    Austria
    Posts
    963
    Quote Originally Posted by Wiredcharlie View Post
    From what I can see that is button/keypress only.
    It uses python-evdev, which is a interface to evdev and reports all device events (X uses evdev as well btw). Haven't tested it with inputexec, but this works:

    Code:
    #!/usr/bin/env python3
    import evdev
    device = evdev.InputDevice('/dev/input/event10')
    print(device)
    for event in device.read_loop():
    	if event.code == evdev.ecodes.REL_WHEEL:
    		print("Event: {} value: {}".format( evdev.categorize(event), event.value))
    Code:
    device /dev/input/event10, name "Logitech USB Laser Mouse", phys "usb-0000:00:1d.0-1.6/input0"
    Event: relative axis event at 1574017671.107975, REL_WHEEL  value: -1
    Event: relative axis event at 1574017672.227968, REL_WHEEL  value: 1
    so you could just add a line to send something (e.g. with requests) to LMS in that if clause

    Anyway, I've switched to triggerhappy in the meantime, which will also work with the wheel:
    Code:
    $  cat ../THD.conf
    REL_WHEEL	1	echo "wheel up" >> /tmp/thd.log
    REL_WHEEL	-1	echo "wheel down" >> /tmp/thd.log
    Code:
    ./thd --triggers ../THD.conf /dev/input/event10
    Executing trigger action: echo "wheel down" >> /tmp/thd.log
    Executing trigger action: echo "wheel up" >> /tmp/thd.log
    Various SW: Web Interface | Playlist Editor / Generator | Music Classification | Similar Music | Announce | EventTrigger | LMSlib2go | ...
    Various HowTos: build a self-contained LMS | Bluetooth/ALSA | Control LMS with any device | ...

  5. #5


    Code:
    #!/bin/bash
    
    #use command - xinput - to find id of mouse who's scroll wheel you want to use
    mouseID=" USB OPTICAL MOUSE"
    player="Dining Room"
    IP="127.0.0.1 9090"
    #Button inactive after press registered - tenths of sec
    locktime=10
    #Press Commands
    Long="playlist play Radio2"
    Short="playlist play Radio4"
    Double="playlist play RadioCornwall"
    
      #Need to convert player name to mac address
      #find number of players
     
      nplayers=$(echo "player count ?" | nc -q1 $IP | tail -c 2 )
    
    
      n=0
    	#loop through players to determine mac address of player
    
    	while [ $n -lt $nplayers ]
    	do
    		pmac=$(printf "player id $n ?\nexit\n" | nc $IP | tail -c 28 | sed -r 's/%3A/:/g' )
    		pname=$(printf "player name $n ?\nexit\n" | nc $IP | cut -c 15- | sed -r 's/%20/ /g')
    		pmode=$(printf "$pmac mode ?\nexit\n" | nc $IP | cut -c 34-37)
    			if [ "$pname" = "$player" ]; then
    				playerID=$pmac
    				echo
    				echo "$pname"   "$pmac"   "$pmode"
    			fi
    		n=$(($n + 1))
    	done
    
    echo
    
    MouseState=$(xinput --query-state "$mouseID")
    OldScroll="${MouseState#*valuator\[2\]\=}"
    scroll=$OldScroll
    echo "Initial scroll wheel position: $OldScroll"
    playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 3 | tr -dc '0-9')
    echo "Initial Volume: $playerVOL"
    echo
    pressed=0
    loop=0
    lockout=0
    # Infinite loop checks to see if mouse scroll wheel has turned and in which direction - Ctrl C to quit
    
    while :
    do
    loop=$((loop + 1))
    lockout=$((lockout - 1))
    if [ $lockout -eq 0 ]; then
    	echo Unlocked
    fi
    
    MouseState=$(xinput --query-state "$mouseID")
    scroll="${MouseState#*valuator\[2\]\=}"
    playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 4 | tr -dc '0-9')
    
    #Long press Playlist
    #Short pressPause
    
    #echo                $loop 
    #echo press: $pressed 
    #echo lockout: $lockout
    
    if [ $lockout -lt 0 ]; then
    	if [[ $MouseState == *"down"* ]]; then		
    	    pressed=$((pressed + 1))
    		if [[ $pressed -eq 1 ]]; then
    			loop=1
    		fi
    		if [[ $pressed -gt 1 ]] && [[ $pressed -ne $loop ]]; then		
    					pressed=0
    					loop=0
    					lockout=$locktime
    				if [ $loop -lt 5 ]; then
    					printf "$playerID $Double\nexit\n" | nc $IP
    					echo Double Press
    				fi
    		fi
    
    		
    		if [[ $pressed -gt 4 ]] && [[ $pressed -eq $loop ]];then
    			printf "$playerID $Long\nexit\n" | nc $IP
    			echo Long Press
    			pressed=0
    			loop=0
    			lockout=$locktime
    		fi
    	fi
    
    
    	if [[ $loop -gt 6 ]] && [[ $pressed -lt 5 ]] && [[ $pressed -gt 0 ]]; then
    				printf "$playerID $Short\nexit\n" | nc $IP
    				echo Short Press
    				pressed=0
    				loop=0
    				lockout=$locktime 
    	fi
    fi
    
    
    
    
    #Volume
    if [ $scroll -ne $OldScroll ]; then
    	echo Read Vol: $playerVOL 	Scroll: $scroll
      if (( $scroll < $(( OldScroll - 20 )) || $scroll > $(( OldScroll + 20 )) )); then
     		echo "Scroll value implausible!"
      else  	  
    
    	if [ $scroll -gt $OldScroll ]; then
    			pmode=$(printf "$playerID mode ?\nexit\n" | nc $IP | cut -c 34-37)
    				if [[ $pmode == *"stop"* ]]; then
    					printf  "$playerID play\nexit\n" | nc $IP
    					printf  "$playerID mixer volume 10\nexit\n" | nc $IP
    					playerVOL=10
    					sleep 2
    				fi
    
    			if [ $playerVOL -lt 96 ]; then
    			if [ $((scroll - 2)) -gt $OldScroll ]; then
    				setVOL=$((playerVOL + 10))
    			else
    				setVOL=$((playerVOL + 5))
    			fi
    				printf  "$playerID mixer volume $setVOL\nexit\n" | nc $IP
    			fi
    	fi
    
    
    	if [ $scroll -lt $OldScroll ]; then
    
    		if [ $playerVOL -gt 0 ]; then
    			if [ $((scroll + 2)) -lt $OldScroll ]; then
    				setVOL=$((playerVOL - 10))
    			else
    				setVOL=$((playerVOL - 5))
    			fi
    			printf  "$playerID mixer volume $setVOL\nexit\n" | nc $IP
    		else #Power off player when volume gets to zero
    			printf  "$playerID power 0\nexit\n" | nc $IP
    			echo Volume Zero Off
    			sleep 10
    			echo unlocked
    		fi
    	fi
      fi
    fi
    
    
    OldScroll=$scroll 
    sleep 0.1
    done

  6. #6
    Improved script:

    Code:
    #!/bin/bash
    
    #use command - xinput - to find id of mouse who's scroll wheel you want to use
    mouseID=" USB OPTICAL MOUSE"
    player="Dining Room"
    IP="127.0.0.1 9090"
    #Button inactive after press registered - tenths of sec
    locktime=10
    #Ratio of mouse scroll value to volume change - sensitivity
    gear=2
    #Press Commands
    Long="playlist play Radio2"
    Short="playlist play Radio4"
    Double="playlist play RadioCornwall"
    
      #Need to convert player name to mac address
      #find number of players
     
      nplayers=$(echo "player count ?" | nc -q1 $IP | tail -c 2 )
    
    
      n=0
    	#loop through players to determine mac address of player
    
    	while [ $n -lt $nplayers ]
    	do
    		pmac=$(printf "player id $n ?\nexit\n" | nc $IP | tail -c 28 | sed -r 's/%3A/:/g' )
    		pname=$(printf "player name $n ?\nexit\n" | nc $IP | cut -c 15- | sed -r 's/%20/ /g')
    		pmode=$(printf "$pmac mode ?\nexit\n" | nc $IP | cut -c 34-37)
    			if [ "$pname" = "$player" ]; then
    				playerID=$pmac
    				echo
    				echo "$pname"   "$pmac"   "$pmode"
    			fi
    		n=$(($n + 1))
    	done
    
    echo
    
    MouseState=$(xinput --query-state "$mouseID")
    OldScroll="${MouseState#*valuator\[2\]\=}"
    scroll=$OldScroll
    echo "Initial scroll wheel position: $OldScroll"
    playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 3 | tr -dc '0-9')
    echo "Initial Volume: $playerVOL"
    echo
    pressed=0
    loop=0
    lockout=0
    # Infinite loop checks to see if mouse scroll wheel has turned and in which direction - Ctrl C to quit
    
    while :
    do
    loop=$((loop + 1))
    lockout=$((lockout - 1))
    if [ $lockout -eq 0 ]; then
    	echo Unlocked
    fi
    
    MouseState=$(xinput --query-state "$mouseID")
    scroll="${MouseState#*valuator\[2\]\=}"
    playerVOL=$(printf "$playerID mixer volume ?\nexit\n" | nc $IP | tail -c 4 | tr -dc '0-9')
    
    #Long press Playlist
    #Short pressPause
    
    #echo                $loop 
    #echo press: $pressed 
    #echo lockout: $lockout
    
    if [ $lockout -lt 0 ]; then
    	if [[ $MouseState == *"down"* ]]; then		
    	    pressed=$((pressed + 1))
    		if [[ $pressed -eq 1 ]]; then
    			loop=1
    		fi
    		if [[ $pressed -gt 1 ]] && [[ $pressed -ne $loop ]]; then		
    					pressed=0
    					loop=0
    					lockout=$locktime
    				if [ $loop -lt 5 ]; then
    					printf "$playerID $Double\nexit\n" | nc $IP
    					echo Double Press
    				fi
    		fi
    
    		
    		if [[ $pressed -gt 4 ]] && [[ $pressed -eq $loop ]];then
    			printf "$playerID $Long\nexit\n" | nc $IP
    			echo Long Press
    			pressed=0
    			loop=0
    			lockout=$locktime
    		fi
    	fi
    
    
    	if [[ $loop -gt 6 ]] && [[ $pressed -lt 5 ]] && [[ $pressed -gt 0 ]]; then
    				printf "$playerID $Short\nexit\n" | nc $IP
    				echo Short Press
    				pressed=0
    				loop=0
    				lockout=$locktime 
    	fi
    fi
    
    
    
    
    #Volume
    delta=$((scroll - OldScroll))
    
    
    if [ $delta -ne 0 ]; then
    	echo Read Vol: $playerVOL 	Scroll: $scroll
      if [ $delta -lt "-25" -o $delta -gt 25 ]; then
     		echo "Scroll value implausible!"
      else  	  
    
    	if [ $delta -gt 0 ]; then
    			pmode=$(printf "$playerID mode ?\nexit\n" | nc $IP | cut -c 34-37)
    				if [[ $pmode == *"stop"* ]]; then
    					printf  "$playerID play\nexit\n" | nc $IP
    					printf  "$playerID mixer volume 10\nexit\n" | nc $IP
    					playerVOL=10
    					sleep 2
    				fi
    			if [ $playerVOL -lt 96 ]; then
    				setVOL=$((playerVOL + delta*gear))
    				printf  "$playerID mixer volume $setVOL\nexit\n" | nc $IP
    			fi
    	fi
    
    
    	if [ $delta -lt 0 ]; then
    		if [ $playerVOL -gt 0 ]; then
    			setVOL=$((playerVOL + delta*gear))
                    if [ $setVOL -lt 0 ]; then
                        setVOL=0
                    fi
    			printf  "$playerID mixer volume $setVOL\nexit\n" | nc $IP
                
                
    		    #Power off player when volume gets to zero
                if [ $setVOL -eq 0 ]; then
    			     printf  "$playerID power 0\nexit\n" | nc $IP
    			     echo Volume Zero Off
    			     sleep 10
    			     echo unlocked
                fi
    		fi
    	fi
      fi
    fi
    
    
    OldScroll=$scroll 
    sleep 0.1
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •