-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbutton.js
365 lines (314 loc) · 8.39 KB
/
button.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const Board = require("./board");
const Collection = require("./mixins/collection");
const Emitter = require("./mixins/emitter");
const EVS = require("./evshield");
const Fn = require("./fn");
const priv = new Map();
const Pins = Board.Pins;
const aliases = {
down: ["down", "press"],
up: ["up", "release"]
};
const Controllers = {
DEFAULT: {
initialize: {
value(options, callback) {
const state = priv.get(this);
if (Pins.isFirmata(this) &&
(typeof options.pinValue === "string" &&
(options.pinValue.length > 1 && options.pinValue[0] === "A"))) {
options.pinValue = this.io.analogPins[+options.pinValue.slice(1)];
}
this.pin = Number.isNaN(+options.pinValue) ? options.pinValue : +options.pinValue;
this.io.pinMode(this.pin, this.io.MODES.INPUT);
// Enable the pullup resistor after setting pin mode
if (this.pullup) {
this.io.digitalWrite(this.pin, this.io.HIGH);
}
// Enable the pulldown resistor after setting pin mode
if (this.pulldown) {
this.io.digitalWrite(this.pin, this.io.LOW);
}
this.io.digitalRead(this.pin, data => {
if (data !== state.last) {
callback(data);
}
});
}
},
toBoolean: {
writable: true,
value(raw) {
return raw === this.downValue;
}
}
},
TINKERKIT: {
initialize: {
value(options, callback) {
const state = priv.get(this);
let value = 0;
this.io.pinMode(this.pin, this.io.MODES.ANALOG);
this.io.analogRead(this.pin, data => {
data = data > 512 ? 1 : 0;
// This condition simulates digitalRead's
// behavior of limiting calls to changes in
// pin value.
/* istanbul ignore else */
if (data !== value && data !== state.last) {
callback(data);
}
value = data;
});
}
},
toBoolean: {
writable: true,
value(raw) {
return raw === this.downValue;
}
}
},
EVS_EV3: {
initialize: {
value(options, callback) {
const state = priv.get(this);
state.previous = 0;
state.shield = EVS.shieldPort(options.pin);
state.register = EVS.Touch;
state.ev3 = new EVS(Object.assign(options, {
io: this.io
}));
state.ev3.setup(state.shield, EVS.Type_EV3_TOUCH);
state.ev3.read(state.shield, EVS.Touch, EVS.Touch_Bytes, data => {
const value = data[0];
// Since i2cRead is continuous regardless of the reading,
// and digitalRead is continuous but only called for changes
// in reading value, we need to suppress repeated calls to
// callback by limiting to only changed values.
/* istanbul ignore else */
if (state.previous !== value) {
callback(value);
}
state.previous = value;
});
}
},
toBoolean: {
writable: true,
value(raw) {
return raw === this.downValue;
}
}
},
EVS_NXT: {
initialize: {
value(options, callback) {
const state = priv.get(this);
state.previous = 0;
state.shield = EVS.shieldPort(options.pin);
state.ev3 = new EVS(Object.assign(options, {
io: this.io
}));
state.ev3.setup(state.shield, EVS.Type_ANALOG);
state.ev3.read(state.shield, state.shield.analog, EVS.Analog_Bytes, data => {
let value = data[0] | (data[1] << 8);
// Since i2cRead is continuous regardless of the reading,
// and digitalRead is continuous but only called for changes
// in reading value, we need to suppress repeated calls to
// callback by limiting to only changed values.
value = value < 300 ? 1 : 0;
/* istanbul ignore else */
if (state.previous !== value) {
callback(value);
}
state.previous = value;
});
}
},
toBoolean: {
writable: true,
value(raw) {
return raw === this.downValue;
}
}
}
};
/**
* Button
* @constructor
*
* Button();
*
* Button({
* pin: 10
* });
*
*
* @param {Object} options [description]
*
*/
class Button extends Emitter {
constructor(options) {
super();
let raw;
let invert = false;
let downValue = 1;
let upValue = 0;
const state = {
interval: null,
last: null
};
// Create a debounce boundary on event triggers
// this avoids button events firing on
// press noise and false positives
const trigger = Fn.debounce(key => {
aliases[key].forEach(type => this.emit(type));
}, 7);
let pinValue = typeof options === "object" ? options.pin : options;
Board.Component.call(
this, options = Board.Options(options)
);
Board.Controller.call(this, Controllers, options);
options.pinValue = pinValue;
// `holdtime` is used by an interval to determine
// if the button has been released within a specified
// time frame, in milliseconds.
this.holdtime = options.holdtime || 500;
// `options.isPullup` is included as part of an effort to
// phase out "isFoo" options properties
this.pullup = options.pullup || options.isPullup || false;
this.pulldown = options.pulldown || options.isPulldown || false;
// Turns out some button circuits will send
// 0 for up and 1 for down, and some the inverse,
// so we can invert our function with this option.
// Default to invert in pullup mode, but use options.invert
// if explicitly defined (even if false)
invert = typeof options.invert !== "undefined" ?
options.invert : (this.pullup || false);
if (invert) {
downValue = downValue ^ 1;
upValue = upValue ^ 1;
}
state.last = upValue;
// Create a "state" entry for privately
// storing the state of the button
priv.set(this, state);
Object.defineProperties(this, {
value: {
get() {
return Number(this.isDown);
}
},
invert: {
get() {
return invert;
},
set(value) {
invert = value;
downValue = invert ? 0 : 1;
upValue = invert ? 1 : 0;
state.last = upValue;
}
},
downValue: {
get() {
return downValue;
},
set(value) {
downValue = value;
upValue = value ^ 1;
invert = value ? true : false;
state.last = upValue;
}
},
upValue: {
get() {
return upValue;
},
set(value) {
upValue = value;
downValue = value ^ 1;
invert = value ? true : false;
state.last = downValue;
}
},
isDown: {
get() {
return this.toBoolean(raw);
}
}
});
/* istanbul ignore else */
if (typeof this.initialize === "function") {
this.initialize(options, data => {
// Update the raw data value, which
// is used by isDown = toBoolean()
raw = data;
if (!this.isDown) {
/* istanbul ignore else */
if (state.interval) {
clearInterval(state.interval);
}
trigger("up");
}
if (this.isDown) {
trigger("down");
state.interval = setInterval(() => {
/* istanbul ignore else */
if (this.isDown) {
this.emit("hold");
}
}, this.holdtime);
}
state.last = data;
});
}
}
}
/**
* Fired when the button is pressed down
*
* @event
* @name down
* @memberOf Button
*/
/**
* Fired when the button is held
*
* @event
* @name hold
* @memberOf Button
*/
/**
* Fired when the button is released
*
* @event
* @name up
* @memberOf Button
*/
/**
* Buttons()
* new Buttons()
*/
class Buttons extends Collection.Emitter {
constructor(numsOrObjects) {
super(numsOrObjects);
}
get type() {
return Button;
}
}
Collection.installMethodForwarding(
Buttons.prototype, Button.prototype
);
// Assign Buttons Collection class as static "method" of Button.
Button.Collection = Buttons;
/* istanbul ignore else */
if (!!process.env.IS_TEST_MODE) {
Button.Controllers = Controllers;
Button.purge = function() {
priv.clear();
};
}
module.exports = Button;