Skip to content

Commit

Permalink
Temperature: Add C, K, F values to the data event and temperature obj…
Browse files Browse the repository at this point in the history
…ects
  • Loading branch information
gnarf committed Aug 23, 2015
1 parent 5285514 commit aae6400
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 116 deletions.
19 changes: 12 additions & 7 deletions lib/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ function Temperature(opts) {
this.toCelsius = opts.toCelsius || function(x) { return x; };
}

Object.defineProperties(this, {
var propDescriptors = {
celsius: {
get: function() {
return this.toCelsius(raw);
Expand All @@ -408,7 +408,13 @@ function Temperature(opts) {
return this.celsius + 273.15;
}
}
});
};
// Convenience aliases
propDescriptors.C = propDescriptors.celsius;
propDescriptors.F = propDescriptors.fahrenheit;
propDescriptors.K = propDescriptors.kelvin;

Object.defineProperties(this, propDescriptors);

if (typeof this.initialize === "function") {
this.initialize(opts, function(data) {
Expand All @@ -421,11 +427,10 @@ function Temperature(opts) {
return;
}

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

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

Expand Down
240 changes: 131 additions & 109 deletions test/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@ var MockFirmata = require("./util/mock-firmata"),
Board = five.Board,
Temperature = five.Temperature;

function setUpShape(suite) {
// Base Shape for all Temperature tests
suite.proto = [];

suite.instance = [{
name: "celsius"
}, {
name: "fahrenheit"
}, {
name: "kelvin"
}, {
name: "C"
}, {
name: "F"
}, {
name: "K"
}];

}

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

this.proto.forEach(function testProtoMethods(method) {
test.equal(typeof this.temperature[method.name], "function", method.name);
}, this);

this.instance.forEach(function testInstanceProperties(property) {
test.notEqual(typeof this.temperature[property.name], "undefined", property.name);
}, this);

test.done();
}

function newBoard() {
var io = new MockFirmata();
var board = new Board({
Expand Down Expand Up @@ -35,6 +69,99 @@ function restore(target) {
}
}


function createAnalog(toCelsius) {
return new Temperature({
pins: ["A0"],
toCelsius: toCelsius,
freq: 100,
board: this.board
});
}

exports["Temperature -- ANALOG (Base)"] = {
setUp: function(done) {
this.board = newBoard();
this.clock = sinon.useFakeTimers();
this.analogRead = sinon.spy(MockFirmata.prototype, "analogRead");

setUpShape(this);
this.proto.push({ name: "toCelsius" });

done();
},

tearDown: function(done) {
Board.purge();
restore(this);
done();
},

shape: function(test) {
this.temperature = createAnalog.call(this);
var raw = this.analogRead.args[0][1];
raw(50);
shapeTests.call(this, test);
},

rawData: function(test) {
var temperature = createAnalog.call(this);
var raw = this.analogRead.args[0][1],
spy = sinon.spy();

test.expect(13);
temperature.on("data", spy);

raw(50);

this.clock.tick(100);

test.ok(spy.calledOnce);
var data = spy.args[0][1];

var expected = {
celsius: 50,
C: 50,
fahrenheit: 122,
F: 122,
kelvin: 323,
K: 323,
};

Object.keys(expected).forEach(function(prop) {
test.equal(Math.round(data[prop]), expected[prop], "data event." + prop);
test.equal(Math.round(temperature[prop]), expected[prop], "temperature." + prop);
});

test.done();
},

customData: function(test) {
var toCelsius = function() { return 22; };
var temperature = createAnalog.call(this, toCelsius);
var raw = this.analogRead.args[0][1],
spy = sinon.spy();

test.expect(7);
temperature.on("data", spy);

raw(50);

this.clock.tick(100);

test.ok(spy.calledOnce);
var data = spy.args[0][1];
test.equals(Math.round(data.celsius), 22, "celsius");
test.equals(Math.round(data.C), 22, "C");
test.equals(Math.round(data.fahrenheit), 72, "fahrenheit");
test.equals(Math.round(data.F), 72, "F");
test.equals(Math.round(data.kelvin), 295, "kelvin");
test.equals(Math.round(data.K), 295, "K");

test.done();
}
};

exports["Temperature -- LM335"] = {

setUp: function(done) {
Expand All @@ -48,15 +175,7 @@ exports["Temperature -- LM335"] = {
board: this.board
});

this.proto = [];

this.instance = [{
name: "celsius"
}, {
name: "fahrenheit"
}, {
name: "kelvin"
}];
setUpShape(this);

done();
},
Expand All @@ -67,19 +186,7 @@ exports["Temperature -- LM335"] = {
done();
},

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

this.proto.forEach(function(method) {
test.equal(typeof this.temperature[method.name], "function");
}, this);

this.instance.forEach(function(property) {
test.notEqual(typeof this.temperature[property.name], "undefined");
}, this);

test.done();
},
shape: shapeTests,

data: function(test) {

Expand Down Expand Up @@ -146,15 +253,7 @@ exports["Temperature -- LM35"] = {
board: this.board
});

this.proto = [];

this.instance = [{
name: "celsius"
}, {
name: "fahrenheit"
}, {
name: "kelvin"
}];
setUpShape(this);

done();
},
Expand All @@ -165,19 +264,7 @@ exports["Temperature -- LM35"] = {
done();
},

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

this.proto.forEach(function(method) {
test.equal(typeof this.temperature[method.name], "function");
}, this);

this.instance.forEach(function(property) {
test.notEqual(typeof this.temperature[property.name], "undefined");
}, this);

test.done();
},
shape: shapeTests,

data: function(test) {

Expand Down Expand Up @@ -519,71 +606,6 @@ exports["Temperature -- MPU6050"] = {
}
};

function createAnalog(toCelsius) {
return new Temperature({
pins: ["A0"],
toCelsius: toCelsius,
freq: 100,
board: this.board
});
}

exports["Temperature -- ANALOG"] = {
setUp: function(done) {
this.board = newBoard();
this.clock = sinon.useFakeTimers();
this.analogRead = sinon.spy(MockFirmata.prototype, "analogRead");

done();
},

tearDown: function(done) {
Board.purge();
restore(this);
done();
},

rawData: function(test) {
var temperature = createAnalog.call(this);
var raw = this.analogRead.args[0][1],
spy = sinon.spy();

test.expect(4);
temperature.on("data", spy);

raw(50);

this.clock.tick(100);

test.ok(spy.calledOnce);
test.equals(Math.round(spy.args[0][1].celsius), 50);
test.equals(Math.round(spy.args[0][1].fahrenheit), 122);
test.equals(Math.round(spy.args[0][1].kelvin), 323);

test.done();
},

customData: function(test) {
var toCelsius = function() { return 22; };
var temperature = createAnalog.call(this, toCelsius);
var raw = this.analogRead.args[0][1],
spy = sinon.spy();

test.expect(4);
temperature.on("data", spy);

raw(50);

this.clock.tick(100);

test.ok(spy.calledOnce);
test.equals(Math.round(spy.args[0][1].celsius), 22);
test.equals(Math.round(spy.args[0][1].fahrenheit), 72);
test.equals(Math.round(spy.args[0][1].kelvin), 295);

test.done();
}
};

exports["Temperature -- GROVE"] = {

Expand Down

0 comments on commit aae6400

Please sign in to comment.