-
Notifications
You must be signed in to change notification settings - Fork 70
/
mega-multipin.js
71 lines (58 loc) · 1.8 KB
/
mega-multipin.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
// This example shows how to use node-pixel
// to control multiple strips on the same board
const five = require('johnny-five');
const pixel = require('node-pixel');
const opts = {};
opts.port = process.argv[2] || '';
const board = new five.Board(opts);
let strip = null;
const fps = 10; // how many frames per second do you want to try?
board.on('ready', function() {
console.log('Board ready, lets add light');
strip = new pixel.Strip({
board: this,
controller: 'FIRMATA',
strips: [
{pin: 2, length: 8}, {pin: 3, length: 8},
{pin: 4, length: 8}, {pin: 5, length: 8},
{pin: 6, length: 8}, {pin: 7, length: 8},
{pin: 8, length: 8}, {pin: 9, length: 8}
]
});
strip.on('ready', function() {
console.log('Strip ready');
const strips = 8;
const lengths = 8;
const current_pos = new Array();
for (let i=0; i< strips; i++) {
current_pos.push(new Array());
}
// periodically drop a new item onto one of the strips randomly.
const dripper = setInterval(function() {
const s = Math.round(Math.random()*strips);
try {
current_pos[s].push(0);
} catch (e) {
if (e instanceof TypeError) {
// this usually happens if we're splicing and writing at the
// same time.
return;
}
}
}, 75);
const iterator = setInterval(function() {
strip.color('#000'); // blanks it out
for (let i=0; i< current_pos.length; i++) {
for (let j=0; j< current_pos[i].length; j++) {
if (current_pos[i][j] >= 8) {
current_pos[i].splice(j, 1);// remove the item
} else {
strip.pixel(i * lengths + current_pos[i][j]).color('#440');
current_pos[i][j]++;
}
}
}
strip.show();
}, 1000/fps);
});
});