diff --git a/examples/include/lv_conf.h b/examples/include/lv_conf.h index 50314eee..ec245c44 100644 --- a/examples/include/lv_conf.h +++ b/examples/include/lv_conf.h @@ -87,7 +87,7 @@ *It removes the need to manually update the tick with `lv_tick_inc()`)*/ #define LV_TICK_CUSTOM 0 #if LV_TICK_CUSTOM - #define LV_TICK_CUSTOM_INCLUDE "rs_timer.h" /*Header for the system time function*/ + #define LV_TICK_CUSTOM_INCLUDE /*Header for the system time function*/ #define LV_TICK_CUSTOM_SYS_TIME_EXPR (rs_lv_timer()) /*Expression evaluating to current system time in ms*/ /*If using lvgl as ESP32 component*/ // #define LV_TICK_CUSTOM_INCLUDE "esp_timer.h" diff --git a/lvgl-sys/build.rs b/lvgl-sys/build.rs index ae421f57..561bbbbd 100644 --- a/lvgl-sys/build.rs +++ b/lvgl-sys/build.rs @@ -28,6 +28,8 @@ fn main() { let shims_dir = project_dir.join("shims"); let vendor = project_dir.join("vendor"); let lvgl_src = vendor.join("lvgl").join("src"); + #[cfg(feature = "rust_timer")] + let timer_shim = vendor.join("include").join("timer"); let current_dir = canonicalize(PathBuf::from(env::var("PWD").unwrap())); let font_extra_src = { @@ -140,12 +142,12 @@ fn main() { if let Some(p) = &font_extra_src { cfg.includes(p); } + #[cfg(feature = "rust_timer")] + cfg.include(&timer_shim); #[cfg(feature = "drivers")] cfg.include(&drivers); #[cfg(feature = "drivers")] cfg.includes(incl_extra.split(',')); - //#[cfg(feature = "rust_timer")] - //cfg.include(&shims_dir.join("rs_timer.h").to_str().unwrap()); cfg.compile("lvgl"); diff --git a/examples/include/rs_timer.h b/lvgl-sys/vendor/include/timer/rs_timer.h similarity index 100% rename from examples/include/rs_timer.h rename to lvgl-sys/vendor/include/timer/rs_timer.h diff --git a/lvgl/Cargo.toml b/lvgl/Cargo.toml index 7d8c3018..b09ac13e 100644 --- a/lvgl/Cargo.toml +++ b/lvgl/Cargo.toml @@ -48,6 +48,10 @@ lvgl_alloc = ["alloc"] # need any extra features, but the default config is quite conservative. use-vendored-config = ["lvgl-sys/use-vendored-config"] +# Enables using a custom tick function in Rust for LVGL. See the documentation +# on the timer module for usage notes. +rust_timer = ["lvgl-sys/rust_timer"] + # Enables some unstable features. Currently, only #[cfg_accessible] is used. # This feature will currently allow: # - Using built-in LVGL fonts other than the default @@ -67,9 +71,6 @@ nightly = [] # function before constructing or using anything LVGL-related. unsafe_no_autoinit = [] -# -rust_timer = ["lvgl-sys/rust_timer"] - [build-dependencies] quote = "1.0.23" proc-macro2 = "1.0.51" diff --git a/lvgl/src/timer.rs b/lvgl/src/timer.rs index ecca56db..d45239e1 100644 --- a/lvgl/src/timer.rs +++ b/lvgl/src/timer.rs @@ -5,12 +5,7 @@ //! //! # Building //! -//! Ensure that the `rs_timer.h` file is copied in the same directory as the -//! LVGL `lv_conf.h` (i.e. `DEP_LV_CONFIG_PATH`). This is necessary due to the -//! way LVGL handles the timer function import. The `rs_timer.h` file can be -//! found in the `examples/include` directory in the `lvgl-rs` repository. -//! -//! Set `LV_TICK_CUSTOM` to `1` and `LV_TICK_CUSTOM_INCLUDE` to `"rs_timer.h"` +//! Set `LV_TICK_CUSTOM` to `1` and `LV_TICK_CUSTOM_INCLUDE` to `` //! in `lv_conf.h`, and enable the `rust_timer` feature on the `lvgl` crate to //! enable this functionality. //! @@ -19,9 +14,42 @@ //! Implement the `lvgl::timer::LvClock` trait on a type and initialize it on //! the first frame. The `since_init()` function should return a `Duration` //! representing time elapsed since the beginning of the first frame of the -//! program. For an example implementation using the Rust standard library, see -//! the `rust_timer` example. When running, make sure to modify the config -//! in `examples/include/lv_conf.h` (or your own) as above first. +//! program. +//! +//! ```no_run +//! use lvgl::timer::LvClock; +//! +//! struct Clock { +//! start: Instant, +//! } +//! +//! impl Default for Clock { +//! fn default() -> Self { +//! Self { +//! start: Instant::now(), +//! } +//! } +//! } +//! +//! impl LvClock for Clock { +//! fn since_init(&self) -> Duration { +//! Instant::now().duration_since(self.start) +//! } +//! } +//! +//! fn main() { +//! // Initialize displays, etc. +//! let clock = Clock::default(); +//! loop { +//! // Looping UI logic +//! lvgl::timer::update_clock(&clock).unwrap(); +//! } +//! } +//! ``` +//! +//! For a full example implementation similar to the above, see the +//! `rust_timer` example. When running, make sure to modify the config in +//! `examples/include/lv_conf.h` (or your own) as above first. use core::num::TryFromIntError; use core::time::Duration;