2020-05-31 07:12:37 +00:00
|
|
|
use embedded_graphics::pixelcolor::Rgb565;
|
|
|
|
use embedded_graphics::prelude::*;
|
|
|
|
use embedded_graphics_simulator::{
|
|
|
|
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
|
|
|
};
|
2020-06-01 20:45:33 +00:00
|
|
|
use lvgl::widgets::{Bar, BarComponent, Label, LabelAlign};
|
|
|
|
use lvgl::{self, Align, Animation, Border, Color, DisplayDriver, Event, Object, Style, UI};
|
2020-05-31 07:12:37 +00:00
|
|
|
use lvgl_sys;
|
|
|
|
use std::sync::{mpsc, Arc, Mutex};
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
fn main() -> Result<(), String> {
|
|
|
|
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
|
|
|
|
lvgl_sys::LV_HOR_RES_MAX,
|
|
|
|
lvgl_sys::LV_VER_RES_MAX,
|
|
|
|
));
|
|
|
|
|
|
|
|
let output_settings = OutputSettingsBuilder::new().scale(2).build();
|
|
|
|
let mut window = Window::new("Bar Example", &output_settings);
|
|
|
|
|
|
|
|
let mut ui = UI::init().unwrap();
|
|
|
|
|
|
|
|
// Implement and register your display:
|
|
|
|
let display_driver = DisplayDriver::new(&mut display);
|
|
|
|
ui.disp_drv_register(display_driver);
|
|
|
|
|
|
|
|
// Create screen and widgets
|
|
|
|
let mut screen = ui.scr_act();
|
|
|
|
|
2020-06-02 17:59:41 +00:00
|
|
|
let mut screen_style = Style::default();
|
2020-06-01 20:45:33 +00:00
|
|
|
screen_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
|
|
|
|
screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
|
2020-05-31 07:30:26 +00:00
|
|
|
screen_style.set_body_radius(0);
|
2020-05-31 07:12:37 +00:00
|
|
|
screen.set_style(screen_style);
|
|
|
|
|
|
|
|
// Create the bar object
|
|
|
|
let mut bar = Bar::new(&mut screen);
|
2020-05-31 07:30:26 +00:00
|
|
|
bar.set_size(175, 50);
|
2020-06-01 20:45:33 +00:00
|
|
|
bar.set_align(&mut screen, Align::Center, 0, 20);
|
2020-05-31 07:12:37 +00:00
|
|
|
bar.set_range(0, 100);
|
2020-05-31 10:07:24 +00:00
|
|
|
bar.set_value(0, Animation::OFF);
|
2020-05-31 07:12:37 +00:00
|
|
|
|
2020-05-31 18:52:05 +00:00
|
|
|
// Set the indicator style for the bar object
|
2020-06-02 17:59:41 +00:00
|
|
|
let mut ind_style = Style::default();
|
2020-05-31 18:52:05 +00:00
|
|
|
ind_style.set_body_main_color(Color::from_rgb((100, 245, 0)));
|
|
|
|
ind_style.set_body_grad_color(Color::from_rgb((100, 245, 0)));
|
|
|
|
bar.set_bar_style(BarComponent::Indicator, ind_style);
|
|
|
|
|
|
|
|
// Set the background style for the bar object
|
2020-06-02 17:59:41 +00:00
|
|
|
let mut bg_style = Style::default();
|
2020-05-31 18:52:05 +00:00
|
|
|
bg_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
|
|
|
|
bg_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
|
|
|
|
bg_style.set_body_radius(0);
|
2020-06-01 20:45:33 +00:00
|
|
|
bg_style.set_body_border_part(Border::TOP);
|
|
|
|
bg_style.set_body_border_width(1);
|
2020-05-31 18:52:05 +00:00
|
|
|
bar.set_bar_style(BarComponent::Background, bg_style);
|
|
|
|
|
2020-05-31 07:30:26 +00:00
|
|
|
let mut loading_lbl = Label::new(&mut screen);
|
|
|
|
loading_lbl.set_text("Loading...");
|
|
|
|
loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10);
|
2020-06-01 20:45:33 +00:00
|
|
|
loading_lbl.set_label_align(LabelAlign::Center);
|
2020-05-31 16:48:17 +00:00
|
|
|
|
2020-06-02 17:59:41 +00:00
|
|
|
let mut loading_style = Style::default();
|
2020-06-01 20:45:33 +00:00
|
|
|
loading_style.set_text_color(Color::from_rgb((0, 0, 0)));
|
2020-05-31 07:30:26 +00:00
|
|
|
loading_lbl.set_style(loading_style);
|
|
|
|
|
2020-05-31 07:12:37 +00:00
|
|
|
let threaded_ui = Arc::new(Mutex::new(ui));
|
|
|
|
|
|
|
|
let (stop_ch, read_ch) = mpsc::channel();
|
|
|
|
let closure_ui = threaded_ui.clone();
|
|
|
|
let tick_thr = std::thread::spawn(move || loop {
|
|
|
|
let period = Duration::from_millis(5);
|
|
|
|
closure_ui.lock().unwrap().tick_inc(period);
|
|
|
|
|
|
|
|
sleep(period);
|
|
|
|
if read_ch.try_recv().is_ok() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
'running: loop {
|
|
|
|
if i > 100 {
|
|
|
|
i = 0;
|
2020-05-31 17:24:45 +00:00
|
|
|
threaded_ui
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.event_send(&mut loading_lbl, Event::Clicked)
|
2020-05-31 07:12:37 +00:00
|
|
|
}
|
2020-05-31 10:07:24 +00:00
|
|
|
bar.set_value(i, Animation::OFF);
|
2020-05-31 07:12:37 +00:00
|
|
|
i += 1;
|
|
|
|
|
|
|
|
sleep(Duration::from_millis(25));
|
|
|
|
|
|
|
|
threaded_ui.lock().unwrap().task_handler();
|
|
|
|
|
|
|
|
window.update(&display);
|
|
|
|
|
|
|
|
for event in window.events() {
|
|
|
|
match event {
|
|
|
|
SimulatorEvent::Quit => break 'running,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop_ch.send(true).unwrap();
|
|
|
|
tick_thr.join().unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|