-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
234 lines (192 loc) · 7.49 KB
/
index.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
const ATEM = require('atem');
const fs = require('fs');
const JSON5 = require('json5');
const xkeys = require('xkeys');
const config = JSON5.parse(fs.readFileSync('config.json5'));
const keyMappings = {};
config.keys.forEach(mapping => keyMappings[mapping.key] = mapping);
let panel, switcher;
let currentWipe = 0;
let currentBrightness = config.initialBrightness;
let currentSourcePreview, currentSourceProgram;
let isShifted = false;
const COLOR_OFF = false;
const COLOR_RED = 'red';
const COLOR_BLUE = 'blue';
const COLOR_RED_BLUE = 'redblue';
const backlightCache = new Map();
const setLEDForKeyMapping = (keyMapping, color, flash) => {
const params = [color, flash];
let keys = [keyMapping.key];
if (keyMapping.additionalLEDs) keys = keys.concat(keyMapping.additionalLEDs);
keys.forEach(keyIndex => {
const cachedParams = backlightCache.get(keyIndex);
if (cachedParams && cachedParams.every((p, i) => p === params[i])) return;
panel.setBacklight(keyIndex, color, flash && color !== false);
backlightCache.set(keyIndex, params);
});
};
const forEachMappingOfType = (type, callback) => {
Object.values(keyMappings).filter(mapping => mapping.function == type).forEach(callback);
};
const flashAllSources = function() {
forEachMappingOfType('source', mapping => setLEDForKeyMapping(mapping, COLOR_RED, true));
};
function updateSourceLights() {
forEachMappingOfType('source', mapping => {
let source = isShifted ? mapping.shiftSource : mapping.source;
let color = COLOR_OFF;
if (source !== undefined) {
if (source == currentSourceProgram) {
color = COLOR_RED;
if (source == currentSourcePreview) color = COLOR_RED_BLUE;
} else if (source == currentSourcePreview) {
color = COLOR_BLUE;
}
}
setLEDForKeyMapping(mapping, color)
});
forEachMappingOfType('source_pgm', mapping => {
let source = isShifted ? mapping.shiftSource : mapping.source;
setLEDForKeyMapping(mapping, source !== undefined && source == currentSourceProgram ? COLOR_RED : COLOR_OFF);
});
}
async function main() {
panel = await xkeys.setupXkeysPanel();
switcher = new ATEM();
panel.setBacklightIntensity(currentBrightness);
panel.setFrequency(8);
if (config.clearBacklightOnStartup) {
panel.setAllBacklights(COLOR_OFF);
} else {
Object.values(keyMappings).forEach(mapping => setLEDForKeyMapping(mapping, COLOR_OFF));
}
switcher.ip = config.switcherIP;
switcher.connect();
flashAllSources();
setupSwitcherHandlers();
setupPanelHandlers();
}
function setupSwitcherHandlers() {
switcher.on('connectionStateChange', state => {
console.log(`ATEM connection state: ${state.description}`);
if (state !== ATEM.ConnectionState.open) flashAllSources();
});
switcher.on('previewBus', source => {
currentSourcePreview = source;
updateSourceLights();
});
switcher.on('programBus', source => {
currentSourceProgram = source;
updateSourceLights();
});
let currentlyTransitioning = false;
switcher.on('TrPs', packet => {
const transitioning = (packet[1] & 1) == 1;
if (transitioning == currentlyTransitioning) return;
currentlyTransitioning = transitioning;
forEachMappingOfType('auto', mapping => setLEDForKeyMapping(mapping, transitioning ? COLOR_RED : COLOR_OFF, true));
});
let currentlyFTB = false;
switcher.on('FtbS', packet => {
const isFTB = (packet[1] & 1) == 1 || (packet[2] & 1) == 1;
if (isFTB == currentlyFTB) return;
currentlyFTB = isFTB;
forEachMappingOfType('ftb', mapping => setLEDForKeyMapping(mapping, isFTB ? COLOR_RED : COLOR_OFF, true));
});
switcher.on('TrSS', packet => {
forEachMappingOfType('transition', mapping => setLEDForKeyMapping(mapping, mapping.transition == packet[1] ? COLOR_BLUE : COLOR_OFF));
});
switcher.on('TWpP', packet => {
currentWipe = packet[2];
});
/*
// For development purposes
switcher.on('rawCommand', cmd => {
if (cmd.name != 'Time') console.log(`Command ${cmd.name} received`);
});
*/
}
function setupPanelHandlers() {
let programMode = false;
panel.on('down', keyIndex => {
if (config.showKeyPresses) console.log(`Key ${keyIndex} pressed`);
const mapping = keyMappings[keyIndex];
if (!mapping) return;
switch (mapping.function) {
case 'cut':
switcher.cut();
break;
case 'auto':
switcher.auto();
break;
case 'ftb':
switcher.sendCommand(new ATEM.Command('FtbA', Buffer.from([0, 2, 0, 0])));
break;
case 'transition':
switcher.sendCommand(new ATEM.Command('CTTp', Buffer.from([1, 0, mapping.transition, 0])));
break;
case 'wipe_prev':
case 'wipe_next':
const data = Buffer.alloc(20, 0);
data[1] = 2;
if (mapping.function == 'wipe_prev') data[4] = Math.max(currentWipe - 1, 0);
else data[4] = Math.min(currentWipe + 1, 17);
switcher.sendCommand(new ATEM.Command('CTWp', data));
break;
case 'program_mode':
programMode = !programMode;
setLEDForKeyMapping(mapping, programMode ? COLOR_RED : COLOR_OFF);
break;
case 'shift':
isShifted = true;
updateSourceLights();
break;
case 'shift_toggle':
isShifted = !isShifted;
updateSourceLights();
break;
case 'source':
case 'source_pgm':
let source = isShifted ? mapping.shiftSource : mapping.source;
if (source === undefined) break;
if (programMode || mapping.function == 'source_pgm') switcher.setProgram(source);
else switcher.setPreview(source);
break;
case 'backlight_up':
currentBrightness = Math.min(currentBrightness + 10, 255);
panel.setBacklightIntensity(currentBrightness);
break;
case 'backlight_down':
currentBrightness = Math.max(currentBrightness - 10, 0);
panel.setBacklightIntensity(currentBrightness);
break;
}
});
panel.on('up', keyIndex => {
const mapping = keyMappings[keyIndex];
if (!mapping) return;
switch (mapping.function) {
case 'shift':
isShifted = false;
updateSourceLights();
break;
}
});
if (!config.disableTbar) {
let lastTbarPosition = -1;
let tbarReverse = false;
panel.on('tbar', (index, position) => {
if (lastTbarPosition === -1) {
lastTbarPosition = position;
if (position >= 128) tbarReverse = true;
}
if (position === lastTbarPosition) return;
lastTbarPosition = position;
const atemPosition = Math.round((tbarReverse ? 255 - position : position) * 10000 / 255);
switcher.sendCommand(new ATEM.Command('CTPs', Buffer.from([0, 0, atemPosition >>> 8, atemPosition & 0xFF])))
if (atemPosition === 10000) tbarReverse = !tbarReverse;
});
}
}
main();