Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'usb-detection' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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.
}
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');
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
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;
}
public init() {
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();
});
public deinit() {
this.keyboardEvents.removeSettingsListener(this.onSettingsChanged);
this.keyboardEvents.removeStateChangeListener(this.onStateChangeRequested);
// stop the redrawTimer
if (this.redrawTimer != null) {
clearTimeout(this.redrawTimer);
this.redrawTimer = null;
}
// restore the default rainbow pattern
this.restoreHardwareProfile();
// stop monitoring the usbs
usbDetect.stopMonitoring();
// disconnect from any current keyboard
this.disconnectKeyboard();
}
public init() {
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();
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();
}
}
// 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);
}
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);
}