-
Notifications
You must be signed in to change notification settings - Fork 1
/
photocell.js
50 lines (40 loc) · 1.12 KB
/
photocell.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
var webduino = require('webduino-js'),
utils = require('./utils');
module.exports = function (RED) {
'use strict';
function Photocell(n) {
var node = this,
boardNode, photocell;
RED.nodes.createNode(this, n);
node.pin = n.pin;
node.autoStart = n.autoStart;
node.samplingInterval = parseInt(n.samplingInterval);
boardNode = RED.nodes.getNode(n.board);
boardNode.mount(node, function (board) {
photocell = new webduino.module.Photocell(board, node.pin);
if (node.samplingInterval && !isNaN(node.samplingInterval)) {
board.samplingInterval = node.samplingInterval;
}
if (node.autoStart) {
photocell.on(function (evt) {
node.send({
payload: evt
});
});
}
});
node.on('input', function (msg) {
utils.invoke(photocell, msg, function (evt) {
node.send({
payload: evt
});
});
});
node.on('close', function () {
if (photocell && photocell.state === 'on') {
photocell.off();
}
});
}
RED.nodes.registerType('photocell', Photocell);
};