// https://github.com/node-hid/node-hid
var HID = require('node-hid'),
// Get all devices connected to the computer
devices = HID.devices();
// Show all devices
console.log(devices);
// Informations about the connected device
[{ vendorId: 8483,
productId: 4112,
path: '0003:0006:00',
manufacturer: 'Syntek',
product: 'USB Missile Launcher',
release: 1,
interface: 0 }]
vendorId + productId = unique
path = system specific
// Options
var options = { vendorID : 8483, productID : 4112 };
// Iterate over all USB devices
devices.forEach(function(device, index) {
// Get a specific device
if (device.vendorId == options.vendorID
&& device.productId == options.productID) {
// Open a connection to the device
console.log(new HID.HID(device.path));
}
}, this);
// The handler for the device
{ domain: null,
_events: { newListener: [Function] },
_maxListeners: 10,
_raw: {},
read: [Function],
write: [Function],
getFeatureReport: [Function],
sendFeatureReport: [Function],
setNonBlocking: [Function],
_paused: true }
// Open the device
var connectedDevice = new HID.HID(device.path);
// Read from device
connectedDevice.on("data", function(data) {
console.log(data);
});
var command = {
left : [2, 4, 0, 0, 0, 0, 0, 0],
right : [2, 8, 0, 0, 0, 0, 0, 0],
stop : [2, 32, 0, 0, 0, 0, 0, 0] }
connectedDevice.write(command.left); // Turn left
setTimeout(function() {
connectedDevice.write(command.right); // Turn right
setTimeout(function() {
connectedDevice.write(command.stop); // Stop
}, 500);
}, 500);
var express = require('express'),
ThunderConnector = require('thunder-connector');
// Initialize Express
var app = express();
// Handle the default route
app.get('/', function(request, response) {
// Handle request and send a response
});
// Listen on port x for a connection
app.listen(1337);
// Display the frontend if no action is given
if (!request.param('action')) {
response.sendfile('app/app.html');
} else {
var result = {
// Send 'action' as a command to the device
'result' : ThunderConnector.command(request.param('action')),
'action' : request.param('action')
};
// Send the result back to the client
response.json(result);
}
<!-- Root element of the application -->
<html ng-app="ThunderCommander">
<body
<!-- Attach controller class to view -->
ng-controller="CommandCenterController"
<!-- Listen to keyDown/keyUp events -->
ng-keydown="sendKey($event)"
ng-keyup="sendKey($event)"
<!-- Change class based on the connection status -->
ng-class="{'connected': connected, 'not-connected': !connected}"
>
// Container of the application
var ThunderCommander = angular.module('ThunderCommander', []);
// Controller to manipulate the client scope
ThunderCommander.controller('CommandCenterController',
['$scope', '$http', function($scope, $http) {
// Application logic
}]);
// Send a command based on the pressed key
$scope.sendKey = function(event) {
switch (event.keyCode) {
// Left arrow
case 37 : $scope.sendCommand('left'); break;
}
}
// Send a command to the server
$scope.sendCommand = function(command) {
// Send an HTTP request to the server
$http.get('/?action='+command).success(function(data) {
// Do something with the result
});
};