Skip to content
This repository was archived by the owner on Dec 14, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion minidsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = constants;
33 changes: 20 additions & 13 deletions src/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
});
}
Expand Down Expand Up @@ -126,24 +126,31 @@ 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');
}
}
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) {
Expand Down