UNPKG

jvsveml6070

Version:

Node.js package for the Vishay VEML6070 UVA Light Sensor, written in TypeScript.

183 lines (182 loc) 7.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandRegister = void 0; const module_1 = require("./module"); const module_2 = require("./enums/module"); const module_3 = require("./errors/module"); class CommandRegister extends module_1.Byte { /** * Creates a new `CommandRegister` instance. * If the optional `Buffer` instance is not provided, the correct initial bit values are set based on the datasheet. * * @see {@link https://www.vishay.com/docs/84277/veml6070.pdf|Vishay VEML6070 datasheet}, pages 6 to 8. * * @param buffer - An optional `Buffer` instance to use. */ constructor(buffer) { super(buffer); // If no `Buffer` instance is provided, set the initial bit values based on the datasheet. if (!(buffer instanceof Buffer)) { this.writeBits(new Map([ // Set the value of the shutdown mode setting bit to 1. [module_2.CommandRegisterBit.SD, 1], // Set the value of the first reserved bit to 1. [module_2.CommandRegisterBit.RESERVED_0, 1], // Set the value of the first integration time setting bit to 1, to match the default 1T integration // time. [module_2.CommandRegisterBit.IT_0, 1], ])); } } /** * Creates a new `CommandRegister` instance from the values of one or more bits. * The initial bit values are set based on the datasheet, but they can be overwritten by passing their values via * the `bits` argument. * * @see {@link https://www.vishay.com/docs/84277/veml6070.pdf|Vishay VEML6070 datasheet}, pages 6 to 8. * * @param bits - A map of bit values keyed by the bit index (in binary order, right to left). * * @returns The created `CommandRegister` instance. */ static fromBits(bits) { const commandRegister = new CommandRegister(); commandRegister.writeBits(bits); return commandRegister; } /** * Creates a new `CommandRegister` instance from a hexadecimal value. * * @param hex - A hexadecimal value, e.g. `0x01` (binary `00000001`). * * @returns The created `CommandRegister` instance. */ static fromHex(hex) { const buffer = Buffer.from([hex]), commandRegister = new CommandRegister(buffer); return commandRegister; } /** * Retrieves the shutdown mode. * * @returns The `ShutdownMode` enumeration value. */ getShutdownMode() { return this.readBit(module_2.CommandRegisterBit.SD); } /** * Sets the shutdown mode. * * @param shutdownMode - The `ShutdownMode` enumeration value. */ setShutdownMode(shutdownMode) { if (!Number.isInteger(shutdownMode) || !Object.values(module_2.ShutdownMode).includes(shutdownMode)) { // eslint-disable-next-line max-len throw new module_3.CommandRegisterError(`Invalid shutdown mode provided. ${shutdownMode} provided, expected a valid \`ShutdownMode\` enum value.`); } this.writeBit(module_2.CommandRegisterBit.SD, shutdownMode); } /** * Retrieves the integration time. * * @returns The `IntegrationTime` enumeration value. */ getIntegrationTime() { const firstBitValue = this.readBit(module_2.CommandRegisterBit.IT_0), secondBitValue = this.readBit(module_2.CommandRegisterBit.IT_1); if (0 === firstBitValue && 0 === secondBitValue) { return module_2.IntegrationTime.IT_HALF_T; } else if (1 === firstBitValue && 0 === secondBitValue) { return module_2.IntegrationTime.IT_1T; } else if (0 === firstBitValue && 1 === secondBitValue) { return module_2.IntegrationTime.IT_2T; } else if (1 === firstBitValue && 1 === secondBitValue) { return module_2.IntegrationTime.IT_4T; } return module_2.IntegrationTime.IT_1T; } /** * Sets the integration time. * * @param integrationTime - The `IntegrationTime` enumeration value. */ setIntegrationTime(integrationTime) { if (!Number.isInteger(integrationTime) || !Object.values(module_2.IntegrationTime).includes(integrationTime)) { // eslint-disable-next-line max-len throw new module_3.CommandRegisterError(`Invalid integration time provided. ${integrationTime} provided, expected a valid \`IntegrationTime\` enum value.`); } let firstBitValue = this.readBit(module_2.CommandRegisterBit.IT_0), secondBitValue = this.readBit(module_2.CommandRegisterBit.IT_1); switch (integrationTime) { case module_2.IntegrationTime.IT_HALF_T: firstBitValue = 0; secondBitValue = 0; break; case module_2.IntegrationTime.IT_1T: firstBitValue = 1; secondBitValue = 0; break; case module_2.IntegrationTime.IT_2T: firstBitValue = 0; secondBitValue = 1; break; case module_2.IntegrationTime.IT_4T: firstBitValue = 1; secondBitValue = 1; break; } this.writeBits(new Map([ [module_2.CommandRegisterBit.IT_0, firstBitValue], [module_2.CommandRegisterBit.IT_1, secondBitValue], ])); } /** * Retrieves the acknowledge mode. * * @returns The `AcknowledgeMode` enumeration value. */ getAcknowledgeMode() { return this.readBit(module_2.CommandRegisterBit.ACK); } /** * Sets the acknowledge mode. * * @param ackMode - The `AcknowledgeMode` enumeration value. */ setAcknowledgeMode(ackMode) { if (!Number.isInteger(ackMode) || !Object.values(module_2.AcknowledgeMode).includes(ackMode)) { // eslint-disable-next-line max-len throw new module_3.CommandRegisterError(`Invalid acknowledge mode provided. ${ackMode} provided, expected a valid \`AcknowledgeMode\` enum value.`); } this.writeBit(module_2.CommandRegisterBit.ACK, ackMode); } /** * Retrieves the acknowledge threshold. * * @returns The `AcknowledgeThreshold` enumeration value. */ getAcknowledgeThreshold() { return this.readBit(module_2.CommandRegisterBit.ACK_THD); } /** * Sets the acknowledge threshold. * * @param ackThreshold - The `AcknowledgeThreshold` enumeration value. */ setAcknowledgeThreshold(ackThreshold) { if (!Number.isInteger(ackThreshold) || !Object.values(module_2.AcknowledgeThreshold).includes(ackThreshold)) { // eslint-disable-next-line max-len throw new module_3.CommandRegisterError(`Invalid acknowledge threshold provided. ${ackThreshold} provided, expected a valid \`AcknowledgeThreshold\` enum value.`); } this.writeBit(module_2.CommandRegisterBit.ACK_THD, ackThreshold); } /** * Clones the `CommandRegister` instance. * * @returns The cloned `CommandRegister` instance. */ clone() { return CommandRegister.fromBits(this.readBits()); } } exports.CommandRegister = CommandRegister;