|
var serialport = require('serialport'); |
|
|
|
var port = new serialport.SerialPort( |
|
'COM6', { |
|
baudrate: 9600, |
|
parser: serialport.parsers.readline('\r\n') |
|
} |
|
); |
|
|
|
var html = require('fs').readFileSync('index.html'); |
|
var http = require('http').createServer(function(req, res) { |
|
res.writeHead(200, { |
|
'Content-Type': 'text/html' |
|
}); |
|
res.end(html); |
|
}); |
|
|
|
var io = require('socket.io')(http); |
|
http.listen(3000); |
|
|
|
var eqmode = 0; |
|
|
|
port.on('open', function () { |
|
SelectPlayerDevice(0x02); |
|
SetVolume(1); |
|
SetEQ(0); |
|
|
|
io.on('connection', function(socket) { |
|
socket.on('msg', function(data) { |
|
console.log(data); |
|
if(data == "play"){ |
|
SpecifyMusicPlay(1); |
|
} |
|
if(data == "pause"){ |
|
PlayPause(); |
|
} |
|
if(data == "resume"){ |
|
PlayResume(); |
|
} |
|
if(data == "prev"){ |
|
PlayPrevious(); |
|
} |
|
if(data == "next"){ |
|
PlayNext(); |
|
} |
|
if(data == "loop"){ |
|
PlayLoop(); |
|
} |
|
if(data == "down"){ |
|
DecreaseVolume(); |
|
} |
|
if(data == "up"){ |
|
IncreaseVolume(); |
|
} |
|
if(data == "repon"){ |
|
StartRepeat(); |
|
} |
|
if(data == "repoff"){ |
|
StopRepeat(); |
|
} |
|
if(data == "eq"){ |
|
eqmode ++; |
|
if (eqmode > 5) eqmode = 0; |
|
SetEQ(eqmode); |
|
} |
|
|
|
}); |
|
}); |
|
|
|
}); |
|
|
|
function SelectPlayerDevice(device) { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x09,0x00,0,device,0xEF]); |
|
setTimeout(port.write(buf),100); |
|
} |
|
|
|
function SpecifyMusicPlay(index) { |
|
var hbyte = parseInt(index / 256); |
|
var lbyte = parseInt(index % 256); |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x03,0x00,hbyte,lbyte,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function PlayPause() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x0E,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function PlayResume() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x0D,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function PlayNext() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x01,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function PlayPrevious() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x02,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function PlayLoop() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x11,0x00,0x00,0x01,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function SetVolume(volume) { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x06,0x00,0x00,volume,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function IncreaseVolume() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x04,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function DecreaseVolume() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x05,0x00,0x00,0x00,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function StartRepeat() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x19,0x00,0x00,0x00,0xFE,0xE2,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function StopRepeat() { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x19,0x00,0x00,0x01,0xFE,0xE1,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |
|
|
|
function SetEQ(mode) { |
|
var buf = new Buffer([0x7E,0xFF,0x06,0x07,0x00,0x00,mode,0xEF]); |
|
setTimeout(port.write(buf),1000); |
|
} |