UNPKG

@growbox/sensors

Version:

An abstraction layer for a selection of raspberry-compatible sensors

53 lines (42 loc) 1.65 kB
/** * @author nstCactus * @date 05/12/2017 14:02 */ const AbstractSensor = require('./AbstractSensor'); /** {WeakMap<object>} An object representing the analog to digital converter the sensor is connected to */ const _adcInstance = new WeakMap(); /** {WeakMap<*>} The address used to access the sensor (relative to the ADC). Ex: This can be the ADC pin number */ const _adcAddress = new WeakMap(); /** * An abstract class to represent an analog sensor */ class AbstractAnalogSensor extends AbstractSensor { /** * Constructs a new instance * @param {string} name The name of the sensor * @param {string} uid The unique identifier of the sensor * @param {object} adcInstance An object representing the analog to digital converter the sensor is connected to * @param {*} adcAddress The address used to access the sensor (relative to the ADC). Ex: This can be the ADC pin number. */ constructor(name, uid, adcInstance, adcAddress) { super(name, uid); if (this.constructor === AbstractAnalogSensor) { throw new TypeError('Abstract class "AbstractSensor" cannot be instantiated directly.'); } adcInstance.set(this, adcInstance); adcAddress.set(this, adcAddress); } /** * @returns {object} An object representing the analog to digital converter the sensor is connected to */ get adcInstance() { return _adcInstance.get(this); } /** * @returns {*} The address used to access the sensor (relative to the ADC). Ex: This can be the ADC pin number */ get adcAddress() { return _adcAddress.get(this); } } module.exports = AbstractAnalogSensor;