Skip to content

Commit

Permalink
Replace the built-in defmt debug logging to the platform trait
Browse files Browse the repository at this point in the history
This way we don't have to add defmt, esp32-println and others as features to corelib and the slint api crate
(which would also expose them as public dependencies),
but instead this can be simply delegated to the Platform trait.
  • Loading branch information
tronical committed Sep 2, 2022
1 parent deee971 commit 437218d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
4 changes: 0 additions & 4 deletions api/rs/slint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ renderer-software = [
## This feature is an alias for `backend-qt`
renderer-qt = ["backend-qt"]

## This feature enables debug output to be sent via the [deferred formatting framework](https://defmt.ferrous-systems.com).
## Use this in MCU environments where defmt is supported.
defmt = ["i-slint-core/defmt"]

## This feature enables floating point arithmetic emulation using the [libm](https://crates.io/crates/libm) crate. Use this
## in MCU environments where the processor does not support floating point arithmetic.
libm = ["i-slint-core/libm"]
Expand Down
4 changes: 2 additions & 2 deletions examples/mcu-board-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ links = "mcu_board_support" # just so we can pass metadata to the slint build cr
path = "lib.rs"

[features]
pico-st7789 = ["slint/unsafe-single-threaded", "rp-pico", "embedded-hal", "cortex-m-rt", "alloc-cortex-m", "fugit", "cortex-m", "cortex-m", "display-interface", "st7789", "defmt", "defmt-rtt", "slint/defmt", "shared-bus", "slint/libm", "embedded-dma", "embedded-graphics", "euclid/libm"]
stm32h735g = ["slint/unsafe-single-threaded", "embedded-hal", "cortex-m-rt", "alloc-cortex-m", "embedded-time", "cortex-m", "slint/defmt", "stm32h7xx-hal/stm32h735", "defmt", "defmt-rtt", "embedded-display-controller", "ft5336", "panic-probe", "slint/libm"]
pico-st7789 = ["slint/unsafe-single-threaded", "rp-pico", "embedded-hal", "cortex-m-rt", "alloc-cortex-m", "fugit", "cortex-m", "cortex-m", "display-interface", "st7789", "defmt", "defmt-rtt", "shared-bus", "slint/libm", "embedded-dma", "embedded-graphics", "euclid/libm"]
stm32h735g = ["slint/unsafe-single-threaded", "embedded-hal", "cortex-m-rt", "alloc-cortex-m", "embedded-time", "cortex-m", "stm32h7xx-hal/stm32h735", "defmt", "defmt-rtt", "embedded-display-controller", "ft5336", "panic-probe", "slint/libm"]
esp32-s2-kaluga-1 = ["slint/unsafe-single-threaded", "esp32s2-hal", "embedded-hal", "xtensa-lx-rt", "esp-alloc", "esp-println", "display-interface", "display-interface-spi", "st7789", "slint/libm"]

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions examples/mcu-board-support/pico_st7789.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ impl slint::platform::Platform for PicoBackend {
cortex_m::asm::wfe();
}
}

fn debug_log(&self, text: &str) {
defmt::println!("{=str}", text);
}
}

enum PioTransfer<TO: WriteTarget, CH: SingleChannel> {
Expand Down
4 changes: 4 additions & 0 deletions examples/mcu-board-support/stm32h735g.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,8 @@ impl slint::platform::Platform for StmBackend {
let val = self.timer.get().map_or(0, |t| t.counter() / 10);
core::time::Duration::from_millis(val.into())
}

fn debug_log(&self, text: &str) {
defmt::println!("{=str}", text);
}
}
1 change: 0 additions & 1 deletion internal/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ vtable = { version="0.1.9", path = "../../helper_crates/vtable" }
atomic-polyfill = "1.0.1"
auto_enums = "0.7"
cfg-if = "1"
defmt = { version = "0.3.0", optional = true }
derive_more = "0.99.5"
euclid = { version = "0.22.1", default-features = false }
instant = { version = "0.1", features = [ "now" ], optional = true }
Expand Down
24 changes: 23 additions & 1 deletion internal/core/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ pub trait Platform {
fn clipboard_text(&self) -> Option<String> {
None
}

/// This function is called when debug() is used in .slint files. The implementation
/// should direct the output to some developer visible terminal. The default implementation
/// uses stderr if available, or `console.log` when targeting wasm.
fn debug_log(&self, _text: &str) {
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}

log(_text);
} else if #[cfg(feature = "std")] {
eprintln!("{}", _text);
}
}
}
}

/// Trait that is returned by the [`Platform::new_event_loop_proxy`]
Expand Down Expand Up @@ -103,7 +124,8 @@ impl std::convert::From<crate::animations::Instant> for instant::Instant {
}

thread_local! {
pub(crate) static PLATFORM_INSTANCE : once_cell::unsync::OnceCell<Box<dyn Platform>>
/// Internal: Singleton of the platform abstraction.
pub static PLATFORM_INSTANCE : once_cell::unsync::OnceCell<Box<dyn Platform>>
= once_cell::unsync::OnceCell::new()
}
static EVENTLOOP_PROXY: OnceCell<Box<dyn EventLoopProxy + 'static>> = OnceCell::new();
Expand Down
38 changes: 6 additions & 32 deletions internal/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,47 +84,21 @@ pub extern "C" fn send_keyboard_string_sequence(
}

cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}

if #[cfg(feature = "std")] {
#[macro_export]
/// This macro allows producing debug output that will appear on stderr in regular builds
/// and in the console log for wasm builds.
macro_rules! debug_log {
($($t:tt)*) => ($crate::tests::log(&format_args!($($t)*).to_string()))
}
} else if #[cfg(feature = "std")] {
#[doc(hidden)]
pub use std::eprintln;

/// This macro allows producing debug output that will appear on stderr in regular builds
/// and in the console log for wasm builds.
#[macro_export]
macro_rules! debug_log {
($($t:tt)*) => ($crate::tests::eprintln!($($t)*))
}
} else if #[cfg(feature = "defmt")] {
#[doc(hidden)]
pub fn log(s: &str) {
defmt::println!("{=str}", s);
}

#[macro_export]
/// This macro allows producing debug output that will appear on the output of the debug probe
macro_rules! debug_log {
($($t:tt)*) => ($crate::tests::log({ use alloc::string::ToString; &format_args!($($t)*).to_string() }))
($($t:tt)*) => ($crate::platform::PLATFORM_INSTANCE.with(|p| {p.get().map(|p| p.debug_log(&format_args!($($t)*).to_string()));}))
}
} else {
#[macro_export]
/// Do nothing
/// This macro allows producing debug output that will appear on stderr in regular builds
/// and in the console log for wasm builds.
macro_rules! debug_log {
($($t:tt)*) => (let _ = &format_args!($($t)*);)
($($t:tt)*) => ($crate::platform::PLATFORM_INSTANCE.with(|p| { use alloc::string::ToString; p.get().map(|p| p.debug_log(&format_args!($($t)*).to_string()));}))
}
}

}

0 comments on commit 437218d

Please sign in to comment.