forked from loveolsson/node-oscdeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (69 loc) · 1.73 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
const gfx = require('./btn-render.js');
const streamDeck = require('elgato-stream-deck');
const _ = require('lodash');
var sys = require('util')
var exec = require('child_process').exec;
const settings = require('./configs/default.json');
var buttons = [];
var clients = [];
function ControllerButton(b) {
var self = this;
if (!_.isString(b.text)) {
b.text = "";
}
if (_.isString(b.symbol)) {
b.name = "help";
}
this.desc = b;
this.imgLoaded = gfx.Load(b.color, b.symbol, b.text);
this.setImage = function (state) {
var index = state ? 1 : 0;
this.imgLoaded.then(function(x) {
streamDeck.fillImage(self.desc.key, x[index].data);
});
}
this.sendCmd = function (state) {
var o = state ? self.desc.cmdDown : self.desc.cmdUp;
if (o) {
var cmd = o[1];
var val = o[2];
exec(cmd, function(err, stdout, stderr) {
console.log(stdout);
});
console.log("Sent", cmd, val);
}
}
this.down = function () {
self.sendCmd(true);
self.setImage(true);
}
this.up = function () {
self.sendCmd(false);
self.setImage(false);
}
self.setImage(false);
}
// Clear button displays
_.forEach(_.range(0, 15), function (i) {
streamDeck.fillColor(i, 0, 0, 0);
});
// Set up buttons from settings file
_.forEach(settings.buttons, function (b) {
if (_.isNumber(b.key) && _.inRange(b.key, 0, 15)) {
buttons[b.key] = new ControllerButton(b);
}
});
// Set up Stream Deck callbacks
streamDeck.on('down', keyIndex => {
if (buttons[keyIndex]) {
buttons[keyIndex].down();
}
});
streamDeck.on('up', keyIndex => {
if (buttons[keyIndex]) {
buttons[keyIndex].up();
}
});
streamDeck.on('error', error => {
console.error(error);
});