UNPKG

di-sensors

Version:

Drivers and examples for using DI_Sensors in Node.js

104 lines (79 loc) 4.13 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 _ = require('lodash'); var EventEmitter = require('events'); var DexterI2C = require('../dexterI2c'); var Sensor = function (_EventEmitter) { _inherits(Sensor, _EventEmitter); // ms function Sensor(bus, address, opts) { _classCallCheck(this, Sensor); var _this2 = _possibleConstructorReturn(this, (Sensor.__proto__ || Object.getPrototypeOf(Sensor)).call(this)); _this2.i2c = new DexterI2C(bus, address, opts); _this2.lastValue = 0; _this2.currentValue = 0; _this2.streamInterval = _this2.watchInterval = undefined; _this2.watchDelay = Sensor.WATCH_DELAY; _this2.streamDelay = Sensor.STREAM_DELAY; return _this2; } // ms _createClass(Sensor, [{ key: 'read', value: function read() {} }, { key: 'write', value: function write() {} }, { key: 'stream', value: function stream() { var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.streamDelay; var callBack = arguments[1]; var _this = this; this.stopStream(); this.streamInterval = setInterval(function () { var res = _this.read(); callBack(res); }, delay); } }, { key: 'stopStream', value: function stopStream() { clearInterval(this.streamInterval); } }, { key: 'watch', value: function watch() { var _this3 = this; var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.watchDelay; var _this = this; this.stopWatch(); this.watchInterval = setInterval(function () { var res = _this.read(); _this.lastValue = _this.currentValue; _this3.currentValue = res; if (!_.isEqual(_this.currentValue, _this.lastValue)) { _this.emit('change', _this.currentValue); } }, delay); } }, { key: 'stopWatch', value: function stopWatch() { clearInterval(this.watchInterval); } }]); return Sensor; }(EventEmitter); Sensor.WATCH_DELAY = 100; Sensor.STREAM_DELAY = 100; module.exports = Sensor;