Skip to content

Commit

Permalink
Light: Modernizing the source (class syntax and other small changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Sep 18, 2019
1 parent 6fe5c2c commit b5a7f4f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 167 deletions.
186 changes: 86 additions & 100 deletions lib/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ const EVS = require("./evshield");
const within = require("./mixins/within");
const { uint16, toFixed, scale } = require("./fn");
const Emitter = require("events");
const util = require("util");
const priv = new Map();

const Controllers = {
DEFAULT: {
initialize: {
value(opts, dataHandler) {
value(options, dataHandler) {
this.io.pinMode(this.pin, this.io.MODES.ANALOG);
this.io.analogRead(this.pin, dataHandler);
},
Expand All @@ -22,17 +21,17 @@ const Controllers = {
},
EVS_EV3: {
initialize: {
value(opts, dataHandler) {
value(options, dataHandler) {
const state = priv.get(this);

if (opts.mode) {
opts.mode = opts.mode.toUpperCase();
if (options.mode) {
options.mode = options.mode.toUpperCase();
}

state.mode = opts.mode === "REFLECTED" ? EVS.Type_EV3_LIGHT_REFLECTED : EVS.Type_EV3_LIGHT;
state.mode = options.mode === "REFLECTED" ? EVS.Type_EV3_LIGHT_REFLECTED : EVS.Type_EV3_LIGHT;

state.shield = EVS.shieldPort(opts.pin);
state.ev3 = new EVS(Object.assign(opts, {
state.shield = EVS.shieldPort(options.pin);
state.ev3 = new EVS(Object.assign(options, {
io: this.io
}));
state.ev3.setup(state.shield, EVS.Type_EV3);
Expand All @@ -51,23 +50,22 @@ const Controllers = {
},
EVS_NXT: {
initialize: {
value(opts, dataHandler) {
value(options, dataHandler) {
const state = priv.get(this);

if (opts.mode) {
opts.mode = opts.mode.toUpperCase();
if (options.mode) {
options.mode = options.mode.toUpperCase();
}

state.mode = opts.mode === "REFLECTED" ? EVS.Type_NXT_LIGHT_REFLECTED : EVS.Type_NXT_LIGHT;
state.mode = options.mode === "REFLECTED" ? EVS.Type_NXT_LIGHT_REFLECTED : EVS.Type_NXT_LIGHT;

state.shield = EVS.shieldPort(opts.pin);
state.ev3 = new EVS(Object.assign(opts, {
state.shield = EVS.shieldPort(options.pin);
state.ev3 = new EVS(Object.assign(options, {
io: this.io
}));
state.ev3.setup(state.shield, state.mode);
state.ev3.read(state.shield, state.shield.analog, EVS.Analog_Bytes, (data) => {
const value = data[0] | (data[1] << 8);
dataHandler(value);
dataHandler(data[0] | (data[1] << 8));
});
}
},
Expand All @@ -91,13 +89,13 @@ const Controllers = {
},

initialize: {
value(opts, dataHandler) {
const address = opts.address || 0x39;
value(options, dataHandler) {
const address = options.address || 0x39;
const command = byte => byte | 0b10000000;

opts.address = address;
options.address = address;

this.io.i2cConfig(opts);
this.io.i2cConfig(options);

// Page 15
// Control Register (0h)
Expand Down Expand Up @@ -229,14 +227,14 @@ const Controllers = {
let Tint = TintMs[TintIndex];
let lux = 0;

// if (typeof opts.gain !== "undefined") {
// if (typeof options.gain !== "undefined") {
// isAutoGain = false;
// gain = opts.gain;
// gain = options.gain;
// }

// if (typeof opts.integration !== "undefined") {
// if (typeof options.integration !== "undefined") {
// isAutoGain = false;
// Tint = opts.integration;
// Tint = options.integration;
// }


Expand Down Expand Up @@ -395,19 +393,16 @@ const Controllers = {
value: [0x23, 0x5C]
},
initialize: {
value(opts, dataHandler) {
const address = opts.address || 0x23;
const mode = opts.mode || 0x10;
opts.address = address;
this.io.i2cConfig(opts);
value(options, dataHandler) {
const address = options.address || 0x23;
const mode = options.mode || 0x10;
options.address = address;
this.io.i2cConfig(options);
this.io.i2cWrite(address, mode);
const read = () => {
setTimeout(() => {
this.io.i2cReadOnce(address, 2, (data) => {
let raw = data[0];
raw <<= 8;
raw |= data[1];
dataHandler(raw);
dataHandler((data[0] << 8) | data[1]);
read();
});
}, 120);
Expand Down Expand Up @@ -439,91 +434,82 @@ Controllers.ALSPT19 = Controllers["ALS-PT19"] = Controllers.DEFAULT;
*
*/

function Light(opts) {

if (!(this instanceof Light)) {
return new Light(opts);
}
class Light extends Emitter {
constructor(options) {
super();

let controller = null;
let raw = 0;
let last = 0;
const freq = opts.freq || 25;
let controller = null;
let raw = 0;
let last = 0;
const freq = options.freq || 25;

Board.Component.call(
this, opts = Board.Options(opts)
);
Board.Component.call(
this, options = Board.Options(options)
);

if (typeof opts.controller === "string") {
controller = Controllers[opts.controller];
} else {
controller = opts.controller || Controllers.DEFAULT;
}
if (typeof options.controller === "string") {
controller = Controllers[options.controller];
} else {
controller = options.controller || Controllers.DEFAULT;
}

Board.Controller.call(this, controller, opts);
Board.Controller.call(this, controller, options);

if (!this.toIntensityLevel) {
this.toIntensityLevel = opts.toIntensityLevel || function(x) {
return x;
};
}
if (!this.toIntensityLevel) {
this.toIntensityLevel = options.toIntensityLevel || (x => x);
}

if (!this.toLux) {
this.toLux = opts.toLux || function(x) {
return x;
};
}
if (!this.toLux) {
this.toLux = options.toLux || (x => x);
}

Object.defineProperties(this, {
value: {
get() {
return raw;
Object.defineProperties(this, {
value: {
get() {
return raw;
},
},
},
level: {
get() {
return this.toIntensityLevel(raw);
level: {
get() {
return this.toIntensityLevel(raw);
},
},
},
});
});

priv.set(this, {});
priv.set(this, {});

/* istanbul ignore else */
if (typeof this.initialize === "function") {
this.initialize(opts, (data) => {
raw = data;
});
}
/* istanbul ignore else */
if (typeof this.initialize === "function") {
this.initialize(options, data => raw = data);
}

if (typeof this.lux === "undefined") {
Object.defineProperty(this, "lux", {
get() {
return this.toLux(raw);
},
});
}
if (typeof this.lux === "undefined") {
Object.defineProperty(this, "lux", {
get() {
return this.toLux(raw);
},
});
}

const data = {
level: 0,
lux: 0,
};
const data = {
level: 0,
lux: 0,
};

setInterval(() => {
data.level = this.level;
data.lux = this.lux;
setInterval(() => {
data.level = this.level;
data.lux = this.lux;

this.emit("data", data);
this.emit("data", data);

if (raw !== last) {
last = raw;
this.emit("change", data);
}
}, freq);
if (raw !== last) {
last = raw;
this.emit("change", data);
}
}, freq);
}
}

util.inherits(Light, Emitter);

Object.assign(Light.prototype, within);


Expand Down
75 changes: 47 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5a7f4f

Please sign in to comment.