Remove threads from bar example

This commit is contained in:
Rafael Caricio 2020-06-19 15:32:58 +02:00 committed by Rafael Carício
parent 0761e36c76
commit 385fc6b764

View file

@ -8,7 +8,6 @@ use lvgl::style::Style;
use lvgl::widgets::{Bar, Label, LabelAlign}; use lvgl::widgets::{Bar, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, Event, LvError, Part, State, Widget, UI}; use lvgl::{self, Align, Animation, Color, Event, LvError, Part, State, Widget, UI};
use lvgl_sys; use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep; use std::thread::sleep;
use std::time::{Duration, Instant}; 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_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
loading_lbl.add_style(Part::Main, loading_style)?; 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 i = 0;
let mut loop_started = Instant::now();
'running: loop { 'running: loop {
if i > 100 { if i > 100 {
i = 0; i = 0;
threaded_ui ui.event_send(&mut bar, Event::Clicked)?;
.lock()
.unwrap()
.event_send(&mut bar, Event::Clicked)?
} }
bar.set_value(i, Animation::ON)?; bar.set_value(i, Animation::ON)?;
i += 1; i += 1;
sleep(Duration::from_millis(50)); ui.task_handler();
window.update(ui.get_display_ref().unwrap());
threaded_ui.lock().unwrap().task_handler();
if let Some(disp) = threaded_ui.lock().unwrap().get_display_ref() {
window.update(disp);
}
for event in window.events() { for event in window.events() {
match event { match event {
@ -101,10 +75,12 @@ fn main() -> Result<(), LvError> {
_ => {} _ => {}
} }
} }
}
stop_ch.send(true).unwrap(); sleep(Duration::from_millis(50));
tick_thr.join().unwrap();
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(()) Ok(())
} }