Skip to content

Commit

Permalink
fixed rust clock
Browse files Browse the repository at this point in the history
  • Loading branch information
nia-e committed Apr 1, 2023
1 parent a14e413 commit c31619b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/include/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <rs_timer.h> /*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"
Expand Down
6 changes: 4 additions & 2 deletions lvgl-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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");

Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions lvgl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
46 changes: 37 additions & 9 deletions lvgl/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<rs_timer.h>`
//! in `lv_conf.h`, and enable the `rust_timer` feature on the `lvgl` crate to
//! enable this functionality.
//!
Expand All @@ -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;
Expand Down

0 comments on commit c31619b

Please sign in to comment.