forked from joeledwards/sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser.js
36 lines (27 loc) · 803 Bytes
/
laser.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
const _ = require('lodash');
const P = require('bluebird');
const pi = require('./pi');
const laser = { pins: [16], label: "laser" };
const duration = 500;
function die(message) {
console.error(message);
pi.shutdown().then(() => process.exit(1));
}
function pulse(color, duration) {
console.log(`Pulsing ${color.label}; pins ${color.pins}`);
return P.all(_(color.pins).map((pin) => pi.pulse(pin, duration)).value());
}
var stop = false;
function cycle(count) {
if (count > 0 && !stop) {
pulse(laser, duration)
.then(() => cycle(count - 1))
.catch((error) => {
die(`Error controlling pin ${pin.number} for output: ${error}\n${error.stack}`);
});
} else {
pi.shutdown().then(() => process.exit(0));
}
}
process.on('SIGINT', () => stop = true);
cycle(100);