diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 3cd3713..0fdf9f0 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -26,3 +26,7 @@ path = "button_click.rs" [[example]] name = "gauge" path = "gauge.rs" + +[[example]] +name = "arc" +path = "arc.rs" diff --git a/examples/arc.rs b/examples/arc.rs new file mode 100644 index 0000000..eecb056 --- /dev/null +++ b/examples/arc.rs @@ -0,0 +1,103 @@ +use embedded_graphics::pixelcolor::Rgb565; +use embedded_graphics::prelude::*; +use embedded_graphics_simulator::{ + OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, +}; +use lvgl::style::Style; +use lvgl::widgets::{Arc, ArcPart, Label, LabelAlign}; +use lvgl::Widget; +use lvgl::{self, Align, Color, DisplayDriver, Event, Part, State, UI}; +use lvgl_sys; +use std::sync::{mpsc, Arc as StdArc, Mutex}; +use std::thread::sleep; +use std::time::Duration; + +fn main() -> Result<(), String> { + let mut display: SimulatorDisplay = 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("Arc 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(); + + 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); + screen.add_style(Part::Main, screen_style); + + // Create the arc object + let mut arc = Arc::new(&mut screen); + arc.set_size(150, 150); + arc.set_align(&mut screen, Align::Center, 0, 10); + arc.set_start_angle(135, ArcPart::Indicator); + arc.set_end_angle(135, ArcPart::Indicator); + + let mut loading_lbl = Label::new(&mut screen); + loading_lbl.set_text("Loading..."); + loading_lbl.set_align(&mut arc, Align::OutTopMid, 0, -10); + loading_lbl.set_label_align(LabelAlign::Center); + + let mut loading_style = Style::default(); + loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0))); + loading_lbl.add_style(Part::Main, loading_style); + + let threaded_ui = StdArc::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(10); + closure_ui.lock().unwrap().tick_inc(period); + + sleep(period); + if read_ch.try_recv().is_ok() { + break; + } + }); + + let mut angle = 0; + let mut forward = true; + + let mut i = 0; + 'running: loop { + if i > 270 { + forward = if forward { false } else { true }; + i = 1; + threaded_ui + .lock() + .unwrap() + .event_send(&mut loading_lbl, Event::Clicked) + } + angle = if forward { angle + 1 } else { angle - 1 }; + arc.set_end_angle(angle + 135, ArcPart::Indicator); + i += 1; + + sleep(Duration::from_millis(10)); + + 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(()) +} diff --git a/lvgl/src/widgets/arc.rs b/lvgl/src/widgets/arc.rs new file mode 100644 index 0000000..0ccf966 --- /dev/null +++ b/lvgl/src/widgets/arc.rs @@ -0,0 +1,56 @@ +use crate::NativeObject; + +define_object!(Arc, lv_arc_create, part = ArcPart); + +impl Arc { + /// Set the start angle, for the given arc part. + /// 0 degrees for the right, 90 degrees for the bottom, etc. + pub fn set_start_angle(&mut self, angle: u16, part: ArcPart) { + match part { + ArcPart::Background => unsafe { + lvgl_sys::lv_arc_set_bg_start_angle(self.core.raw().as_mut(), angle) + }, + ArcPart::Indicator => unsafe { + lvgl_sys::lv_arc_set_start_angle(self.core.raw().as_mut(), angle) + }, + } + } + + /// Set the end angle, for the given arc part. + /// 0 degrees for the right, 90 degrees for the bottom, etc. + pub fn set_end_angle(&mut self, angle: u16, part: ArcPart) { + match part { + ArcPart::Background => unsafe { + lvgl_sys::lv_arc_set_bg_start_angle(self.core.raw().as_mut(), angle) + }, + ArcPart::Indicator => unsafe { + lvgl_sys::lv_arc_set_end_angle(self.core.raw().as_mut(), angle) + }, + } + } + + /// Rotate the arc, `angle` degrees clockwise. + pub fn set_rotation(&mut self, angle: u16) { + unsafe { + lvgl_sys::lv_arc_set_rotation(self.core.raw().as_mut(), angle); + } + } +} + +/// The different parts, of an arc object. +pub enum ArcPart { + /// The background of the arc. + Background, + /// The indicator of the arc. + /// This is what moves/changes, depending on the arc's value. + Indicator, +} + +impl From for u8 { + fn from(component: ArcPart) -> Self { + match component { + ArcPart::Background => lvgl_sys::LV_ARC_PART_BG as u8, + ArcPart::Indicator => lvgl_sys::LV_ARC_PART_INDIC as u8, + } + } +} diff --git a/lvgl/src/widgets/mod.rs b/lvgl/src/widgets/mod.rs index e0b4182..153cbb5 100644 --- a/lvgl/src/widgets/mod.rs +++ b/lvgl/src/widgets/mod.rs @@ -1,9 +1,11 @@ +mod arc; mod bar; mod btn; mod gauge; mod label; -pub use self::bar::*; -pub use self::btn::*; -pub use self::gauge::*; -pub use self::label::*; +pub use arc::*; +pub use bar::*; +pub use btn::*; +pub use gauge::*; +pub use label::*;