Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi: SHT31D wait 15ms for conversion and convert raw bytes to uint16 #1319

Merged
merged 1 commit into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions lib/imu.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,32 @@ var Drivers = {
this.REGISTER.SOFT_RESET & 0xFF,
]);


// Page 10
// Table 8
// Send high repeatability measurement command
io.i2cWrite(address, [
this.REGISTER.MEASURE_HIGH_REPEATABILITY >> 8,
this.REGISTER.MEASURE_HIGH_REPEATABILITY & 0xFF,
]);

var computed = {
temperature: null,
humidity: null,
};

// temp msb, temp lsb, temp CRC, humidity msb, humidity lsb, humidity CRC
io.i2cRead(address, READLENGTH, function(data) {
computed.temperature = int16(data[0], data[1]);
computed.humidity = int16(data[3], data[4]);
this.emit("data", computed);
}.bind(this));
// temp msb, temp lsb, temp CRC, humidity msb, humidity lsb, humidity CRC
var readCycle = function() {
// Page 10
// Table 8
// Send high repeatability measurement command
io.i2cWrite(address, [
this.REGISTER.MEASURE_HIGH_REPEATABILITY >> 8,
this.REGISTER.MEASURE_HIGH_REPEATABILITY & 0xFF,
]);

setTimeout(function() {
io.i2cReadOnce(address, READLENGTH, function(data) {
computed.temperature = uint16(data[0], data[1]);
computed.humidity = uint16(data[3], data[4]);
this.emit("data", computed);
readCycle();
}.bind(this));
}.bind(this), 16);
}.bind(this);

readCycle();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect

}
},
identifier: {
Expand Down
29 changes: 28 additions & 1 deletion test/hygrometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports["Hygrometer -- SHT31D"] = {

setUp: function(done) {
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cRead = this.sandbox.spy(MockFirmata.prototype, "i2cRead");
this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");

this.hygrometer = new Hygrometer({
controller: "SHT31D",
Expand Down Expand Up @@ -76,6 +76,33 @@ exports["Hygrometer -- SHT31D"] = {

test.done();
},

oneHundredPercentHumidity: function(test) {
test.expect(5);
var readOnce;
var spy = this.sandbox.spy();

this.hygrometer.on("data", spy);

this.clock.tick(20);

test.equal(this.i2cReadOnce.callCount, 1);
test.equal(this.i2cReadOnce.lastCall.args[0], 0x44);
test.equal(this.i2cReadOnce.lastCall.args[1], 6);

readOnce = this.i2cReadOnce.lastCall.args[2];
readOnce([
0, 0, // temperature
0, // crc
0xff, 0xff, // 100% humidity
0 // crc
]);
this.clock.tick(10);

test.equal(spy.callCount, 1);
test.equal(this.hygrometer.relativeHumidity, 100);
test.done();
}
};

exports["Hygrometer -- HTU21D"] = {
Expand Down
15 changes: 9 additions & 6 deletions test/imu.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ exports["Multi -- SHT31D"] = {
this.clock = this.sandbox.useFakeTimers();
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");
this.i2cRead = this.sandbox.spy(MockFirmata.prototype, "i2cRead");
this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");
this.imu = new IMU({
controller: "SHT31D",
freq: 100,
Expand Down Expand Up @@ -324,19 +324,22 @@ exports["Multi -- SHT31D"] = {
test.ok(this.i2cWrite.calledTwice);
test.deepEqual(this.i2cWrite.firstCall.args, [ 68, [ 48, 162 ] ]);
test.deepEqual(this.i2cWrite.lastCall.args, [ 68, [ 36, 0 ] ]);
test.equal(this.i2cRead.callCount, 1);

var i2cRead = this.i2cRead.lastCall.args[2];
this.clock.tick(100);

test.equal(this.i2cReadOnce.callCount, 1);

var i2cReadOnce = this.i2cReadOnce.lastCall.args[2];

i2cRead([ 100, 200, 169, 93, 90, 131 ]);
i2cReadOnce([ 100, 200, 169, 93, 90, 131 ]);

this.clock.tick(100);

i2cRead([ 100, 200, 169, 93, 90, 131 ]);
i2cReadOnce([ 100, 200, 169, 93, 90, 131 ]);

this.clock.tick(100);

i2cRead([ 100, 200, 169, 93, 90, 131 ]);
i2cReadOnce([ 100, 200, 169, 93, 90, 131 ]);

this.clock.tick(100);

Expand Down
64 changes: 64 additions & 0 deletions test/thermometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,70 @@ exports["Thermometer -- SI7020"] = {
}
};

exports["Thermometer -- SHT31D"] = {

setUp: function(done) {
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cReadOnce = this.sandbox.spy(MockFirmata.prototype, "i2cReadOnce");

this.thermometer = new Thermometer({
controller: "SHT31D",
board: this.board,
freq: 10
});

done();
},

fwdOptionsToi2cConfig: function(test) {
test.expect(3);

this.i2cConfig.reset();

new Thermometer({
controller: "SHT31D",
address: 0xff,
bus: "i2c-1",
board: this.board
});

var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

test.done();
},

oneHundredDegreesCelsius: function(test) {
test.expect(5);
var readOnce;
var spy = this.sandbox.spy();

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

this.clock.tick(20);

test.equal(this.i2cReadOnce.callCount, 1);
test.equal(this.i2cReadOnce.lastCall.args[0], 0x44);
test.equal(this.i2cReadOnce.lastCall.args[1], 6);

readOnce = this.i2cReadOnce.lastCall.args[2];
readOnce([
0xd4, 0x1d, // temperature (100 degrees celsius)
0, // crc
0, 0, // humidity
0 // crc
]);
this.clock.tick(10);

test.equal(spy.callCount, 1);
test.equal(Math.round(this.thermometer.C), 100);
test.done();
}
};

exports["Thermometer -- HTU21D"] = {

setUp: function(done) {
Expand Down