Remove threads from bar example
This commit is contained in:
parent
0761e36c76
commit
385fc6b764
1 changed files with 9 additions and 33 deletions
|
@ -8,7 +8,6 @@ use lvgl::style::Style;
|
|||
use lvgl::widgets::{Bar, Label, LabelAlign};
|
||||
use lvgl::{self, Align, Animation, Color, Event, LvError, Part, State, Widget, UI};
|
||||
use lvgl_sys;
|
||||
use std::sync::{mpsc, Arc, Mutex};
|
||||
use std::thread::sleep;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
|
@ -57,43 +56,18 @@ fn main() -> Result<(), LvError> {
|
|||
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
|
||||
loading_lbl.add_style(Part::Main, loading_style)?;
|
||||
|
||||
let threaded_ui = Arc::new(Mutex::new(ui));
|
||||
|
||||
let (stop_ch, read_ch) = mpsc::channel();
|
||||
let closure_ui = threaded_ui.clone();
|
||||
let mut loop_started = Instant::now();
|
||||
let tick_thr = std::thread::spawn(move || loop {
|
||||
// Needs to be called periodically for LittlevGL internal timing calculations.
|
||||
{
|
||||
let mut ui = closure_ui.lock().unwrap();
|
||||
ui.tick_inc(loop_started.elapsed());
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(5));
|
||||
if read_ch.try_recv().is_ok() {
|
||||
break;
|
||||
}
|
||||
loop_started = Instant::now();
|
||||
});
|
||||
|
||||
let mut i = 0;
|
||||
let mut loop_started = Instant::now();
|
||||
'running: loop {
|
||||
if i > 100 {
|
||||
i = 0;
|
||||
threaded_ui
|
||||
.lock()
|
||||
.unwrap()
|
||||
.event_send(&mut bar, Event::Clicked)?
|
||||
ui.event_send(&mut bar, Event::Clicked)?;
|
||||
}
|
||||
bar.set_value(i, Animation::ON)?;
|
||||
i += 1;
|
||||
|
||||
sleep(Duration::from_millis(50));
|
||||
|
||||
threaded_ui.lock().unwrap().task_handler();
|
||||
if let Some(disp) = threaded_ui.lock().unwrap().get_display_ref() {
|
||||
window.update(disp);
|
||||
}
|
||||
ui.task_handler();
|
||||
window.update(ui.get_display_ref().unwrap());
|
||||
|
||||
for event in window.events() {
|
||||
match event {
|
||||
|
@ -101,10 +75,12 @@ fn main() -> Result<(), LvError> {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stop_ch.send(true).unwrap();
|
||||
tick_thr.join().unwrap();
|
||||
sleep(Duration::from_millis(50));
|
||||
|
||||
ui.tick_inc(loop_started.elapsed());
|
||||
loop_started = Instant::now();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue