From e24528051b9ed26157bee93e32e37e7b06b8f4cc Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 13 Sep 2021 14:35:40 +0200 Subject: Add WASM support for executor * Adds an executor for WASM runtimes based on wasm_bindgen. * Add time driver based on JS time handling. * Add example that can run in browser locally. * Update to critical-section version that supports 'std' flag --- examples/wasm/Cargo.toml | 17 +++++++++++++++++ examples/wasm/README.md | 26 ++++++++++++++++++++++++++ examples/wasm/index.html | 25 +++++++++++++++++++++++++ examples/wasm/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 examples/wasm/Cargo.toml create mode 100644 examples/wasm/README.md create mode 100644 examples/wasm/index.html create mode 100644 examples/wasm/src/lib.rs (limited to 'examples/wasm') diff --git a/examples/wasm/Cargo.toml b/examples/wasm/Cargo.toml new file mode 100644 index 000000000..77f513ffa --- /dev/null +++ b/examples/wasm/Cargo.toml @@ -0,0 +1,17 @@ +[package] +authors = ["Ulf Lilleengen "] +edition = "2018" +name = "embassy-wasm-example" +version = "0.1.0" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +embassy = { version = "0.1.0", path = "../../embassy", features = ["log", "wasm"] } + +wasm-logger = "0.2.0" +wasm-bindgen = "0.2" +web-sys = { version = "0.3", features = ["Document", "Element", "HtmlElement", "Node", "Window" ] } +log = "0.4.11" +critical-section = "0.2.2" diff --git a/examples/wasm/README.md b/examples/wasm/README.md new file mode 100644 index 000000000..4bed4a797 --- /dev/null +++ b/examples/wasm/README.md @@ -0,0 +1,26 @@ +# WASM example + +Examples use a CLI tool named `wasm-pack` to build this example: + +``` +cargo install wasm-pack +``` + +## Building + +To build the example, run: + +``` +wasm-pack build --target web +``` + +## Running + +To run the example, start a webserver server the local folder: + + +``` +python -m http.server +``` + +Then, open a browser at https://127.0.0.1:8000 and watch the ticker print entries to the window. diff --git a/examples/wasm/index.html b/examples/wasm/index.html new file mode 100644 index 000000000..05e8b29f8 --- /dev/null +++ b/examples/wasm/index.html @@ -0,0 +1,25 @@ + + + + + + + + +

Log

+
+
    +
+
+ + diff --git a/examples/wasm/src/lib.rs b/examples/wasm/src/lib.rs new file mode 100644 index 000000000..0aa32a70b --- /dev/null +++ b/examples/wasm/src/lib.rs @@ -0,0 +1,37 @@ +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +use embassy::{ + executor::Spawner, + time::{Duration, Timer}, +}; + +#[embassy::task] +async fn ticker() { + let window = web_sys::window().expect("no global `window` exists"); + + let mut counter = 0; + loop { + let document = window.document().expect("should have a document on window"); + let list = document + .get_element_by_id("log") + .expect("should have a log element"); + + let li = document + .create_element("li") + .expect("error creating list item element"); + li.set_text_content(Some(&format!("tick {}", counter))); + + list.append_child(&li).expect("error appending list item"); + log::info!("tick {}", counter); + counter += 1; + + Timer::after(Duration::from_secs(1)).await; + } +} + +#[embassy::main] +async fn main(spawner: Spawner) { + wasm_logger::init(wasm_logger::Config::default()); + spawner.spawn(ticker()).unwrap(); +} -- cgit