1 | var Collection = require("./mixins/collection");
|
2 | var Board = require("./board");
|
3 | var events = require("events");
|
4 | var util = require("util");
|
5 | var priv = new Map();
|
6 |
|
7 |
|
8 | function analogInitializer(opts, dataHandler) {
|
9 | var state = priv.get(this);
|
10 |
|
11 | this.io.pinMode(opts.pin, this.io.MODES.ANALOG);
|
12 |
|
13 | setTimeout(function() {
|
14 | state.isCalibrated = true;
|
15 | this.emit("calibrated");
|
16 | }.bind(this), 10);
|
17 |
|
18 | this.io.analogRead(opts.pin, dataHandler);
|
19 | }
|
20 |
|
21 | var Controllers = {
|
22 | PIR: {
|
23 | initialize: {
|
24 | value: function(opts, dataHandler) {
|
25 | var state = priv.get(this);
|
26 | var calibrationDelay = typeof opts.calibrationDelay !== "undefined" ?
|
27 | opts.calibrationDelay : 2000;
|
28 |
|
29 | this.io.pinMode(opts.pin, this.io.MODES.INPUT);
|
30 |
|
31 | setTimeout(function() {
|
32 | state.isCalibrated = true;
|
33 | this.emit("calibrated");
|
34 | }.bind(this), calibrationDelay);
|
35 |
|
36 | this.io.digitalRead(opts.pin, dataHandler);
|
37 | }
|
38 | },
|
39 | toBoolean: {
|
40 | value: function(raw) {
|
41 | return !!raw;
|
42 | }
|
43 | }
|
44 | },
|
45 | GP2Y0D805Z0F: {
|
46 | initialize: {
|
47 | value: function(opts, dataHandler) {
|
48 | var address = opts.address || 0x26;
|
49 | var state = priv.get(this);
|
50 |
|
51 | opts.address = address;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | setTimeout(function() {
|
57 | state.isCalibrated = true;
|
58 | this.emit("calibrated");
|
59 | }.bind(this), 10);
|
60 |
|
61 |
|
62 |
|
63 | this.io.i2cConfig(opts);
|
64 |
|
65 | this.io.i2cWriteReg(address, 0x03, 0xFE);
|
66 | this.io.i2cWrite(address, [0x00]);
|
67 | this.io.i2cRead(address, 1, function(data) {
|
68 | dataHandler(data[0] & 0x02);
|
69 | });
|
70 | }
|
71 | },
|
72 | toBoolean: {
|
73 | value: function(raw) {
|
74 | return raw === 0;
|
75 | }
|
76 | }
|
77 | },
|
78 | GP2Y0D810Z0F: {
|
79 | initialize: {
|
80 | value: analogInitializer
|
81 | },
|
82 | toBoolean: {
|
83 | value: function(raw) {
|
84 | return raw >> 9 === 0;
|
85 | }
|
86 | }
|
87 | },
|
88 | GP2Y0A60SZLF: {
|
89 | initialize: {
|
90 | value: analogInitializer
|
91 | },
|
92 | toBoolean: {
|
93 | value: function(raw) {
|
94 | return raw >> 9 === 1;
|
95 | }
|
96 | }
|
97 | }
|
98 | };
|
99 |
|
100 | Controllers.GP2Y0D815Z0F = Controllers.GP2Y0D810Z0F;
|
101 |
|
102 | Controllers["HC-SR501"] = Controllers.PIR;
|
103 | Controllers["HCSR501"] = Controllers.PIR;
|
104 | Controllers["0D805"] = Controllers.GP2Y0D805Z0F;
|
105 | Controllers["805"] = Controllers.GP2Y0D805Z0F;
|
106 | Controllers["0D810"] = Controllers.GP2Y0D810Z0F;
|
107 | Controllers["810"] = Controllers.GP2Y0D810Z0F;
|
108 | Controllers["0D815"] = Controllers.GP2Y0D815Z0F;
|
109 | Controllers["815"] = Controllers.GP2Y0D815Z0F;
|
110 | Controllers["0A60SZLF"] = Controllers.GP2Y0A60SZLF;
|
111 | Controllers["60SZLF"] = Controllers.GP2Y0A60SZLF;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function Motion(opts) {
|
132 |
|
133 | if (!(this instanceof Motion)) {
|
134 | return new Motion(opts);
|
135 | }
|
136 |
|
137 | var freq = opts.freq || 25;
|
138 | var last = false;
|
139 | var controller;
|
140 | var state;
|
141 |
|
142 | Board.Component.call(
|
143 | this, opts = Board.Options(opts)
|
144 | );
|
145 |
|
146 | if (typeof opts.controller === "string") {
|
147 | controller = Controllers[opts.controller];
|
148 | } else {
|
149 | controller = opts.controller || Controllers["PIR"];
|
150 | }
|
151 |
|
152 | Board.Controller.call(this, controller, opts);
|
153 |
|
154 | state = {
|
155 | value: false,
|
156 | isCalibrated: false
|
157 | };
|
158 |
|
159 | priv.set(this, state);
|
160 |
|
161 | Object.defineProperties(this, {
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 | detectedMotion: {
|
168 | get: function() {
|
169 | return this.toBoolean(state.value);
|
170 | }
|
171 | },
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 | isCalibrated: {
|
178 | get: function() {
|
179 | return state.isCalibrated;
|
180 | }
|
181 | },
|
182 | });
|
183 |
|
184 | if (typeof this.initialize === "function") {
|
185 | this.initialize(opts, function(data) {
|
186 | state.value = data;
|
187 | });
|
188 | }
|
189 |
|
190 | setInterval(function() {
|
191 | var isChange = false;
|
192 | var eventData = {
|
193 | timestamp: Date.now(),
|
194 | detectedMotion: this.detectedMotion,
|
195 | isCalibrated: state.isCalibrated
|
196 | };
|
197 |
|
198 | if (state.isCalibrated && this.detectedMotion && !last) {
|
199 | this.emit("motionstart", eventData);
|
200 | }
|
201 |
|
202 | if (state.isCalibrated && !this.detectedMotion && last) {
|
203 | this.emit("motionend", eventData);
|
204 | }
|
205 |
|
206 | if (last !== this.detectedMotion) {
|
207 | isChange = true;
|
208 | }
|
209 |
|
210 | this.emit("data", eventData);
|
211 |
|
212 | if (isChange) {
|
213 | this.emit("change", eventData);
|
214 | }
|
215 |
|
216 | last = this.detectedMotion;
|
217 | }.bind(this), freq);
|
218 | }
|
219 |
|
220 | util.inherits(Motion, events.EventEmitter);
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | Motion.Collection = function(numsOrObjects) {
|
232 | if (!(this instanceof Motion.Collection)) {
|
233 | return new Motion.Collection(numsOrObjects);
|
234 | }
|
235 |
|
236 | Object.defineProperty(this, "type", {
|
237 | value: Motion
|
238 | });
|
239 |
|
240 | Collection.Emitter.call(this, numsOrObjects);
|
241 | };
|
242 |
|
243 | util.inherits(Motion.Collection, Collection.Emitter);
|
244 |
|
245 | Collection.installMethodForwarding(
|
246 | Motion.Collection.prototype, Motion.prototype
|
247 | );
|
248 |
|
249 |
|
250 |
|
251 | if (!!process.env.IS_TEST_MODE) {
|
252 | Motion.Controllers = Controllers;
|
253 | Motion.purge = function() {
|
254 | priv.clear();
|
255 | };
|
256 | }
|
257 |
|
258 | module.exports = Motion;
|