-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPir.js
More file actions
47 lines (38 loc) · 920 Bytes
/
Pir.js
File metadata and controls
47 lines (38 loc) · 920 Bytes
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
+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
var PinEvent = scope.PinEvent,
Pin = scope.Pin,
Module = scope.Module;
var PirEvent = {
DETECTED: 'detected',
ENDED: 'ended'
};
function Pir(board, pin) {
Module.call(this);
this._board = board;
this._pin = pin;
board.setDigitalPinMode(pin.number, Pin.DIN);
this._pin.value = Pin.HIGH;
this._pin.on(PinEvent.CHANGE, onPinChange.bind(this));
}
function onPinChange(pin) {
if (pin.value === Pin.HIGH) {
this.emit(PirEvent.DETECTED);
} else {
this.emit(PirEvent.ENDED);
}
}
Pir.prototype = Object.create(Module.prototype, {
constructor: {
value: Pir
}
});
scope.module.PirEvent = PirEvent;
scope.module.Pir = Pir;
}));