Update Bar example

This commit is contained in:
Rafael Caricio 2020-06-04 19:26:51 +02:00 committed by Rafael Carício
parent c494d8fac0
commit 52e31496c6
5 changed files with 54 additions and 100 deletions

View file

@ -3,8 +3,9 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{ use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
}; };
use lvgl::widgets::{Bar, BarComponent, Label, LabelAlign}; use lvgl::style::{State, Style};
use lvgl::{self, Align, Animation, Border, Color, DisplayDriver, Event, Object, Style, UI}; use lvgl::widgets::{Bar, BarPart, Label, LabelAlign};
use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, ObjPart, Object, 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;
@ -29,32 +30,20 @@ fn main() -> Result<(), String> {
let mut screen = ui.scr_act(); let mut screen = ui.scr_act();
let mut screen_style = Style::default(); let mut screen_style = Style::default();
screen_style.set_body_main_color(Color::from_rgb((255, 255, 255))); screen_style.set_bg_color(State::DEFAULT, Color::from_rgb((255, 255, 255)));
screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255))); screen_style.set_radius(State::DEFAULT, 0);
screen_style.set_body_radius(0); screen.add_style(ObjPart::Main, screen_style);
screen.set_style(screen_style);
// Create the bar object // Create the bar object
let mut bar = Bar::new(&mut screen); let mut bar = Bar::new(&mut screen);
bar.set_size(175, 50); bar.set_size(175, 20);
bar.set_align(&mut screen, Align::Center, 0, 20); bar.set_align(&mut screen, Align::Center, 0, 10);
bar.set_range(0, 100); bar.set_range(0, 100);
bar.set_value(0, Animation::OFF);
// Set the indicator style for the bar object // // Set the indicator style for the bar object
let mut ind_style = Style::default(); let mut ind_style = Style::default();
ind_style.set_body_main_color(Color::from_rgb((100, 245, 0))); ind_style.set_bg_color(State::DEFAULT, Color::from_rgb((100, 245, 100)));
ind_style.set_body_grad_color(Color::from_rgb((100, 245, 0))); bar.add_style(BarPart::Indicator, ind_style);
bar.set_bar_style(BarComponent::Indicator, ind_style);
// Set the background style for the bar object
let mut bg_style = Style::default();
bg_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
bg_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
bg_style.set_body_radius(0);
bg_style.set_body_border_part(Border::TOP);
bg_style.set_body_border_width(1);
bar.set_bar_style(BarComponent::Background, bg_style);
let mut loading_lbl = Label::new(&mut screen); let mut loading_lbl = Label::new(&mut screen);
loading_lbl.set_text("Loading..."); loading_lbl.set_text("Loading...");
@ -62,8 +51,8 @@ fn main() -> Result<(), String> {
loading_lbl.set_label_align(LabelAlign::Center); loading_lbl.set_label_align(LabelAlign::Center);
let mut loading_style = Style::default(); let mut loading_style = Style::default();
loading_style.set_text_color(Color::from_rgb((0, 0, 0))); loading_style.set_text_color(State::DEFAULT, Color::from_rgb((0, 0, 0)));
loading_lbl.set_style(loading_style); loading_lbl.add_style(ObjPart::Main, loading_style);
let threaded_ui = Arc::new(Mutex::new(ui)); let threaded_ui = Arc::new(Mutex::new(ui));

View file

@ -1,3 +1 @@
pub mod style; pub mod style;
use style::*;

View file

@ -1,6 +1,5 @@
use crate::Color; use crate::Color;
use alloc::boxed::Box; use alloc::boxed::Box;
use core::borrow::BorrowMut;
use core::mem; use core::mem;
use cstr_core::CString; use cstr_core::CString;
use lvgl_sys; use lvgl_sys;

View file

@ -1,7 +1,6 @@
use crate::lv_core::style::Style; use crate::lv_core::style::Style;
use alloc::boxed::Box; use alloc::boxed::Box;
use core::convert::{TryFrom, TryInto}; use core::convert::{TryFrom, TryInto};
use core::mem;
use core::ptr; use core::ptr;
use core::ptr::NonNull; use core::ptr::NonNull;
use embedded_graphics::pixelcolor::{Rgb565, Rgb888}; use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
@ -32,6 +31,7 @@ impl NativeObject for GenericObject {
/// 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 Object: NativeObject {
type SpecialEvent; type SpecialEvent;
type Part: Into<u8>;
/// Construct an instance of the object from a raw pointer. /// Construct an instance of the object from a raw pointer.
/// ///
@ -40,6 +40,12 @@ pub trait Object: NativeObject {
/// ///
unsafe fn from_raw(raw_pointer: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self; unsafe fn from_raw(raw_pointer: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self;
fn add_style(&mut self, part: Self::Part, style: Style) {
unsafe {
lvgl_sys::lv_obj_add_style(self.raw().as_mut(), part.into(), Box::into_raw(style.raw));
};
}
fn set_pos(&mut self, x: i16, y: i16) { fn set_pos(&mut self, x: i16, y: i16) {
unsafe { unsafe {
lvgl_sys::lv_obj_set_pos( lvgl_sys::lv_obj_set_pos(
@ -109,26 +115,31 @@ pub trait Object: NativeObject {
); );
} }
} }
fn add_style(&mut self, style: Style) {
unsafe {
lvgl_sys::lv_obj_add_style(
self.raw().as_mut(),
lvgl_sys::LV_OBJ_PART_MAIN as u8,
Box::into_raw(style.raw),
);
};
}
} }
impl Object for GenericObject { impl Object for GenericObject {
type SpecialEvent = (); type SpecialEvent = ();
type Part = ObjPart;
unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self { unsafe fn from_raw(raw: ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self { raw: raw.as_ptr() } Self { raw: raw.as_ptr() }
} }
} }
pub enum ObjPart {
Main,
All,
}
impl Into<u8> for ObjPart {
fn into(self) -> u8 {
match self {
ObjPart::Main => lvgl_sys::LV_OBJ_PART_MAIN as u8,
ObjPart::All => lvgl_sys::LV_OBJ_PART_ALL as u8,
}
}
}
impl Default for GenericObject { impl Default for GenericObject {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -139,48 +150,18 @@ impl Default for GenericObject {
macro_rules! define_object { macro_rules! define_object {
($item:ident) => { ($item:ident) => {
pub struct $item { define_object!($item, event = (), part = $crate::support::ObjPart);
core: $crate::support::GenericObject,
}
impl $item {
pub fn on_event<F>(&mut self, f: F)
where
F: FnMut(
Self,
$crate::support::Event<<Self as $crate::support::Object>::SpecialEvent>,
),
{
unsafe {
let mut raw = self.raw();
let obj = raw.as_mut();
let user_closure = alloc::boxed::Box::new(f);
obj.user_data = alloc::boxed::Box::into_raw(user_closure) as *mut cty::c_void;
lvgl_sys::lv_obj_set_event_cb(
obj,
lvgl_sys::lv_event_cb_t::Some($crate::support::event_callback::<Self, F>),
);
}
}
}
impl $crate::support::NativeObject for $item {
fn raw(&self) -> core::ptr::NonNull<lvgl_sys::lv_obj_t> {
self.core.raw()
}
}
impl $crate::support::Object for $item {
type SpecialEvent = ();
unsafe fn from_raw(raw_pointer: core::ptr::NonNull<lvgl_sys::lv_obj_t>) -> Self {
Self {
core: $crate::support::GenericObject::from_raw(raw_pointer),
}
}
}
}; };
($item:ident, $event_type:ident) => { ($item:ident, event = $event_type:ty) => {
define_object!($item, event = $event_type, part = $crate::support::ObjPart);
};
($item:ident, part = $part_type:ty) => {
define_object!($item, event = (), part = $part_type);
};
($item:ident, part = $part_type:ty, event = $event_type:ty) => {
define_object!($item, event = $event_type, part = $part_type);
};
($item:ident, event = $event_type:ty, part = $part_type:ty) => {
pub struct $item { pub struct $item {
core: $crate::support::GenericObject, core: $crate::support::GenericObject,
} }
@ -214,6 +195,7 @@ macro_rules! define_object {
impl $crate::support::Object for $item { impl $crate::support::Object for $item {
type SpecialEvent = $event_type; type SpecialEvent = $event_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 {

View file

@ -1,10 +1,8 @@
use crate::style::Style;
use crate::support::{Animation, GenericObject, NativeObject}; use crate::support::{Animation, GenericObject, NativeObject};
use crate::Object; use crate::Object;
use alloc::boxed::Box;
use core::ptr; use core::ptr;
define_object!(Bar); define_object!(Bar, part = BarPart);
impl Bar { impl Bar {
pub fn new<C>(parent: &mut C) -> Self pub fn new<C>(parent: &mut C) -> Self
@ -32,22 +30,10 @@ impl Bar {
lvgl_sys::lv_bar_set_value(self.core.raw().as_mut(), value, anim.into()); lvgl_sys::lv_bar_set_value(self.core.raw().as_mut(), value, anim.into());
} }
} }
/// Set the style, for the given `BarComponent`
pub fn set_bar_style(&mut self, style: Style) {
let boxed = style.raw;
unsafe {
lvgl_sys::lv_obj_add_style(
self.core.raw().as_mut(),
lvgl_sys::LV_OBJ_PART_MAIN as u8,
Box::into_raw(boxed),
);
}
}
} }
/// The different components, of a bar object. /// The different parts, of a bar object.
pub enum BarComponent { pub enum BarPart {
/// The background of the bar. /// The background of the bar.
Background, Background,
/// The indicator of the bar. /// The indicator of the bar.
@ -55,11 +41,11 @@ pub enum BarComponent {
Indicator, Indicator,
} }
impl From<BarComponent> for lvgl_sys::lv_bar_style_t { impl From<BarPart> for u8 {
fn from(component: BarComponent) -> Self { fn from(component: BarPart) -> Self {
match component { match component {
BarComponent::Background => lvgl_sys::LV_BAR_STYLE_BG as u8, BarPart::Background => lvgl_sys::LV_BAR_PART_BG as u8,
BarComponent::Indicator => lvgl_sys::LV_BAR_STYLE_INDIC as u8, BarPart::Indicator => lvgl_sys::LV_BAR_PART_INDIC as u8,
} }
} }
} }