jvsveml6070
Version:
Node.js package for the Vishay VEML6070 UVA Light Sensor, written in TypeScript.
108 lines (107 loc) • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Byte = void 0;
const module_1 = require("./errors/module");
class Byte {
/**
* Creates a new `Byte` instance.
*
* @param buffer - An optional `Buffer` instance to use.
*/
constructor(buffer) {
this._buffer = (buffer instanceof Buffer) ? buffer : Buffer.alloc(1);
}
/**
* Creates a new `Byte` instance from the values of one or more bits.
*
* @param bits - A map of bit values keyed by the bit index (in binary order, right to left).
*
* @returns The created `Byte` instance.
*/
static fromBits(bits) {
const byte = new Byte();
byte.writeBits(bits);
return byte;
}
/**
* Creates a new `Byte` instance from a hexadecimal value.
*
* @param hex - A hexadecimal value, e.g. `0x01` (binary `00000001`).
*
* @returns The created `Byte` instance.
*/
static fromHex(hex) {
const buffer = Buffer.from([hex]), byte = new Byte(buffer);
return byte;
}
/**
* Reads the value of a specific bit.
*
* @param index - The index of the bit to read (in binary order, right to left). Should be a value from 0 to 7.
*
* @returns The value of the bit.
*/
readBit(index) {
return (this._buffer[0] >> index) % 2;
}
/**
* Reads the values of all eight bits.
*
* @returns A map of bit values keyed by the bit index (in binary order, right to left).
*/
readBits() {
const bits = new Map();
for (let index = 0; index < 8; index++) {
bits.set(index, this.readBit(index));
}
return bits;
}
/**
* Writes the value of a specific bit.
*
* @param index - The index of the bit to write (in binary order, right to left). Should be a value from 0 to 7.
* @param value - The value of the bit.
*
* @throws {@link LogicError}
* Thrown if an invalid bit index and/or value is provided.
*/
writeBit(index, value) {
if (0 > index || 7 < index) {
throw new module_1.LogicError(`Invalid bit index provided. ${index} provided, expected a value from 0 to 7.`);
}
if (0 > value || 1 < value) {
throw new module_1.LogicError(`Invalid bit value provided. ${value} provided, expected 0 or 1.`);
}
if (0 === value) {
this._buffer[0] &= ~(1 << index);
}
else {
this._buffer[0] |= (1 << index);
}
}
/**
* Writes the values of one or more bits.
*
* @param bits - A map of bit values keyed by the bit index (in binary order, right to left).
*
* @throws {@link LogicError}
* Thrown if too many bits values are provided, or if invalid bit indexes and/or values are provided.
*/
writeBits(bits) {
if (8 < bits.size) {
throw new module_1.LogicError(`Too many bit values provided. ${bits.size} provided, expected a maximum of 8.`);
}
for (const [index, value] of bits.entries()) {
this.writeBit(index, value);
}
}
/**
* Returns a `Buffer` instance representing the `Byte` instance.
*
* @returns The `Buffer` instance representing the `Byte` instance.
*/
toBuffer() {
return this._buffer;
}
}
exports.Byte = Byte;