-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (188 loc) · 6.29 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
234
235
236
237
238
239
240
require('dotenv').config();
const chalk = require('chalk');
const raf = require('raf');
const frekvens = process.env.FAKEVENS
? require('fakevens')
: require('frekvens');
const { ButtonAction } = require('./lib/button-action');
const { Client } = require('./lib/client');
const scenes = require('./lib/scenes');
const midi = require('./lib/midi');
const COLS = 16;
const ROWS = 16;
const BUTTON_OFFSET = 4;
const BUTTON_DIAMETER = 3;
function buttonOverlay(x, y) {
const buttonRows = [
[ 0, 1, 0 ],
[ 1, 1, 1 ],
[ 0, 1, 0 ]
].map((row) => Uint8Array.from(row));
return {
render(pixels) {
buttonRows.forEach((row, idx) => pixels.set(row, (y + idx) * COLS + x));
}
};
}
const overlays = {
disconnected: {
render(pixels, t) {
if (t & 1) {
pixels[1 * COLS + 1] = 1;
}
}
},
redButton: buttonOverlay(COLS - (BUTTON_DIAMETER + BUTTON_OFFSET), BUTTON_OFFSET),
yellowButton: buttonOverlay(BUTTON_OFFSET, BUTTON_OFFSET),
choke: {
render(pixels) {
pixels[(ROWS - 2) * COLS + COLS - 2] = 1;
}
}
};
async function quit() {
if (process.env.LOG_SYSTEM) {
frekvens.log(chalk`{magenta Terminating}`);
}
await frekvens.stop();
process.exit();
}
process.on('SIGINT', quit);
async function init() {
if (process.env.LOG_SYSTEM) {
frekvens.log(chalk`{green Initializing}`);
}
let isBlackout = false;
let currentScene = null;
function toggleBlackout() {
isBlackout = !isBlackout;
}
function setScene(idx) {
currentScene && currentScene.cleanup && currentScene.cleanup();
currentScene = {
...scenes[idx],
idx,
state: {}
};
currentScene && currentScene.init && currentScene.init();
}
function nextScene() {
const nextIdx = currentScene && currentScene.idx >= 0
? (currentScene.idx + 1) % scenes.length
: scenes.findIndex((scene) => scene.default);
setScene(nextIdx);
}
nextScene();
function compileScript(script) {
try {
currentScene = {
render: new Function([ 'pixels', 't', 'state' ], script),
state: {}
};
} catch (error) {
frekvens.error(`Syntax error in script: ${error.message}`);
currentScene = null;
client.send('error', `Syntax error: ${error.message}`);
}
}
const client = new Client({
serverUrl: process.env.WEBSOCKET_SERVER_URL,
clientSecret: process.env.FREKVENS_CLIENT_SECRET
});
client.on('script', compileScript);
if (process.env.OVERLAYS) {
overlays.disconnected.isActive = true;
client.on('connect', () => overlays.disconnected.isActive = false);
client.on('disconnect', () => overlays.disconnected.isActive = true);
}
const redButton = new ButtonAction({ longPressDuration: 5 * 1000 });
const yellowButton = new ButtonAction({ longPressDuration: 5 * 1000 });
client.on('yellowDown', () => yellowButton.down());
client.on('yellowUp', () => yellowButton.up());
client.on('redDown', () => redButton.down());
client.on('redUp', () => redButton.up());
client.on('midi', (message) => midi.process(message));
if (process.env.LOG_BUTTONS) {
redButton.on('down', () => frekvens.log(chalk`{red Red} button down`));
redButton.on('up', () => frekvens.log(chalk`{red Red} button up`));
redButton.on('press', () => frekvens.log(chalk`{red Red} button press`));
redButton.on('longPress', () => frekvens.log(chalk`{red Red} button long press`));
yellowButton.on('down', () => frekvens.log(chalk`{yellow Yellow} button down`));
yellowButton.on('up', () => frekvens.log(chalk`{yellow Yellow} button up`));
yellowButton.on('press', () => frekvens.log(chalk`{yellow Yellow} button press`));
yellowButton.on('longPress', () => frekvens.log(chalk`{yellow Yellow} button long press`));
}
if (process.env.LOG_CLIENT) {
client.on('connect', () => frekvens.log(chalk`{green Connected}`));
client.on('disconnect', () => frekvens.log(chalk`{magenta Disconnected}`));
}
if (process.env.LOG_SYSTEM) {
redButton.on('longPress', () => frekvens.log(chalk`{red POWERING OFF}`));
yellowButton.on('longPress', () => frekvens.log(chalk`{yellow REBOOTING}`));
}
redButton.on('down', () => client.send('buttonDown', 'red'));
redButton.on('up', () => client.send('buttonUp', 'red'));
redButton.on('press', toggleBlackout);
redButton.on('longPress', () => frekvens.powerOff());
yellowButton.on('down', () => client.send('buttonDown', 'yellow'));
yellowButton.on('up', () => client.send('buttonUp', 'yellow'));
yellowButton.on('press', nextScene);
yellowButton.on('longPress', () => frekvens.reboot());
if (process.env.OVERLAYS) {
redButton.on('change', (isDown) => overlays.redButton.isActive = isDown);
yellowButton.on('change', (isDown) => overlays.yellowButton.isActive = isDown);
frekvens.on('choke', () => overlays.choke.isActive = true);
}
frekvens.on('redDown', () => redButton.down());
frekvens.on('redUp', () => redButton.up());
frekvens.on('yellowDown', () => yellowButton.down());
frekvens.on('yellowUp', () => yellowButton.up());
// Used for FAKEVENS only
frekvens.on('quit', quit);
let transform = Int8Array.from([
1, 0,
0, 1
]);
if (process.env.ROTATE) {
const turns = parseInt(process.env.ROTATE);
const theta = Math.PI / 2 * turns;
const sin = Math.round(Math.sin(theta));
const cos = Math.round(Math.cos(theta));
transform = Int8Array.from([
cos, -sin,
sin, cos
]);
}
switch (process.env.FLIP) {
case 'H':
transform[0] = -transform[0];
break;
case 'V':
transform[3] = -transform[3];
break;
}
await frekvens.start(transform);
const pixels = new Uint8Array(COLS * ROWS);
raf(function render() {
raf(render);
pixels.fill(0);
if (!isBlackout) {
const t = Date.now() / 1000;
if (currentScene) {
try {
currentScene.render(pixels, t, currentScene.state);
} catch (error) {
frekvens.error(`Runtime error in script: ${error.message}`);
currentScene = null;
client.send('error', `Runtime error: ${error.message}`);
}
}
Object.values(overlays)
.filter((overlay) => overlay.isActive)
.forEach((overlay) => overlay.render(pixels, t));
overlays.choke.isActive = false;
}
frekvens.render(pixels);
});
}
init();