Permalink
Browse files

Fixed usb/knob sample to comply with changes on USB API

  • Loading branch information...
1 parent b586b05 commit ae2f4b29e4ff718fdd56550a619e8b99f786f40e @mangini mangini committed Dec 27, 2012
Showing with 47 additions and 32 deletions.
  1. +2 −1 usb/knob/knob.html
  2. +43 −28 usb/knob/knob.js
  3. +2 −3 usb/knob/manifest.json
View
@@ -15,7 +15,8 @@
</style>
</head>
<body>
+ <button id="requestPermission">Request permissions</button>
+ <div id="knob" style="display: none;"></div>
<script src="knob.js"></script>
- <div id="knob"></div>
</body>
</html>
View
@@ -1,8 +1,11 @@
-var POWERMATE_VENDOR_ID = 0x077d;
-var POWERMATE_PRODUCT_ID = 0x0410;
+var POWERMATE_VENDOR_ID = 1917;//0x077d;
+var POWERMATE_PRODUCT_ID = 1040; //0x0410;
+var DEVICE_INFO = {"vendorId": POWERMATE_VENDOR_ID, "productId": POWERMATE_PRODUCT_ID};
var powerMateDevice;
-var usb = chrome.experimental.usb;
+var usb = chrome.usb;
+var knob = document.getElementById('knob');
+var requestButton = document.getElementById("requestPermission");
var amount = 0;
@@ -12,8 +15,7 @@ var transfer = {
length: 6
};
-var deviceOptions = {
- onEvent: function(usbEvent) {
+var onEvent=function(usbEvent) {
if (usbEvent.resultCode) {
console.log("Error: " + usbEvent.error);
return;
@@ -22,30 +24,43 @@ var deviceOptions = {
var buffer = new Int8Array(usbEvent.data);
amount += buffer[1] * 4;
- var knob = document.getElementById('knob');
knob.style.webkitTransform = 'rotate(' + amount + 'deg)';
- usb.interruptTransfer(
- powerMateDevice,
- transfer,
- function() {
- console.log('Sent event transfer');
- });
- }
-};
+ usb.interruptTransfer( powerMateDevice, transfer, onEvent );
+ };
-usb.findDevice(
- POWERMATE_VENDOR_ID,
- POWERMATE_PRODUCT_ID,
- deviceOptions,
- function(device) {
- if (!device) {
- console.log('device not found');
- return;
- }
- console.log('Found device: ' + device.handle);
- powerMateDevice = device;
- usb.interruptTransfer(powerMateDevice, transfer, function() {
- console.log('Sent initial transfer');
- });
+var gotPermission = function(result) {
+ requestButton.style.display = 'none';
+ knob.style.display = 'block';
+ console.log('App was granted the "usbDevices" permission.');
+ usb.findDevices( DEVICE_INFO,
+ function(devices) {
+ if (!devices || !devices.length) {
+ console.log('device not found');
+ return;
+ }
+ console.log('Found device: ' + devices[0].handle);
+ powerMateDevice = devices[0];
+ usb.interruptTransfer(powerMateDevice, transfer, onEvent);
});
+ };
+
+var permissionObj = {permissions: [{'usbDevices': [DEVICE_INFO] }]};
+
+requestButton.addEventListener('click', function() {
+ chrome.permissions.request( permissionObj, function(result) {
+ if (result) {
+ gotPermission();
+ } else {
+ console.log('App was not granted the "usbDevices" permission.');
+ console.log(chrome.runtime.lastError);
+ }
+ });
+});
+
+chrome.permissions.contains( permissionObj, function(result) {
+ if (result) {
+ gotPermission();
+ }
+});
+
View
@@ -8,7 +8,6 @@
"scripts": ["background.js"]
}
},
- "permissions": [
- "experimental"
- ]
+ "permissions": ["usb"],
+ "optional_permissions": [ {"usbDevices": [{"vendorId": 1917, "productId": 1040}]}]
}

3 comments on commit ae2f4b2

@jdjkelly

Do you have a link to the API doc changes? The developer page is out of date: http://developer.chrome.com/apps/app_hardware.html

Thanks for this example - I was about to start reading through what's in chrome.usb

@sowbug
Member
Please sign in to comment.