-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathRelay.js
More file actions
65 lines (53 loc) · 1.38 KB
/
Relay.js
File metadata and controls
65 lines (53 loc) · 1.38 KB
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
+(function (factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function (scope) {
'use strict';
var Pin = scope.Pin,
Module = scope.Module,
proto;
function Relay(board, pin) {
Module.call(this);
this._type = 'KY-019';
this._board = board;
this._pin = pin;
this._onValue = 1;
this._offValue = 0;
board.setDigitalPinMode(pin.number, Pin.DOUT);
this.off();
}
function checkPinState(self, pin, state, callback) {
self._board.queryPinState(pin, function (pin) {
if (pin.state === state) {
callback.call(self);
}
});
}
Relay.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Relay
}
});
proto.on = function (callback) {
this._pin.value = this._onValue;
if (typeof callback === 'function') {
checkPinState(this, this._pin, this._pin.value, callback);
}
};
proto.off = function (callback) {
this._pin.value = this._offValue;
if (typeof callback === 'function') {
checkPinState(this, this._pin, this._pin.value, callback);
}
};
proto.toggle = function (callback) {
this._pin.value = 1 - this._pin.value;
if (typeof callback === 'function') {
checkPinState(this, this._pin, this._pin.value, callback);
}
};
scope.module.Relay = Relay;
}));