diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/wasm/Cargo.toml | 17 | ||||
| -rw-r--r-- | examples/wasm/README.md | 26 | ||||
| -rw-r--r-- | examples/wasm/index.html | 25 | ||||
| -rw-r--r-- | examples/wasm/src/lib.rs | 37 |
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 | |||
| 35 | rtt-target = { version = "0.3.1", features = ["cortex-m"] } | 35 | rtt-target = { version = "0.3.1", features = ["cortex-m"] } |
| 36 | heapless = { version = "0.7.5", default-features = false } | 36 | heapless = { version = "0.7.5", default-features = false } |
| 37 | rand_core = "0.6.3" | 37 | rand_core = "0.6.3" |
| 38 | critical-section = "0.2.1" | 38 | critical-section = "0.2.2" |
| 39 | 39 | ||
| 40 | micromath = "2.0.0" | 40 | micromath = "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] | ||
| 2 | authors = ["Ulf Lilleengen <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-wasm-example" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [lib] | ||
| 8 | crate-type = ["cdylib"] | ||
| 9 | |||
| 10 | [dependencies] | ||
| 11 | embassy = { version = "0.1.0", path = "../../embassy", features = ["log", "wasm"] } | ||
| 12 | |||
| 13 | wasm-logger = "0.2.0" | ||
| 14 | wasm-bindgen = "0.2" | ||
| 15 | web-sys = { version = "0.3", features = ["Document", "Element", "HtmlElement", "Node", "Window" ] } | ||
| 16 | log = "0.4.11" | ||
| 17 | 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 @@ | |||
| 1 | # WASM example | ||
| 2 | |||
| 3 | Examples use a CLI tool named `wasm-pack` to build this example: | ||
| 4 | |||
| 5 | ``` | ||
| 6 | cargo install wasm-pack | ||
| 7 | ``` | ||
| 8 | |||
| 9 | ## Building | ||
| 10 | |||
| 11 | To build the example, run: | ||
| 12 | |||
| 13 | ``` | ||
| 14 | wasm-pack build --target web | ||
| 15 | ``` | ||
| 16 | |||
| 17 | ## Running | ||
| 18 | |||
| 19 | To run the example, start a webserver server the local folder: | ||
| 20 | |||
| 21 | |||
| 22 | ``` | ||
| 23 | python -m http.server | ||
| 24 | ``` | ||
| 25 | |||
| 26 | 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 @@ | |||
| 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 | |||
| 4 | use embassy::{ | ||
| 5 | executor::Spawner, | ||
| 6 | time::{Duration, Timer}, | ||
| 7 | }; | ||
| 8 | |||
| 9 | #[embassy::task] | ||
| 10 | async 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] | ||
| 34 | async fn main(spawner: Spawner) { | ||
| 35 | wasm_logger::init(wasm_logger::Config::default()); | ||
| 36 | spawner.spawn(ticker()).unwrap(); | ||
| 37 | } | ||
