From f0ec94cbf9a1c535397d33065dc3e86522183e2f Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Fri, 12 Jun 2020 12:41:20 +0200 Subject: [PATCH] Use generated widgets --- lvgl-codegen/Cargo.toml | 1 + lvgl-codegen/src/lib.rs | 8 ++++++- lvgl-sys/Cargo.toml | 2 +- lvgl/Cargo.toml | 5 ++++ lvgl/build.rs | 48 +++++++++++++++++++++++++++++++++++++++ lvgl/src/lib.rs | 1 + lvgl/src/lv_core/obj.rs | 32 +++++++++----------------- lvgl/src/widgets/arc.rs | 20 ++++++++-------- lvgl/src/widgets/bar.rs | 3 +-- lvgl/src/widgets/gauge.rs | 12 ---------- lvgl/src/widgets/label.rs | 28 +++++++---------------- lvgl/src/widgets/mod.rs | 4 ++-- 12 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 lvgl/build.rs diff --git a/lvgl-codegen/Cargo.toml b/lvgl-codegen/Cargo.toml index 3906211..2dc7cad 100644 --- a/lvgl-codegen/Cargo.toml +++ b/lvgl-codegen/Cargo.toml @@ -17,6 +17,7 @@ name = "lvgl_codegen" regex = "1.3.9" quote = "1.0.7" lazy_static = "1.4.0" +clang = { path = "../../clang-rs" } itertools = "0.9.0" proc-macro2 = "1.0.18" Inflector = "0.11.4" diff --git a/lvgl-codegen/src/lib.rs b/lvgl-codegen/src/lib.rs index b124c2f..8bab7aa 100644 --- a/lvgl-codegen/src/lib.rs +++ b/lvgl-codegen/src/lib.rs @@ -48,7 +48,13 @@ impl Rusty for LvWidget { fn code(&self, _parent: &Self::Parent) -> WrapperResult { let widget_name = format_ident!("{}", to_pascal_case(self.name.as_str())); - let methods: Vec = self.methods.iter().flat_map(|m| m.code(self)).collect(); + let methods: Vec = self + .methods + .iter() + .take(1) + .into_iter() + .flat_map(|m| m.code(self)) + .collect(); Ok(quote! { define_object!(#widget_name); diff --git a/lvgl-sys/Cargo.toml b/lvgl-sys/Cargo.toml index 580d119..b3a9479 100644 --- a/lvgl-sys/Cargo.toml +++ b/lvgl-sys/Cargo.toml @@ -21,4 +21,4 @@ cty = "0.2.1" [build-dependencies] cc = "1.0.50" -bindgen = "0.53.2" +bindgen = "0.54.0" diff --git a/lvgl/Cargo.toml b/lvgl/Cargo.toml index a4d0bd2..27acea9 100644 --- a/lvgl/Cargo.toml +++ b/lvgl/Cargo.toml @@ -16,3 +16,8 @@ cty = "0.2.1" embedded-graphics = "0.6.2" cstr_core = { version = "0.2.0", default-features = false, features = ["alloc"] } bitflags = "1.2.1" + +[build-dependencies] +lvgl-codegen = { path = "../lvgl-codegen" } +quote = "1.0.7" +proc-macro2 = "1.0.18" diff --git a/lvgl/build.rs b/lvgl/build.rs new file mode 100644 index 0000000..01ac3d8 --- /dev/null +++ b/lvgl/build.rs @@ -0,0 +1,48 @@ +use std::ffi::OsStr; +use std::path::PathBuf; +use std::process::Command; +use std::{env, fs, path}; + +fn main() { + let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) + .canonicalize() + .unwrap(); + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + let gen_code = out_path.join("bindings.rs"); + + let code = invoke_command("../target/debug/lvgl-codegen", &[""], &project_dir); + fs::write(&gen_code, code).unwrap(); + + // // Format generated code + // let _ = invoke_command( + // "cargo", + // &["fmt", "-p", gen_code.to_str().unwrap()], + // &project_dir, + // ); +} + +fn invoke_command(command: C, args: I, cur_dir: D) -> String +where + C: AsRef, + I: IntoIterator, + S: AsRef, + D: AsRef, +{ + Command::new(command) + .current_dir(cur_dir) + .args(args) + .output() + .ok() + .and_then(|output| { + if output.status.success() { + Some(String::from_utf8_lossy(&output.stdout).trim().to_string()) + } else { + panic!( + "{}", + String::from_utf8_lossy(&output.stderr).trim().to_string() + ); + } + }) + .unwrap() +} diff --git a/lvgl/src/lib.rs b/lvgl/src/lib.rs index d7060e0..1f7f6be 100644 --- a/lvgl/src/lib.rs +++ b/lvgl/src/lib.rs @@ -1,6 +1,7 @@ #![feature(try_trait)] #![no_std] +#[macro_use] extern crate alloc; #[macro_use] extern crate bitflags; diff --git a/lvgl/src/lv_core/obj.rs b/lvgl/src/lv_core/obj.rs index 6aaea6e..feffc11 100644 --- a/lvgl/src/lv_core/obj.rs +++ b/lvgl/src/lv_core/obj.rs @@ -118,36 +118,26 @@ impl Default for Obj { } macro_rules! define_object { - ($item:ident, $create_fn:ident) => { - define_object!($item, $create_fn, event = (), part = $crate::Part); + ($item:ident) => { + define_object!($item, event = (), part = $crate::Part); }; - ($item:ident, $create_fn:ident, event = $event_type:ty) => { - define_object!($item, $create_fn, event = $event_type, part = $crate::Part); + ($item:ident, event = $event_type:ty) => { + define_object!($item, event = $event_type, part = $crate::Part); }; - ($item:ident, $create_fn:ident, part = $part_type:ty) => { - define_object!($item, $create_fn, event = (), part = $part_type); + ($item:ident, part = $part_type:ty) => { + define_object!($item, event = (), part = $part_type); }; - ($item:ident, $create_fn:ident, part = $part_type:ty, event = $event_type:ty) => { - define_object!($item, $create_fn, event = $event_type, part = $part_type); + ($item:ident, part = $part_type:ty, event = $event_type:ty) => { + define_object!($item, event = $event_type, part = $part_type); }; - ($item:ident, $create_fn:ident, event = $event_type:ty, part = $part_type:ty) => { + ($item:ident, event = $event_type:ty, part = $part_type:ty) => { pub struct $item { core: $crate::Obj, } - impl $item { - pub fn new(parent: &mut C) -> $crate::LvResult - where - C: $crate::NativeObject, - { - unsafe { - let ptr = lvgl_sys::$create_fn(parent.raw()?.as_mut(), core::ptr::null_mut()); - let raw = core::ptr::NonNull::new(ptr)?; - let core = <$crate::Obj as $crate::Widget>::from_raw(raw); - Ok(Self { core }) - } - } + unsafe impl Send for $item {} + impl $item { pub fn on_event(&mut self, f: F) -> $crate::LvResult<()> where F: FnMut(Self, $crate::support::Event<::SpecialEvent>), diff --git a/lvgl/src/widgets/arc.rs b/lvgl/src/widgets/arc.rs index 9a3ddd1..515fe01 100644 --- a/lvgl/src/widgets/arc.rs +++ b/lvgl/src/widgets/arc.rs @@ -1,7 +1,6 @@ +use crate::widgets::Arc; use crate::{LvResult, 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. @@ -19,10 +18,10 @@ impl Arc { /// 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) -> LvResult<()> { + pub fn set_end_angle(&self, angle: u16, part: ArcPart) -> LvResult<()> { match part { ArcPart::Background => unsafe { - lvgl_sys::lv_arc_set_bg_start_angle(self.core.raw()?.as_mut(), angle) + lvgl_sys::lv_arc_set_bg_end_angle(self.core.raw()?.as_mut(), angle) }, ArcPart::Indicator => unsafe { lvgl_sys::lv_arc_set_end_angle(self.core.raw()?.as_mut(), angle) @@ -41,19 +40,18 @@ impl Arc { } /// The different parts, of an arc object. +#[derive(Debug, Copy, Clone, PartialEq)] +#[repr(u8)] pub enum ArcPart { /// The background of the arc. - Background, + Background = lvgl_sys::LV_ARC_PART_BG as u8, /// The indicator of the arc. /// This is what moves/changes, depending on the arc's value. - Indicator, + Indicator = lvgl_sys::LV_ARC_PART_INDIC as u8, } 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, - } + fn from(part: ArcPart) -> Self { + part as u8 } } diff --git a/lvgl/src/widgets/bar.rs b/lvgl/src/widgets/bar.rs index 21e4f27..88e4aec 100644 --- a/lvgl/src/widgets/bar.rs +++ b/lvgl/src/widgets/bar.rs @@ -1,8 +1,7 @@ use crate::support::Animation; +use crate::widgets::Bar; use crate::{LvResult, NativeObject}; -define_object!(Bar, lv_bar_create, part = BarPart); - impl Bar { /// Set minimum and the maximum values of the bar pub fn set_range(&mut self, min: i16, max: i16) -> LvResult<()> { diff --git a/lvgl/src/widgets/gauge.rs b/lvgl/src/widgets/gauge.rs index db3ca40..6805eb0 100644 --- a/lvgl/src/widgets/gauge.rs +++ b/lvgl/src/widgets/gauge.rs @@ -1,17 +1,5 @@ use crate::{LvResult, NativeObject}; -define_object!(Gauge, lv_gauge_create, part = GaugePart); - -impl Gauge { - /// Set a new value on the gauge - pub fn set_value(&mut self, needle_id: u8, value: i32) -> LvResult<()> { - unsafe { - lvgl_sys::lv_gauge_set_value(self.core.raw()?.as_mut(), needle_id, value); - } - Ok(()) - } -} - pub enum GaugePart { Main, Major, diff --git a/lvgl/src/widgets/label.rs b/lvgl/src/widgets/label.rs index 8a59f47..5744f32 100644 --- a/lvgl/src/widgets/label.rs +++ b/lvgl/src/widgets/label.rs @@ -1,8 +1,7 @@ +use crate::widgets::Label; use crate::{LvResult, NativeObject}; use cstr_core::CString; -define_object!(Label, lv_label_create); - impl Label { pub fn set_text(&mut self, text: &str) -> LvResult<()> { let text = CString::new(text).unwrap(); @@ -13,29 +12,18 @@ impl Label { } pub fn set_label_align(&mut self, align: LabelAlign) -> LvResult<()> { - let align = match align { - LabelAlign::Left => lvgl_sys::LV_LABEL_ALIGN_LEFT, - LabelAlign::Center => lvgl_sys::LV_LABEL_ALIGN_CENTER, - LabelAlign::Right => lvgl_sys::LV_LABEL_ALIGN_RIGHT, - LabelAlign::Auto => lvgl_sys::LV_LABEL_ALIGN_AUTO, - } as lvgl_sys::lv_label_align_t; unsafe { - lvgl_sys::lv_label_set_align(self.core.raw()?.as_mut(), align); - } - Ok(()) - } - - pub fn set_recolor(&mut self, recolor: bool) -> LvResult<()> { - unsafe { - lvgl_sys::lv_label_set_recolor(self.core.raw()?.as_mut(), recolor); + lvgl_sys::lv_label_set_align(self.core.raw()?.as_mut(), align as u8); } Ok(()) } } +#[derive(Debug, Copy, Clone, PartialEq)] +#[repr(u8)] pub enum LabelAlign { - Left, - Center, - Right, - Auto, + Left = lvgl_sys::LV_LABEL_ALIGN_LEFT as u8, + Center = lvgl_sys::LV_LABEL_ALIGN_CENTER as u8, + Right = lvgl_sys::LV_LABEL_ALIGN_RIGHT as u8, + Auto = lvgl_sys::LV_LABEL_ALIGN_AUTO as u8, } diff --git a/lvgl/src/widgets/mod.rs b/lvgl/src/widgets/mod.rs index 153cbb5..f28c06e 100644 --- a/lvgl/src/widgets/mod.rs +++ b/lvgl/src/widgets/mod.rs @@ -1,11 +1,11 @@ mod arc; mod bar; -mod btn; mod gauge; mod label; +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + pub use arc::*; pub use bar::*; -pub use btn::*; pub use gauge::*; pub use label::*;