-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbme280.js
502 lines (421 loc) · 12.9 KB
/
bme280.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
'use strict';
const i2c = require('i2c-bus');
const DEFAULT_I2C_BUS = 1;
const DEFAULT_I2C_ADDRESS = 0x77;
const OVERSAMPLE = {
SKIPPED: 0,
X1: 1,
X2: 2,
X4: 3,
X8: 4,
X16: 5
};
const FILTER = {
OFF: 0,
F2: 1,
F4: 2,
F8: 3,
F16: 4
};
const STANDBY = {
MS_0_5: 0,
MS_62_5: 1,
MS_125: 2,
MS_250: 3,
MS_500: 4,
MS_1000: 5,
MS_10: 6,
MS_20: 7
};
const MODE = {
SLEEP: 0,
FORCED: 1,
NORMAL: 3
};
const REGS = {
TP_COEFFICIENT: 0x88,
CHIP_ID: 0xd0,
RESET: 0xe0,
H_COEFFICIENT: 0xe1,
CTRL_HUM: 0xf2,
STATUS: 0xf3,
CTRL_MEAS: 0xf4,
CONFIG: 0xf5,
DATA: 0xf7
};
const REG_LENGTHS = {
TP_COEFFICIENT: 26,
H_COEFFICIENT: 7,
DATA: 8
};
const CHIP_ID = 0x60;
const SOFT_RESET_COMMAND = 0xb6;
// STATUS register
const STATUS = {
IM_UPDATE_BIT: 0x01,
MEASURING_BIT: 0x08
};
// CTRL_HUM register
const CTRL_HUM = {
OSRS_H_MASK: 0x07,
OSRS_H_POS: 0x00
};
// CTRL_MEAS register
const CTRL_MEAS = {
MODE_POS: 0x00,
MODE_MASK: 0x03,
OSRS_P_POS: 0x02,
OSRS_T_POS: 0x05
};
// CONFIG register
const CONFIG = {
FILTER_MASK: 0x1c,
FILTER_POS: 2,
STANDBY_MASK: 0xe0,
STANDBY_POS: 5
};
const delay = milliseconds =>
new Promise(resolve => setTimeout(resolve, milliseconds + 1));
const open = options => {
return Promise.resolve().then(_ => {
options = options || {};
validateOpenOptions(options);
options = Object.assign({
i2cBusNumber: DEFAULT_I2C_BUS,
i2cAddress: DEFAULT_I2C_ADDRESS,
humidityOversampling: OVERSAMPLE.X1,
pressureOversampling: OVERSAMPLE.X1,
temperatureOversampling: OVERSAMPLE.X1,
filterCoefficient: FILTER.OFF,
standby: STANDBY.MS_0_5,
forcedMode: false
}, options);
return i2c.openPromisified(options.i2cBusNumber).then(i2cBus => {
const bme280I2c = new Bme280I2c(i2cBus, options);
return bme280I2c.initialize().then(_ => new Bme280(bme280I2c));
});
});
};
const validateOpenOptions = options => {
if (typeof options !== 'object') {
throw new Error('Expected options to be of type object.' +
` Got type ${typeof options}.`);
}
if (options.hasOwnProperty('i2cBusNumber') &&
(!Number.isSafeInteger(options.i2cBusNumber) ||
options.i2cBusNumber < 0)) {
throw new Error('Expected i2cBusNumber to be a non-negative integer.' +
` Got "${options.i2cBusNumber}".`);
}
if (options.hasOwnProperty('i2cAddress') &&
(!Number.isSafeInteger(options.i2cAddress) ||
options.i2cAddress < 0 ||
options.i2cAddress > 0x7f)) {
throw new Error('Expected i2cAddress to be an integer' +
` >= 0 and <= 0x7f. Got "${options.i2cAddress}".`);
}
if (options.hasOwnProperty('humidityOversampling') &&
!Object.values(OVERSAMPLE).includes(options.humidityOversampling)) {
throw new Error('Expected humidityOversampling to be a value from' +
` Enum OVERSAMPLE. Got "${options.humidityOversampling}".`);
}
if (options.hasOwnProperty('pressureOversampling') &&
!Object.values(OVERSAMPLE).includes(options.pressureOversampling)) {
throw new Error('Expected pressureOversampling to be a value from' +
` Enum OVERSAMPLE. Got "${options.pressureOversampling}".`);
}
if (options.hasOwnProperty('temperatureOversampling') &&
!Object.values(OVERSAMPLE).includes(options.temperatureOversampling)) {
throw new Error('Expected temperatureOversampling to be a value from' +
` Enum OVERSAMPLE. Got "${options.temperatureOversampling}".`);
}
if (options.hasOwnProperty('standby') &&
!Object.values(STANDBY).includes(options.standby)) {
throw new Error('Expected standby to be a value from Enum' +
` STANDBY. Got "${options.standby}".`);
}
if (options.hasOwnProperty('filterCoefficient') &&
!Object.values(OVERSAMPLE).includes(options.filterCoefficient)) {
throw new Error('Expected filterCoefficient to be a value from Enum' +
` FILTER. Got "${options.filterCoefficient}".`);
}
if (options.hasOwnProperty('forcedMode') &&
typeof options.forcedMode !== 'boolean') {
throw new Error('Expected forcedMode to be a value of type' +
` boolean. Got type "${typeof options.forcedMode}".`);
}
};
class Bme280I2c {
constructor(i2cBus, options) {
this._i2cBus = i2cBus;
this._opts = options;
this._coefficients = null;
}
readByte(register) {
return this._i2cBus.readByte(this._opts.i2cAddress, register);
}
writeByte(register, byte) {
return this._i2cBus.writeByte(this._opts.i2cAddress, register, byte);
}
readI2cBlock(register, length, buffer) {
return this._i2cBus.readI2cBlock(
this._opts.i2cAddress, register, length, buffer
);
}
checkChipId(tries = 5) {
return this.readByte(REGS.CHIP_ID).
then(chipId => {
if (chipId !== CHIP_ID) {
return Promise.reject(new Error(
`Expected bme280 chip id to be 0x${CHIP_ID.toString(16)}` +
`, got chip id 0x${chipId.toString(16)}.`
));
}
}).catch(err => {
if (tries > 1) {
return delay(1).then(_ => this.checkChipId(tries - 1));
}
return Promise.reject(err);
});
}
softReset() {
return this.writeByte(REGS.RESET, SOFT_RESET_COMMAND);
}
waitForImageRegisterUpdate(tries = 5) {
return delay(2).
then(_ => this.readByte(REGS.STATUS)).
then(statusReg => {
if ((statusReg & STATUS.IM_UPDATE_BIT) !== 0) {
if (tries - 1 > 0) {
return this.waitForImageRegisterUpdate(tries - 1);
}
return Promise.reject(new Error('Image register update failed.'));
}
});
}
readCoefficients() {
const tpRegs = Buffer.alloc(REG_LENGTHS.TP_COEFFICIENT);
const hRegs = Buffer.alloc(REG_LENGTHS.H_COEFFICIENT);
return this.readI2cBlock(
REGS.TP_COEFFICIENT, REG_LENGTHS.TP_COEFFICIENT, tpRegs
).
then(_ => this.readI2cBlock(
REGS.H_COEFFICIENT, REG_LENGTHS.H_COEFFICIENT, hRegs
)).
then(_ => {
this._coefficients = Object.freeze({
t1: tpRegs.readUInt16LE(0),
t2: tpRegs.readInt16LE(2),
t3: tpRegs.readInt16LE(4),
p1: tpRegs.readUInt16LE(6),
p2: tpRegs.readInt16LE(8),
p3: tpRegs.readInt16LE(10),
p4: tpRegs.readInt16LE(12),
p5: tpRegs.readInt16LE(14),
p6: tpRegs.readInt16LE(16),
p7: tpRegs.readInt16LE(18),
p8: tpRegs.readInt16LE(20),
p9: tpRegs.readInt16LE(22),
h1: tpRegs.readUInt8(25),
h2: hRegs.readInt16LE(0),
h3: hRegs.readUInt8(2),
h4: (hRegs.readInt8(3) * 16) | (hRegs[4] & 0xf),
h5: (hRegs.readInt8(5) * 16) | (hRegs[4] >> 4),
h6: hRegs.readInt8(6)
});
});
}
async configureSettings() {
// The following comment is from the BME280 datasheet (page 30, section
// 5.4.6 Register 0xF5 "config"):
//
// Writes to the "config" register in normal mode may be ignored. In sleep
// mode writes are not ignored.
//
// So ensure that the config register is set while in sleep mode before
// setting the mode to normal/forced in the ctrl_meas register.
const configReg = await this.readByte(REGS.CONFIG);
await this.writeByte(
REGS.CONFIG,
(configReg & ~(CONFIG.STANDBY_MASK | CONFIG.FILTER_MASK)) |
(this._opts.standby << CONFIG.STANDBY_POS) |
(this._opts.filterCoefficient << CONFIG.FILTER_POS)
);
const ctrlHumReg = await this.readByte(REGS.CTRL_HUM);
await this.writeByte(
REGS.CTRL_HUM,
(ctrlHumReg & ~CTRL_HUM.OSRS_H_MASK) |
(this._opts.humidityOversampling << CTRL_HUM.OSRS_H_POS)
);
const mode = this._opts.forcedMode ? MODE.SLEEP : MODE.NORMAL;
await this.writeByte(
REGS.CTRL_MEAS,
(this._opts.temperatureOversampling << CTRL_MEAS.OSRS_T_POS) |
(this._opts.pressureOversampling << CTRL_MEAS.OSRS_P_POS) |
(mode << CTRL_MEAS.MODE_POS)
);
}
initialize() {
return this.checkChipId().
then(_ => this.softReset()).
then(_ => this.waitForImageRegisterUpdate()).
then(_ => this.readCoefficients()).
then(_ => this.configureSettings()).
then(_ => {
if (!this.forcedMode) {
return delay(this.maximumMeasurementTime());
}
});
}
readRawData() {
return this.readI2cBlock(
REGS.DATA, REG_LENGTHS.DATA, Buffer.alloc(REG_LENGTHS.DATA)
).
then(dataRegs => {
const regs = dataRegs.buffer;
return {
pressure: regs[0] << 12 | regs[1] << 4 | regs[2] >> 4,
temperature: regs[3] << 12 | regs[4] << 4 | regs[5] >> 4,
humidity: regs[6] << 8 | regs[7]
};
});
}
compensateTemperature(adcT) {
const c = this._coefficients;
return ((adcT / 16384 - c.t1 / 1024) * c.t2) +
((adcT / 131072 - c.t1 / 8192) * (adcT / 131072 - c.t1 / 8192) * c.t3);
}
compensateHumidity(adcH, tFine) {
const c = this._coefficients;
let h = tFine - 76800;
h = (adcH - (c.h4 * 64 + c.h5 / 16384 * h)) *
(c.h2 / 65536 * (1 + c.h6 / 67108864 * h * (1 + c.h3 / 67108864 * h)));
h = h * (1 - c.h1 * h / 524288);
if (h > 100) {
h = 100;
} else if (h < 0) {
h = 0;
}
return h;
}
compensatePressure(adcP, tFine) {
const c = this._coefficients;
let var1 = tFine / 2 - 64000;
let var2 = var1 * var1 * c.p6 / 32768;
var2 = var2 + var1 * c.p5 * 2;
var2 = (var2 / 4) + (c.p4 * 65536);
var1 = (c.p3 * var1 * var1 / 524288 + c.p2 * var1) / 524288;
var1 = (1 + var1 / 32768) * c.p1;
if (var1 === 0) {
return 0; // avoid exception caused by division by zero
}
let p = 1048576 - adcP;
p = (p - (var2 / 4096)) * 6250 / var1;
var1 = c.p9 * p * p / 2147483648;
var2 = p * c.p8 / 32768;
p = p + (var1 + var2 + c.p7) / 16;
return p;
}
compensateRawData(rawData) {
const tFine = this.compensateTemperature(rawData.temperature);
let pressure = this.compensatePressure(rawData.pressure, tFine);
let humidity = this.compensateHumidity(rawData.humidity, tFine);
let temperature = tFine / 5120;
if (this._opts.temperatureOversampling === OVERSAMPLE.SKIPPED) {
temperature = undefined;
}
pressure = pressure / 100;
if (this._opts.pressureOversampling === OVERSAMPLE.SKIPPED) {
pressure = undefined;
}
if (this._opts.humidityOversampling === OVERSAMPLE.SKIPPED) {
humidity = undefined;
}
return {
temperature: temperature,
pressure: pressure,
humidity: humidity
};
}
read() {
return this.readRawData().
then(rawData => this.compensateRawData(rawData));
}
async triggerForcedMeasurement() {
let ctrlMeas;
let mode;
const TRIES = 5;
ctrlMeas = await this.readByte(REGS.CTRL_MEAS);
mode = (ctrlMeas & CTRL_MEAS.MODE_MASK) >> CTRL_MEAS.MODE_POS;
if (mode === MODE.NORMAL) {
throw new Error(
'triggerForcedMeasurement can\'t be invoked in normal mode.'
);
}
// If a forced measurement is currently progress give it a chance to
// complete before proceeding.
for (let i = 1; i <= TRIES && mode !== MODE.SLEEP; ++i) {
const millis = Math.ceil(this.maximumMeasurementTime() / TRIES);
await delay(millis);
ctrlMeas = await this.readByte(REGS.CTRL_MEAS);
mode = (ctrlMeas & CTRL_MEAS.MODE_MASK) >> CTRL_MEAS.MODE_POS;
}
if (mode !== MODE.SLEEP) {
throw new Error(
'Failed to trigger forced measurement, sensor not in SLEEP mode.'
);
}
await this.writeByte(
REGS.CTRL_MEAS,
(ctrlMeas & ~CTRL_MEAS.MODE_MASK) |
(MODE.FORCED << CTRL_MEAS.MODE_POS)
);
}
typicalMeasurementTime() {
const to = this._opts.temperatureOversampling;
const po = this._opts.pressureOversampling;
const ho = this._opts.humidityOversampling;
return Math.ceil(1 +
(to === OVERSAMPLE.SKIPPED ? 0 : 2 * Math.pow(2, to - 1)) +
(po === OVERSAMPLE.SKIPPED ? 0 : 2 * Math.pow(2, po - 1) + 0.5) +
(ho === OVERSAMPLE.SKIPPED ? 0 : 2 * Math.pow(2, ho - 1) + 0.5));
}
maximumMeasurementTime() {
const to = this._opts.temperatureOversampling;
const po = this._opts.pressureOversampling;
const ho = this._opts.humidityOversampling;
return Math.ceil(1.25 +
(to === OVERSAMPLE.SKIPPED ? 0 : 2.3 * Math.pow(2, to - 1)) +
(po === OVERSAMPLE.SKIPPED ? 0 : 2.3 * Math.pow(2, po - 1) + 0.575) +
(ho === OVERSAMPLE.SKIPPED ? 0 : 2.3 * Math.pow(2, ho - 1) + 0.575));
}
close() {
return this._i2cBus.close();
}
}
class Bme280 {
constructor(bme280I2c) {
this._bme280I2c = bme280I2c;
}
read() {
return this._bme280I2c.read();
}
triggerForcedMeasurement() {
return this._bme280I2c.triggerForcedMeasurement();
}
typicalMeasurementTime() {
return this._bme280I2c.typicalMeasurementTime();
}
maximumMeasurementTime() {
return this._bme280I2c.maximumMeasurementTime();
}
close() {
return this._bme280I2c.close();
}
}
module.exports = {
open: open,
OVERSAMPLE: OVERSAMPLE,
FILTER: FILTER,
STANDBY: STANDBY
};