di-sensors
Version:
Drivers and examples for using DI_Sensors in Node.js
241 lines (211 loc) • 8.63 kB
JavaScript
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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"); } }
// 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 I2C = require('i2c-bus');
var sleep = require('sleep');
var DexterI2C = function () {
function DexterI2C(bus, address, opts) {
_classCallCheck(this, DexterI2C);
this.device = opts.device || undefined;
this.busName = bus;
this.setAddress(address);
this.bigEndian = opts.bigEndian || true;
switch (true) {
default:
throw new Error('I2C bus not supported');
case bus === 'RPI_1':
this.i2cBus = I2C.openSync(1);
break;
case bus === 'GPG3_AD1' || bus === 'GPG3_AD2':
if (typeof this.device === 'undefined') {
throw new Error('Device not sent');
}
this.gpg = this.device;
if (bus === 'GPG3_AD1') {
this.port = this.device.GROVE_1;
} else if (bus === 'GPG3_AD2') {
this.port = this.device.GROVE_2;
}
this.device.setGroveType(this.port, this.device.GROVE_TYPE.I2C);
sleep.usleep(1);
break;
case bus === 'BP3_1' || bus === 'BP3_2' || bus === 'BP3_3' || bus === 'BP3_4':
// Not Supported yet
throw new Error('BrickPi not supported yet. Sorry :-(');
}
}
_createClass(DexterI2C, [{
key: 'setAddress',
value: function setAddress(address) {
this.address = address;
}
}, {
key: 'transfer',
value: function transfer(dataOut) {
var inBytesLen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
if (this.busName === 'RPI_1') {
var dataLen = dataOut.length;
var buffer = void 0;
if (dataLen > 1 && inBytesLen === 0) {
buffer = new Buffer(dataOut);
this.i2cBus.i2cWriteSync(this.address, buffer.length, buffer);
} else if (dataLen === 1 && inBytesLen >= 1) {
buffer = new Buffer(inBytesLen);
return this.i2cBus.readI2cBlockSync(this.address, dataOut[0], inBytesLen, buffer);
} else if (dataLen === 0 && inBytesLen >= 1) {
return this.i2cBus.receiveByteSync(this.address);
} else {
throw new Error('I2C operation not supported');
}
} else if (this.busName === 'GPG3_AD1' || this.busName === 'GPG3_AD2') {
if (typeof this.device === 'undefined') {
throw new Error('Device not sent');
}
try {
return this.device.groveI2cTransfer(this.port, this.address, dataOut, inBytesLen);
} catch (err) {
console.log(err);
throw new Error('[Errno 5] I/O error');
}
} else if (this.busName === 'BP3_1' || this.busName === 'BP3_2' || this.busName === 'BP3_3' || this.busName === 'BP3_4') {
// Not Supported yet
throw new Error('BrickPi not supported yet. Sorry :-(');
}
return false;
}
}, {
key: 'write8',
value: function write8(val) {
val = parseInt(val, 0);
this.transfer([val]);
}
}, {
key: 'writeReg8',
value: function writeReg8(reg, val) {
val = parseInt(val, 0);
this.transfer([reg, val]);
}
}, {
key: 'writeReg16',
value: function writeReg16(reg, val) {
var bigEndian = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
val = parseInt(val, 0);
if (typeof bigEndian === 'undefined') {
bigEndian = this.bigEndian;
}
if (bigEndian) {
this.transfer([reg, val >> 8 & 0xFF, val & 0xFF]);
} else {
this.transfer([reg, val & 0xFF, val >> 8 & 0xFF]);
}
}
}, {
key: 'writeReg32',
value: function writeReg32(reg, val) {
var bigEndian = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
val = parseInt(val, 0);
if (typeof bigEndian === 'undefined') {
bigEndian = this.bigEndian;
}
if (bigEndian === true) {
this.transfer([reg, val >> 24 & 0xFF, val >> 16 & 0xFF, val >> 8 & 0xFF, val & 0xFF]);
} else {
this.transfer([reg, val & 0xFF, val >> 8 & 0xFF, val >> 16 & 0xFF, val >> 24 & 0xFF]);
}
}
}, {
key: 'wait',
value: function wait(s) {
sleep.sleep(s);
}
}, {
key: 'mwait',
value: function mwait(ms) {
sleep.msleep(ms);
}
}, {
key: 'uwait',
value: function uwait(us) {
sleep.usleep(us);
}
}, {
key: 'writeRegList',
value: function writeRegList(reg, list) {
if ((typeof reg === 'undefined' ? 'undefined' : _typeof(reg)) !== 'object') {
reg = [reg];
}
this.transfer(reg.concat(list));
}
}, {
key: 'readByte',
value: function readByte() {
return this.readBytes(1);
}
}, {
key: 'readBytes',
value: function readBytes() {
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DexterI2C.BYTES_LENGTH;
var buffer = new Buffer(length);
return this.get(length, buffer);
}
}, {
key: 'read8u',
value: function read8u() {
return this.transfer([], 1);
}
}, {
key: 'readReg8u',
value: function readReg8u(reg) {
return this.transfer([reg], 1)[0];
}
}, {
key: 'readReg8s',
value: function readReg8s(reg) {
var val = this.read8u(reg);
if (val & 0x80) {
val -= 0x100;
}
return val;
}
}, {
key: 'readReg16u',
value: function readReg16u(reg) {
var bigEndian = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
var val = this.transfer([reg], 2);
if (typeof bigEndian === 'undefined') {
bigEndian = this.bigEndian;
}
if (bigEndian) {
val = val[0] << 8 | val[1];
} else {
val = val[1] << 8 | val[0];
}
return val;
}
}, {
key: 'readReg16s',
value: function readReg16s(reg) {
var bigEndian = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
var val = this.readReg16u(reg, bigEndian);
if (val & 0x8000) {
val -= 0x10000;
}
return val;
}
}, {
key: 'readRegList',
value: function readRegList(reg, len) {
return this.transfer([reg], len);
}
}]);
return DexterI2C;
}();
DexterI2C.BYTES_LENGTH = 4;
module.exports = DexterI2C;