Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} else {
this._singlePin = false;
}
this._triggerGpio = new Gpio(triggerGpioNo, {
mode: Gpio.OUTPUT,
pullUpDown: Gpio.PUD_OFF,
alert: this._singlePin
});
if (this._singlePin) {
this._echoGpio = this._triggerGpio;
} else {
this._echoGpio = new Gpio(echoGpioNo, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_OFF,
alert: true
});
}
this._callback = undefined;
this._startTick = undefined;
this._ignoreAlerts = false;
this._timeout = undefined;
this._triggerGpio.digitalWrite(0);
// If a single pin is used for both trigger and echo there will be alert
// events for trigger pulses and echo pulses. Trigger pulses are ignored.
this._echoGpio.on('alert', function (level, tick) {
let endTick;
let diff;
function RangeFinder(triggerGpioNo, echoGpioNo) {
if (!(this instanceof RangeFinder)) {
return new RangeFinder(triggerGpioNo, echoGpioNo);
}
if (triggerGpioNo === echoGpioNo || typeof(echoGpioNo) !== 'number') {
this._singlePin = true;
echoGpioNo = triggerGpioNo;
} else {
this._singlePin = false;
}
this._triggerGpio = new Gpio(triggerGpioNo, {
mode: Gpio.OUTPUT,
pullUpDown: Gpio.PUD_OFF,
alert: this._singlePin
});
if (this._singlePin) {
this._echoGpio = this._triggerGpio;
} else {
this._echoGpio = new Gpio(echoGpioNo, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_OFF,
alert: true
});
}
this._callback = undefined;
this._startTick = undefined;
this._ignoreAlerts = false;
import { IDigitalInput, IDigitalOutput, IGPIOModule } from 'j5-io-types';
export interface IConfig {
pin: number | string;
pullResistor?: number;
}
interface INormalizedConfig {
pin: number | string;
pullResistor: number;
}
export const LOW = 0;
export const HIGH = 1;
export const PULL_NONE = Gpio.PUD_OFF;
export const PULL_DOWN = Gpio.PUD_DOWN;
export const PULL_UP = Gpio.PUD_UP;
function parseConfig(config: number | string | IConfig): INormalizedConfig {
let pin: number | string;
let pullResistor: number;
if (typeof config === 'number' || typeof config === 'string') {
pin = config;
pullResistor = PULL_NONE;
} else if (typeof config === 'object') {
pin = config.pin;
pullResistor = config.pullResistor || PULL_NONE;
if ([ PULL_NONE, PULL_DOWN, PULL_UP].indexOf(pullResistor) === -1) {
throw new Error('Invalid pull resistor option ' + pullResistor);
}
} else {