|
| 1 | +<!-- Based on https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/subsys/usb/webusb --> |
| 2 | + |
| 3 | +<!-- |
| 4 | + Example of WebUSB web page to communicate with a USB device. |
| 5 | + This can be used as a starting point for your self-hosted WebUSB landing page. |
| 6 | +--> |
| 7 | + |
| 8 | +<!DOCTYPE html> |
| 9 | +<html lang="en"> |
| 10 | + <head> |
| 11 | + <title>Espressif WebUSB Console Example</title> |
| 12 | + </head> |
| 13 | + |
| 14 | +<body> |
| 15 | + <script> |
| 16 | + var serial = {}; |
| 17 | + |
| 18 | + (function() { |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + serial.getPorts = function() { |
| 22 | + return navigator.usb.getDevices().then(devices => { |
| 23 | + return devices.map(device => new serial.Port(device)); |
| 24 | + }); |
| 25 | + }; |
| 26 | + |
| 27 | + serial.requestPort = function() { |
| 28 | + const filters = [ |
| 29 | + { 'vendorId': 0x10c4, 'productId': 0xea60 }, |
| 30 | + { 'vendorId': 0x303a, 'productId': 0x1001 }, |
| 31 | + { 'vendorId': 0x303a, 'productId': 0x0002 }, |
| 32 | + ]; |
| 33 | + return navigator.usb.requestDevice({ 'filters': filters }).then( |
| 34 | + device => new serial.Port(device) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + serial.Port = function(device) { |
| 39 | + this.device_ = device; |
| 40 | + }; |
| 41 | + |
| 42 | + serial.Port.prototype.connect = function() { |
| 43 | + let readLoop = () => { |
| 44 | + const { |
| 45 | + endpointNumber |
| 46 | + } = this.device_.configuration.interfaces[0].alternate.endpoints[0] |
| 47 | + this.device_.transferIn(endpointNumber, 64).then(result => { |
| 48 | + this.onReceive(result.data); |
| 49 | + readLoop(); |
| 50 | + }, error => { |
| 51 | + this.onReceiveError(error); |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + return this.device_.open() |
| 56 | + .then(() => { |
| 57 | + if (this.device_.configuration === null) { |
| 58 | + return this.device_.selectConfiguration(1); |
| 59 | + } |
| 60 | + }) |
| 61 | + .then(() => this.device_.claimInterface(0)) |
| 62 | + .then(() => { |
| 63 | + readLoop(); |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + serial.Port.prototype.disconnect = function() { |
| 68 | + return this.device_.close(); |
| 69 | + }; |
| 70 | + |
| 71 | + serial.Port.prototype.send = function(data) { |
| 72 | + const { |
| 73 | + endpointNumber |
| 74 | + } = this.device_.configuration.interfaces[0].alternate.endpoints[1] |
| 75 | + return this.device_.transferOut(endpointNumber, data); |
| 76 | + }; |
| 77 | + })(); |
| 78 | + |
| 79 | + let port; |
| 80 | + |
| 81 | + function connect() { |
| 82 | + port.connect().then(() => { |
| 83 | + port.onReceive = data => { |
| 84 | + let textDecoder = new TextDecoder(); |
| 85 | + console.log("Received:", textDecoder.decode(data)); |
| 86 | + document.getElementById('output').value += textDecoder.decode(data); |
| 87 | + } |
| 88 | + port.onReceiveError = error => { |
| 89 | + console.error(error); |
| 90 | + document.querySelector("#connect").style = "visibility: initial"; |
| 91 | + port.disconnect(); |
| 92 | + }; |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + function send(string) { |
| 97 | + console.log("sending to serial:" + string.length); |
| 98 | + if (string.length === 0) |
| 99 | + return; |
| 100 | + console.log("sending to serial: [" + string +"]\n"); |
| 101 | + |
| 102 | + let view = new TextEncoder('utf-8').encode(string); |
| 103 | + console.log(view); |
| 104 | + if (port) { |
| 105 | + port.send(view); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + window.onload = _ => { |
| 110 | + document.querySelector("#connect").onclick = function() { |
| 111 | + serial.requestPort().then(selectedPort => { |
| 112 | + port = selectedPort; |
| 113 | + this.style = "visibility: hidden"; |
| 114 | + connect(); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + document.querySelector("#submit").onclick = () => { |
| 119 | + let source = document.querySelector("#input").value; |
| 120 | + send(source); |
| 121 | + } |
| 122 | + } |
| 123 | + </script> |
| 124 | + |
| 125 | + <button id="connect" style="visibility: initial">Connect To ESP Device</button> |
| 126 | + <br><br><label for="input">Sender: </label> <br> |
| 127 | + <textarea id="input" rows="25" cols="80">Send to ESP Device</textarea> |
| 128 | + <br><button id="submit">Send</button> |
| 129 | + <br><br> |
| 130 | + <label for="output">Receiver: </label> <br> |
| 131 | + <textarea id="output" rows="25" cols="80"></textarea> |
| 132 | +</body> |
| 133 | +</html> |
0 commit comments