Simplify object creation
This commit is contained in:
parent
88d7d426ca
commit
25efb6da91
13 changed files with 31 additions and 36 deletions
|
@ -102,7 +102,7 @@ List of LVGL features that impacts the library usage in general.
|
||||||
- [ ] Container (lv_cont)
|
- [ ] Container (lv_cont)
|
||||||
- [ ] Color picker (lv_cpicker)
|
- [ ] Color picker (lv_cpicker)
|
||||||
- [ ] Drop-down list (lv_dropdown)
|
- [ ] Drop-down list (lv_dropdown)
|
||||||
- [ ] Gauge (lv_gauge)
|
- [x] Gauge (lv_gauge)
|
||||||
- [ ] Image (lv_img)
|
- [ ] Image (lv_img)
|
||||||
- [ ] Image button (lv_imgbtn)
|
- [ ] Image button (lv_imgbtn)
|
||||||
- [ ] Keyboard (lv_keyboard)
|
- [ ] Keyboard (lv_keyboard)
|
||||||
|
@ -123,7 +123,7 @@ List of LVGL features that impacts the library usage in general.
|
||||||
- [ ] Tabview (lv_tabview)
|
- [ ] Tabview (lv_tabview)
|
||||||
- [ ] Text area (lv_textarea)
|
- [ ] Text area (lv_textarea)
|
||||||
- [ ] Tile view (lv_tileview)
|
- [ ] 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
|
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.
|
is missing a feature you want to make use, please send a Pull Request or open an issue.
|
||||||
|
|
|
@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
|
||||||
};
|
};
|
||||||
use lvgl::style::Style;
|
use lvgl::style::Style;
|
||||||
use lvgl::widgets::{Bar, BarPart, Label, LabelAlign};
|
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 lvgl_sys;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
|
||||||
};
|
};
|
||||||
use lvgl::style::Style;
|
use lvgl::style::Style;
|
||||||
use lvgl::widgets::{Btn, Label};
|
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 lvgl_sys;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use embedded_graphics_simulator::{
|
||||||
use lvgl;
|
use lvgl;
|
||||||
use lvgl::style::Style;
|
use lvgl::style::Style;
|
||||||
use lvgl::widgets::{Label, LabelAlign};
|
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 lvgl_sys;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
|
||||||
};
|
};
|
||||||
use lvgl::style::{Opacity, Style};
|
use lvgl::style::{Opacity, Style};
|
||||||
use lvgl::widgets::{Gauge, GaugePart};
|
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 lvgl_sys;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{DisplayDriver, Event, GenericObject, Object};
|
use crate::{DisplayDriver, Event, Obj, Widget};
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
use core::ptr;
|
use core::ptr;
|
||||||
|
@ -47,16 +47,16 @@ impl UI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scr_act(&self) -> GenericObject {
|
pub fn scr_act(&self) -> Obj {
|
||||||
unsafe {
|
unsafe {
|
||||||
let screen = lvgl_sys::lv_disp_get_scr_act(ptr::null_mut());
|
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<T>(&mut self, obj: &mut T, event: Event<T::SpecialEvent>)
|
pub fn event_send<T>(&mut self, obj: &mut T, event: Event<T::SpecialEvent>)
|
||||||
where
|
where
|
||||||
T: Object,
|
T: Widget,
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
lvgl_sys::lv_event_send(obj.raw().as_mut(), event.into(), ptr::null_mut());
|
lvgl_sys::lv_event_send(obj.raw().as_mut(), event.into(), ptr::null_mut());
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod object;
|
mod obj;
|
||||||
pub mod style;
|
pub mod style;
|
||||||
|
|
||||||
pub use object::*;
|
pub use obj::*;
|
||||||
|
|
|
@ -14,20 +14,20 @@ pub trait NativeObject {
|
||||||
/// Generic LVGL object.
|
/// Generic LVGL object.
|
||||||
///
|
///
|
||||||
/// This is the parent object of all widget types. It stores the native LVGL raw pointer.
|
/// 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
|
// We use a raw pointer here because we do not control this memory address, it is controlled
|
||||||
// by LVGL's global state.
|
// by LVGL's global state.
|
||||||
raw: *mut lvgl_sys::lv_obj_t,
|
raw: *mut lvgl_sys::lv_obj_t,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NativeObject for GenericObject {
|
impl NativeObject for Obj {
|
||||||
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
|
fn raw(&self) -> ptr::NonNull<lvgl_sys::lv_obj_t> {
|
||||||
ptr::NonNull::new(self.raw).expect(PANIC_MESSAGE)
|
ptr::NonNull::new(self.raw).expect(PANIC_MESSAGE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A wrapper for all LittlevGL common operations on generic objects.
|
/// A wrapper for all LittlevGL common operations on generic objects.
|
||||||
pub trait Object: NativeObject {
|
pub trait Widget: NativeObject {
|
||||||
type SpecialEvent;
|
type SpecialEvent;
|
||||||
type Part: Into<u8>;
|
type Part: Into<u8>;
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ pub trait Object: NativeObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Object for GenericObject {
|
impl Widget for Obj {
|
||||||
type SpecialEvent = ();
|
type SpecialEvent = ();
|
||||||
type Part = Part;
|
type Part = Part;
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ impl Object for GenericObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for GenericObject {
|
impl Default for Obj {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
raw: unsafe { lvgl_sys::lv_obj_create(ptr::null_mut(), ptr::null_mut()) },
|
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) => {
|
($item:ident, $create_fn:ident, event = $event_type:ty, part = $part_type:ty) => {
|
||||||
pub struct $item {
|
pub struct $item {
|
||||||
core: $crate::GenericObject,
|
core: $crate::Obj,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $item {
|
impl $item {
|
||||||
pub fn new<C>(parent: &mut C) -> Self
|
pub fn new<C>(parent: &mut C) -> Self
|
||||||
where
|
where
|
||||||
C: NativeObject,
|
C: $crate::NativeObject,
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = lvgl_sys::$create_fn(parent.raw().as_mut(), ptr::null_mut());
|
let ptr = lvgl_sys::$create_fn(parent.raw().as_mut(), core::ptr::null_mut());
|
||||||
let raw = ptr::NonNull::new_unchecked(ptr);
|
let raw = core::ptr::NonNull::new_unchecked(ptr);
|
||||||
let core = GenericObject::from_raw(raw);
|
let core = <$crate::Obj as $crate::Widget>::from_raw(raw);
|
||||||
Self { core }
|
Self { core }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_event<F>(&mut self, f: F)
|
pub fn on_event<F>(&mut self, f: F)
|
||||||
where
|
where
|
||||||
F: FnMut(Self, $crate::support::Event<<Self as $crate::Object>::SpecialEvent>),
|
F: FnMut(Self, $crate::support::Event<<Self as $crate::Widget>::SpecialEvent>),
|
||||||
{
|
{
|
||||||
|
use $crate::NativeObject;
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut raw = self.raw();
|
let mut raw = self.raw();
|
||||||
let obj = raw.as_mut();
|
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 SpecialEvent = $event_type;
|
||||||
type Part = $part_type;
|
type Part = $part_type;
|
||||||
|
|
||||||
unsafe fn from_raw(raw_pointer: core::ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
|
unsafe fn from_raw(raw_pointer: core::ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
core: $crate::GenericObject::from_raw(raw_pointer),
|
core: $crate::Obj::from_raw(raw_pointer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::Object;
|
use crate::Widget;
|
||||||
use core::convert::{TryFrom, TryInto};
|
use core::convert::{TryFrom, TryInto};
|
||||||
use core::ptr::NonNull;
|
use core::ptr::NonNull;
|
||||||
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
|
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
|
||||||
|
@ -140,7 +140,7 @@ pub(crate) unsafe extern "C" fn event_callback<T, F>(
|
||||||
obj: *mut lvgl_sys::lv_obj_t,
|
obj: *mut lvgl_sys::lv_obj_t,
|
||||||
event: lvgl_sys::lv_event_t,
|
event: lvgl_sys::lv_event_t,
|
||||||
) where
|
) where
|
||||||
T: Object + Sized,
|
T: Widget + Sized,
|
||||||
F: FnMut(T, Event<T::SpecialEvent>),
|
F: FnMut(T, Event<T::SpecialEvent>),
|
||||||
{
|
{
|
||||||
// convert the lv_event_t to lvgl-rs Event type
|
// convert the lv_event_t to lvgl-rs Event type
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::support::Animation;
|
use crate::support::Animation;
|
||||||
use crate::{GenericObject, NativeObject, Object};
|
use crate::NativeObject;
|
||||||
use core::ptr;
|
|
||||||
|
|
||||||
define_object!(Bar, lv_bar_create, part = BarPart);
|
define_object!(Bar, lv_bar_create, part = BarPart);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1 @@
|
||||||
use crate::{GenericObject, NativeObject, Object};
|
|
||||||
use core::ptr;
|
|
||||||
|
|
||||||
define_object!(Btn, lv_btn_create);
|
define_object!(Btn, lv_btn_create);
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::{GenericObject, NativeObject, Object};
|
use crate::NativeObject;
|
||||||
use core::ptr;
|
|
||||||
|
|
||||||
define_object!(Gauge, lv_gauge_create, part = GaugePart);
|
define_object!(Gauge, lv_gauge_create, part = GaugePart);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::{GenericObject, NativeObject, Object};
|
use crate::NativeObject;
|
||||||
use core::ptr;
|
|
||||||
use cstr_core::CString;
|
use cstr_core::CString;
|
||||||
|
|
||||||
define_object!(Label, lv_label_create);
|
define_object!(Label, lv_label_create);
|
||||||
|
|
Loading…
Reference in a new issue