|
| 1 | +var serial = {}; |
| 2 | + |
| 3 | +(function() { |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + serial.getPorts = function() { |
| 7 | + return navigator.usb.getDevices().then(devices => { |
| 8 | + return devices.map(device => new serial.Port(device)); |
| 9 | + }); |
| 10 | + }; |
| 11 | + |
| 12 | + serial.requestPort = function() { |
| 13 | + const filters = [ |
| 14 | + { 'vendorId': 0x239A }, // Adafruit boards |
| 15 | + { 'vendorId': 0xcafe }, // TinyUSB example |
| 16 | + ]; |
| 17 | + return navigator.usb.requestDevice({ 'filters': filters }).then( |
| 18 | + device => new serial.Port(device) |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + serial.Port = function(device) { |
| 23 | + this.device_ = device; |
| 24 | + this.interfaceNumber = 0; |
| 25 | + this.endpointIn = 0; |
| 26 | + this.endpointOut = 0; |
| 27 | + }; |
| 28 | + |
| 29 | + serial.Port.prototype.connect = function() { |
| 30 | + let readLoop = () => { |
| 31 | + this.device_.transferIn(this.endpointIn, 64).then(result => { |
| 32 | + this.onReceive(result.data); |
| 33 | + readLoop(); |
| 34 | + }, error => { |
| 35 | + this.onReceiveError(error); |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + return this.device_.open() |
| 40 | + .then(() => { |
| 41 | + if (this.device_.configuration === null) { |
| 42 | + return this.device_.selectConfiguration(1); |
| 43 | + } |
| 44 | + }) |
| 45 | + .then(() => { |
| 46 | + var interfaces = this.device_.configuration.interfaces; |
| 47 | + interfaces.forEach(element => { |
| 48 | + element.alternates.forEach(elementalt => { |
| 49 | + if (elementalt.interfaceClass==0xFF) { |
| 50 | + this.interfaceNumber = element.interfaceNumber; |
| 51 | + elementalt.endpoints.forEach(elementendpoint => { |
| 52 | + if (elementendpoint.direction == "out") { |
| 53 | + this.endpointOut = elementendpoint.endpointNumber; |
| 54 | + } |
| 55 | + if (elementendpoint.direction=="in") { |
| 56 | + this.endpointIn =elementendpoint.endpointNumber; |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + }) |
| 61 | + }) |
| 62 | + }) |
| 63 | + .then(() => this.device_.claimInterface(this.interfaceNumber)) |
| 64 | + .then(() => this.device_.selectAlternateInterface(this.interfaceNumber, 0)) |
| 65 | + .then(() => this.device_.controlTransferOut({ |
| 66 | + 'requestType': 'class', |
| 67 | + 'recipient': 'interface', |
| 68 | + 'request': 0x22, |
| 69 | + 'value': 0x01, |
| 70 | + 'index': this.interfaceNumber})) |
| 71 | + .then(() => { |
| 72 | + readLoop(); |
| 73 | + }); |
| 74 | + }; |
| 75 | + |
| 76 | + serial.Port.prototype.disconnect = function() { |
| 77 | + return this.device_.controlTransferOut({ |
| 78 | + 'requestType': 'class', |
| 79 | + 'recipient': 'interface', |
| 80 | + 'request': 0x22, |
| 81 | + 'value': 0x00, |
| 82 | + 'index': this.interfaceNumber}) |
| 83 | + .then(() => this.device_.close()); |
| 84 | + }; |
| 85 | + |
| 86 | + serial.Port.prototype.send = function(data) { |
| 87 | + return this.device_.transferOut(this.endpointOut, data); |
| 88 | + }; |
| 89 | +})(); |
0 commit comments