web.html 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>CocoBlockly Pi – Preview</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <meta http-equiv="Content-Type" content="text/html; scharset=UTF-8">
  8. <meta name="theme-color" content="#2C4FCD">
  9. <!-- <script src="./webusb.js" type="module"></script> -->
  10. <!-- <script type="text/javascript" src="uform.js"></script>
  11. <script type="text/javascript" src="libs/jquery-3.3.1.min.js"></script>
  12. <script type="text/javascript" src="libs/jquery-ui.min.js"></script>
  13. <script type="text/javascript" src="libs/jquery.hotkeys.js"></script>
  14. <script type="text/javascript" src="libs/jquery.multi-select.js"></script>
  15. <script type="text/javascript" src="//x.cocorobo.cn/src/blockly/web.js"></script> -->
  16. <script type="module">
  17. import Pl2303WebUsbSerial from './webusb.js';
  18. // import { debounce } from '';
  19. const VENDOR_ID = 0x01000000;
  20. const PRODUCT_ID = 0x01000001
  21. const NUMBER_OF_BYTES_TO_READ = 64;
  22. const READ_DEBOUNCE_DELAY_IN_MILLIS = 100;
  23. let usbDevice;
  24. // Invoke this function when the user clicked on a button to connect to the USB device. Due to security limitations of
  25. // WebUSB, it's required to request the device within the event handler.
  26. // E.g. <button onclick="handleConnectButtonClick">Connect to USB device</button>
  27. const handleConnectButtonClick = async () => {
  28. const device = await navigator.usb.requestDevice({ filters: [{ vendorId: VENDOR_ID, productId: PRODUCT_ID }] });
  29. usbDevice = new Pl2303WebUsbSerial(device);
  30. await usbDevice.connect();
  31. await readUntilSilent();
  32. };
  33. // This function is used to (trivially) batch-process read bytes. To do so, the USB device is read, and the received
  34. // bytes are stored in a local buffer. A function is debounced inovked to consume the buffer. After the wait
  35. // (here: 100 ms) the consume function is executed, the buffer is processed and emptied.
  36. // Reading from the USB device is done in a loop.
  37. const readUntilSilent = async () => {
  38. let buffer = new Blob();
  39. const debouncedConsumeBuffer = debounce(async () => {
  40. const array = await buffer.arrayBuffer();
  41. console.log(`debounced transfer in: ${new TextDecoder().decode(array)}`);
  42. buffer = new Blob();
  43. }, READ_DEBOUNCE_DELAY_IN_MILLIS);
  44. while (usbDevice) {
  45. try {
  46. const readout = await usbDevice.read(NUMBER_OF_BYTES_TO_READ);
  47. buffer = new Blob([buffer, readout.data.buffer]);
  48. debouncedConsumeBuffer();
  49. } catch (error) {
  50. console.debug(`Readout error: ${error}`, error);
  51. }
  52. }
  53. }
  54. document.getElementById("testclick").onclick = function () { handleConnectButtonClick(); }
  55. </script>
  56. </head>
  57. <body>
  58. <div id="testclick">点击测试</div>
  59. </body>
  60. </html>