Skip to content

Commit

Permalink
lib/index.js: updates
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jun 23, 2017
1 parent 62534a4 commit 690088d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 98 deletions.
93 changes: 55 additions & 38 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"use strict";

require("../lib/hack-shims");
if (!Object.values) {
Object.values = function(object) {
return Object.getOwnPropertyNames(object).map(name => object[name]);
};
}

const os = require("os");
const Emitter = require("events").EventEmitter;
const events = require("events");

const { EventEmitter: Emitter } = events;

let tessel;
let factory;

Expand Down Expand Up @@ -39,7 +44,7 @@ if (process.env.IS_TEST_MODE) {
tessel = require("tessel");
}

const priv = new Map();
const priv = new WeakMap();
const tessels = [];

const MODES = Object.freeze({
Expand Down Expand Up @@ -71,32 +76,40 @@ const SERIAL_PIN_TYPES = Object.freeze({
RES_TX3: 0x07,
});

const PIN_MODE_DEFS = Object.freeze([
const PIN_DEFS = Object.freeze([
// Port A
{ modes: [0, 1, 6] },
{ modes: [0, 1, 6] },
{ modes: [0, 1 ] },
{ modes: [0, 1 ] },
{ modes: [0, 1, 2 ], analogChannel: 0 },
{ modes: [0, 1, 3, 4, 10 ] },
{ modes: [0, 1, 3, 4, 10 ] },
{ modes: [0, 1, 2 ], analogChannel: 1 },
{ modes: [0, 1] },
{ modes: [0, 1] },
{ modes: [0, 1, 2] },
{ modes: [0, 1, 3, 4, 10] },
{ modes: [0, 1, 3, 4, 10] },
{ modes: [0, 1, 2] },
// Port B
{ modes: [0, 1, 2 , 6], analogChannel: 2 },
{ modes: [0, 1, 2 , 6], analogChannel: 3 },
{ modes: [0, 1, 2 ], analogChannel: 4 },
{ modes: [0, 1, 2 ], analogChannel: 5 },
{ modes: [0, 1, 2 ], analogChannel: 6 },
{ modes: [0, 1, 2, 3, 4, 10 ], analogChannel: 7 },
{ modes: [0, 1, 2, 3, 4, 10 ], analogChannel: 8 },
{ modes: [0, 1, 2, 3 ], analogChannel: 9 },
{ modes: [0, 1, 2, 6] },
{ modes: [0, 1, 2, 6] },
{ modes: [0, 1, 2] },
{ modes: [0, 1, 2] },
{ modes: [0, 1, 2] },
{ modes: [0, 1, 2, 3, 4, 10] },
{ modes: [0, 1, 2, 3, 4, 10] },
{ modes: [0, 1, 2, 3] },
// LEDs
{ modes: [1] },
{ modes: [1] },
{ modes: [1] },
{ modes: [1] },
]);

let ac = 0;

PIN_DEFS.forEach(def => {
if (def.modes.includes(2)) {
def.analogChannel = ac++;
}
});

const PIN_DATA_POOL = Array.from({ length: 20 }, _ => Object.create(null));

const A = "A";
Expand Down Expand Up @@ -168,7 +181,7 @@ function ToPinIndex(value) {
}

if (!Number.isNaN(index)) {
if (index >= 0 && index < PIN_MODE_DEFS.length) {
if (index >= 0 && index < PIN_DEFS.length) {
return index;
} else {
return -1;
Expand All @@ -190,7 +203,7 @@ function ToPinIndex(value) {
// Out of range, no pin object exists here.
// TODO: Determine if and how this is reachable.
/* istanbul ignore if */
if (PIN_MODE_DEFS[pinIndex] === undefined) {
if (PIN_DEFS[pinIndex] === undefined) {
return -1;
}

Expand All @@ -206,8 +219,8 @@ function ToPortIdentity(index) {
index = +index;
const port = PORTS.BY_NUMBER[index >> 3];

if ((index < 0 || index >= PIN_MODE_DEFS.length) ||
port === undefined || PIN_MODE_DEFS[index] === undefined) {
if ((index < 0 || index >= PIN_DEFS.length) ||
port === undefined || PIN_DEFS[index] === undefined) {
return ToPortIdentity.cache.null;
}

Expand Down Expand Up @@ -288,16 +301,18 @@ Port.CMD = {
};

Port.REPLY = {
ACK: 0x80,
NACK: 0x81,
// Unused
// ACK: 0x80,
// NACK: 0x81,
HIGH: 0x82,
LOW: 0x83,
DATA: 0x84,

MIN_ASYNC: 0xA0,
// Unused
// MIN_ASYNC: 0xA0,
// c0 to c8 is all async pin assignments
ASYNC_PIN_CHANGE_N: 0xC0,
ASYNC_UART_RX: 0xD0
// ASYNC_PIN_CHANGE_N: 0xC0,
// ASYNC_UART_RX: 0xD0
};

const adcReadRequests = [];
Expand Down Expand Up @@ -492,6 +507,7 @@ class Pin extends Emitter {
callback,
});

/* istanbul ignore else */
if (!adcReadRequestLoop.isActive) {
adcReadRequestLoop();
}
Expand Down Expand Up @@ -588,7 +604,7 @@ class Board extends Emitter {
this.analogPins = ANALOG_PINS;

// List of all pin objects
this.pins = PIN_MODE_DEFS.map((config, index) => {
this.pins = PIN_DEFS.map((config, index) => {
return new Pin(Object.assign(PIN_DATA_POOL[index], config, ToPortIdentity(index)));
});

Expand All @@ -602,11 +618,11 @@ class Board extends Emitter {

// There is actually nothing to wait for here, but we MUST
// emit this asynchronously to avoid "releasing zalgo"
setImmediate(_ => this.emit("connect"));
setImmediate(() => this.emit("connect"));

// The "ready event" is needed to signal to Johnny-Five that
// communication with the pinouts is ready.
setImmediate(_ => {
setImmediate(() => {
this.isReady = true;
this.emit("ready");
});
Expand Down Expand Up @@ -835,7 +851,7 @@ class Board extends Emitter {
}
}

buffer = new Buffer([cmdRegOrData].concat(dataBytes));
buffer = new Buffer([cmdRegOrData, ...dataBytes]);

/* istanbul ignore else */
if (buffer.length) {
Expand Down Expand Up @@ -937,13 +953,14 @@ class Board extends Emitter {
if (options.portId === undefined ||
(options.portId !== A && options.portId !== B)) {
throw new Error(`serialConfig expected "options.portId" (A or B)`);

}

const baud = options.baud || 57600;
const dataBits = options.dataBits || 8;
const parity = options.parity || "none";
const stopBits = options.stopBits || 1;
const {
baud = 57600,
dataBits = 8,
parity = "none",
stopBits = 1,
} = options;

state.uart[options.portId] = new tessel.port[options.portId].UART({
// "baud" is the property that was already defined by Firmata
Expand Down
Loading

0 comments on commit 690088d

Please sign in to comment.