lvgl-rs/examples/gauge.rs

87 lines
2.8 KiB
Rust
Raw Permalink Normal View History

2020-06-04 21:09:54 +00:00
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl::style::{Opacity, Style};
use lvgl::widgets::Gauge;
use lvgl::{self, Align, Color, LvError, Part, State, Widget, UI};
2020-06-19 13:45:26 +00:00
use std::time::Instant;
2020-06-04 21:09:54 +00:00
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));
2020-06-04 21:09:54 +00:00
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Gauge Example", &output_settings);
2020-06-07 18:47:49 +00:00
let mut ui = UI::init()?;
2020-06-04 21:09:54 +00:00
// Implement and register your display:
ui.disp_drv_register(display)?;
2020-06-04 21:09:54 +00:00
// Create screen and widgets
2020-06-07 18:47:49 +00:00
let mut screen = ui.scr_act()?;
2020-06-04 21:09:54 +00:00
let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
2020-06-07 18:47:49 +00:00
screen.add_style(Part::Main, screen_style)?;
2020-06-04 21:09:54 +00:00
// Create the gauge
let mut gauge_style = Style::default();
// Set a background color and a radius
gauge_style.set_radius(State::DEFAULT, 5);
gauge_style.set_bg_opa(State::DEFAULT, Opacity::OPA_COVER);
gauge_style.set_bg_color(State::DEFAULT, Color::from_rgb((192, 192, 192)));
2020-06-19 17:43:14 +00:00
// Set some padding's
2020-06-04 21:09:54 +00:00
gauge_style.set_pad_inner(State::DEFAULT, 20);
gauge_style.set_pad_top(State::DEFAULT, 20);
gauge_style.set_pad_left(State::DEFAULT, 5);
gauge_style.set_pad_right(State::DEFAULT, 5);
gauge_style.set_scale_end_color(State::DEFAULT, Color::from_rgb((255, 0, 0)));
gauge_style.set_line_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
gauge_style.set_scale_grad_color(State::DEFAULT, Color::from_rgb((0, 0, 255)));
gauge_style.set_line_width(State::DEFAULT, 2);
gauge_style.set_scale_end_line_width(State::DEFAULT, 4);
gauge_style.set_scale_end_border_width(State::DEFAULT, 4);
2020-06-07 18:47:49 +00:00
let mut gauge = Gauge::new(&mut screen)?;
2020-06-14 14:16:59 +00:00
gauge.add_style(Part::Main, gauge_style)?;
2020-06-07 18:47:49 +00:00
gauge.set_align(&mut screen, Align::Center, 0, 0)?;
gauge.set_value(0, 50)?;
2020-06-04 21:09:54 +00:00
let mut i = 0;
2020-06-19 13:45:26 +00:00
let mut loop_started = Instant::now();
2020-06-04 21:09:54 +00:00
'running: loop {
gauge.set_value(0, i)?;
2020-06-19 13:45:26 +00:00
ui.task_handler();
window.update(ui.get_display_ref().unwrap());
2020-06-04 21:09:54 +00:00
for event in window.events() {
match event {
SimulatorEvent::MouseButtonUp {
mouse_btn: _,
point,
} => {
println!("Clicked on: {:?}", point);
}
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
if i > 99 {
i = 0;
} else {
i = i + 1;
}
2020-06-19 13:45:26 +00:00
ui.tick_inc(loop_started.elapsed());
loop_started = Instant::now();
}
2020-06-04 21:09:54 +00:00
Ok(())
}