openi2c
Version:
This library is a set of cross platform drivers for common I2C devices.
119 lines (118 loc) • 4.83 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Module = void 0;
const i2c = __importStar(require("i2c-bus"));
const debug_1 = require("../debug");
class Module {
constructor(busNumber = 0, address, config = {}) {
this.config = {};
this.config = Object.assign(this.config, config);
this.address = address;
this.bus = i2c.openSync(busNumber).promisifiedBus();
this.debug = debug_1.debug.extend(`${this.constructor.name}`);
}
readInto(buf, length) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.bus.i2cRead(this.address, length, buf);
});
}
readByte(adrs) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.bus.readByte(this.address, adrs);
});
}
readBytes(adrs, length) {
return __awaiter(this, void 0, void 0, function* () {
const buf = Buffer.alloc(length);
yield this.bus.readI2cBlock(this.address, adrs, length, buf);
return buf;
});
}
write(buf) {
return __awaiter(this, void 0, void 0, function* () {
yield this.bus.i2cWrite(this.address, buf.length, buf);
});
}
writeByte(adrs, value) {
return __awaiter(this, void 0, void 0, function* () {
yield this.bus.writeByte(this.address, adrs, value);
});
}
writeBytes(adrs, buf) {
return __awaiter(this, void 0, void 0, function* () {
yield this.bus.writeI2cBlock(this.address, adrs, buf.length, buf);
});
}
readBit(adrs, bit) {
return __awaiter(this, void 0, void 0, function* () {
var buf = yield this.readByte(adrs);
return (buf >> bit) & 1;
});
}
;
bitMask(bit, length) {
return ((1 << length) - 1) << bit;
}
;
/**
* Write a sequence of bits. Note, this will do a read to get the existing value, then a write.
* @param {number} adrs The address of the byte to write.
* @param {number} bit The nth bit to start at.
* @param {number} length The number of bits to change.
* @param {number} value The values to change.
*/
writeBits(adrs, bit, length, value) {
return __awaiter(this, void 0, void 0, function* () {
const oldValue = yield this.readByte(adrs);
const mask = this.bitMask(bit, length);
const newValue = oldValue ^ ((oldValue ^ (value << bit)) & mask);
yield this.writeByte(adrs, newValue);
});
}
;
/**
* Write one bit. Note, this will do a read to get the existing value, then a write.
* @param {number} adrs The address of the byte to write.
* @param {number} bit The nth bit.
* @param {number} value The new value, 1 or 0.
*/
writeBit(adrs, bit, value) {
return __awaiter(this, void 0, void 0, function* () {
yield this.writeBits(adrs, bit, 1, value);
});
}
;
}
exports.Module = Module;