Skip to content

Commit

Permalink
Trap errors in callbacks
Browse files Browse the repository at this point in the history
Change-Id: I824f17b2a6b921535cf8772995fabb40d06d765a
Origin: https://github.com/CrossStream/node-htu21d/
Forwarded: https://github.com/bbx10/node-htu21d/pulls
Signed-off-by: Philippe Coval <rzr@users.sf.net>
  • Loading branch information
rzr committed Jan 17, 2020
1 parent 135e81b commit d640c3f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions htudemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ var i2c_htu21d = require('./index.js');
// For example: i2c_htu21d({device: '/dev/i2c-1'});
var htu21df = new i2c_htu21d();

htu21df.readTemperature(function (temp) {
htu21df.readTemperature(function (err, temp) {
console.log('Temperature, C:', temp);

htu21df.readHumidity(function (humidity) {
htu21df.readHumidity(function (err, humidity) {
console.log('Humidity, RH %:', humidity);
});
});
Expand Down
4 changes: 2 additions & 2 deletions htutest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ var i2c_htu21d = require('./index.js');
// For example: i2c_htu21d({device: '/dev/i2c-1/'});
var htu21df = new i2c_htu21d();

htu21df.readTemperature(function (temp) {
htu21df.readTemperature(function (err, temp) {
console.log('Temperature, C:', temp);

htu21df.readHumidity(function (humidity) {
htu21df.readHumidity(function (err, humidity) {
console.log('Humidity, RH %:', humidity);

console.log('new i2c_htu21d({});');
Expand Down
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ var htu21d = function (i2copts_arg) {

htu21d.prototype.readTemperature = function(callback) {
var that = this;
this.i2c.writeByte(HTU21D_READTEMP_NH, function(err) {
if (err) {
console.log(err);
return err;
}
else {
this.i2c.writeByte(HTU21D_READTEMP_NH, function(err, data) {
if (err) {
return callback(err, null);
} else {
setTimeout(function() {
that.i2c.read(3, function(err, data) {
if (err) {
Expand All @@ -83,7 +81,7 @@ htu21d.prototype.readTemperature = function(callback) {
var rawtemp = ((data[0] << 8) | data[1]) & 0xFFFC;
var temperature = ((rawtemp / 65536.0) * 175.72) - 46.85;
//console.log("Temperature, C:", temperature.toFixed(1));
callback(temperature.toFixed(1));
callback(null, temperature.toFixed(1));
}
}
});
Expand All @@ -96,8 +94,7 @@ htu21d.prototype.readHumidity = function(callback) {
var that = this;
this.i2c.writeByte(HTU21D_READHUMI_NH, function(err) {
if (err) {
console.log(err);
return err;
return callback(err, null);
}
else {
setTimeout(function() {
Expand All @@ -110,7 +107,7 @@ htu21d.prototype.readHumidity = function(callback) {
var rawhumi = ((data[0] << 8) | data[1]) & 0xFFFC;
var humidity = ((rawhumi / 65536.0) * 125.0) - 6.0;
//console.log("Relative Humidity, %:", humidity);
callback(humidity.toFixed(1));
callback(null, humidity.toFixed(1));
}
}
});
Expand Down

0 comments on commit d640c3f

Please sign in to comment.