-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPhotocell.js
More file actions
117 lines (102 loc) · 2.67 KB
/
Photocell.js
File metadata and controls
117 lines (102 loc) · 2.67 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
var Module = scope.Module,
BoardEvent = scope.BoardEvent,
proto;
var PhotocellEvent = {
/**
* Fires when the value of brightness has changed.
*
* @event PhotocellEvent.MESSAGE
*/
MESSAGE: 'message'
};
/**
* The Photocell class.
*
* Photocell is small, inexpensive, low-power sensor that allow you to detect light.
*
* @namespace webduino.module
* @class Photocell
* @constructor
* @param {webduino.Board} board Board that the photocell is attached to.
* @param {Integer} analogPinNumber The pin that the photocell is connected to.
* @extends webduino.Module
*/
function Photocell(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(PhotocellEvent.MESSAGE, pin.value);
}
Photocell.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: Photocell
},
/**
* The state indicating whether the module is measuring.
*
* @attribute state
* @type {String} `on` or `off`
*/
state: {
get: function () {
return this._state;
},
set: function (val) {
this._state = val;
}
}
});
/**
* Start detection.
*
* @method measure
* @param {Function} [callback] Callback after starting detection.
*/
/**
* Start detection.
*
* @method on
* @param {Function} [callback] Callback after starting detection.
* @deprecated `on()` is deprecated, use `measure()` instead.
*/
proto.measure = proto.on = function(callback) {
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(PhotocellEvent.MESSAGE, this._callback);
};
/**
* Stop detection.
*
* @method off
*/
proto.off = function () {
this._state = 'off';
this._board.disableAnalogPin(this._pinNumber);
this._board.removeListener(BoardEvent.ANALOG_DATA, this._messageHandler);
this.removeListener(PhotocellEvent.MESSAGE, this._callback);
this._callback = null;
};
scope.module.Photocell = Photocell;
}));