lvgl-rs/examples/arc.rs

119 lines
3.4 KiB
Rust
Raw Normal View History

use cstr_core::CString;
2020-06-05 23:05:43 +00:00
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
2021-06-02 13:31:46 +00:00
use lvgl::display::Display;
2020-06-05 23:05:43 +00:00
use lvgl::style::Style;
use lvgl::widgets::{Arc, Label, LabelAlign};
2021-06-02 13:31:46 +00:00
use lvgl::{self, Align, Color, Part, State};
2020-06-07 18:29:35 +00:00
use lvgl::{LvError, Widget};
2020-06-05 23:05:43 +00:00
use lvgl_sys;
2021-06-02 13:31:46 +00:00
use parking_lot::Mutex;
use std::sync::Arc as SyncArc;
use std::thread;
use std::time::{Duration, Instant};
2020-06-05 23:05:43 +00:00
2021-05-25 23:03:07 +00:00
fn mem_info() -> lvgl_sys::lv_mem_monitor_t {
let mut info = lvgl_sys::lv_mem_monitor_t {
total_size: 0,
free_cnt: 0,
free_size: 0,
free_biggest_size: 0,
used_cnt: 0,
max_used: 0,
used_pct: 0,
frag_pct: 0,
};
unsafe {
lvgl_sys::lv_mem_monitor(&mut info as *mut _);
}
info
}
2020-06-07 18:29:35 +00:00
fn main() -> Result<(), LvError> {
2021-05-25 23:03:07 +00:00
println!("meminfo init: {:?}", mem_info());
run_arc_demo()?;
println!("meminfo end: {:?}", mem_info());
Ok(())
}
fn run_arc_demo() -> Result<(), LvError> {
2021-06-02 13:31:46 +00:00
lvgl::init();
let embedded_graphics_display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(
2020-06-05 23:05:43 +00:00
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("Arc Example", &output_settings);
2021-06-02 13:31:46 +00:00
let shared_native_display = SyncArc::new(Mutex::new(embedded_graphics_display));
let display = Display::register_shared(&shared_native_display)?;
2020-06-05 23:05:43 +00:00
2021-06-02 13:31:46 +00:00
let mut screen = display.get_str_act()?;
2020-06-05 23:05:43 +00:00
let mut screen_style = Style::default();
screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_radius(State::DEFAULT, 0);
2021-06-02 13:31:46 +00:00
screen.add_style(Part::Main, &mut screen_style)?;
2020-06-05 23:05:43 +00:00
// Create the arc object
2021-06-02 13:31:46 +00:00
let mut arc = Arc::new()?;
2020-06-07 18:29:35 +00:00
arc.set_size(150, 150)?;
arc.set_align(&mut screen, Align::Center, 0, 10)?;
2020-06-14 14:16:59 +00:00
arc.set_start_angle(135)?;
arc.set_end_angle(135)?;
2020-06-05 23:05:43 +00:00
2021-06-02 13:31:46 +00:00
let mut loading_lbl = Label::new()?;
loading_lbl.set_text(CString::new("Loading...").unwrap().as_c_str())?;
2020-06-07 18:29:35 +00:00
loading_lbl.set_align(&mut arc, Align::OutTopMid, 0, -10)?;
loading_lbl.set_label_align(LabelAlign::Center)?;
2020-06-05 23:05:43 +00:00
let mut loading_style = Style::default();
loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
2021-06-02 13:31:46 +00:00
loading_lbl.add_style(Part::Main, &mut loading_style)?;
2020-06-05 23:05:43 +00:00
let mut angle = 0;
let mut forward = true;
let mut i = 0;
2020-06-19 13:45:26 +00:00
2021-06-02 13:31:46 +00:00
// LVGL timer thread
thread::spawn(|| {
let interval = Duration::from_millis(5);
loop {
thread::sleep(interval);
lvgl::tick_inc(interval);
}
});
2020-06-05 23:05:43 +00:00
'running: loop {
if i > 270 {
forward = if forward { false } else { true };
i = 1;
2021-06-02 13:31:46 +00:00
println!("mem info running: {:?}", mem_info());
2020-06-05 23:05:43 +00:00
}
angle = if forward { angle + 1 } else { angle - 1 };
2020-06-14 14:16:59 +00:00
arc.set_end_angle(angle + 135)?;
2020-06-05 23:05:43 +00:00
i += 1;
2021-06-02 13:31:46 +00:00
lvgl::task_handler();
{
let eg_display = shared_native_display.lock();
window.update(&eg_display);
}
2020-06-05 23:05:43 +00:00
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
2021-06-02 13:31:46 +00:00
thread::sleep(Duration::from_millis(15));
2020-06-19 13:45:26 +00:00
}
2020-06-05 23:05:43 +00:00
Ok(())
}