-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneopixel-jewel-candle.js
90 lines (73 loc) · 2.27 KB
/
neopixel-jewel-candle.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
//
// Flickering NeoPixel candle
//
// Inspired by and uses data from:
// - https://cpldcpu.wordpress.com/2016/01/05/reverse-engineering-a-real-candle/
// - https://github.com/cpldcpu/RealCandle
//
var pixel = require('node-pixel');
var five = require('johnny-five');
var child_process = require('child_process');
var chalk = require('chalk');
var board = new five.Board();
var strip = null;
board.on('ready', function boardReady() {
console.log(chalk.white.bold('🤖 board.ready'));
var led = new five.Led(10);
led.brightness(48);
// How often to refresh per second, ~300 is max.
// The sample data was collected at 175HZ.
var fps = 175;
const NUM_ROWS = 1;
const NUM_COLS = 7;
strip = new pixel.Strip({
board: this,
controller: 'FIRMATA',
strips: [ {pin: 6, length: NUM_ROWS*NUM_COLS}, ],
gamma: 2.2, // 3.6 = night, 2.6 = bright day
});
strip.on('ready', function stripReady() {
strip.off();
console.log(chalk.bold('👍 strip.ready with ' + strip.length + ' LEDs'));
var forever = setInterval(function forever() {
loop(fps);
}, 1);
});
// Load data from JSON. Original was CSV with timing column but I stripped
// it out and the JSON is just a n array of points inside `data` prop.
var pattern = require('./data/candle-1.json').data;
var patternLength = pattern.length;
var patternMax = Math.max.apply(Math, pattern);
// counter
var counter = 0;
// 8-bit color: 0-255
var color = {
r: 255,
g: 130,
b: 0,
};
function loop(framerate) {
var r = Math.floor(color.r * pattern[counter] / patternMax);
var g = Math.floor(color.g * pattern[counter] / patternMax);
var b = Math.floor(color.b * pattern[counter] / patternMax);
// DEBUG
console.log('🔥 ', chalk.red(r), chalk.green(g), chalk.blue(b));
// draw new pixel
strip.color(`rgb(${r}, ${g}, ${b})`);
// increment counter
if (counter < patternLength - 1) {
counter++;
} else {
counter = 0;
console.log('🔥 Restarting pattern...');
}
// update strip and delay
strip.show();
child_process.execSync('sleep ' + 1 / framerate);
}
// cleanup when this program is terminated
this.on('exit', function exit() {
led.off();
strip.off(); // doesn't work, not sure why
});
});