Skip to content

Commit

Permalink
Give Thermometer enable/disable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
islemaster committed May 25, 2017
1 parent b050bf8 commit f684a20
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 22 deletions.
102 changes: 82 additions & 20 deletions lib/thermometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,6 @@ function Thermometer(opts) {
this, opts = Board.Options(opts)
);

var freq = opts.freq || 25;

// Analog Reference Voltage (default to board.io.aref || 5)
this.aref = opts.aref || this.io.aref || 5;

Expand All @@ -943,7 +941,13 @@ function Thermometer(opts) {
controller = Controllers.ANALOG;
}

priv.set(this, {});
var state = {
enabled: typeof opts.enabled === "undefined" ? true : opts.enabled,
intervalId: null,
freq: opts.freq || 25,
previousFreq: opts.freq || 25,
};
priv.set(this, state);

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

Expand All @@ -953,6 +957,25 @@ function Thermometer(opts) {
};
}

// TODO: Move this out of the constructor
var eventProcessing = function() {
if (raw == null) {
return;
}

var data = {};
data.C = data.celsius = this.celsius;
data.F = data.fahrenheit = this.fahrenheit;
data.K = data.kelvin = this.kelvin;

this.emit("data", data);

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

var descriptors = {
celsius: {
get: function() {
Expand All @@ -968,7 +991,22 @@ function Thermometer(opts) {
get: function() {
return toFixed(this.celsius + CELSIUS_TO_KELVIN, 2);
}
}
},
freq: {
get: function() {
return state.freq;
},
set: function(newFreq) {
state.freq = newFreq;
if (state.intervalId) {
clearInterval(state.intervalId);
}

if (state.freq !== null) {
state.intervalId = setInterval(eventProcessing, newFreq);
}
}
},
};
// Convenience aliases
descriptors.C = descriptors.celsius;
Expand All @@ -983,26 +1021,50 @@ function Thermometer(opts) {
});
}

setInterval(function() {
if (raw == null) {
return;
}
// Set the freq property only after the get and set functions are defined
// and only if the sensor is not `enabled: false`
if (state.enabled) {
this.freq = state.freq;
}
}

var data = {};
data.C = data.celsius = this.celsius;
data.F = data.fahrenheit = this.fahrenheit;
data.K = data.kelvin = this.kelvin;
util.inherits(Thermometer, Emitter);

this.emit("data", data);
/**
* enable Enable a disabled thermometer.
*
* @return {Object} instance
*
*/
Thermometer.prototype.enable = function() {
var state = priv.get(this);

/* istanbul ignore else */
if (!state.enabled) {
this.freq = state.freq || state.previousFreq;
}

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

util.inherits(Thermometer, Emitter);
/**
* disable Disable an enabled thermometer.
*
* @return {Object} instance
*
*/
Thermometer.prototype.disable = function() {
var state = priv.get(this);

/* istanbul ignore else */
if (state.enabled) {
state.enabled = false;
state.previousFreq = state.freq;
this.freq = null;
}

return this;
};

Thermometer.Drivers = Drivers;

Expand Down
104 changes: 102 additions & 2 deletions test/thermometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,88 @@ function testAnalogChange(test) {
test.done();
}

function testConstructDisabled(test) {
this.thermometer = new Thermometer({
controller: this.thermometer.controller,
pin: this.thermometer.pin,
freq: this.thermometer.freq,
board: this.board,
enabled: false,
});

var raw = this.analogRead.firstCall.yield.bind(this.analogRead.firstCall);
var spy = this.sandbox.spy();

test.expect(2);

this.thermometer.on("change", spy);

raw(100);
this.clock.tick(this.freq);
raw(200);
this.clock.tick(this.freq);

test.equal(spy.callCount, 0);

this.thermometer.enable();

raw(100);
this.clock.tick(this.freq);
raw(200);
this.clock.tick(this.freq);

test.equal(spy.callCount, 1);
test.done();
}

function testEnable(test) {
var raw = this.analogRead.firstCall.yield.bind(this.analogRead.firstCall);
var spy = this.sandbox.spy();

test.expect(2);

this.thermometer.disable();

this.thermometer.on("change", spy);

raw(100);
this.clock.tick(this.freq);
raw(200);
this.clock.tick(this.freq);

test.equal(spy.callCount, 0);

this.thermometer.enable();

raw(100);
this.clock.tick(this.freq);
raw(200);
this.clock.tick(this.freq);

test.equal(spy.callCount, 2);
test.done();
}

function testDisable(test) {
var raw = this.analogRead.firstCall.yield.bind(this.analogRead.firstCall);
var spy = this.sandbox.spy();

test.expect(1);

this.thermometer.disable();

this.thermometer.on("change", spy);

raw(100);
this.clock.tick(this.freq);
raw(200);
this.clock.tick(this.freq);

test.equal(spy.callCount, 0);

test.done();
}

function testShape(test) {
test.expect(this.proto.length + this.instance.length);

Expand Down Expand Up @@ -219,6 +301,9 @@ exports["Thermometer -- ANALOG"] = {

shape: testShape,
change: testAnalogChange,
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,

rawData: makeTestAnalogConversion({
raw: 50,
Expand Down Expand Up @@ -296,6 +381,9 @@ exports["Thermometer -- ANALOG"] = {
K: 373,
}),
change: testAnalogChange,
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,
digits: function(test) {
test.expect(1);
test.equal(digits.fractional(this.thermometer.C), 0);
Expand Down Expand Up @@ -335,6 +423,9 @@ exports["Thermometer -- ANALOG"] = {
K: 378,
}),
change: testAnalogChange,
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,
digits: function(test) {
test.expect(1);
test.equal(digits.fractional(this.thermometer.C), 0);
Expand All @@ -356,6 +447,9 @@ exports["Thermometer -- ANALOG"] = {

shape: testShape,
change: testAnalogChange,
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,

aref: makeTestAnalogConversion({
aref: 3.3,
Expand Down Expand Up @@ -416,7 +510,10 @@ exports["Thermometer -- ANALOG"] = {
test.expect(1);
test.equal(digits.fractional(this.thermometer.C), 0);
test.done();
}
},
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,
},

TINKERKIT: {
Expand Down Expand Up @@ -450,7 +547,10 @@ exports["Thermometer -- ANALOG"] = {
test.expect(1);
test.equal(digits.fractional(this.thermometer.C), 0);
test.done();
}
},
enable: testEnable,
disable: testDisable,
constructDisabled: testConstructDisabled,
},
};

Expand Down

0 comments on commit f684a20

Please sign in to comment.