Template for creating Widgets for WG-Display-Embedded-multi, included with an automatic build pipeline to create the final widget binary.
Note
The template for the legacy WG-Display-Embedded project was moved to the legacy branch.
Widgets are Rust crates compiled to wasm32-unknown-unknown WebAssembly and packaged as WASM components using the WIT Component Model. They implement a standard interface (defined in WIT) and are installed and managed through the Web UI without needing to reflash the device.
Create a repo based on this template, or clone it:
git clone https://github.com/Siryll/wg_display_embedded_widget_template my-widget
cd my-widgetThe template includes the correct Cargo.toml, a working lib.rs example and the release workflow.
- Push your widget to GitHub
- Create a new Tag (needs to start with 'v', e.g. v0.0.1), the Pipeline will automatically build and put the
widget.precompiled.wasmbinary into the release. - In the Web UI of the embedded WG-Display, click Install from URL and enter the direct download URL
package widget:widget;
export get-name: func() -> string;
export get-version: func() -> string;
export get-config-schema: func() -> string;
export get-run-update-cycle-seconds: func() -> u32;
export run: func(context: widget-context) -> widget-result;| Export | Return | Description |
|---|---|---|
get-name |
string |
Display name shown in the Web UI and on screen |
get-version |
string |
Semver string (e.g. "0.1.0") |
get-config-schema |
string |
JSON Schema for the configuration form. Return "{}" if no config is needed |
get-run-update-cycle-seconds |
u32 |
How often the widget should be invoked (seconds) |
run |
widget-result |
Main entry point — fetch data, compute output, return string |
record datetime {
seconds: u64,
nanoseconds: u32,
}
record widget-context {
last-invocation: datetime, // UTC time of previous run (0 on first run)
config: string, // JSON matching your config schema
}
record widget-result {
data: string, // Layout JSON {"elements": [...]} (see `render` in widget/src/lib.rs), or plain text / error message
}interface http {
type status = u16;
variant method { get, head, post, put, delete }
record response {
status: status,
content-length: option<u64>,
bytes: list<u8>,
}
request: func(method: method, url: string, body: option<list<u8>>) -> result<response>;
}Limitations:
- Timeout: 30 seconds
- No streaming — entire response is buffered
interface clocks {
now: func() -> datetime;
}Returns the current UTC time. seconds is a Unix timestamp; nanoseconds is the sub-second part. Returns {0, 0} if time has not been synced yet.
interface logging {
enum level { debug, info, warn, error }
log: func(level: level, context: string, message: string);
}Output appears in the defmt serial monitor.
interface random {
get-random: func() -> u64;
}Returns a 64-bit random value from the ESP32 hardware RNG. Not cryptographically secure.
Return a valid JSON Schema from get-config-schema. The Web UI uses the jsonform library to render a form from the schema automatically.
In run(), parse the config from context.config:
#[derive(serde::Deserialize)]
struct Config {
location: String,
}
fn run(context: WidgetContext) -> WidgetResult {
let config: Config = serde_json::from_str(&context.config)
.unwrap_or(Config { location: "Bern".into() });
// ...
}rustup target add wasm32-unknown-unknown
cargo build --release --manifest-path widget/Cargo.toml --target wasm32-unknown-unknownInstall wasm-tools:
cargo install wasm-toolswasm-tools component new \
widget/target/wasm32-unknown-unknown/release/widget.wasm \
-o widget/widget.component.wasmThe on-device runtime is unable to compile the binary at runtime, for that reason it needs to be precompiled beforehand with the precompiler, see its README.
Should new versions with changes to the widget build process be released, then your widget needs to be update to be complatible again with the new version:
- Update the
tagof thewg-display-widgetgit dependency inwidget/Cargo.toml. - Update the version of
Siryll/wg_display_embedded_precompiler@vXin.github/workflows/build_release.yml.
Change both versions, run cargo update -p wg-display-widget in widget/ and create a new tag.