esp32_wasm_hal/
lib.rs

1#![no_std]
2
3extern crate embedded_hal;
4use embedded_hal::blocking::delay::DelayMs;
5
6pub mod print;
7
8pub mod delay;
9use delay::WasmDelay;
10
11#[cfg(feature = "logger")]
12pub mod logger;
13
14pub mod prelude;
15
16mod runtime;
17
18/// Internal hardware singleton
19static mut ESP32: Option<Esp32> = Some(Esp32{
20    _extensible: (),
21});
22
23/// ESP32 WASM API object
24pub struct Esp32 {
25    // Block construction of this object outside of the library
26    _extensible: (),
27}
28
29#[link(wasm_import_module = "env")]
30extern {
31    pub fn get_ticks(m: &u32);
32}
33
34impl Esp32 {
35    /// Take the ESP32 configuration object
36    pub fn take() -> Option<Self> {
37        // TODO: mutable static is not ideal, but at this point we only
38        // have one process within the WASM runtime so...
39        unsafe { ESP32.take() }
40    }
41
42    /// Get the current tick count from the underlying OS
43    pub fn get_ticks(&self) -> u32 {
44        let mut v = 0;
45        unsafe { get_ticks(&mut v) };
46        v as u32
47    }
48}
49
50// Convenience implementation of delay
51impl DelayMs<u32> for Esp32 {
52    fn delay_ms(&mut self, m: u32) {
53        WasmDelay::delay_ms(&mut WasmDelay, m);
54    }
55}
56
57
58