aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-09-13 14:35:40 +0200
committerUlf Lilleengen <[email protected]>2021-09-13 16:42:39 +0200
commite24528051b9ed26157bee93e32e37e7b06b8f4cc (patch)
treebb0c4f201a6a7df9099d27b3a64f92036d645d45 /examples
parentf1c35b40c74db489da8e04f1c2e87a1d4030c617 (diff)
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
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h7/Cargo.toml2
-rw-r--r--examples/wasm/Cargo.toml17
-rw-r--r--examples/wasm/README.md26
-rw-r--r--examples/wasm/index.html25
-rw-r--r--examples/wasm/src/lib.rs37
5 files changed, 106 insertions, 1 deletions
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index 94586b8ac..c581e212a 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -35,7 +35,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
35rtt-target = { version = "0.3.1", features = ["cortex-m"] } 35rtt-target = { version = "0.3.1", features = ["cortex-m"] }
36heapless = { version = "0.7.5", default-features = false } 36heapless = { version = "0.7.5", default-features = false }
37rand_core = "0.6.3" 37rand_core = "0.6.3"
38critical-section = "0.2.1" 38critical-section = "0.2.2"
39 39
40micromath = "2.0.0" 40micromath = "2.0.0"
41 41
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 @@
1[package]
2authors = ["Ulf Lilleengen <[email protected]>"]
3edition = "2018"
4name = "embassy-wasm-example"
5version = "0.1.0"
6
7[lib]
8crate-type = ["cdylib"]
9
10[dependencies]
11embassy = { version = "0.1.0", path = "../../embassy", features = ["log", "wasm"] }
12
13wasm-logger = "0.2.0"
14wasm-bindgen = "0.2"
15web-sys = { version = "0.3", features = ["Document", "Element", "HtmlElement", "Node", "Window" ] }
16log = "0.4.11"
17critical-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 @@
1# WASM example
2
3Examples use a CLI tool named `wasm-pack` to build this example:
4
5```
6cargo install wasm-pack
7```
8
9## Building
10
11To build the example, run:
12
13```
14wasm-pack build --target web
15```
16
17## Running
18
19To run the example, start a webserver server the local folder:
20
21
22```
23python -m http.server
24```
25
26Then, 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 @@
1<!DOCTYPE html>
2<html>
3 <head>
4 <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
5 </head>
6 <body>
7 <!-- Note the usage of `type=module` here as this is an ES6 module -->
8 <script type="module">
9 // Use ES module import syntax to import functionality from the module
10 // that we have compiled.
11 //
12 // Note that the `default` import is an initialization function which
13 // will "boot" the module and make it ready to use. Currently browsers
14 // don't support natively imported WebAssembly as an ES module, but
15 // eventually the manual initialization won't be required!
16 import init from './pkg/embassy_wasm_example.js';
17 await init();
18 </script>
19 <h1>Log</h1>
20 <div>
21 <ul id="log">
22 </ul>
23 </div>
24 </body>
25</html>
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 @@
1#![feature(type_alias_impl_trait)]
2#![allow(incomplete_features)]
3
4use embassy::{
5 executor::Spawner,
6 time::{Duration, Timer},
7};
8
9#[embassy::task]
10async fn ticker() {
11 let window = web_sys::window().expect("no global `window` exists");
12
13 let mut counter = 0;
14 loop {
15 let document = window.document().expect("should have a document on window");
16 let list = document
17 .get_element_by_id("log")
18 .expect("should have a log element");
19
20 let li = document
21 .create_element("li")
22 .expect("error creating list item element");
23 li.set_text_content(Some(&format!("tick {}", counter)));
24
25 list.append_child(&li).expect("error appending list item");
26 log::info!("tick {}", counter);
27 counter += 1;
28
29 Timer::after(Duration::from_secs(1)).await;
30 }
31}
32
33#[embassy::main]
34async fn main(spawner: Spawner) {
35 wasm_logger::init(wasm_logger::Config::default());
36 spawner.spawn(ticker()).unwrap();
37}