1 | const Emitter = require("events");
|
2 | const chalk = require("chalk");
|
3 | const Collection = require("./mixins/collection");
|
4 | const Fn = require("./fn");
|
5 | const Repl = require("./repl");
|
6 | const Options = require("./board.options");
|
7 | const Pins = require("./board.pins");
|
8 |
|
9 | let IS_TEST_MODE = !!process.env.IS_TEST_MODE;
|
10 | let Expander;
|
11 |
|
12 |
|
13 |
|
14 | const boards = [];
|
15 | const rport = /usb|acm|^com/i;
|
16 |
|
17 |
|
18 |
|
19 | const UNDEFINED = "undefined";
|
20 |
|
21 | const Serial = {
|
22 | used: [],
|
23 | attempts: [],
|
24 | detect(callback) {
|
25 |
|
26 | const maxAttempts = 10;
|
27 |
|
28 | const retryDelay = 400;
|
29 | let serialport;
|
30 |
|
31 |
|
32 | if (parseFloat(process.versions.nw) >= 0.13) {
|
33 | serialport = require("browser-serialport");
|
34 | } else {
|
35 | serialport = require("serialport");
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | serialport.list().then(results => {
|
43 | const portPaths = results.reduce((accum, result) => {
|
44 | let available = true;
|
45 |
|
46 |
|
47 |
|
48 | if (!rport.test(result.path)) {
|
49 | available = false;
|
50 | }
|
51 |
|
52 |
|
53 | if (Serial.used.includes(result.path)) {
|
54 | available = false;
|
55 | }
|
56 |
|
57 | if (available) {
|
58 | accum.push(result.path);
|
59 | }
|
60 |
|
61 | return accum;
|
62 | }, []);
|
63 |
|
64 |
|
65 | if (!portPaths.length) {
|
66 |
|
67 |
|
68 | if (IS_TEST_MODE && this.abort) {
|
69 |
|
70 | return;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | if (!Serial.attempts[Serial.used.length]) {
|
76 | Serial.attempts[Serial.used.length] = 0;
|
77 |
|
78 |
|
79 | this.info("Board", "Looking for connected device");
|
80 | }
|
81 |
|
82 |
|
83 | Serial.attempts[Serial.used.length]++;
|
84 |
|
85 |
|
86 | if (Serial.attempts[Serial.used.length] > maxAttempts) {
|
87 | this.fail("Board", "No connected device found");
|
88 | return;
|
89 | }
|
90 | setTimeout(() => {
|
91 | Serial.detect.call(this, callback);
|
92 | }, retryDelay);
|
93 |
|
94 | return;
|
95 | }
|
96 |
|
97 | this.info("Available", chalk.grey(portPaths));
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | callback.call(this, portPaths[0]);
|
103 | });
|
104 | },
|
105 |
|
106 | connect(portOrPath, callback) {
|
107 | const IO = require("firmata").Board;
|
108 |
|
109 | let caught = null;
|
110 | let io;
|
111 | let isConnected;
|
112 | let path;
|
113 | let type;
|
114 |
|
115 | if (typeof portOrPath === "object" && portOrPath.path) {
|
116 |
|
117 |
|
118 |
|
119 | path = portOrPath.path;
|
120 |
|
121 | this.info(
|
122 | (portOrPath.transport || "SerialPort"),
|
123 | chalk.grey(path)
|
124 | );
|
125 | } else {
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | path = portOrPath;
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | Serial.used.push(path);
|
140 |
|
141 | try {
|
142 | io = new IO(portOrPath, error => {
|
143 | if (error) {
|
144 | caught = error;
|
145 | }
|
146 |
|
147 | callback.call(this, caught, caught ? "error" : "ready", io);
|
148 | });
|
149 |
|
150 |
|
151 |
|
152 | io.name = "Firmata";
|
153 | io.defaultLed = 13;
|
154 | io.port = path;
|
155 |
|
156 |
|
157 | isConnected = true;
|
158 | } catch (error) {
|
159 | caught = error;
|
160 | }
|
161 |
|
162 | if (caught) {
|
163 | caught = caught.message || caught;
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 | type = isConnected ? "connect" : "error";
|
169 |
|
170 |
|
171 | callback.call(this, caught, type, io);
|
172 | }
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | class Board extends Emitter {
|
183 | constructor(options = {}) {
|
184 | super();
|
185 |
|
186 |
|
187 |
|
188 | const replContext = {};
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | let isPostponed = false;
|
200 |
|
201 |
|
202 |
|
203 | Object.assign(this, options);
|
204 |
|
205 | this.timer = null;
|
206 |
|
207 | this.isConnected = false;
|
208 |
|
209 |
|
210 | this.isReady = false;
|
211 |
|
212 |
|
213 | this.io = this.io || null;
|
214 |
|
215 |
|
216 | this.register = [];
|
217 |
|
218 |
|
219 | this.occupied = [];
|
220 |
|
221 |
|
222 | this.Drivers = {};
|
223 |
|
224 |
|
225 | if (!this.id) {
|
226 | this.id = Fn.uid();
|
227 | }
|
228 |
|
229 |
|
230 | if (typeof this.debug === UNDEFINED) {
|
231 | this.debug = true;
|
232 | }
|
233 |
|
234 |
|
235 | if (typeof this.repl === UNDEFINED) {
|
236 | this.repl = true;
|
237 | }
|
238 |
|
239 |
|
240 | if (typeof this.sigint === UNDEFINED) {
|
241 | this.sigint = true;
|
242 | }
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | this.pins = null;
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | if (this.repl) {
|
260 |
|
261 | if (Repl.ref) {
|
262 |
|
263 | replContext[this.id] = this;
|
264 |
|
265 | Repl.ref.on("ready", function() {
|
266 |
|
267 | Repl.ref.inject(replContext);
|
268 | });
|
269 |
|
270 | this.repl = Repl.ref;
|
271 | } else {
|
272 | replContext[this.id] = replContext.board = this;
|
273 | this.repl = new Repl(replContext);
|
274 | }
|
275 | }
|
276 |
|
277 | if (options.io) {
|
278 |
|
279 | this.io = options.io;
|
280 | this.isReady = options.io.isReady;
|
281 | this.transport = this.io.transport || null;
|
282 | this.port = this.io.name;
|
283 | this.pins = Board.Pins(this);
|
284 | this.RESOLUTION = Object.assign({ ADC: 1023, DAC: null, PWM: 255 }, this.io.RESOLUTION || {});
|
285 |
|
286 | } else {
|
287 |
|
288 | if (this.port && options.port) {
|
289 | Serial.connect.call(this, this.port, finalizeAndBroadcast);
|
290 | } else {
|
291 | Serial.detect.call(this, function(path) {
|
292 | Serial.connect.call(this, path, finalizeAndBroadcast);
|
293 | });
|
294 | }
|
295 | }
|
296 |
|
297 |
|
298 | if (!options.port && this.io !== null) {
|
299 |
|
300 | this.info("Available", chalk.grey(this.io.name || "unknown"));
|
301 |
|
302 | ["connect", "ready"].forEach((type) => {
|
303 | this.io.once(type, () => {
|
304 |
|
305 |
|
306 |
|
307 | if (type === "ready" && !this.isConnected) {
|
308 | isPostponed = true;
|
309 | } else {
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | finalizeAndBroadcast.call(this, null, type, this.io);
|
316 | }
|
317 |
|
318 | if (type === "connect" && isPostponed) {
|
319 | finalizeAndBroadcast.call(this, null, "ready", this.io);
|
320 | }
|
321 | });
|
322 |
|
323 | if (this.io.isReady) {
|
324 |
|
325 |
|
326 |
|
327 | process.nextTick(() => this.io.emit(type));
|
328 | }
|
329 | });
|
330 | }
|
331 |
|
332 | this.once("ready", () => {
|
333 | const hrstart = process.hrtime();
|
334 |
|
335 | this.millis = function() {
|
336 | const now = process.hrtime(hrstart);
|
337 | return (now[1] / 1000000);
|
338 | };
|
339 |
|
340 | ["close", "disconnect", "error", "string"].forEach(type => {
|
341 | this.io.on(type, data => this.emit(type, data));
|
342 | });
|
343 | });
|
344 |
|
345 |
|
346 | boards.push(this);
|
347 | }
|
348 | }
|
349 |
|
350 | function finalizeAndBroadcast(data, type, io) {
|
351 | let hasBeenEmitted = false;
|
352 |
|
353 |
|
354 | if (!this.io) {
|
355 | this.io = io;
|
356 | }
|
357 |
|
358 |
|
359 | if (type === "error") {
|
360 |
|
361 | if (data && data.message) {
|
362 | hasBeenEmitted = true;
|
363 | this.error("Error", data.message);
|
364 | }
|
365 | }
|
366 |
|
367 | if (type === "connect") {
|
368 | this.isConnected = true;
|
369 | this.port = io.port || io.name;
|
370 |
|
371 | this.info(
|
372 | "Connected",
|
373 | chalk.grey(this.port)
|
374 | );
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 | if (!IS_TEST_MODE) {
|
382 |
|
383 | this.timer = setTimeout(() => {
|
384 | this.error(
|
385 | "Device or Firmware Error",
|
386 |
|
387 | "A timeout occurred while connecting to the Board. \n\n" +
|
388 | "Please check that you've properly flashed the board with the correct firmware.\n" +
|
389 | "See: https://github.com/rwaldron/johnny-five/wiki/Getting-Started#trouble-shooting\n\n" +
|
390 | "If connecting to a Leonardo or Leonardo clone, press the 'Reset' button on the " +
|
391 | "board, wait approximately 11 seconds for complete reset, then run your program again."
|
392 | );
|
393 |
|
394 | this.emit("error", new Error("A timeout occurred while connecting to the Board."));
|
395 | }, this.timeout || 1e4);
|
396 | }
|
397 | }
|
398 |
|
399 | if (type === "ready") {
|
400 | if (this.timer) {
|
401 | clearTimeout(this.timer);
|
402 | }
|
403 |
|
404 |
|
405 | this.isReady = true;
|
406 | this.pins = Board.Pins(this);
|
407 | this.MODES = this.io.MODES;
|
408 |
|
409 | if (typeof io.debug !== UNDEFINED &&
|
410 | io.debug === false) {
|
411 | this.debug = false;
|
412 | }
|
413 |
|
414 | if (typeof io.repl !== UNDEFINED &&
|
415 | io.repl === false) {
|
416 | this.repl = false;
|
417 | }
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | if (this.repl) {
|
428 | this.repl.initialize(() => this.emit("ready"));
|
429 | }
|
430 |
|
431 | if (io.name !== "Mock" && this.sigint) {
|
432 | process.on("SIGINT", () => {
|
433 |
|
434 | const failExitTimeout = 1000;
|
435 |
|
436 | this.emit("exit");
|
437 | this.warn("Board", "Closing.");
|
438 |
|
439 | const timeout = setTimeout(() => {
|
440 | process.reallyExit();
|
441 | }, failExitTimeout);
|
442 | const interval = setInterval(() => {
|
443 | if (!this.io.pending) {
|
444 | clearInterval(interval);
|
445 | clearTimeout(timeout);
|
446 | process.nextTick(process.reallyExit);
|
447 | }
|
448 | }, 1);
|
449 | });
|
450 | }
|
451 |
|
452 |
|
453 |
|
454 | this.RESOLUTION = Object.assign({ ADC: 1023, DAC: null, PWM: 255 }, io.RESOLUTION || {});
|
455 |
|
456 | }
|
457 |
|
458 |
|
459 | if (this.repl) {
|
460 |
|
461 |
|
462 |
|
463 | if (type === "connect") {
|
464 | this.emit(type, data);
|
465 | }
|
466 | } else {
|
467 |
|
468 | if (!hasBeenEmitted) {
|
469 | this.emit(type, data);
|
470 | }
|
471 | }
|
472 | }
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 | [
|
483 | "digitalWrite", "analogWrite",
|
484 | "analogRead", "digitalRead",
|
485 | "pinMode", "queryPinState",
|
486 | "stepperConfig", "stepperStep",
|
487 | "sendI2CConfig", "sendI2CWriteRequest", "sendI2CReadRequest",
|
488 | "i2cConfig", "i2cWrite", "i2cWriteReg", "i2cRead", "i2cReadOnce",
|
489 | "pwmWrite",
|
490 | "servoConfig", "servoWrite",
|
491 | "sysexCommand", "sysexResponse",
|
492 | "serialConfig", "serialWrite", "serialRead", "serialStop", "serialClose", "serialFlush", "serialListen",
|
493 | ].forEach(function(method) {
|
494 |
|
495 | Board.prototype[method] = function() {
|
496 | this.io[method].apply(this.io, arguments);
|
497 | return this;
|
498 | };
|
499 | });
|
500 |
|
501 |
|
502 | Board.prototype.snapshot = function(reducer) {
|
503 | const blacklist = this.snapshot.blacklist;
|
504 | const special = this.snapshot.special;
|
505 | const hasReducer = typeof reducer === "function";
|
506 |
|
507 | return this.register.reduce((cAccum, component) => {
|
508 |
|
509 | if (typeof component.components === UNDEFINED) {
|
510 | cAccum.push(
|
511 | Object.getOwnPropertyNames(component).reduce((pAccum, prop) => {
|
512 | const value = component[prop];
|
513 |
|
514 | if (!blacklist.includes(prop) && typeof value !== "function") {
|
515 |
|
516 | if (hasReducer) {
|
517 | const result = reducer(prop, value, component);
|
518 |
|
519 | if (result !== undefined) {
|
520 | pAccum[prop] = result;
|
521 | }
|
522 | } else {
|
523 | pAccum[prop] = special[prop] ?
|
524 | special[prop](value) : value;
|
525 | }
|
526 | }
|
527 | return pAccum;
|
528 | }, Object.create(null))
|
529 | );
|
530 | }
|
531 |
|
532 | return cAccum;
|
533 | }, []);
|
534 | };
|
535 |
|
536 | Board.prototype.serialize = function(reducer) {
|
537 | return JSON.stringify(this.snapshot(reducer));
|
538 | };
|
539 |
|
540 | Board.prototype.snapshot.blacklist = [
|
541 | "board", "io", "_events", "_eventsCount", "state",
|
542 | ];
|
543 |
|
544 | Board.prototype.samplingInterval = function(ms) {
|
545 |
|
546 | if (this.io.setSamplingInterval) {
|
547 | this.io.setSamplingInterval(ms);
|
548 | } else {
|
549 | throw new Error("This IO plugin does not implement an interval adjustment method");
|
550 | }
|
551 | return this;
|
552 | };
|
553 |
|
554 |
|
555 | Board.prototype.snapshot.special = {
|
556 | mode: function(value) {
|
557 | return ["INPUT", "OUTPUT", "ANALOG", "PWM", "SERVO"][value] || "unknown";
|
558 | }
|
559 | };
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 | Board.prototype.shiftOut = function(dataPin, clockPin, isBigEndian, value) {
|
566 | if (arguments.length === 3) {
|
567 | value = isBigEndian;
|
568 | isBigEndian = true;
|
569 | }
|
570 |
|
571 | for (let i = 0; i < 8; i++) {
|
572 | this.io.digitalWrite(clockPin, 0);
|
573 | if (isBigEndian) {
|
574 | this.io.digitalWrite(dataPin, !!(value & (1 << (7 - i))) | 0);
|
575 | } else {
|
576 | this.io.digitalWrite(dataPin, !!(value & (1 << i)) | 0);
|
577 | }
|
578 | this.io.digitalWrite(clockPin, 1);
|
579 | }
|
580 | };
|
581 |
|
582 | const logging = {
|
583 | specials: [
|
584 | "error",
|
585 | "fail",
|
586 | "warn",
|
587 | "info",
|
588 | ],
|
589 | colors: {
|
590 | log: "white",
|
591 | error: "red",
|
592 | fail: "inverse",
|
593 | warn: "yellow",
|
594 | info: "cyan"
|
595 | }
|
596 | };
|
597 |
|
598 | Board.prototype.log = function( /* type, klass, message [, long description] */ ) {
|
599 | var args = Array.from(arguments);
|
600 |
|
601 |
|
602 |
|
603 | if (!logging.specials.includes(args[0])) {
|
604 | args.unshift("log");
|
605 | }
|
606 |
|
607 | var type = args.shift();
|
608 | var klass = args.shift();
|
609 | var message = args.shift();
|
610 | var color = logging.colors[type];
|
611 | var now = Date.now();
|
612 | var event = {
|
613 | type: type,
|
614 | timestamp: now,
|
615 | class: klass,
|
616 | message: "",
|
617 | data: null,
|
618 | };
|
619 |
|
620 | if (typeof args[args.length - 1] === "object") {
|
621 | event.data = args.pop();
|
622 | }
|
623 |
|
624 | message += " " + args.join(", ");
|
625 | event.message = message.trim();
|
626 |
|
627 |
|
628 | if (this.debug) {
|
629 |
|
630 | console.log([
|
631 |
|
632 | chalk.grey(now),
|
633 |
|
634 | chalk.magenta(klass),
|
635 |
|
636 | chalk[color](message),
|
637 |
|
638 | args.join(", ")
|
639 | ].join(" "));
|
640 | }
|
641 |
|
642 | this.emit(type, event);
|
643 | this.emit("message", event);
|
644 | };
|
645 |
|
646 |
|
647 |
|
648 | logging.specials.forEach(function(type) {
|
649 | Board.prototype[type] = function() {
|
650 | var args = [].slice.call(arguments);
|
651 | args.unshift(type);
|
652 |
|
653 | this.log.apply(this, args);
|
654 | };
|
655 | });
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 | Board.prototype.wait = function(time, callback) {
|
689 | setTimeout(callback, time);
|
690 | return this;
|
691 | };
|
692 |
|
693 | Board.prototype.loop = function(time, callback) {
|
694 | var handler = function() {
|
695 | callback(function() {
|
696 | clearInterval(interval);
|
697 | });
|
698 | };
|
699 | var interval = setInterval(handler, time);
|
700 | return this;
|
701 | };
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 | Board.map = Fn.map;
|
712 | Board.fmap = Fn.fmap;
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 | Board.constrain = Fn.constrain;
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 | Board.range = Fn.range;
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 | Board.uid = Fn.uid;
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 | Board.mount = function(arg) {
|
751 | var index = typeof arg === "number" && arg,
|
752 | hardware;
|
753 |
|
754 |
|
755 | if (arg && arg.board) {
|
756 | return arg.board;
|
757 | }
|
758 |
|
759 |
|
760 |
|
761 |
|
762 | if (typeof index === "number") {
|
763 | hardware = boards[index];
|
764 | return hardware ? hardware : null;
|
765 | }
|
766 |
|
767 |
|
768 |
|
769 | if (boards.length) {
|
770 | return boards[0];
|
771 | }
|
772 |
|
773 |
|
774 | return null;
|
775 | };
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 | Board.Component = function(opts, componentOpts) {
|
796 | if (typeof opts === UNDEFINED) {
|
797 | opts = {};
|
798 | }
|
799 |
|
800 | if (typeof componentOpts === UNDEFINED) {
|
801 | componentOpts = {};
|
802 | }
|
803 |
|
804 |
|
805 | this.board = Board.mount(opts);
|
806 | this.io = this.board.io;
|
807 |
|
808 |
|
809 | this.id = opts.id || Board.uid();
|
810 | this.custom = opts.custom || {};
|
811 |
|
812 | var originalPins;
|
813 |
|
814 | if (typeof opts.pin === "number" || typeof opts.pin === "string") {
|
815 | originalPins = [opts.pin];
|
816 | } else {
|
817 | if (Array.isArray(opts.pins)) {
|
818 | originalPins = opts.pins.slice();
|
819 | } else {
|
820 | if (typeof opts.pins === "object" && opts.pins !== null) {
|
821 |
|
822 | var pinset = opts.pins || opts.pin;
|
823 |
|
824 | originalPins = [];
|
825 | for (var p in pinset) {
|
826 | originalPins.push(pinset[p]);
|
827 | }
|
828 | }
|
829 | }
|
830 | }
|
831 |
|
832 | if (opts.controller) {
|
833 |
|
834 | if (typeof opts.controller === "string") {
|
835 | opts.controller = opts.controller.replace(/-/g, "");
|
836 | }
|
837 |
|
838 | if (!Expander) {
|
839 | Expander = require("./expander");
|
840 | }
|
841 |
|
842 | if (Expander.hasController(opts.controller)) {
|
843 | componentOpts = {
|
844 | normalizePin: false,
|
845 | requestPin: false,
|
846 | };
|
847 | }
|
848 | }
|
849 |
|
850 | componentOpts = Board.Component.initialization(componentOpts);
|
851 |
|
852 | if (componentOpts.normalizePin) {
|
853 | opts = Board.Pins.normalize(opts, this.board);
|
854 | }
|
855 |
|
856 | if (typeof opts.pins !== UNDEFINED) {
|
857 | this.pins = opts.pins || [];
|
858 | }
|
859 |
|
860 | if (typeof opts.pin !== UNDEFINED) {
|
861 | this.pin = opts.pin;
|
862 | }
|
863 |
|
864 |
|
865 |
|
866 | if (typeof opts.emitter !== UNDEFINED) {
|
867 |
|
868 | this.emitter = opts.emitter;
|
869 | }
|
870 |
|
871 | if (typeof opts.address !== UNDEFINED) {
|
872 | this.address = opts.address;
|
873 | }
|
874 |
|
875 | if (typeof opts.controller !== UNDEFINED) {
|
876 | this.controller = opts.controller;
|
877 | }
|
878 |
|
879 |
|
880 |
|
881 | if (typeof opts.bus !== UNDEFINED) {
|
882 |
|
883 | this.bus = opts.bus;
|
884 | }
|
885 |
|
886 | this.board.register.push(this);
|
887 | };
|
888 |
|
889 | Board.Component.initialization = function(opts) {
|
890 | var defaults = {
|
891 | requestPin: true,
|
892 | normalizePin: true
|
893 | };
|
894 |
|
895 | return Object.assign({}, defaults, opts);
|
896 | };
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 | Board.Controller = function(controllers, options) {
|
912 |
|
913 | let controller;
|
914 |
|
915 | if (typeof options.controller === "string") {
|
916 | controller = controllers[options.controller] || controllers[options.controller.toUpperCase()];
|
917 | } else {
|
918 | controller = options.controller || controllers.DEFAULT || null;
|
919 | }
|
920 |
|
921 | if (controller === null) {
|
922 | throw new Error("No Valid Controller Found");
|
923 | }
|
924 |
|
925 | let requirements = controller.requirements && controller.requirements.value;
|
926 |
|
927 | if (requirements) {
|
928 |
|
929 | if (requirements.options) {
|
930 | Object.keys(requirements.options).forEach(function(key) {
|
931 | |
932 |
|
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 | if (typeof options[key] === UNDEFINED ||
|
945 | typeof options[key] !== requirements.options[key].typeof) {
|
946 | if (requirements.options[key].throws) {
|
947 | throw new Error(requirements.options[key].message);
|
948 | } else {
|
949 | this.board.warn(this.constructor.name, requirements.options[key].message);
|
950 | }
|
951 | }
|
952 | }, this);
|
953 | }
|
954 | }
|
955 |
|
956 | Object.defineProperties(this, controller);
|
957 | };
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | Board.Pins = Pins;
|
965 | Board.Options = function(options) {
|
966 | return new Options(options);
|
967 | };
|
968 |
|
969 |
|
970 |
|
971 | Object.defineProperty(Board, "cache", {
|
972 | get() {
|
973 | return boards;
|
974 | }
|
975 | });
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 | Board.Event = function(event) {
|
985 |
|
986 | if (typeof event === UNDEFINED) {
|
987 | throw new Error("Board.Event missing Event object");
|
988 | }
|
989 |
|
990 |
|
991 | this.type = event.type || "data";
|
992 |
|
993 |
|
994 | this.target = event.target || null;
|
995 |
|
996 |
|
997 |
|
998 | Object.assign(this, event);
|
999 | };
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 | class Boards extends Collection {
|
1014 | constructor(options) {
|
1015 | let ports;
|
1016 |
|
1017 |
|
1018 | if (Array.isArray(options)) {
|
1019 | ports = options.slice();
|
1020 | options = {
|
1021 | ports,
|
1022 | };
|
1023 | }
|
1024 |
|
1025 |
|
1026 |
|
1027 | if (!Array.isArray(options) && typeof options === "object" && options.ports !== undefined) {
|
1028 | ports = options.ports;
|
1029 | }
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 | if (!Array.isArray(ports)) {
|
1035 | throw new Error("Expected ports to be an array");
|
1036 | }
|
1037 |
|
1038 | if (typeof options.debug === UNDEFINED) {
|
1039 | options.debug = true;
|
1040 | }
|
1041 |
|
1042 | if (typeof options.repl === UNDEFINED) {
|
1043 | options.repl = true;
|
1044 | }
|
1045 |
|
1046 | const initialized = {};
|
1047 | const noRepl = ports.some(({repl}) => repl === false);
|
1048 | const noDebug = ports.some(({debug}) => debug === false);
|
1049 | const boardObjects = ports.map((port) => {
|
1050 | let portOpts;
|
1051 |
|
1052 | if (typeof port === "string") {
|
1053 | portOpts = {};
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 | if (rport.test(port)) {
|
1059 | portOpts.port = port;
|
1060 | } else {
|
1061 |
|
1062 |
|
1063 |
|
1064 | portOpts.id = port;
|
1065 | }
|
1066 | } else {
|
1067 | portOpts = port;
|
1068 | }
|
1069 |
|
1070 |
|
1071 | portOpts.repl = false;
|
1072 |
|
1073 | return new Board(portOpts);
|
1074 | });
|
1075 |
|
1076 | super(boardObjects);
|
1077 |
|
1078 |
|
1079 | this.debug = options.debug;
|
1080 | this.repl = options.repl;
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 | if (noDebug) {
|
1089 | this.debug = false;
|
1090 | }
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 | if (noRepl) {
|
1097 | this.repl = false;
|
1098 | }
|
1099 |
|
1100 | const expecteds = this.map((board, index) => {
|
1101 | initialized[board.id] = board;
|
1102 | return new Promise((resolve) => {
|
1103 | this[index].on("error", error => this.emit("error", error));
|
1104 | this[index].on("fail", event => this.emit("fail", event));
|
1105 | this[index].on("ready", () => resolve(this[index]));
|
1106 | });
|
1107 | });
|
1108 |
|
1109 | Promise.all(expecteds).then((/* boards */) => {
|
1110 | this.each(board => {
|
1111 | board.info("Board ID: ", chalk.green(board.id));
|
1112 | });
|
1113 |
|
1114 |
|
1115 |
|
1116 | if (this.repl) {
|
1117 | this.repl = new Repl(
|
1118 | Object.assign({}, initialized, {
|
1119 | board: this
|
1120 | })
|
1121 | );
|
1122 | this.repl.initialize(() => this.emit("ready", initialized));
|
1123 | } else {
|
1124 |
|
1125 | this.emit("ready", initialized);
|
1126 | }
|
1127 | }).catch(error => {
|
1128 | console.error(chalk.red(error));
|
1129 | });
|
1130 | }
|
1131 |
|
1132 | static get type() {
|
1133 | return Board;
|
1134 | }
|
1135 | }
|
1136 |
|
1137 | Collection.installMethodForwarding(
|
1138 | Boards.prototype, Board.prototype
|
1139 | );
|
1140 |
|
1141 | Object.assign(
|
1142 | Boards.prototype,
|
1143 | Emitter.prototype
|
1144 | );
|
1145 |
|
1146 | Boards.prototype.byId = function(id) {
|
1147 | for (var i = 0; i < this.length; i++) {
|
1148 | if (this[i].id === id) {
|
1149 | return this[i];
|
1150 | }
|
1151 | }
|
1152 |
|
1153 | return null;
|
1154 | };
|
1155 |
|
1156 | Boards.prototype.log = Board.prototype.log;
|
1157 |
|
1158 | logging.specials.forEach(function(type) {
|
1159 |
|
1160 | Boards.prototype[type] = function() {
|
1161 | var args = [].slice.call(arguments);
|
1162 | args.unshift(type);
|
1163 |
|
1164 | this.log.apply(this, args);
|
1165 | };
|
1166 | });
|
1167 |
|
1168 |
|
1169 | if (IS_TEST_MODE) {
|
1170 | Serial.purge = function() {
|
1171 | Serial.used.length = 0;
|
1172 | };
|
1173 | Board.Serial = Serial;
|
1174 |
|
1175 | Board.purge = function() {
|
1176 | Board.Pins.normalize.clear();
|
1177 | Repl.isActive = false;
|
1178 | Repl.isBlocked = true;
|
1179 | Repl.ref = null;
|
1180 | boards.length = 0;
|
1181 | };
|
1182 |
|
1183 | Board.testMode = function(state) {
|
1184 | if (!arguments.length) {
|
1185 | return IS_TEST_MODE;
|
1186 | } else {
|
1187 | IS_TEST_MODE = state;
|
1188 | }
|
1189 | };
|
1190 | }
|
1191 |
|
1192 |
|
1193 | Board.Array = Boards;
|
1194 | Board.Collection = Boards;
|
1195 |
|
1196 | module.exports = Board;
|
1197 |
|
1198 |
|
1199 |
|