-
Notifications
You must be signed in to change notification settings - Fork 74
/
rust_timer.rs
104 lines (86 loc) · 2.95 KB
/
rust_timer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use cstr_core::CString;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use lvgl;
use lvgl::style::Style;
use lvgl::widgets::{Bar, Label};
use lvgl::{Align, AnimationState, Color, Display, DrawBuffer, Event, LvError, Part, Widget};
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;
// Example clock, here just reusing standard library features
use lvgl::timer::LvClock;
struct Clock {
start: Instant,
}
impl Default for Clock {
fn default() -> Self {
Self {
start: Instant::now(),
}
}
}
impl LvClock for Clock {
fn since_init(&self) -> Duration {
Instant::now().duration_since(self.start)
}
}
fn main() -> Result<(), LvError> {
const HOR_RES: u32 = 240;
const VER_RES: u32 = 240;
let mut sim_display: SimulatorDisplay<Rgb565> =
SimulatorDisplay::new(Size::new(HOR_RES, VER_RES));
let output_settings = OutputSettingsBuilder::new().scale(2).build();
let mut window = Window::new("Bar Example", &output_settings);
let buffer = DrawBuffer::<{ (HOR_RES * VER_RES) as usize }>::default();
let display = Display::register(buffer, HOR_RES, VER_RES, |refresh| {
sim_display.draw_iter(refresh.as_pixels()).unwrap();
})?;
let mut screen = display.get_scr_act()?;
let mut screen_style = Style::default();
screen_style.set_bg_color(Color::from_rgb((255, 255, 255)));
screen_style.set_radius(0);
screen.add_style(Part::Main, &mut screen_style)?;
// Create the bar object
let mut bar = Bar::create(&mut screen)?;
bar.set_size(175, 20)?;
bar.set_align(Align::Center, 0, 10)?;
bar.set_range(0, 100)?;
bar.on_event(|_b, _e| {
println!("Completed!");
})?;
// Set the indicator style for the bar object
let mut ind_style = Style::default();
ind_style.set_bg_color(Color::from_rgb((100, 245, 100)));
bar.add_style(Part::Any, &mut ind_style)?;
let mut loading_lbl = Label::create(&mut screen)?;
loading_lbl.set_text(CString::new("Loading...").unwrap().as_c_str())?;
loading_lbl.set_align(Align::OutTopMid, 0, 0)?;
let mut loading_style = Style::default();
loading_style.set_text_color(Color::from_rgb((0, 0, 0)));
loading_lbl.add_style(Part::Main, &mut loading_style)?;
let mut i = 0;
let clock = Clock::default();
'running: loop {
if i > 100 {
i = 0;
lvgl::event_send(&mut bar, Event::Clicked)?;
}
bar.set_value(i, AnimationState::ON)?;
i += 1;
lvgl::task_handler();
window.update(&mut sim_display);
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
_ => {}
}
}
sleep(Duration::from_millis(15));
lvgl::timer::update_clock(&clock).unwrap();
}
Ok(())
}