forked from dbridges/bmp085-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (135 loc) · 4.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var async = require('async');
var i2c = require('i2c-bus');
module.exports = function BMP180(options) {
var sensor = function() {};
var i2cOpen = new i2c(options.address);
var i2cAddr=options.address;
var cal = {};
var BMP180_CONTROL_REGISTER = 0xF4;
var BMP180_SELECT_TEMP = 0x2E;
var BMP180_SELECT_PRESSURE = 0x34;
var BMP180_CONVERSION_RESULT = 0xF6;
var BMP180_XLSB = 0xF7;
sensor.scan = function () {
wire.scan(function (err, data) {
data.forEach(function (item) {
console.log(item);
});
});
};
function toS16(high, low) {
if (high > 127)
high -= 256;
return (high << 8) + low;
}
function toU16(high, low) {
return (high << 8) + low;
}
sensor.calibrate = function () {
wire.readBytes(0xAA, 22, function (err, data) {
if (err) {
console.error('Error calibrating.');
return;
}
cal = {
ac1: toS16(data[0], data[1]),
ac2: toS16(data[2], data[3]),
ac3: toS16(data[4], data[5]),
ac4: toU16(data[6], data[7]),
ac5: toU16(data[8], data[9]),
ac6: toU16(data[10], data[11]),
b1: toS16(data[12], data[13]),
b2: toS16(data[14], data[15]),
mb: toS16(data[16], data[17]),
mc: toS16(data[18], data[19]),
md: toS16(data[20], data[21])
};
});
};
sensor.read = function (call) {
async.waterfall([
function (callback) {
// Write select pressure command to control register
i2cOpen.writeByte(i2cAddr,i2cAddrBMP180_CONTROL_REGISTER,
[BMP180_SELECT_PRESSURE + (options.mode << 6)]);
setTimeout(function () {
callback(null); }, 28);
},
function (callback) {
// Read uncalibrated pressure.
i2cOpen.readI2cBlock(i2cAddr.BMP180_CONVERSION_RESULT, 3,data, function (err,nbytes,data) {
callback(null, ((data[0] << 16) + (data[1] << 8) + data[2])
>> (8 - options.mode));
});
},
function (pressure, callback) {
i2cOpen.writeByte(i2cAddr,BMP180_CONTROL_REGISTER, [BMP180_SELECT_TEMP]);
setTimeout(function () {
callback(null, pressure); }, 8);
},
function (pressure, callback) {
i2cOpen.readI2cBlock(i2cAddr,BMP180_CONVERSION_RESULT, 2, function (err,nbytes, data) {
callback(null, [pressure, toU16(data[0], data[1])]);
});
}
], function (err, res) {
if (err)
call(err, {});
var uncal_pressure = res[0];
var uncal_temp = res[1];
// Get calibrated temp
var x1 = 0;
var x2 = 0;
var x3 = 0;
var b3 = 0;
var b4 = 0;
var b5 = 0;
var b6 = 0;
var b7 = 0;
var p = 0;
x1 = ((uncal_temp - cal.ac6) * cal.ac5) >> 15;
x2 = (cal.mc << 11) / (x1 + cal.md);
b5 = x1 + x2;
var corrected_temp = ((b5 + 8) >> 4) / 10.0;
if (options.units !== 'metric')
corrected_temp = (9 * corrected_temp / 5) + 32;
// Get calibrated pressure
x1 = 0;
x2 = 0;
x3 = 0;
b3 = 0;
b4 = 0;
b5 = 0;
b6 = 0;
b7 = 0;
p = 0;
x1 = ((uncal_temp - cal.ac6) * cal.ac5) >> 15;
x2 = (cal.mc << 11) / (x1 + cal.md);
b5 = x1 + x2;
b6 = b5 - 4000;
x1 = (cal.b2 * (b6 * b6) >> 12) >> 11;
x2 = (cal.ac2 * b6) >> 11;
x3 = x1 + x2;
b3 = (((cal.ac1 * 4 + x3) << options.mode) + 2) / 4;
x1 = (cal.ac3 * b6) >> 13;
x2 = (cal.b1 * ((b6 * b6) >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (cal.ac4 * (x3 + 32768)) >> 15;
b7 = (uncal_pressure - b3) * (50000 >> options.mode);
if (b7 < 0x80000000)
p = (b7 * 2) / b4;
else
p = (b7 / b4) * 2;
x1 = (p >> 8) * (p >> 8);
x1 = ((x1 * 3038) >> 16);
x2 = ((-7357 * p) >> 16);
p = p + ((x1 + x2 + 3791) >> 4);
if (options.units !== 'metric')
p /= 3386.0;
call(err, {pressure: p,
temperature: corrected_temp});
});
};
sensor.calibrate();
return sensor;
};