lvgl-rs/examples/bar.rs

81 lines
2.5 KiB
Rust
Raw Permalink Normal View History

2020-06-16 21:19:26 +00:00
use cstr_core::CString;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
2020-06-04 18:14:43 +00:00
use lvgl::style::Style;
2020-06-16 21:19:26 +00:00
use lvgl::widgets::{Bar, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, Event, LvError, Part, State, Widget, UI};
2020-06-19 13:45:26 +00:00
use std::time::Instant;
2020-06-07 18:47:49 +00:00
fn main() -> Result<(), LvError> {
2020-06-19 17:43:14 +00:00
let display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(lvgl::HOR_RES_MAX, lvgl::VER_RES_MAX));
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Bar Example", &output_settings);
2020-06-07 18:47:49 +00:00
let mut ui = UI::init()?;
// Implement and register your display:
2020-06-16 21:19:26 +00:00
ui.disp_drv_register(display).unwrap();
// Create screen and widgets
2020-06-07 18:47:49 +00:00
let mut screen = ui.scr_act()?;
2020-06-02 17:59:41 +00:00
let mut screen_style = Style::default();
2020-06-04 17:26:51 +00:00
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_radius(State::DEFAULT, 0);
2020-06-07 18:47:49 +00:00
screen.add_style(Part::Main, screen_style)?;
// Create the bar object
2020-06-07 18:47:49 +00:00
let mut bar = Bar::new(&mut screen)?;
bar.set_size(175, 20)?;
bar.set_align(&mut screen, Align::Center, 0, 10)?;
bar.set_range(0, 100)?;
2020-06-16 21:19:26 +00:00
bar.on_event(|_b, _e| {
2020-06-19 12:14:46 +00:00
println!("Completed!");
2020-06-16 21:19:26 +00:00
})?;
2020-06-04 17:26:51 +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-06-04 17:26:51 +00:00
ind_style.set_bg_color(State::DEFAULT, Color::from_rgb((100, 245, 100)));
2020-06-14 14:16:59 +00:00
bar.add_style(Part::All, ind_style)?;
2020-06-07 18:47:49 +00:00
let mut loading_lbl = Label::new(&mut screen)?;
2020-06-16 21:19:26 +00:00
loading_lbl.set_text(CString::new("Loading...").unwrap().as_c_str())?;
2020-06-07 18:47:49 +00:00
loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10)?;
loading_lbl.set_label_align(LabelAlign::Center)?;
2020-06-02 17:59:41 +00:00
let mut loading_style = Style::default();
2020-06-04 17:26:51 +00:00
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
2020-06-07 18:47:49 +00:00
loading_lbl.add_style(Part::Main, loading_style)?;
2020-05-31 07:30:26 +00:00
let mut i = 0;
2020-06-19 13:32:58 +00:00
let mut loop_started = Instant::now();
'running: loop {
if i > 100 {
i = 0;
2020-06-19 13:32:58 +00:00
ui.event_send(&mut bar, Event::Clicked)?;
}
2020-06-19 12:14:46 +00:00
bar.set_value(i, Animation::ON)?;
i += 1;
2020-06-19 13:32:58 +00:00
ui.task_handler();
window.update(ui.get_display_ref().unwrap());
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
2020-06-19 13:32:58 +00:00
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
Ok(())
}