From 25efb6da912fb82ef6af3efeb641bab46fee7a8a Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 5 Jun 2020 23:22:21 +0200 Subject: [PATCH] Simplify object creation --- README.md | 4 ++-- examples/bar.rs | 2 +- examples/button_click.rs | 2 +- examples/demo.rs | 2 +- examples/gauge.rs | 2 +- lvgl/src/global.rs | 8 ++++---- lvgl/src/lv_core/mod.rs | 4 ++-- lvgl/src/lv_core/{object.rs => obj.rs} | 27 +++++++++++++------------- lvgl/src/support.rs | 4 ++-- lvgl/src/widgets/bar.rs | 3 +-- lvgl/src/widgets/btn.rs | 3 --- lvgl/src/widgets/gauge.rs | 3 +-- lvgl/src/widgets/label.rs | 3 +-- 13 files changed, 31 insertions(+), 36 deletions(-) rename lvgl/src/lv_core/{object.rs => obj.rs} (91%) diff --git a/README.md b/README.md index 2a77455..f089a96 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ List of LVGL features that impacts the library usage in general. - [ ] Container (lv_cont) - [ ] Color picker (lv_cpicker) - [ ] Drop-down list (lv_dropdown) -- [ ] Gauge (lv_gauge) +- [x] Gauge (lv_gauge) - [ ] Image (lv_img) - [ ] Image button (lv_imgbtn) - [ ] Keyboard (lv_keyboard) @@ -123,7 +123,7 @@ List of LVGL features that impacts the library usage in general. - [ ] Tabview (lv_tabview) - [ ] Text area (lv_textarea) - [ ] Tile view (lv_tileview) -- [ ] Window (lv_wn- [ ] +- [ ] Window (lv_win) Widgets currently implemented might have some missing features. If the widget you want to use is not exposed or is missing a feature you want to make use, please send a Pull Request or open an issue. diff --git a/examples/bar.rs b/examples/bar.rs index eff4aa7..356fcfb 100644 --- a/examples/bar.rs +++ b/examples/bar.rs @@ -5,7 +5,7 @@ use embedded_graphics_simulator::{ }; use lvgl::style::Style; use lvgl::widgets::{Bar, BarPart, Label, LabelAlign}; -use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, Object, Part, State, UI}; +use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, Part, State, Widget, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; diff --git a/examples/button_click.rs b/examples/button_click.rs index 8db3074..687f997 100644 --- a/examples/button_click.rs +++ b/examples/button_click.rs @@ -5,7 +5,7 @@ use embedded_graphics_simulator::{ }; use lvgl::style::Style; use lvgl::widgets::{Btn, Label}; -use lvgl::{self, Align, Color, DisplayDriver, Event, Object, Part, State, UI}; +use lvgl::{self, Align, Color, DisplayDriver, Event, Part, State, Widget, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; diff --git a/examples/demo.rs b/examples/demo.rs index 56335d8..f56d20f 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -6,7 +6,7 @@ use embedded_graphics_simulator::{ use lvgl; use lvgl::style::Style; use lvgl::widgets::{Label, LabelAlign}; -use lvgl::{Align, Color, Object, Part, State, UI}; +use lvgl::{Align, Color, Part, State, Widget, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; diff --git a/examples/gauge.rs b/examples/gauge.rs index 76c3d2e..5e30fca 100644 --- a/examples/gauge.rs +++ b/examples/gauge.rs @@ -5,7 +5,7 @@ use embedded_graphics_simulator::{ }; use lvgl::style::{Opacity, Style}; use lvgl::widgets::{Gauge, GaugePart}; -use lvgl::{self, Align, Color, DisplayDriver, Object, Part, State, UI}; +use lvgl::{self, Align, Color, DisplayDriver, Part, State, Widget, UI}; use lvgl_sys; use std::sync::{mpsc, Arc, Mutex}; use std::thread::sleep; diff --git a/lvgl/src/global.rs b/lvgl/src/global.rs index cf8454a..04e8591 100644 --- a/lvgl/src/global.rs +++ b/lvgl/src/global.rs @@ -1,4 +1,4 @@ -use crate::{DisplayDriver, Event, GenericObject, Object}; +use crate::{DisplayDriver, Event, Obj, Widget}; use alloc::boxed::Box; use core::marker::PhantomData; use core::ptr; @@ -47,16 +47,16 @@ impl UI { } } - pub fn scr_act(&self) -> GenericObject { + pub fn scr_act(&self) -> Obj { unsafe { let screen = lvgl_sys::lv_disp_get_scr_act(ptr::null_mut()); - GenericObject::from_raw(NonNull::new_unchecked(screen)) + Obj::from_raw(NonNull::new_unchecked(screen)) } } pub fn event_send(&mut self, obj: &mut T, event: Event) where - T: Object, + T: Widget, { unsafe { lvgl_sys::lv_event_send(obj.raw().as_mut(), event.into(), ptr::null_mut()); diff --git a/lvgl/src/lv_core/mod.rs b/lvgl/src/lv_core/mod.rs index b509c13..b1103a7 100644 --- a/lvgl/src/lv_core/mod.rs +++ b/lvgl/src/lv_core/mod.rs @@ -1,5 +1,5 @@ #[macro_use] -mod object; +mod obj; pub mod style; -pub use object::*; +pub use obj::*; diff --git a/lvgl/src/lv_core/object.rs b/lvgl/src/lv_core/obj.rs similarity index 91% rename from lvgl/src/lv_core/object.rs rename to lvgl/src/lv_core/obj.rs index 4617f97..bcc10ac 100644 --- a/lvgl/src/lv_core/object.rs +++ b/lvgl/src/lv_core/obj.rs @@ -14,20 +14,20 @@ pub trait NativeObject { /// Generic LVGL object. /// /// This is the parent object of all widget types. It stores the native LVGL raw pointer. -pub struct GenericObject { +pub struct Obj { // We use a raw pointer here because we do not control this memory address, it is controlled // by LVGL's global state. raw: *mut lvgl_sys::lv_obj_t, } -impl NativeObject for GenericObject { +impl NativeObject for Obj { fn raw(&self) -> ptr::NonNull { ptr::NonNull::new(self.raw).expect(PANIC_MESSAGE) } } /// A wrapper for all LittlevGL common operations on generic objects. -pub trait Object: NativeObject { +pub trait Widget: NativeObject { type SpecialEvent; type Part: Into; @@ -92,7 +92,7 @@ pub trait Object: NativeObject { } } -impl Object for GenericObject { +impl Widget for Obj { type SpecialEvent = (); type Part = Part; @@ -101,7 +101,7 @@ impl Object for GenericObject { } } -impl Default for GenericObject { +impl Default for Obj { fn default() -> Self { Self { raw: unsafe { lvgl_sys::lv_obj_create(ptr::null_mut(), ptr::null_mut()) }, @@ -124,26 +124,27 @@ macro_rules! define_object { }; ($item:ident, $create_fn:ident, event = $event_type:ty, part = $part_type:ty) => { pub struct $item { - core: $crate::GenericObject, + core: $crate::Obj, } impl $item { pub fn new(parent: &mut C) -> Self where - C: NativeObject, + C: $crate::NativeObject, { unsafe { - let ptr = lvgl_sys::$create_fn(parent.raw().as_mut(), ptr::null_mut()); - let raw = ptr::NonNull::new_unchecked(ptr); - let core = GenericObject::from_raw(raw); + let ptr = lvgl_sys::$create_fn(parent.raw().as_mut(), core::ptr::null_mut()); + let raw = core::ptr::NonNull::new_unchecked(ptr); + let core = <$crate::Obj as $crate::Widget>::from_raw(raw); Self { core } } } pub fn on_event(&mut self, f: F) where - F: FnMut(Self, $crate::support::Event<::SpecialEvent>), + F: FnMut(Self, $crate::support::Event<::SpecialEvent>), { + use $crate::NativeObject; unsafe { let mut raw = self.raw(); let obj = raw.as_mut(); @@ -163,13 +164,13 @@ macro_rules! define_object { } } - impl $crate::Object for $item { + impl $crate::Widget for $item { type SpecialEvent = $event_type; type Part = $part_type; unsafe fn from_raw(raw_pointer: core::ptr::NonNull) -> Self { Self { - core: $crate::GenericObject::from_raw(raw_pointer), + core: $crate::Obj::from_raw(raw_pointer), } } } diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index 71a55bd..a30b97c 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -1,4 +1,4 @@ -use crate::Object; +use crate::Widget; use core::convert::{TryFrom, TryInto}; use core::ptr::NonNull; use embedded_graphics::pixelcolor::{Rgb565, Rgb888}; @@ -140,7 +140,7 @@ pub(crate) unsafe extern "C" fn event_callback( obj: *mut lvgl_sys::lv_obj_t, event: lvgl_sys::lv_event_t, ) where - T: Object + Sized, + T: Widget + Sized, F: FnMut(T, Event), { // convert the lv_event_t to lvgl-rs Event type diff --git a/lvgl/src/widgets/bar.rs b/lvgl/src/widgets/bar.rs index 2e870af..98ab99a 100644 --- a/lvgl/src/widgets/bar.rs +++ b/lvgl/src/widgets/bar.rs @@ -1,6 +1,5 @@ use crate::support::Animation; -use crate::{GenericObject, NativeObject, Object}; -use core::ptr; +use crate::NativeObject; define_object!(Bar, lv_bar_create, part = BarPart); diff --git a/lvgl/src/widgets/btn.rs b/lvgl/src/widgets/btn.rs index ee6d2db..b9d9a22 100644 --- a/lvgl/src/widgets/btn.rs +++ b/lvgl/src/widgets/btn.rs @@ -1,4 +1 @@ -use crate::{GenericObject, NativeObject, Object}; -use core::ptr; - define_object!(Btn, lv_btn_create); diff --git a/lvgl/src/widgets/gauge.rs b/lvgl/src/widgets/gauge.rs index 5275f25..42b28bf 100644 --- a/lvgl/src/widgets/gauge.rs +++ b/lvgl/src/widgets/gauge.rs @@ -1,5 +1,4 @@ -use crate::{GenericObject, NativeObject, Object}; -use core::ptr; +use crate::NativeObject; define_object!(Gauge, lv_gauge_create, part = GaugePart); diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs index 8c152c2..2b46273 100644 --- a/lvgl/src/widgets/label.rs +++ b/lvgl/src/widgets/label.rs @@ -1,5 +1,4 @@ -use crate::{GenericObject, NativeObject, Object}; -use core::ptr; +use crate::NativeObject; use cstr_core::CString; define_object!(Label, lv_label_create);