Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodetect buzz device #4

Merged
merged 4 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ Then release the button and press it once more. Now you can start your node prog

## Changelog

### 1.0.3
- Some buzzer devices seem to use a different vendorId/productId and connecting the buzzers failed. Now `buzz-buzzers` is additionally searching for the name `Buzz` of connected usb devices if a device with could not be found by vendorId/productId. See: https://github.com/functino/buzz-buzzers/pull/3

- Update of all dependencies

### 1.0.2
Update of all dependencies.

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "buzz-buzzers",
"version": "1.0.2",
"version": "1.0.3",
"main": "src/index.js",
"scripts": {
"dev": "node examples/simple",
Expand All @@ -12,13 +12,13 @@
},
"author": "Andreas",
"dependencies": {
"node-hid": "0.7.3"
"node-hid": "0.7.6"
},
"devDependencies": {
"ava": "0.25.0",
"eslint": "5.6.1",
"prettier": "1.14.3",
"sinon": "6.3.5"
"ava": "1.0.1",
"eslint": "5.12.0",
"prettier": "1.15.3",
"sinon": "7.2.2"
},
"description": "Simple access to buzz buzzers",
"repository": {
Expand Down
13 changes: 13 additions & 0 deletions src/connectDevice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function findDeviceByName(nodeHid) {
const buzzDevice = nodeHid.devices().find(d => d.product.match(/Buzz/));
return new nodeHid.HID(buzzDevice.vendorId, buzzDevice.productId);
}
module.exports = function(nodeHid) {
const VENDOR_ID = '1356';
const PRODUCT_ID = '4096';
try {
return new nodeHid.HID(VENDOR_ID, PRODUCT_ID);
} catch (e) {
return findDeviceByName(nodeHid);
}
};
8 changes: 3 additions & 5 deletions src/device.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module.exports = (nodeHid, mapDeviceDataToPressedButtons) => {
const VENDOR_ID = '1356';
const PRODUCT_ID = '4096';

const device = new nodeHid.HID(VENDOR_ID, PRODUCT_ID);
const nodeHid = require('node-hid');

module.exports = (connectDevice, mapDeviceDataToPressedButtons) => {
const device = connectDevice(nodeHid);
return {
setLeds(states) {
device.write(
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const buzzer = require('./buzzer');
const device = require('./device');
const nodeHid = require('node-hid');
const connectDevice = require('./connectDevice');
const readBytes = require('./parser/readBytes');
const mapDeviceDataToPressedButtons = require('./parser/mapDeviceDataToPressedButtons')(
readBytes
);

module.exports = () => buzzer(device(nodeHid, mapDeviceDataToPressedButtons));
module.exports = () => buzzer(device(connectDevice, mapDeviceDataToPressedButtons));
32 changes: 32 additions & 0 deletions test/connectDeviceSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const test = require('ava');
const sinon = require('sinon');
const connectDevice = require('../src/connectDevice');
const usbDevice = {};

test('connectDevice with vendorId and productId', t => {
const nodeHid = {
HID: sinon.stub().returns(usbDevice)
};
const device = connectDevice(nodeHid);
t.is(device, usbDevice);
});

test('connectDevice by name after vendorId and productId attempt failed', t => {
const nodeHid = {
HID: sinon.stub(),
devices: sinon.stub().returns([
{
product: 'foo'
},
{
product: 'Sony Buzz! Wireless',
vendorId: 1,
productId: 2
}
])
};
nodeHid.HID.onFirstCall().throws('device not found');
nodeHid.HID.withArgs(1, 2).returns(usbDevice);
const device = connectDevice(nodeHid);
t.is(device, usbDevice);
});
11 changes: 7 additions & 4 deletions test/deviceSpec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const test = require('ava');
const sinon = require('sinon');
const nodeHid = require('node-hid');

const mapDeviceDataToPressedButtons = sinon.stub();
const usbDevice = {
write: sinon.stub(),
on: sinon.stub()
};
const nodeHid = {
HID: sinon.stub().returns(usbDevice)
};
const connectDevice = sinon.stub().returns(usbDevice);

const device = require('../src/device')(connectDevice, mapDeviceDataToPressedButtons);

const device = require('../src/device')(nodeHid, mapDeviceDataToPressedButtons);
test('use connectDevice to connect to device', t => {
t.true(connectDevice.calledWith(nodeHid));
});

test('setLeds to switch off all leds ', t => {
device.setLeds([false, false, false, false]);
Expand Down