From ed03bc692e1786c6bc2af6a58c8a758c56164172 Mon Sep 17 00:00:00 2001 From: markubiak Date: Thu, 18 May 2017 15:39:43 -0400 Subject: [PATCH] Added getSource(), fixed _getMasterStatus(), Constants cleanup --- minidsp.js | 2 +- src/constants.js | 16 ++++++++++++---- src/device.js | 33 ++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/minidsp.js b/minidsp.js index 52e1347..e571f26 100755 --- a/minidsp.js +++ b/minidsp.js @@ -71,7 +71,7 @@ program .description('Set input source [analog|toslink|usb]') .action((source) => { let dsp = device(); - actions.push(dsp.setInput(source)); + actions.push(dsp.setSource(source)); }); program diff --git a/src/constants.js b/src/constants.js index e5f55bd..21f428b 100644 --- a/src/constants.js +++ b/src/constants.js @@ -3,9 +3,17 @@ const constants = { USB_VID: 0x2752, USB_PID: 0x0011, - INPUT_ANALOG: 0, - INPUT_TOSLINK: 1, - INPUT_USB: 2 + SOURCE_INDEX: { + 'analog': 0, + 'toslink': 1, + 'usb': 2 + }, + + SOURCE_NAME: { + 0: 'analog', + 1: 'toslink', + 2: 'usb' + } }; -module.exports = constants; \ No newline at end of file +module.exports = constants; diff --git a/src/device.js b/src/device.js index 1ec95e6..05e5229 100644 --- a/src/device.js +++ b/src/device.js @@ -66,14 +66,14 @@ class Device { _getMasterStatus() { return this.sendCommand([ 0x05, 0xFF, 0xDA, 0x02 ]).then((data) => { // Expecting response: 05 ff da 00 00 where - if (data.slice(1, 3).compare(Buffer.from([ 0x05, 0xFF, 0xDA ])) !== 0) { + if (data.slice(1, 4).compare(Buffer.from([ 0x05, 0xFF, 0xDA ])) !== 0) { throw new Error('Unexpected response ' + data); } // Convert back to dB return { - volume: -2 * data.readUInt8(3), - mute: !!data.readUInt8(4) + volume: -0.5 * data.readUInt8(4), + mute: !!data.readUInt8(5) }; }); } @@ -126,17 +126,10 @@ class Device { * TOSLink: 1 * USB: 2 */ - setInput(value) { - debug('setInput', value); + setSource(value) { + debug('setSource', value); if (typeof value === 'string') { - const inputs = { - analog: Constants.INPUT_ANALOG, - toslink: Constants.INPUT_TOSLINK, - usb: Constants.INPUT_USB - }; - - value = inputs[value.toLowerCase()]; - + value = Constants.SOURCE_INDEX[value.toLowerCase()]; if (typeof value === 'undefined') { throw new Error('No such input'); } @@ -144,6 +137,20 @@ class Device { return this.sendCommand([ 0x34, value ]); } + getSource() { + return this.sendCommand([ 0x05, 0xFF, 0xD9, 0x01 ]).then((data) => { + // Expecting response: 05 ff d9 00 where + if (data.slice(1, 4).compare(Buffer.from([ 0x05, 0xFF, 0xD9])) !== 0) { + throw new Error('Unexpected response ' + data); + } + var value = Constants.SOURCE_NAME[data.readUInt8(4)]; + if (typeof value === 'undefined') { + throw new Error('No such input'); + } + return value; + }); + } + getInputLevels() { return this.sendCommand([ 0x14, 0x00, 0x44, 0x02 ]).then((data) => { if (data.slice(1, 4).compare(Buffer.from([ 0x14, 0x00, 0x44 ])) !== 0) {