Skip to content

Instantly share code, notes, and snippets.

@nuta
Last active June 14, 2024 01:03
Show Gist options
  • Select an option

  • Save nuta/2c70ba8855f50c536a51f0c5993c1e4c to your computer and use it in GitHub Desktop.

Select an option

Save nuta/2c70ba8855f50c536a51f0c5993c1e4c to your computer and use it in GitHub Desktop.

Revisions

  1. nuta revised this gist Jul 29, 2019. No changes.
  2. nuta revised this gist Jul 29, 2019. No changes.
  3. nuta created this gist Jan 27, 2018.
    35 changes: 35 additions & 0 deletions index.html
    Original 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>
    21 changes: 21 additions & 0 deletions index.js
    Original 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;
    }
    14 changes: 14 additions & 0 deletions package.json
    Original 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"
    }
    }
    73 changes: 73 additions & 0 deletions serial.js
    Original 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 }
    12 changes: 12 additions & 0 deletions webpack.config.js
    Original 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"
    }
    };