forked from sydlawrence/node-midi-launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.js
140 lines (111 loc) · 3.64 KB
/
button.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
var _ = require("underscore");
var Backbone = require("backbone");
var Launchpad = require("./launchpad");
/*
* Button
* Represents a single button on the Launchpad
*/
var Button = function(launchpad, note, y) {
this.launchpad = launchpad;
_.extend(this, Backbone.Events);
this._grid = launchpad;
this._state = launchpad.LED_OFF;
var that = this;
this._uuid = [];
this.special = false;
// Are we being assigned via a note or x, y?
if(y === undefined) {
var map = mapButtonToLaunchpad(note);
this.x = map[0];
this.y = map[1];
} else {
this.x = note;
this.y = y;
}
if (launchpad.specials[this.y][this.x] !== undefined) {
this.special = launchpad.specials[this.y][this.x];
}
this.light = function(color) {
this.stopBlink();
if(color === undefined)
color = launchpad.colors.red.high;
if (this._state === color) return;
// Send the instruction to the launchpad
if(this.y === 8)
launchpad.output.sendMessage([176, this.toNote(), color]);
else
launchpad.output.sendMessage([144, this.toNote(), color]);
// Save the state
this._state = color;
this.trigger("state_change");
};
this.setState = function(state) {
if (this._state === state) return;
// Send the instruction to the launchpad
if(this.y === 8)
launchpad.output.sendMessage([176, this.toNote(), state]);
else
launchpad.output.sendMessage([144, this.toNote(), state]);
// Save the state
this._state = state;
this.trigger("state_change");
};
this.dark = function() {
if (this._state === Launchpad.colors.off) return;
if(this.y === 8)
launchpad.output.sendMessage([176, this.toNote(), Launchpad.colors.off]);
else
launchpad.output.sendMessage([144, this.toNote(), Launchpad.colors.off]);
this._state = Launchpad.colors.off;
this.trigger("state_change");
};
// this.on("press", function() {
// launchpad.trigger("press",that);
// });
// this.on("release", function() {
// launchpad.trigger("release",that);
// });
// this.on("state_change", function() {
// launchpad.trigger("state_change",that);
// });
this.startBlink = function(color) {
this._blink_color = color;
this._grid._blinking.push(this);
// If we're adding the first blinking LED, start the interval
if(this._grid._blinking.length == 1)
this._grid._blink_interval = setInterval(this._grid._tick, 500);
this.trigger("state_change");
};
this.stopBlink = function() {
if (this._grid._blinking === undefined)
this._grid._blinking = [];
var index = this._grid._blinking.indexOf(this);
if(index === -1)
return;
delete this._blink_color;
this._grid._blinking.splice(index, 1);
this.trigger("state_change");
};
this.getState = function() {
return this._state;
};
// Converts x,y -> MIDI note
this.toNote = function() {
if(this.y == 8)
return 104 + this.x;
else
return (this.y * 16) + this.x;
};
this.toString = function() {
return "(" + this.x + ", " + this.y + ")";
}
};
var mapButtonToLaunchpad = function (note) {
// For right buttons
if(note % 8 === 0 && ((note / 8) % 2 == 1))
return [8, Math.floor(note / 8 / 2)];
var x = note % 8;
var y = Math.floor(note / 8) / 2;
return [x, y];
};
exports.Button = Button;