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

Trap errors in callbacks #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ var i2c_htu21d = require('htu21d-i2c');
// For example: i2c_htu21d({device: '/dev/i2c-1/'});
var htu21df = new i2c_htu21d();

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

htu21df.readHumidity(function (humidity) {
console.log('Humidity, RH %:', humidity);
htu21df.readHumidity(function (err, humidity) {
if (!err) { console.log('Humidity, RH %:', humidity); }
});
});
````
Expand Down
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
23 changes: 9 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,20 @@ 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) {
console.log(err);
return err;
return callback(err, null);
} else {
if ((data.length === 3) && calc_crc8(data, 3)) {
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));
return callback(null, temperature.toFixed(1));
}
}
});
Expand All @@ -96,21 +93,19 @@ 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() {
that.i2c.read(3, function(err, data) {
if (err) {
console.log(err);
return err;
return callback(err, null);
} else {
if ((data.length === 3) && calc_crc8(data, 3)) {
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));
return callback(null, humidity.toFixed(1));
}
}
});
Expand Down