@@ -72,72 +72,53 @@ pub mod widgets;
72
72
#[ cfg( feature = "rust_timer" ) ]
73
73
pub mod timer;
74
74
75
- /*
76
- struct RunOnce(AtomicBool);
77
-
78
- impl RunOnce {
79
- const fn new() -> Self {
80
- Self(AtomicBool::new(false))
81
- }
82
-
83
- fn swap_and_check(&self) -> bool {
84
- self.0
85
- .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
86
- .is_ok()
87
- }
88
- }
89
- */
90
-
91
75
#[ cfg( feature = "unsafe_no_autoinit" ) ]
92
- static LVGL_INITIALIZED : RunOnce = RunOnce :: new ( ) ;
76
+ static mut IS_INIT : bool = false ;
77
+ #[ cfg( not( feature = "unsafe_no_autoinit" ) ) ]
78
+ static mut IS_INIT : bool = true ;
93
79
94
- /// Initializes LVGL. Call at the start of the program.
95
- # [ cfg ( feature = "unsafe_no_autoinit" ) ]
80
+ /// Initializes LVGL. Call at the start of the program, or after safely
81
+ /// deinitializing with `deinit()`.
96
82
pub fn init ( ) {
97
- if LVGL_INITIALIZED . swap_and_check ( ) {
98
- unsafe {
83
+ unsafe {
84
+ if ! IS_INIT {
99
85
lvgl_sys:: lv_init ( ) ;
86
+ IS_INIT = true ;
100
87
}
101
88
}
102
89
}
103
90
104
- #[ cfg( not( feature = "unsafe_no_autoinit" ) ) ]
105
- #[ ctor:: ctor]
106
- fn once_init ( ) {
91
+ /// Uninitializes LVGL. Make sure to reinitialize LVGL with `init()` before
92
+ /// accessing its functionality
93
+ ///
94
+ /// # Safety
95
+ ///
96
+ /// After calling, ensure existing LVGL-related values are not accessed even if
97
+ /// LVGL is reinitialized.
98
+ pub unsafe fn deinit ( ) {
107
99
unsafe {
108
- lvgl_sys:: lv_init ( ) ;
100
+ if IS_INIT {
101
+ lvgl_sys:: lv_deinit ( ) ;
102
+ IS_INIT = false ;
103
+ }
109
104
}
110
105
}
111
106
112
- /// Initializes LVGL.
113
- ///
114
- /// # Safety
115
- ///
116
- /// Unless `unsafe_no_autoinit` is enabled, do not call this function without
117
- /// first calling `deinit()` and dropping all old values.
118
107
#[ cfg( not( feature = "unsafe_no_autoinit" ) ) ]
119
- pub unsafe fn init ( ) {
108
+ #[ ctor:: ctor]
109
+ fn once_init ( ) {
120
110
unsafe {
121
111
lvgl_sys:: lv_init ( ) ;
122
112
}
123
113
}
124
114
125
- /// Uninitializes LVGL. Make sure to reinitialize it before reusing it.
126
- ///
127
- /// # Safety
128
- ///
129
- /// This function should not be called if LVGL is already uninitialized.
130
- pub unsafe fn deinit ( ) {
131
- unsafe { lvgl_sys:: lv_deinit ( ) }
132
- }
133
-
134
115
#[ cfg( test) ]
135
116
pub ( crate ) mod tests {
136
117
use crate :: display:: { Display , DrawBuffer } ;
137
118
138
119
pub ( crate ) fn initialize_test ( buf : bool ) {
139
120
unsafe { crate :: deinit ( ) } ;
140
- unsafe { crate :: init ( ) } ;
121
+ crate :: init ( ) ;
141
122
if buf {
142
123
const REFRESH_BUFFER_SIZE : usize = 240 * 240 / 10 ;
143
124
let buffer = DrawBuffer :: < REFRESH_BUFFER_SIZE > :: default ( ) ;
0 commit comments