UNPKG

di-sensors

Version:

Drivers and examples for using DI_Sensors in Node.js

277 lines (235 loc) 12.1 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } // https://www.dexterindustries.com/GoPiGo/ // https://github.com/DexterInd/DI_Sensors // // Copyright (c) 2017 Dexter Industries // Released under the MIT license (http://choosealicense.com/licenses/mit/). // For more information see https://github.com/DexterInd/GoPiGo3/blob/master/LICENSE.md var Sensor = require('./base/sensor'); var BME280 = function (_Sensor) { _inherits(BME280, _Sensor); // Trimming parameter registers // Filter Settings // Standby Settings // BME280 default address. function BME280() { var bus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'RPI_1'; var tMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : BME280.OSAMPLE_1; var hMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : BME280.OSAMPLE_1; var pMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : BME280.OSAMPLE_1; var standby = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : BME280.STANDBY_250; var filter = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : BME280.FILTER_OFF; _classCallCheck(this, BME280); var _this = _possibleConstructorReturn(this, (BME280.__proto__ || Object.getPrototypeOf(BME280)).call(this, bus, BME280.ADDRESS, { bigEndian: false })); if ([BME280.OSAMPLE_1, BME280.OSAMPLE_2, BME280.OSAMPLE_4, BME280.OSAMPLE_8, BME280.OSAMPLE_16].indexOf(tMode) < 0) { throw new Error('Unexpected tMode ' + tMode + '. Valid modes are OSAMPLE_1, OSAMPLE_2, OSAMPLE_4, OSAMPLE_8, and OSAMPLE_16.'); } _this._tMode = tMode; if ([BME280.OSAMPLE_1, BME280.OSAMPLE_2, BME280.OSAMPLE_4, BME280.OSAMPLE_8, BME280.OSAMPLE_16].indexOf(hMode) < 0) { throw new Error('Unexpected tMode ' + hMode + '. Valid modes are OSAMPLE_1, OSAMPLE_2, OSAMPLE_4, OSAMPLE_8, and OSAMPLE_16.'); } _this._hMode = hMode; if ([BME280.OSAMPLE_1, BME280.OSAMPLE_2, BME280.OSAMPLE_4, BME280.OSAMPLE_8, BME280.OSAMPLE_16].indexOf(pMode) < 0) { throw new Error('Unexpected tMode ' + pMode + '. Valid modes are OSAMPLE_1, OSAMPLE_2, OSAMPLE_4, OSAMPLE_8, and OSAMPLE_16.'); } _this._pMode = pMode; if ([BME280.STANDBY_0P5, BME280.STANDBY_62P5, BME280.STANDBY_125, BME280.STANDBY_250, BME280.STANDBY_500, BME280.STANDBY_1000, BME280.STANDBY_10, BME280.STANDBY_20].indexOf(standby) < 0) { throw new Error('Unexpected tMode ' + standby + '. Valid values are STANDBY_0P5, STANDBY_10, STANDBY_20, STANDBY_62P5, STANDBY_125, STANDBY_250, STANDBY_500, and STANDBY_1000.'); } _this._standby = standby; if ([BME280.FILTER_OFF, BME280.FILTER_2, BME280.FILTER_4, BME280.FILTER_8, BME280.FILTER_16].indexOf(filter) < 0) { throw new Error('Unexpected tMode ' + filter + '. Valid values are FILTER_OFF, FILTER_2, FILTER_4, FILTER_8, and FILTER_16.'); } _this._filter = filter; // Load calibration values _this._loadCalibration(); _this.i2c.writeReg8(BME280.REG_CONTROL, 0x24); // Sleep mode _this.i2c.mwait(2); // Set the standby time _this.i2c.writeReg8(BME280.REG_CONTROL, standby << 5 | filter << 2); _this.i2c.mwait(2); // Set the sample modes _this.i2c.writeReg8(BME280.REG_CONTROL_HUM, hMode); // Set Humidity Oversample _this.i2c.writeReg8(BME280.REG_CONTROL, tMode << 5 | pMode << 2 | 3); // Set Temp/Pressure Oversample and enter Normal mode _this.tFine = 0.0; return _this; } // REG_DATA = 0xF7 // BME280 Registers // Operating Modes _createClass(BME280, [{ key: '_loadCalibration', value: function _loadCalibration() { // Read calibration data this.digT1 = this.i2c.readReg16u(BME280.REG_DIG_T1); this.digT2 = this.i2c.readReg16s(BME280.REG_DIG_T2); this.digT3 = this.i2c.readReg16s(BME280.REG_DIG_T3); this.digP1 = this.i2c.readReg16u(BME280.REG_DIG_P1); this.digP2 = this.i2c.readReg16s(BME280.REG_DIG_P2); this.digP3 = this.i2c.readReg16s(BME280.REG_DIG_P3); this.digP4 = this.i2c.readReg16s(BME280.REG_DIG_P4); this.digP5 = this.i2c.readReg16s(BME280.REG_DIG_P5); this.digP6 = this.i2c.readReg16s(BME280.REG_DIG_P6); this.digP7 = this.i2c.readReg16s(BME280.REG_DIG_P7); this.digP8 = this.i2c.readReg16s(BME280.REG_DIG_P8); this.digP9 = this.i2c.readReg16s(BME280.REG_DIG_P9); this.digH1 = this.i2c.readReg8u(BME280.REG_DIG_H1); this.digH2 = this.i2c.readReg16s(BME280.REG_DIG_H2); this.digH3 = this.i2c.readReg8u(BME280.REG_DIG_H3); this.digH6 = this.i2c.readReg8s(BME280.REG_DIG_H7); var h4 = this.i2c.readReg8s(BME280.REG_DIG_H4); h4 <<= 4; this.digH4 = h4 | this.i2c.readReg8u(BME280.REG_DIG_H5) & 0x0F; var h5 = this.i2c.readReg_8s(BME280.REG_DIG_H6); h5 <<= 4; this.digH5 = h5 | this.i2c.readReg8u(BME280.REG_DIG_H5) >> 4 & 0x0F; } }, { key: '_readRawTemp', value: function _readRawTemp() { // read raw temperature data once it's available while (this.i2c.readReg8u(BME280.REG_STATUS) & 0x08) { this.i2c.mwait(2); } var data = this.i2c.readRegList(BME280.REG_TEMP_DATA, 3); return (data[0] << 16 | data[1] << 8 | data[2]) >> 4; } }, { key: '_readRawPressure', value: function _readRawPressure() { // read raw pressure data once it's available while (this.i2c.readReg8u(BME280.REG_STATUS) & 0x08) { this.i2c.mwait(2); } var data = this.i2c.readRegList(BME280.REG_PRESSURE_DATA, 3); return (data[0] << 16 | data[1] << 8 | data[2]) >> 4; } }, { key: '_readRawHumidity', value: function _readRawHumidity() { // read raw humidity data once it's available while (this.i2c.readReg8u(BME280.REG_STATUS) & 0x08) { this.i2c.mwait(2); } var data = this.i2c.readRegList(BME280.REG_HUMIDITY_DATA, 2); return data[0] << 8 | data[1]; } }, { key: 'readTemperature', value: function readTemperature() { var rawTemp = parseFloat(this._readRawTemp()); var temp1 = (rawTemp / 16384.0 - parseFloat(this.digT1) / 1024.0) * parseFloat(this.digT2); var temp2 = (rawTemp / 131072.0 - parseFloat(this.digT1) / 8192.0) * (rawTemp / 131072.0 - parseFloat(this.digT1) / 8192.0) * parseFloat(this.digT3); var temp = temp1 + temp2; this.tFine = parseInt(temp, 0); return temp1 + temp2 / 5120.0; } }, { key: 'readTemperatureF', value: function readTemperatureF() { return this.readTemperature() * 1.8 + 32; } }, { key: 'readHumidity', value: function readHumidity() { var rawHum = parseFloat(this._readRawHumidity()); var hum = parseFloat(this.tFine) - 76800.0; hum = (rawHum - (parseFloat(this.digH4) * 64.0 + parseFloat(this.digH5) / 16384.0 * hum)) * (parseFloat(this.digH2) / 65536.0 * (1.0 + parseFloat(this.digH6) / 67108864.0 * hum * (1.0 + parseFloat(this.digH3) / 67108864.0 * hum))); hum *= 1.0 - parseFloat(this.digH1) * hum / 524288.0; hum = hum > 100 ? 100 : hum; hum = hum < 0 ? 0 : hum; return hum; } }, { key: 'readPressure', value: function readPressure() { var rawPress = parseFloat(this._readRawPressure()); var press1 = parseFloat(this.tFine) / 2.0 - 64000.0; var press2 = press1 * press1 * parseFloat(this.digP6) / 32768.0; press2 += press1 * parseFloat(this.digP5) * 2.0; press2 = press2 / 4.0 + parseFloat(this.digP4) * 65536.0; press1 = (parseFloat(this.digP3) * press1 * press1 / 524288.0 + parseFloat(this.digP2) * press1) / 524288.0; press1 = (1.0 + press1 / 32768.0) * parseFloat(this.digP1); if (press1 === 0) { return 0; } var press = 1048576.0 - rawPress; press = (press - press2 / 4096.0) * 6250.0 / press1; press1 = parseFloat(this.digP9) * press * press / 2147483648.0; press2 = press * parseFloat(this.digP8) / 32768.0; return press + (press1 + press2 + parseFloat(this.digP7)) / 16.0; } }, { key: 'readPressureInches', value: function readPressureInches() { // Wrapper to get pressure in inches of Hg return this.readPressure() * 0.0002953; } }, { key: 'readDewPoint', value: function readDewPoint() { return this.readTemperature() - (100 - this.readHumidity()) / 5; } }, { key: 'readDewPointF', value: function readDewPointF() { return this.readDewpoint() * 1.8 + 32; } }]); return BME280; }(Sensor); BME280.ADDRESS = 0x76; BME280.OSAMPLE_1 = 1; BME280.OSAMPLE_2 = 2; BME280.OSAMPLE_4 = 3; BME280.OSAMPLE_8 = 4; BME280.OSAMPLE_16 = 5; BME280.STANDBY_0P5 = 0; BME280.STANDBY_62P5 = 1; BME280.STANDBY_125 = 2; BME280.STANDBY_250 = 3; BME280.STANDBY_500 = 4; BME280.STANDBY_1000 = 5; BME280.STANDBY_10 = 6; BME280.STANDBY_20 = 7; BME280.FILTER_OFF = 0; BME280.FILTER_2 = 1; BME280.FILTER_4 = 2; BME280.FILTER_8 = 3; BME280.FILTER_16 = 4; BME280.REG_DIG_T1 = 0x88; BME280.REG_DIG_T2 = 0x8A; BME280.REG_DIG_T3 = 0x8C; BME280.REG_DIG_P1 = 0x8E; BME280.REG_DIG_P2 = 0x90; BME280.REG_DIG_P3 = 0x92; BME280.REG_DIG_P4 = 0x94; BME280.REG_DIG_P5 = 0x96; BME280.REG_DIG_P6 = 0x98; BME280.REG_DIG_P7 = 0x9A; BME280.REG_DIG_P8 = 0x9C; BME280.REG_DIG_P9 = 0x9E; BME280.REG_DIG_H1 = 0xA1; BME280.REG_DIG_H2 = 0xE1; BME280.REG_DIG_H3 = 0xE3; BME280.REG_DIG_H4 = 0xE4; BME280.REG_DIG_H5 = 0xE5; BME280.REG_DIG_H6 = 0xE6; BME280.REG_DIG_H7 = 0xE7; BME280.REG_CHIPID = 0xD0; BME280.REG_VERSION = 0xD1; BME280.REG_SOFTRESET = 0xE0; BME280.REG_STATUS = 0xF3; BME280.REG_CONTROL_HUM = 0xF2; BME280.REG_CONTROL = 0xF4; BME280.REG_CONFIG = 0xF5; BME280.REG_PRESSURE_DATA = 0xF7; BME280.REG_TEMP_DATA = 0xFA; BME280.REG_HUMIDITY_DATA = 0xFD; module.exports = BME280;