-
Notifications
You must be signed in to change notification settings - Fork 1
/
minidsp-control.js
executable file
·244 lines (225 loc) · 6.04 KB
/
minidsp-control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /usr/bin/env node
const Device = require('minidsp/src/device');
const USBTransport = require('minidsp/src/transport/usb');
const Constants = require('minidsp/src/constants');
const fs = require('fs');
const FourteenSegment = require('ht16k33-fourteensegment-display');
const Inotify = require('inotify').Inotify;
const sources = {
'analog': "AUX",
'toslink': "OPT",
'usb': "USB"
}
let _device;
let _display = new FourteenSegment(0x70, 1);
device().transport.device.on('data', process_buffer);
var prev_source = "toslink";
var current_status = {
source: "analog",
volume: 0,
mute: false
}
var sourceTimeout;
var muteTimeout;
// Watch /tmp/MINIDSP-CONTROL for commands
// TODO: non-FS IPC
var inotify = new Inotify();
var watchTmp = {
path: '/tmp',
watch_for: Inotify.IN_CLOSE_WRITE,
callback: function(event) {
if (event.name == "MINIDSP-CONTROL") {
try {
var action = fs.readFileSync("/tmp/MINIDSP-CONTROL", "utf8").trim();
minidsp_control(action);
fs.unlinkSync("/tmp/MINIDSP-CONTROL");
} catch (err) {
console.log(err);
}
}
}
}
inotify.addWatch(watchTmp);
// Add callback to process_buffer whenever something interesting happens, then update the display for init
device().transport.device.on('data', process_buffer);
device().sendCommand([0x05, 0xFF, 0xD9, 0x03]);
function device() {
if (_device) {
return _device;
}
return _device = new Device(USBTransport);
}
function process_buffer(data) {
var status = {
volume: current_status.volume,
mute: current_status.mute,
source: current_status.source
}
var slice = data.slice(1, 4);
// Volume or mute changed
if (slice.compare(Buffer.from([0x05, 0xFF, 0xDA])) === 0) {
status.volume = -0.5 * data.readUInt8(4),
status.mute = !!data.readUInt8(5)
// Source changed
} else if (slice.compare(Buffer.from([0x05, 0xFF, 0xA9])) === 0 ||
slice.compare(Buffer.from([0x05, 0xFF, 0xD9])) === 0) {
// FULL info, called by init
if (slice.compare(Buffer.from([0x05, 0xFF, 0xD9])) === 0 &&
data.slice(0, 1).compare(Buffer.from([0x07])) === 0) {
status.volume = -0.5 * data.readUInt8(5),
status.mute = !!data.readUInt8(6)
}
status.source = Constants.SOURCE_NAME[data.readUInt8(4)];
// Volume changed by Pi
} else if (data[1] === 0x42) {
status.volume = -0.5 * data.readUInt8(2);
// Mute changed by Pi
} else if (data[1] == 0x17) {
status.mute = !!data.readUInt8(2);
} else {
console.log(data);
}
update_status(status);
}
function update_status(new_status) {
// PRIORITY:
// New source > mute > volume
// New source
if (new_status.source != current_status.source) {
var sourceStr = sources[new_status.source];
if (sourceStr) {
_display.writeString(sourceStr);
}
// In 3 seconds, if it ain't muted show the volume
if (sourceTimeout) {
clearTimeout(sourceTimeout);
}
sourceTimeout = setTimeout(function() {
if (!muteTimeout) {
_display.writeString(vol_str(current_status.volume));
}
sourceTimeout = null;
}, 3000);
// Newly muted
} else if (new_status.mute && !muteTimeout) {
mute_loop(true);
// Volume change or unmute
} else {
// Clear the mute timeout if the system is unmuted
if (!new_status.mute && muteTimeout) {
clearTimeout(muteTimeout);
muteTimeout = null;
}
// If nothing else is showing, update the volume
if (!sourceTimeout && !muteTimeout) {
_display.writeString(vol_str(new_status.volume));
}
}
current_status = new_status;
}
function vol_str(source) {
if (typeof source != 'number') {
return "";
}
if (source <= -100) {
return Math.round(source).toString();
} else if (source % 1 === 0) {
return (source.toString() + ".0");
} else {
return source.toString();
}
}
function mute_loop(show_mute) {
if (show_mute) {
if (!sourceTimeout) {
_display.writeString("MUTE");
}
muteTimeout = setTimeout(function() {mute_loop(false);}, 1500);
} else {
if (!sourceTimeout) {
_display.writeString(vol_str(current_status.volume));
}
muteTimeout = setTimeout(function() {mute_loop(true);}, 1500);
}
}
function minidsp_control(command) {
try {
switch (command.split(' ')[0]) {
case "kill":
console.log("exiting...");
if (_device) {
_device.close();
}
process.exit()
break;
case "source-previous":
if (prev_source == null) {
break;
}
if (prev_source == current_status.source) {
prev_source = "toslink";
}
console.log("switching to previous source " + prev_source);
device().setSource(prev_source);
prev_source = null;
break;
case "source-forward":
var sourcesKeys = Object.keys(sources);
var newIndex = (sourcesKeys.indexOf(current_status.source) + 1) % sourcesKeys.length;
var next_source = sourcesKeys[newIndex];
console.log("switching source to " + next_source);
prev_source = current_status.source;
device().setSource(next_source);
break;
case "source-analog":
console.log("switching source to AUX");
prev_source = current_status.source;
device().setSource("analog");
break;
case "source-toslink":
console.log("switching source to TOSLINK1");
prev_source = current_status.source;
device().setSource("toslink");
break;
case "source-usb":
console.log("switching source to USB");
prev_source = current_status.source;
device().setSource("usb");
break;
case "volume-up":
var newvol = current_status.volume + 0.5;
console.log("setting volume to " + newvol);
device().setVolume(newvol);
break;
case "volume-down":
var newvol = current_status.volume - 0.5;
console.log("setting volume to " + newvol);
device().setVolume(newvol);
break;
case "volume-set":
var newvol = command.split(' ')[1];
console.log("setting volume to " + newvol);
device().setVolume(newvol);
break;
case "toggle-mute":
if (current_status.mute) {
console.log("unmuting");
device().setMute(false);
} else {
console.log("muting");
device().setMute(true);
}
break;
case "mute":
console.log("muting");
device().setMute(true);
break;
case "unmute":
console.log("unmuting");
device().setMute(false);
break;
}
} catch (err) {
console.log(err);
}
}