UNPKG

@growbox/sensors

Version:

An abstraction layer for a selection of raspberry-compatible sensors

40 lines (32 loc) 1.08 kB
/** * @author nstCactus * @date 09/12/2017 15:15 */ const AbstractAnalogToDigitalConverter = require('./AbstractAnalogToDigitalConverter'); const Mcp3008 = require('mcp3008.js'); const _devicePath = new WeakMap(); const _mcp3008 = new WeakMap(); class Mcp3008AnalogToDigitalConverter extends AbstractAnalogToDigitalConverter { constructor(name, uid, devicePath) { super(name, uid); _devicePath.set(this, devicePath); _mcp3008.set(this, new Mcp3008(devicePath)); } /** * Read from the ADC to get the value of the sensor connected to `adcPin` * @param {number} adcPin The ADC pin to read from * @returns {Promise<number>} A promise that, when fulfilled, contains the value read from the sensor connected to `adcPin` */ async read(adcPin) { return new Promise((resolve, reject) => { _mcp3008.get(this).read(adcPin, (err, value) => { if (err) { reject(err); } else { resolve(value); } }); }); } } module.exports = Mcp3008AnalogToDigitalConverter;