-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchpad_io.js
409 lines (335 loc) · 8.01 KB
/
launchpad_io.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* Copyright 2015 Alan Drees
*
* Purpose:
* Implementation of the LaunchpadIO object for handling the
* interfacing with the launchpads themselves
*
* Dependencies:
*/
var Launchpad = Launchpad || {};
/**\fn Launchpad.LaunchpadIO
*
* LaunchpadIO constructor object
*
* @param options (object) options object to set the options of the controller for
* @param grid_x (integer) physical launchpad position horizontally
* @param grid_y (integer) physical launchpad position vertically
* @param midi_in (integer) midi IO input to use (OPTIONAL)
* @param midi_out (integer) midi IO output to use (OPTIONAL)
*
* @returns None
*/
Launchpad.LaunchpadIO = function(options, grid_x, grid_y, lmidi_in, midi_out )
{
if(typeof midi_in === 'undefined'){var midi_in = options.layout_order[grid_x][grid_y][0];}
if(typeof midi_out === 'undefined'){var midi_out = options.layout_order[grid_x][grid_y][1];}
this.set_options(options);
this.midi_in = midi_in -1;
this.midi_out = midi_out - 1;
this.grid_x = grid_x;
this.grid_y = grid_y;
this.ctrl = {};
/**
* Cache for LEDs needing to be updated, which is used so we can determine if we want to send the LEDs using the
* optimized approach or not, and to send only the LEDs that has changed.
*/
this.pendingLEDs = new Array(80);
this.activeLEDs = new Array(80);
this.force_optimized_flush = false;
}
/**\fn Launchpad.LaunchpadIO.prototype.init
*
* Initialization function
*
* @param launchpad_controller (LaunchpadControlObject) launchpad controller object which implements a midi in function
*
* @Returns None
*/
Launchpad.LaunchpadIO.prototype.init = function(launchpad_controller)
{
var self = this;
this.ctrl = launchpad_controller
host.getMidiInPort(this.midi_in).setMidiCallback(function(status, data1, data2){self.midi_callback(status, data1, data2);});
this.resetDevice();
this.setGridMappingMode();
this.enableAutoFlashing();
}
/**\fn Launchpad.LaunchpadIO.prototype.clear
*
* Clear all the LEDs
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.clear = function()
{
for(var i=0; i < 80; i++)
{
this.pendingLEDs[i] = Launchpad.Colour.OFF;
}
}
/**\fn Launchpad.LaunchpadController.prototype.resetDevice
*
* send a message to the device to reset the whole unit
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.resetDevice = function()
{
this.send_midi(0xB0,
0,
0);
this.clear();
this.flushLEDs();
}
/**\fn Launchpad.LaunchpadIO.prototype.enableAutoFlashing
*
* Enable device-handled flashing (minimizes the MIDI traffic on the bus)
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.enableAutoFlashing = function()
{
this.send_midi(0xB0,
0,
0x28);
}
/**\fn Launchpad.LaunchpadIO.prototype.setGridMappingMode
*
* This sets the grid mapping mode between the drum pad mode or the grid mode (in this case the grid mode
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setGridMappingMode = function()
{
this.send_midi(0xB0,
0,
1);
}
/**\fn Launchpad.LaunchpadIO.prototype.setGridMappingMode
*
* This sets the grid mapping mode between the drum pad mode or the grid mode (in this case the grid mode
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setDrumMappingMode = function()
{
this.send_midi(0xB0,
0,
1);
}
/**\fn Launchpad.LaunchpadIO.prototype.setDutyCycle
*
* Sets the LED duty cycle by passing a numerator and denominator
*
* @param numerator duty cycle numerator
* @param denominator duty cycle denominator
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setDutyCycle = function(numerator, denominator)
{
if (numerator < 9)
{
this.send_midi(0xB0,
0x1E,
16 * (numerator - 1) + (denominator - 3));
}
else
{
this.send_midi(0xB0,
0x1F,
16 * (numerator - 9) + (denominator - 3));
}
}
/**\fn Launchpad.LaunchpadIO.prototype.midi_callback
*
* Midi callback wrapper to handle midi input to be passed to handler function
*
* @param status (integer) MIDI status byte
* @param data1 (integer) MIDI data1 byte
* @param data2 (integer) MIDI data2 byte
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.midi_callback = function(status, data1, data2)
{
if(typeof this.ctrl.onMidi === 'function')
{
this.ctrl.onMidi(status,
data1,
data2,
this.grid_x,
this.grid_y);
}
}
/**\fn Launchpad.LaunchpadIO.prototype.send_midi
*
* Sends midi to the designated midi_out
*
* @param status (integer) status byte
* @param data1 (integer) first data byte
* @param data2 (integer) second data byte
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.send_midi = function(status, data1, data2)
{
try
{
host.getMidiOutPort(this.midi_out).sendMidi(status, data1, data2);
}
catch(e)
{
}
}
/**\fn Launchpad.LaunchpadIO.prototype.setTopLED
*
* Set one of the top LEDs (navigation or mode LEDs)
*
* @param index (integer) top led index
* @param colour (integer) color to set the LED to
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setTopLED = function(index, colour)
{
this.pendingLEDs[Launchpad.LED.TOP + index] = colour;
}
/**\fn Launchpad.LaunchpadIO.prototype.setRightLED
*
* Sets the scene-firing LEDs on the far right
*
* @param index index to send to
* @param colour colour to set the LED to
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setRightLED = function(index, colour)
{
this.pendingLEDs[Launchpad.LED.SCENE + index] = colour;
}
/**\fn Launchpad.LaunchpadIO.prototype.setCellLED
*
* Set one of the grid LEDs
*
* @param column (integer) track of the grid LED
* @param row (integer) scene of the grid LED
* @param colour (integer) color to set the LED to
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.setCellLED = function(column, row, colour)
{
this.pendingLEDs[row * Launchpad.NUM_CHANNELS + column] = colour;
}
/**\fn Launchpad.LaunchpadController.prototype.flushLEDs
*
* Flush all the output to the controller, updating the LEDs
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.flushLEDs = function()
{
if(typeof this.flushcount === 'undefined')
{
for(var x in this.pendingLEDs)
{
if(typeof this.pendingLEDs[x] === 'object')
{
dump(this.pendingLEDs[x]);
}
}
this.flushcount = true;
}
var changedCount = 0;
for(var i = 0; i < 80; i++)
{
if (this.pendingLEDs[i] != this.activeLEDs[i])
{
changedCount++;
}
}
if (changedCount == 0)
{
return;
}
if (changedCount > 30)
{
for(var i = 0; i<80; i+=2)
{
this.send_midi(0x92,
this.pendingLEDs[i],
this.pendingLEDs[i+1])
this.activeLEDs[i] = this.pendingLEDs[i];
this.activeLEDs[i+1] = this.pendingLEDs[i+1];
}
this.send_midi(0xB0,
104 + 7,
this.activeLEDs[79]); // send dummy message to leave optimized mode
}
else
{
for(var i = 0; i < 80; i++)
{
if (this.pendingLEDs[i] != this.activeLEDs[i])
{
this.activeLEDs[i] = this.pendingLEDs[i];
var colour = this.activeLEDs[i];
if (i < 64) // Main Grid
{
var column = i & 0x7;
var row = i >> 3;
this.send_midi(0x90,
row * 16 + column,
colour);
}
else if (i < 72) // Right buttons
{
this.send_midi(0x90,
8 + (i - 64) * 16,
colour);
}
else // Top buttons
{
this.send_midi(0xB0,
104 + (i - 72),
colour);
}
}
}
}
}
/**\fn Launchpad.LaunchpadIO.prototype.set_options
*
* Sets controller options
*
* @param options options with which to set (use defaults if not set)
*
* @returns None
*/
Launchpad.LaunchpadIO.prototype.set_options = function(options)
{
this.options = { 'tracks' : Launchpad.NUM_TRACKS,
'scenes' : Launchpad.NUM_SCENES,
'instance' : 1 };
if(typeof options === 'object')
{
for(var option in options)
{
this.options[option] = options[option];
}
}
}