How to use the usb-detection.on function in usb-detection

To help you get started, we’ve selected a few usb-detection examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GamesDoneQuick / sgdq18-layouts / dist / extension / timekeeping.js View on Github external
else {
        resumeRunner(index);
    }
});
nodecg.listenFor('editTime', editTime);
if (nodecg.bundleConfig.footpedal.enabled) {
    // tslint:disable:no-var-requires
    const gamepad = require('gamepad');
    const usbDetect = require('usb-detection');
    // tslint:enable:no-var-requires
    gamepad.init();
    usbDetect.startMonitoring();
    // Poll for events
    setInterval(gamepad.processEvents, 16);
    // Update the list of gamepads when usb-detection sees a change.
    usbDetect.on('change', () => {
        nodecg.log.info('USB devices changed, checking for new gamepads.');
        gamepad.detectDevices();
    });
    // Listen for buttonId down event from our target gamepad.
    gamepad.on('down', (_id, num) => {
        if (num !== nodecg.bundleConfig.footpedal.buttonId) {
            return;
        }
        if (stopwatch.value.state === GDQTypes.StopwatchStateEnum.RUNNING) {
            // If this is a race, don't let the pedal finish the timer.
            if (currentRun.value.runners.length > 1 && !currentRun.value.coop) {
                nodecg.log.warn('Footpedal was hit to finish the timer, but this is a race so no action will be taken.');
                return;
            }
            nodecg.log.info('Footpedal hit, finishing timer.');
            // Finish all runners.
github dbkynd / controlcast / app / js / init.js View on Github external
}

usbDetect.on('add', device => {
  if (device.deviceName.toLowerCase().includes('launchpad')) { // Launchpad USB was inserted
    console.log(`'${device.deviceName}' USB detected. Connecting in 4 seconds`);
    if (!usbConnected) { // This stops the random occurrence of the add event firing twice rapidly
      usbConnected = true;
      reconnectTimer = setTimeout(() => {
        connectToLaunchpad();
        setAllLights();
      }, 4000); // Wait 4 seconds for the Launchpad init to finish before attempting to connect.
    }
  }
});

usbDetect.on('remove', device => {
  if (device.deviceName.toLowerCase().includes('launchpad')) { // Launchpad USB was removed
    console.log(`'${device.deviceName}' USB disconnected`);
    if (reconnectTimer) clearTimeout(reconnectTimer); // Stop reconnect timer if it was started
    usbConnected = false;
    launchpad = null;
    isMidiConnected(); // Set midi_connected
  }
});

connectToLaunchpad(); // Connect on startup

function isMidiConnected() {
  if (launchpad) { // Set the midi_connected color based on if launchpad is connected
    $('.midi_connected').addClass('connected');
  } else {
    $('.midi_connected').removeClass('connected');
github dbkynd / controlcast / app / js / init.js View on Github external
console.log(`'${midiIn.getPortName(midiInPort)}' connection successful`);
    isMidiConnected(); // Set midi_connected

    launchpad.on('press', button => { // Create the midi button press handler
      keyEvent('midi', [button.x, button.y], 'press'); // Pass to key event handler
    });

    launchpad.on('release', button => { // Create midi button release handler
      keyEvent('midi', [button.x, button.y], 'release'); // Pass to key event handler
    });
  } else {
    console.log('Unable to connect to the Launchpad Device');
  }
}

usbDetect.on('add', device => {
  if (device.deviceName.toLowerCase().includes('launchpad')) { // Launchpad USB was inserted
    console.log(`'${device.deviceName}' USB detected. Connecting in 4 seconds`);
    if (!usbConnected) { // This stops the random occurrence of the add event firing twice rapidly
      usbConnected = true;
      reconnectTimer = setTimeout(() => {
        connectToLaunchpad();
        setAllLights();
      }, 4000); // Wait 4 seconds for the Launchpad init to finish before attempting to connect.
    }
  }
});

usbDetect.on('remove', device => {
  if (device.deviceName.toLowerCase().includes('launchpad')) { // Launchpad USB was removed
    console.log(`'${device.deviceName}' USB disconnected`);
    if (reconnectTimer) clearTimeout(reconnectTimer); // Stop reconnect timer if it was started
github GamesDoneQuick / agdq18-layouts / extension / timekeeping.js View on Github external
resumeRunner(index);
	}
});
nodecg.listenFor('editTime', editTime);

if (nodecg.bundleConfig.footpedal.enabled) {
	const gamepad = require('gamepad');
	const usbDetect = require('usb-detection');
	gamepad.init();
	usbDetect.startMonitoring();

	// Poll for events
	setInterval(gamepad.processEvents, 16);

	// Update the list of gamepads when usb-detection sees a change.
	usbDetect.on('change', () => {
		nodecg.log.info('USB devices changed, checking for new gamepads.');
		gamepad.detectDevices();
	});

	// Listen for buttonId down event from our target gamepad.
	gamepad.on('down', (id, num) => {
		if (num !== nodecg.bundleConfig.footpedal.buttonId) {
			return;
		}

		if (stopwatch.value.state === STOPWATCH_STATES.RUNNING) {
			// If this is a race, don't let the pedal finish the timer.
			if (currentRun.value.runners.length > 1 && !currentRun.value.coop) {
				nodecg.log.warn('Footpedal was hit to finish the timer, but this is a race so no action will be taken.');
				return;
			}
github FrogTheFrog / steam-gyro-for-cemuhook / src / backend / lib / steam-device / steam-hid-device.ts View on Github external
public static startMonitoring() {
        if (this.motoringCallCount++ === 0) {
            this.updateDeviceList(0, ...this.allTypes);

            // Dongle devices can be connected using usb events
            usbDetect.on(
                `change:${SteamHidId.Vendor}:${SteamHidId.DongleProduct}`,
                () => this.updateDeviceList(1000, "dongle"),
            );
            usbDetect.on(
                `change:${SteamHidId.Vendor}:${SteamHidId.WiredProduct}`,
                () => this.updateDeviceList(1000, "wired"),
            );

            usbDetect.startMonitoring();
        }
    }
github diefarbe / node-rest-api / src / modules / keyboard.ts View on Github external
// There's a filtered search, but it seems broken...
        usbDetect.find((error: any, devices: any) => {
            for (const device of devices) {
                if (device.vendorId === 9456) {
                    this.logger.info("Found a keyboard.");
                    this.setupKeyboard();
                }
            }
        });

        usbDetect.on("remove:9456", (device: any) => {
            this.logger.info("Removed a keyboard.");
            this.disconnectKeyboard();
        });

        usbDetect.on("add:9456", (device: any) => {
            this.logger.info("Added a keyboard.");
            this.setupKeyboard();
        });

        this.redrawTimer = setInterval(() => this.redrawKeyboard(), 1000);
    }
github diefarbe / node-rest-api / src / modules / keyboard.ts View on Github external
this.keyboardEvents.addSettingsListener(this.onSettingsChanged);
        this.keyboardEvents.addStateChangeListener(this.onStateChangeRequested);

        usbDetect.startMonitoring();

        // There's a filtered search, but it seems broken...
        usbDetect.find((error: any, devices: any) => {
            for (const device of devices) {
                if (device.vendorId === 9456) {
                    this.logger.info("Found a keyboard.");
                    this.setupKeyboard();
                }
            }
        });

        usbDetect.on("remove:9456", (device: any) => {
            this.logger.info("Removed a keyboard.");
            this.disconnectKeyboard();
        });

        usbDetect.on("add:9456", (device: any) => {
            this.logger.info("Added a keyboard.");
            this.setupKeyboard();
        });

        this.redrawTimer = setInterval(() => this.redrawKeyboard(), 1000);
    }
github timfish / aurelia-electron-webpack / src / app.ts View on Github external
public async attached() {
    this.devices = await usbDetect.find();

    usbDetect.on('change', async device => {
      this.devices = await usbDetect.find();
    });

    usbDetect.startMonitoring();
  }
}
github FrogTheFrog / steam-gyro-for-cemuhook / src / lib / steam-device.ts View on Github external
static startMonitoring() {
            if (this.motoringCallCount++ === 0) {
                this.updateDeviceList(undefined);
                usbDetect.on(`change:${VendorId}:${WirelessProductId}`, () => this.updateDeviceList(true));
                usbDetect.on(`change:${VendorId}:${WiredProductId}`, () => this.updateDeviceList(true));
            }
        }

usb-detection

Listen to USB devices and detect changes on them.

MIT
Latest version published 1 year ago

Package Health Score

51 / 100
Full package analysis