Last active
June 14, 2024 01:03
-
-
Save nuta/2c70ba8855f50c536a51f0c5993c1e4c to your computer and use it in GitHub Desktop.
Revisions
-
nuta revised this gist
Jul 29, 2019 . No changes.There are no files selected for viewing
-
nuta revised this gist
Jul 29, 2019 . No changes.There are no files selected for viewing
-
nuta created this gist
Jan 27, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>esptool.js</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min.js" integrity="sha256-8rsVcpCnO6HdeJL84i0VdubjM42fjSmO8aONghdq3gc=" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/xterm/2.9.2/xterm.min.css" integrity="sha256-w69o8Y6P5VZjfYFmn2KlqMU7TUi2I+oWObi8FLlVZZg=" crossorigin="anonymous" /> </head> <body> <h1>esptool.js</h1> <button id="flash-button" style="padding: 30px; font-size: 40px">Flash</button> <div id="serial"></div> <script src="esptool.js"></script> <script> const term = new Terminal() term.open(document.querySelector("#serial")) document.querySelector("#flash-button").addEventListener("click", () => { const esp = new Esptool() esp.connect().then(() => { const receive = () => { esp.serial.read().then(data => { const s = String.fromCharCode.apply(null, new Uint8Array(data)) term.write(s) receive() }) } receive() }) }) </script> </body> </html> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ const { Serial } = require("./serial") class Esptool { constructor() { this.serial = new Serial() } connect() { return this.serial.connect().then(() => { return this.serial.initialize() }) } async flash() { } } if (window) { // Web browser window.Esptool = Esptool; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ { "name": "esptool.js", "version": "0.0.1", "description": "Esptool implemented for Node.js and web browsers.", "main": "lib/index.js", "repository": "https://github.com/seiyanuta/esptool.js", "author": "Seiya Nuta <nuta@seiya.me>", "license": "(CC0-1.0 OR MIT)", "devDependencies": { "copy-webpack-plugin": "^4.3.1", "webpack": "^3.10.0", "webpack-dev-server": "^2.11.1" } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ class Serial { constructor() { this.usb = null } connect() { const filters = [ { 'vendorId': 0x10c4, 'productId': 0xea60 } ] return navigator.usb.getDevices().then(devices => { if (devices.length > 0) { const usb = devices[0] this.usb = usb return usb } else { return navigator.usb.requestDevice({ filters }).then(usb => { this.usb = usb return usb }) } }) } initialize() { return this.usb.open() .then(() => { return this.usb.selectConfiguration(1) }) .then(() => { console.log(this.usb) return this.usb.claimInterface(0) }) .then(() => { return this.usb.controlTransferOut({ requestType: 'vendor', recipient: 'device', request: 0x00, index: 0x00, value: 0x01 }) }) .then(() => { return this.usb.controlTransferOut({ requestType: 'vendor', recipient: 'device', request: 0x07, index: 0x00, value: 0x03 | 0x0100 | 0x0200 }) }) .then(() => { return this.usb.controlTransferOut({ requestType: 'vendor', recipient: 'device', request: 0x01, index: 0x00, value: 0x384000 / 115200 }) }) } async read() { const r = await this.usb.transferIn(1, 64) return new Uint8Array(r.data.buffer) } async write(data) { await this.usb.transferIn(1, data) } } module.exports = { Serial } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ const path = require("path") module.exports = { entry: { app: ["./index.js"] }, output: { path: __dirname, publicPath: "/", filename: "esptool.js" } };