forked from dbridges/bmp085-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp085-sensor.js
183 lines (164 loc) · 4.89 KB
/
bmp085-sensor.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var async = require('async');
var i2c = null;
try {
i2c = require('@abandonware/i2c');
} catch(err) {
i2c = require('i2c');
}
module.exports = function BMP085(options) {
var sensor = function() {};
options = options || {};
options.address = options.address || 0x77;
options.mode = options.modes || 0;
options.units = options.units || 'metric';
var wire;
if (!process.iotjs) {
wire = new i2c(options.address);
} else {
options.bus = options.bus || 1;
options.device = options.device || '/dev/i2c-1';
wire = i2c.openSync(options);
wire.writeBytes = function(offset, bytes, callback) {
var bytes = [offset].concat(bytes);
this.writeSync(bytes);
callback && callback(null);
}
wire.readBytes = function(offset, len, callback) {
this.writeSync([offset]);
this.read(len, function(err, res) {
callback && callback(err, res);
});
}
}
var cal = {};
var BMP085_CONTROL_REGISTER = 0xF4;
var BMP085_SELECT_TEMP = 0x2E;
var BMP085_SELECT_PRESSURE = 0x34;
var BMP085_CONVERSION_RESULT = 0xF6;
var BMP085_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 (callback) {
if (!callback) throw "Invalid param";
wire.readBytes(0xAA, 22, function (err, data) {
if (err) {
callback(err, data);
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])
};
callback (err, cal);
});
};
sensor.read = function (call) {
async.waterfall([
function (callback) {
// Write select pressure command to control register
wire.writeBytes(BMP085_CONTROL_REGISTER,
[BMP085_SELECT_PRESSURE + (options.mode << 6)],
function(err, data) {
setTimeout(function () { callback(err); }, 28);
});
},
function (callback) {
// Read uncalibrated pressure.
wire.readBytes(BMP085_CONVERSION_RESULT, 3, function (err, data) {
callback(null, ((data[0] << 16) + (data[1] << 8) + data[2])
>> (8 - options.mode));
});
},
function (pressure, callback) {
wire.writeBytes(BMP085_CONTROL_REGISTER, [BMP085_SELECT_TEMP],
function(err, data) {
setTimeout(function () { callback(err, pressure); }, 8);
});
},
function (pressure, callback) {
wire.readBytes(BMP085_CONVERSION_RESULT, 2, function (err, 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});
});
};
return sensor;
};