-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchLife.js
98 lines (84 loc) · 2.22 KB
/
launchLife.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
var life = require('./life.js').init(8);
var fs = require('fs');
var midiport = 0;
var midiConnector = require('midi-launchpad').connect(midiport);
// set initial state of life area
life.setCell(3, 1, true);
life.setCell(4, 1, true);
life.setCell(5, 1, true);
// wait for the connector to be ready
midiConnector.on("ready",function(launchpad) {
console.log("Launchpad ready, let's do something");
var green = launchpad.colors.green.high;
var orange = launchpad.colors.orange.high;
var red = launchpad.colors.red.high;
var off = launchpad.colors.off;
var isRunning = false;
var stepButton = launchpad.getButton(8, 4);
stepButton.on('press', function(){
if(!isRunning){
life.step(renderToLaunchpad);
}
});
stepButton.light(orange);
var turnOnStepButton = function(){
stepButton.light(orange);
};
var turnOffStepButton = function(){
stepButton.light(off);
}
var ppButton = launchpad.getButton(8, 6);
ppButton.on('press', function(){
isRunning = !isRunning;
if(isRunning){
ppButton.light(orange);
loop();
turnOffStepButton();
} else {
ppButton.light(green);
turnOnStepButton();
}
});
ppButton.light(isRunning ? orange : green);
var clearButton = launchpad.getButton(8, 7);
clearButton.on('press', function(){
launchpad.clear();
life.clear();
clearButton.light(red);
ppButton.light(green);
stepButton.light(orange);
isRunning = false;
});
clearButton.light(launchpad.colors.red.high);
launchpad.on("press", function(btn) {
if(btn.x != 8 || (btn.y != 7 && btn.y != 6 && btn.y != 4)){
if(btn.getState() == 0) {
btn.light(green);
life.setCell(btn.x, btn.y, true);
}
else {
btn.light(off);
life.setCell(btn.x, btn.y, false);
}
}
});
var renderToLaunchpad = function(world, n){
for(x = 0; x < world.length; x ++){
for(y = 0; y < world[x].length; y ++){
var btn = launchpad.getButton(x, y);
if(world[x][y]){
btn.light(green);
} else {
btn.light(off);
}
}
}
}
var loop = function(){
life.step(renderToLaunchpad);
if(isRunning){
setTimeout(loop, 500);
}
}
loop();
});