-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPot.js
More file actions
75 lines (62 loc) · 1.59 KB
/
Pot.js
File metadata and controls
75 lines (62 loc) · 1.59 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
66
67
68
69
70
71
72
73
74
75
+(function(factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function(scope) {
'use strict';
var Module = scope.Module,
BoardEvent = scope.BoardEvent,
proto;
var PotEvent = {
MESSAGE: 'message'
};
function Pot(board, analogPinNumber) {
Module.call(this);
this._board = board;
this._pinNumber = Number(analogPinNumber);
this._messageHandler = onMessage.bind(this);
}
function onMessage(event) {
var pin = event.pin;
if (this._pinNumber !== pin.analogNumber) {
return false;
}
this.emit(PotEvent.MESSAGE, pin.value);
}
Pot.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Pot
},
state: {
get: function() {
return this._state;
},
set: function(val) {
this._state = val;
}
}
});
proto.on = function(callback) {
var _this = this;
this._board.enableAnalogPin(this._pinNumber);
if (typeof callback !== 'function') {
callback = function() {};
}
this._callback = function(val) {
callback(val);
};
this._state = 'on';
this._board.on(BoardEvent.ANALOG_DATA, this._messageHandler);
this.addListener(PotEvent.MESSAGE, this._callback);
};
proto.off = function() {
this._state = 'off';
this._board.disableAnalogPin(this._pinNumber);
this._board.removeListener(BoardEvent.ANALOG_DATA, this._messageHandler);
this.removeListener(PotEvent.MESSAGE, this._callback);
this._callback = null;
};
scope.module.Pot = Pot;
}));