From 18a7ebffa95e7a6e9ff8a6719069b9010a94ca88 Mon Sep 17 00:00:00 2001 From: Nia Espera Date: Wed, 21 Jun 2023 02:23:40 +0200 Subject: [PATCH 1/2] safely manage init --- lvgl/src/lib.rs | 65 +++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/lvgl/src/lib.rs b/lvgl/src/lib.rs index 148cbd05..8a0638fe 100644 --- a/lvgl/src/lib.rs +++ b/lvgl/src/lib.rs @@ -72,72 +72,53 @@ pub mod widgets; #[cfg(feature = "rust_timer")] pub mod timer; -/* -struct RunOnce(AtomicBool); - -impl RunOnce { - const fn new() -> Self { - Self(AtomicBool::new(false)) - } - - fn swap_and_check(&self) -> bool { - self.0 - .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) - .is_ok() - } -} -*/ - #[cfg(feature = "unsafe_no_autoinit")] -static LVGL_INITIALIZED: RunOnce = RunOnce::new(); +static mut IS_INIT: bool = false; +#[cfg(not(feature = "unsafe_no_autoinit"))] +static mut IS_INIT: bool = true; -/// Initializes LVGL. Call at the start of the program. -#[cfg(feature = "unsafe_no_autoinit")] +/// Initializes LVGL. Call at the start of the program, or after safely +/// deinitializing with `deinit()`. pub fn init() { - if LVGL_INITIALIZED.swap_and_check() { - unsafe { + unsafe { + if !IS_INIT { lvgl_sys::lv_init(); + IS_INIT = true; } } } -#[cfg(not(feature = "unsafe_no_autoinit"))] -#[ctor::ctor] -fn once_init() { +/// Uninitializes LVGL. Make sure to reinitialize LVGL with `init()` before +/// accessing its functionality +/// +/// # Safety +/// +/// After calling, ensure existing LVGL-related values are not accessed even if +/// LVGL is reinitialized. +pub unsafe fn deinit() { unsafe { - lvgl_sys::lv_init(); + if IS_INIT { + lvgl_sys::lv_deinit(); + IS_INIT = false; + } } } -/// Initializes LVGL. -/// -/// # Safety -/// -/// Unless `unsafe_no_autoinit` is enabled, do not call this function without -/// first calling `deinit()` and dropping all old values. #[cfg(not(feature = "unsafe_no_autoinit"))] -pub unsafe fn init() { +#[ctor::ctor] +fn once_init() { unsafe { lvgl_sys::lv_init(); } } -/// Uninitializes LVGL. Make sure to reinitialize it before reusing it. -/// -/// # Safety -/// -/// This function should not be called if LVGL is already uninitialized. -pub unsafe fn deinit() { - unsafe { lvgl_sys::lv_deinit() } -} - #[cfg(test)] pub(crate) mod tests { use crate::display::{Display, DrawBuffer}; pub(crate) fn initialize_test(buf: bool) { unsafe { crate::deinit() }; - unsafe { crate::init() }; + crate::init(); if buf { const REFRESH_BUFFER_SIZE: usize = 240 * 240 / 10; let buffer = DrawBuffer::::default(); From 2d47307506820d8494025000738bb34c657a5b30 Mon Sep 17 00:00:00 2001 From: Nia Espera Date: Wed, 21 Jun 2023 02:27:11 +0200 Subject: [PATCH 2/2] simplify test closures --- lvgl/src/input_device/encoder.rs | 2 +- lvgl/src/input_device/pointer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lvgl/src/input_device/encoder.rs b/lvgl/src/input_device/encoder.rs index 56e89e54..97700f14 100644 --- a/lvgl/src/input_device/encoder.rs +++ b/lvgl/src/input_device/encoder.rs @@ -221,6 +221,6 @@ mod test { EncoderInputData::Press.pressed().once() } - let _encoder = Encoder::register(|| read_encoder_device(), &display).unwrap(); + let _encoder = Encoder::register(read_encoder_device, &display).unwrap(); } } diff --git a/lvgl/src/input_device/pointer.rs b/lvgl/src/input_device/pointer.rs index e1925c15..97905ea3 100644 --- a/lvgl/src/input_device/pointer.rs +++ b/lvgl/src/input_device/pointer.rs @@ -228,6 +228,6 @@ mod test { .once() } - let _touch_screen = Pointer::register(|| read_touchpad_device(), &display).unwrap(); + let _touch_screen = Pointer::register(read_touchpad_device, &display).unwrap(); } }