UNPKG

di-sensors

Version:

Drivers and examples for using DI_Sensors in Node.js

212 lines (188 loc) 9.21 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 TCS34725 = function (_Sensor) { _inherits(TCS34725, _Sensor); // 60x gain // 4x gain // Indicates that the RGBC channels have completed an integration cycle // Set the gain level for the sensor // 55 clean channel values outside threshold range generates an interrupt // 45 clean channel values outside threshold range generates an interrupt // 35 clean channel values outside threshold range generates an interrupt // 25 clean channel values outside threshold range generates an interrupt // 15 clean channel values outside threshold range generates an interrupt // 5 clean channel values outside threshold range generates an interrupt // 2 clean channel values outside threshold range generates an interrupt // Every RGBC cycle generates an interrupt // Wait time (if ENABLE_WEN is asserted) // Power on - Writing 1 activates the internal oscillator, 0 disables it // Wait enable - Writing 1 activates the wait timer // Register should be equal to 0x44 for the TCS34721 or TCS34725, or 0x4D for the TCS34723 or TCS34727. function TCS34725() { var integrationTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0.0024; var gain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TCS34725.GAIN_16X; var bus = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'RPI_1'; _classCallCheck(this, TCS34725); // Make sure we're connected to the right sensor. var _this = _possibleConstructorReturn(this, (TCS34725.__proto__ || Object.getPrototypeOf(TCS34725)).call(this, bus, TCS34725.ADDRESS, { bigEndian: false })); var chipId = _this.i2c.readReg8u(TCS34725.COMMAND_BIT | TCS34725.ID); if (chipId !== 0x44) { throw new Error('Incorrect chip ID.'); } // Set default integration time and gain. _this.setIntegrationTime(integrationTime); _this.setGain(gain); // Enable the device (by default, the device is in power down mode on bootup). _this.enable(); return _this; } // 16x gain // 1x gain // Blue channel data // Green channel data // Red channel data // Clear channel data // RGBC Clean channel interrupt // 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 // Choose between short and long (12x) wait times via WTIME // 60 clean channel values outside threshold range generates an interrupt // 50 clean channel values outside threshold range generates an interrupt // 40 clean channel values outside threshold range generates an interrupt // 30 clean channel values outside threshold range generates an interrupt // 20 clean channel values outside threshold range generates an interrupt // 10 clean channel values outside threshold range generates an interrupt // 3 clean channel values outside threshold range generates an interrupt // 1 clean channel value outside threshold range generates an interrupt // Persistence register - basic SW filtering mechanism for interrupts // Clear channel upper interrupt threshold // Clear channel lower interrupt threshold // Integration time // RGBC Enable - Writing 1 actives the ADC, 0 disables it // RGBC Interrupt Enable _createClass(TCS34725, [{ key: 'enable', value: function enable() { this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.ENABLE, TCS34725.ENABLE_PON); this.i2c.mwait(1); this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.ENABLE, TCS34725.ENABLE_PON | TCS34725.ENABLE_AEN); } }, { key: 'disable', value: function disable() { var reg = this.i2c.readReg8u(TCS34725.COMMAND_BIT | TCS34725.ENABLE); reg &= ~(TCS34725.ENABLE_PON | TCS34725.ENABLE_AEN); this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.ENABLE, reg); } }, { key: 'setIntegrationtime', value: function setIntegrationtime(time) { var val = parseInt(0x100 - time / 0.0024, 0); if (val > 255) { val = 255; } else if (val < 0) { val = 0; } this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.ATIME, val); this.integrationTimeVal = val; } }, { key: 'setGain', value: function setGain(gain) { this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.CONTROL, gain); } }, { key: 'setInterrupt', value: function setInterrupt(state) { this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.PERS, TCS34725.PERS_NONE); var enable = this.i2c.readReg8u(TCS34725.COMMAND_BIT | TCS34725.ENABLE); if (state) { enable |= TCS34725.ENABLE_AIEN; } else { enable &= ~TCS34725.ENABLE_AIEN; } this.i2c.writeReg8(TCS34725.COMMAND_BIT | TCS34725.ENABLE, enable); } }, { key: 'getRawData', value: function getRawData() { var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (delay) { // Delay for the integration time to allow reading immediately after the previous read. this.i2c.mwait((256 - this.integrationTimeVal) * 24); } var div = (256 - this.integrationTimeVal) * 1024; var r = this.i2c.readReg16u(TCS34725.COMMAND_BIT | TCS34725.RDATAL) / div; var g = this.i2c.readReg16u(TCS34725.COMMAND_BIT | TCS34725.GDATAL) / div; var b = this.i2c.readReg16u(TCS34725.COMMAND_BIT | TCS34725.BDATAL) / div; var c = this.i2c.readReg16u(TCS34725.COMMAND_BIT | TCS34725.CDATAL) / div; r = r > 1 ? 1 : r; g = g > 1 ? 1 : g; b = b > 1 ? 1 : b; c = c > 1 ? 1 : c; return [r, g, b, c]; } }]); return TCS34725; }(Sensor); TCS34725.ADDRESS = 0x29; TCS34725.ID = 0x12; TCS34725.COMMAND_BIT = 0x80; TCS34725.ENABLE = 0x00; TCS34725.ENABLE_AIEN = 0x10; TCS34725.ENABLE_WEN = 0x08; TCS34725.ENABLE_AEN = 0x02; TCS34725.ENABLE_PON = 0x01; TCS34725.ATIME = 0x01; TCS34725.WTIME = 0x03; TCS34725.AILTL = 0x04; TCS34725.AILTH = 0x05; TCS34725.AIHTL = 0x06; TCS34725.AIHTH = 0x07; TCS34725.PERS = 0x0C; TCS34725.PERS_NONE = 0; TCS34725.PERS_1_CYCLE = 1; TCS34725.PERS_2_CYCLE = 2; TCS34725.PERS_3_CYCLE = 3; TCS34725.PERS_5_CYCLE = 4; TCS34725.PERS_10_CYCLE = 5; TCS34725.PERS_15_CYCLE = 6; TCS34725.PERS_20_CYCLE = 7; TCS34725.PERS_25_CYCLE = 8; TCS34725.PERS_30_CYCLE = 9; TCS34725.PERS_35_CYCLE = 10; TCS34725.PERS_40_CYCLE = 11; TCS34725.PERS_45_CYCLE = 12; TCS34725.PERS_50_CYCLE = 13; TCS34725.PERS_55_CYCLE = 14; TCS34725.PERS_60_CYCLE = 15; TCS34725.CONFIG = 0x0D; TCS34725.CONFIG_WLONG = 0x02; TCS34725.CONTROL = 0x0F; TCS34725.ID = 0x12; TCS34725.STATUS = 0x13; TCS34725.STATUS_AINT = 0x10; TCS34725.STATUS_AVALID = 0x01; TCS34725.CDATAL = 0x14; TCS34725.CDATAH = 0x15; TCS34725.RDATAL = 0x16; TCS34725.RDATAH = 0x17; TCS34725.GDATAL = 0x18; TCS34725.GDATAH = 0x19; TCS34725.BDATAL = 0x1A; TCS34725.BDATAH = 0x1B; TCS34725.GAIN_1X = 0x00; TCS34725.GAIN_4X = 0x01; TCS34725.GAIN_16X = 0x02; TCS34725.GAIN_60X = 0x03; module.exports = TCS34725;