Skip to content

Commit

Permalink
Use NPM package
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Jun 4, 2021
1 parent ae45ddc commit e20c21b
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 1,153 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ESPHome Web: JavaScript SDK for ESPHome
# ESP Web Tools

Allow flashing ESPHome or other ESP-based firmwares via the browser. Will automatically detect the board type and select a supported firmware.

```html
<esphome-web-install-button
<esp-web-install-button
manifest="firmware_esphome/manifest.json"
></esphome-web-install-button>
></esp-web-install-button>
```

Manifest definition:
Expand Down Expand Up @@ -37,10 +37,10 @@ Manifest definition:
Allows for optionally passing an attribute to trigger an erase before installation.

```html
<esphome-web-install-button
<esp-web-install-button
manifest="firmware_esphome/manifest.json"
erase-first
></esphome-web-install-button>
></esp-web-install-button>
```

All attributes can also be set via properties (`manifest`, `eraseFirst`)
Expand Down
14 changes: 11 additions & 3 deletions example.html → index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
</head>
<body>
<p>ESPHome Web is a set of tools to allow working with ESP devices in the browser.</p>
<p>To flash the XX firmware, connect an ESP to your computer and hit the button:</p>
<esphome-web-install-button
<p>To flash the ESPHome firmware, connect an ESP to your computer and hit the button:</p>
<esp-web-install-button
manifest="firmware_build/manifest.json"
></esphome-web-install-button>
></esp-web-install-button>
<p><i>Note, this only works in desktop Chrome and Edge. Android support has not been implemented yet.</i></div>
<p>
This works by combining Web Serial with a <a href="firmware_build/manifest.json">manifest</a> which describes the firmware. It will automatically detect the type of the connected ESP device and find the right firmware files in the manifest.
</p>
<script module>
import(
// In development we import locally.
window.location.hostname === "localhost"
? "/dist/web/install-button.js"
: "https://unpkg.com/[email protected]/dist/web/install-button.js?module"
);
</script>
<script src="./dist/web/install-button.js" type="module"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "esphome-web",
"name": "esp-web-tools",
"version": "0.0.1",
"description": "Web tools for ESPHome",
"main": "dist/flash-button.js",
"description": "Web tools for ESP devices",
"main": "dist/install-button.js",
"repository": "https://github.com/esphome/web",
"author": "ESPHome maintainers",
"license": "Apache-2.0",
Expand All @@ -26,6 +26,7 @@
"@material/mwc-circular-progress": "^0.21.0",
"@material/mwc-dialog": "^0.21.0",
"@material/mwc-textfield": "^0.21.0",
"esp-web-flasher": "^1.0.0",
"lit": "^2.0.0-rc.2",
"tslib": "^2.2.0"
}
Expand Down
14 changes: 14 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { connect } from "esp-web-flasher";

type AsyncReturnType<T extends (...args: any) => any> = T extends (
...args: any
) => Promise<infer U>
? U
: T extends (...args: any) => infer U
? U
: any;

// Waiting for esp-web-flash >1.0.0 release which will include this type
export type ESPLoader = AsyncReturnType<typeof connect>;
export type Logger = Parameters<typeof connect>[0];

export interface Build {
chipFamily: "ESP32" | "ESP8266";
improv: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/install-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ class InstallButton extends HTMLElement {
}
}

customElements.define("esphome-web-install-button", InstallButton);
customElements.define("esp-web-install-button", InstallButton);
21 changes: 14 additions & 7 deletions src/start-flash.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { html } from "lit";
import { connect } from "./vendor/esptool";
import type { Logger } from "./vendor/esptool/const";
import type { ESPLoader } from "./vendor/esptool/esp_loader";
import { Build, Manifest } from "./const";
import { connect } from "esp-web-flasher";
import { Build, ESPLoader, Manifest, Logger } from "./const";
import "./flash-log";
import { getChipFamilyName } from "./util";
import { getChipFamilyName, sleep } from "./util";

export const startFlash = async (
logger: Logger,
Expand Down Expand Up @@ -123,7 +121,10 @@ export const startFlash = async (
}
}

logEl.removeRow("preparing");
logEl.addRow({
id: "preparing",
content: `Ready to install`,
});

if (eraseFirst) {
logEl.addRow({
Expand All @@ -132,7 +133,12 @@ export const startFlash = async (
});
}

let lastPct = -1;
let lastPct = 0;

logEl.addRow({
id: "write",
content: html`Writing progress: ${lastPct}%`,
});

for (const part of build.parts) {
await espStub.flashData(
Expand All @@ -152,6 +158,7 @@ export const startFlash = async (
);
}

await sleep(100);
await esploader.softReset();

const doImprov =
Expand Down
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
CHIP_FAMILY_ESP32,
CHIP_FAMILY_ESP32S2,
CHIP_FAMILY_ESP8266,
} from "./vendor/esptool";
import { ESPLoader } from "./vendor/esptool/esp_loader";
} from "esp-web-flasher";
import { ESPLoader } from "./const";

export const getChipFamilyName = (esploader: ESPLoader) => {
switch (esploader.chipFamily) {
Expand All @@ -17,3 +17,6 @@ export const getChipFamilyName = (esploader: ESPLoader) => {
return "Unknown Chip";
}
};

export const sleep = (time: number) =>
new Promise((resolve) => setTimeout(resolve, time));
9 changes: 0 additions & 9 deletions src/vendor/esptool/LICENSE.md

This file was deleted.

4 changes: 0 additions & 4 deletions src/vendor/esptool/README.md

This file was deleted.

91 changes: 0 additions & 91 deletions src/vendor/esptool/const.ts

This file was deleted.

Loading

0 comments on commit e20c21b

Please sign in to comment.