Volume control using a wheel mouse

I always enjoy listening to music with Volumio.
One thing I miss with Volumio was a hardware volume control like a radio. It is OK to change volume using a mobile phone, but it is much easier if I can change the volume with a knob.
Though there are many articles about changing volume with GPIO switches or encoders, the most familiar knob would be a wheel mouse.

As I am a novice programmer, your comments will be really appreciated.

For your information, I am using Raspberry pi 3B+ with Volumio 2.565 .
An USB DAC and a wheel mouse are connected to my Raspberry pi.

Specification:

  • Rotate the wheel to change the volume
  • Click the left button to select “Capital UK” (web radio) and the right button to select “Venice classic radio”.

Precedures:

  1. Prepare following “click.js”
    This code is also available at the following site as “index.js” .
    github.com/tullyz/mouse-volume


var Mouse = require('node-mouse');
var mouseDevice = new Mouse();

var InputEvent = require('input-event');

// find # of mouse event
const execSync = require('child_process').execSync;
const result =  execSync('cat /proc/bus/input/devices | grep mouse0').toString();
n = result.indexOf("event")
var a = result.substring(n+5, n+6)
var input = new InputEvent('/dev/input/event'+a);

var eventDevice = new InputEvent.Mouse(input);

var io=require('socket.io-client');
var socket= io.connect('http://localhost:3000');

// set initial volume value
var vol = 10 ;  
socket.emit('volume', vol);


// Create play lists if empty
socket.emit('listPlaylist');
socket.on('pushListPlaylist', function(data) {
   if (data.length == 0) {

    socket.emit('createPlaylist',{"name":"Venice Classic Radio"});
    socket.emit('addToPlaylist',{
	"name":"Venice Classic Radio", 
	"service":"webradio", 
	"uri":"http://174.36.206.197:8000"
	});
    socket.emit('createPlaylist',{"name":"Capital FM"});
    socket.emit('addToPlaylist',{
	"name":"Capital FM", 
	"service":"webradio", 
	"uri":"http://media-ice.musicradio.com/CapitalMP3"
	});

    }
});

var playstate = 1 ;

mouseDevice.on('click', function(e){
  if (e.button === 0) {
//    console.log("left click");
    socket.emit('playPlaylist',{name:"Venice Classic Radio"});
  }
  if (e.button === 2) {
//    console.log("right click");
    socket.emit('playPlaylist',{name:"Capital FM"});
  }
  if (e.button === 1) {
//    console.log("wheel click");
//    socket.emit('prev');
//    socket.emit('next');
  if (playstate == 1) {
	socket.emit('pause');
	playstate = 0
	}
  else {
	socket.emit('play');
	playstate = 1 
	}
  }
});

// volume setting    
eventDevice.on('wheel', e => {
  var direction = e.value === 1 ? 1 : -1
// get current volume value
 socket.emit('getState', '');
 socket.on('pushState', function(data) {
   vol = data.volume ;  
 });
  vol = vol + direction ;
// volume limitter
  if (vol < 0) vol = 0 ;
  if (vol > 50) vol = 50 ;
  socket.emit('volume', vol);
})

  1. Login to Volumio and install the following node modules.
sudo su
npm install node-mouse
npm install socket.io-client
npm install input-event
npm install -g forever
  1. Copy “click.js” to
/home/volumio
  1. Run
/lib/node_modules/forever/bin/forever start /home/volumio/click.js

If you turn the wheel, the display of Volumio will be linked, and if you click a button, the channel should be switched.

  1. If it is OK, write a node command in /etc/rc.local to run “click.js” automatically at restart.
nano /etc/rc.local

Paste the following before “exit 0”.

/lib/node_modules/forever/bin/forever start /home/volumio/click.js

When you reboot, “click.js” should run.

Due to the recent update of socket.io-client, please install the module as follows:

sudo npm install socket.io-client@2.3.1