Simplify examples, don't use threads

This commit is contained in:
Rafael Caricio 2020-06-19 15:45:26 +02:00 committed by Rafael Carício
parent 385fc6b764
commit f4618fd635
5 changed files with 30 additions and 129 deletions

View file

@ -9,9 +9,7 @@ use lvgl::widgets::{Arc, Label, LabelAlign};
use lvgl::{self, Align, Color, Part, State, UI};
use lvgl::{LvError, Widget};
use lvgl_sys;
use std::sync::{mpsc, Arc as StdArc, Mutex};
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::time::Instant;
fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
@ -51,29 +49,11 @@ 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 = StdArc::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 angle = 0;
let mut forward = true;
let mut i = 0;
let mut loop_started = Instant::now();
'running: loop {
if i > 270 {
forward = if forward { false } else { true };
@ -83,12 +63,8 @@ fn main() -> Result<(), LvError> {
arc.set_end_angle(angle + 135)?;
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 {
@ -96,10 +72,10 @@ fn main() -> Result<(), LvError> {
_ => {}
}
}
}
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(())
}

View file

@ -8,8 +8,7 @@ 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::thread::sleep;
use std::time::{Duration, Instant};
use std::time::Instant;
fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
@ -76,8 +75,6 @@ fn main() -> Result<(), LvError> {
}
}
sleep(Duration::from_millis(50));
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}

View file

@ -8,9 +8,7 @@ use lvgl::style::Style;
use lvgl::widgets::{Btn, Label};
use lvgl::{self, Align, 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};
use std::time::Instant;
fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
@ -56,30 +54,11 @@ fn main() -> Result<(), LvError> {
}
})?;
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();
});
'running: loop {
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 {
SimulatorEvent::MouseButtonUp {
@ -88,21 +67,16 @@ fn main() -> Result<(), LvError> {
} => {
println!("Clicked on: {:?}", point);
// Send a event to the button directly
threaded_ui
.lock()
.unwrap()
.event_send(&mut button, Event::Clicked)?;
ui.event_send(&mut button, Event::Clicked)?;
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
sleep(Duration::from_millis(5));
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
Ok(())
}

View file

@ -9,7 +9,6 @@ use lvgl::style::Style;
use lvgl::widgets::{Label, LabelAlign};
use lvgl::{Align, Color, LvError, Part, State, Widget, UI};
use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep;
use std::time::{Duration, Instant};
@ -75,26 +74,8 @@ fn main() -> Result<(), LvError> {
power.set_label_align(LabelAlign::Right)?;
power.set_align(&mut screen, Align::InTopRight, 0, 0)?;
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 > 59 {
i = 0;
@ -103,12 +84,8 @@ fn main() -> Result<(), LvError> {
time.set_text(CString::new(val.as_str()).unwrap().as_c_str())?;
i = 1 + i;
sleep(Duration::from_secs(1));
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 {
@ -116,10 +93,11 @@ fn main() -> Result<(), LvError> {
_ => {}
}
}
}
sleep(Duration::from_secs(1));
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(())
}

View file

@ -7,9 +7,7 @@ use lvgl::style::{Opacity, Style};
use lvgl::widgets::Gauge;
use lvgl::{self, Align, Color, LvError, Part, State, Widget, UI};
use lvgl_sys;
use std::sync::{mpsc, Arc, Mutex};
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::time::Instant;
fn main() -> Result<(), LvError> {
let display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
@ -56,33 +54,13 @@ fn main() -> Result<(), LvError> {
gauge.set_align(&mut screen, Align::Center, 0, 0)?;
gauge.set_value(0, 50)?;
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 {
gauge.set_value(0, i)?;
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 {
@ -97,17 +75,15 @@ fn main() -> Result<(), LvError> {
}
}
sleep(Duration::from_millis(15));
if i > 99 {
i = 0;
} else {
i = i + 1;
}
}
stop_ch.send(true).unwrap();
tick_thr.join().unwrap();
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(())
}