Move widgets to designated modules
This commit is contained in:
parent
cb8a78137d
commit
b132edc6fc
6 changed files with 82 additions and 72 deletions
|
@ -3,7 +3,8 @@ use embedded_graphics::prelude::*;
|
||||||
use embedded_graphics_simulator::{
|
use embedded_graphics_simulator::{
|
||||||
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
||||||
};
|
};
|
||||||
use lvgl::{self, widgets::Bar, Align, Animation, Color, DisplayDriver, Label, Object, Style, UI};
|
use lvgl::widgets::{Bar, Label};
|
||||||
|
use lvgl::{self, Align, Animation, Color, DisplayDriver, Object, Style, 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;
|
||||||
|
|
|
@ -4,6 +4,7 @@ use embedded_graphics_simulator::{
|
||||||
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
||||||
};
|
};
|
||||||
use lvgl;
|
use lvgl;
|
||||||
|
use lvgl::widgets::{Label, LabelAlign};
|
||||||
use lvgl::{Object, UI};
|
use lvgl::{Object, UI};
|
||||||
use lvgl_sys;
|
use lvgl_sys;
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
|
@ -37,7 +38,7 @@ fn main() -> Result<(), String> {
|
||||||
screen_style.set_body_radius(0);
|
screen_style.set_body_radius(0);
|
||||||
screen.set_style(screen_style);
|
screen.set_style(screen_style);
|
||||||
|
|
||||||
let mut time = lvgl::Label::new(&mut screen);
|
let mut time = Label::new(&mut screen);
|
||||||
let mut style_time = lvgl::Style::new();
|
let mut style_time = lvgl::Style::new();
|
||||||
style_time.set_text_font(font_noto_sans_numeric_28);
|
style_time.set_text_font(font_noto_sans_numeric_28);
|
||||||
style_time.set_text_color(lvgl::Color::from_rgb((255, 255, 255)));
|
style_time.set_text_color(lvgl::Color::from_rgb((255, 255, 255)));
|
||||||
|
@ -47,7 +48,7 @@ fn main() -> Result<(), String> {
|
||||||
time.set_width(240);
|
time.set_width(240);
|
||||||
time.set_height(240);
|
time.set_height(240);
|
||||||
|
|
||||||
let mut bt = lvgl::Label::new(&mut screen);
|
let mut bt = Label::new(&mut screen);
|
||||||
let mut style_bt = lvgl::Style::new();
|
let mut style_bt = lvgl::Style::new();
|
||||||
style_bt.set_text_font(font_roboto_28);
|
style_bt.set_text_font(font_roboto_28);
|
||||||
let style_power = style_bt.clone();
|
let style_power = style_bt.clone();
|
||||||
|
@ -56,16 +57,16 @@ fn main() -> Result<(), String> {
|
||||||
bt.set_height(80);
|
bt.set_height(80);
|
||||||
bt.set_recolor(true);
|
bt.set_recolor(true);
|
||||||
bt.set_text("#5794f2 \u{F293}#");
|
bt.set_text("#5794f2 \u{F293}#");
|
||||||
bt.set_label_align(lvgl::LabelAlign::Left);
|
bt.set_label_align(LabelAlign::Left);
|
||||||
bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0);
|
bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0);
|
||||||
|
|
||||||
let mut power = lvgl::Label::new(&mut screen);
|
let mut power = Label::new(&mut screen);
|
||||||
power.set_style(style_power);
|
power.set_style(style_power);
|
||||||
power.set_recolor(true);
|
power.set_recolor(true);
|
||||||
power.set_width(80);
|
power.set_width(80);
|
||||||
power.set_height(20);
|
power.set_height(20);
|
||||||
power.set_text("#fade2a 20%#");
|
power.set_text("#fade2a 20%#");
|
||||||
power.set_label_align(lvgl::LabelAlign::Right);
|
power.set_label_align(LabelAlign::Right);
|
||||||
power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0);
|
power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0);
|
||||||
|
|
||||||
let threaded_ui = Arc::new(Mutex::new(ui));
|
let threaded_ui = Arc::new(Mutex::new(ui));
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
use core::mem;
|
use core::mem;
|
||||||
use core::ptr;
|
use core::ptr;
|
||||||
use cstr_core::CString;
|
|
||||||
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
|
use embedded_graphics::pixelcolor::{Rgb565, Rgb888};
|
||||||
use lvgl_sys;
|
use lvgl_sys;
|
||||||
|
|
||||||
|
@ -134,70 +133,6 @@ macro_rules! define_object {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
define_object!(Button);
|
|
||||||
|
|
||||||
impl Button {
|
|
||||||
pub fn new<C>(parent: &mut C) -> Self
|
|
||||||
where
|
|
||||||
C: NativeObject,
|
|
||||||
{
|
|
||||||
let raw = unsafe {
|
|
||||||
let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut());
|
|
||||||
ptr::NonNull::new_unchecked(ptr)
|
|
||||||
};
|
|
||||||
let core = ObjectX::from_raw(raw);
|
|
||||||
Self { core }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum LabelAlign {
|
|
||||||
Left,
|
|
||||||
Center,
|
|
||||||
Right,
|
|
||||||
Auto,
|
|
||||||
}
|
|
||||||
|
|
||||||
define_object!(Label);
|
|
||||||
|
|
||||||
impl Label {
|
|
||||||
pub fn new<C>(parent: &mut C) -> Self
|
|
||||||
where
|
|
||||||
C: NativeObject,
|
|
||||||
{
|
|
||||||
let raw = unsafe {
|
|
||||||
let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut());
|
|
||||||
ptr::NonNull::new_unchecked(ptr)
|
|
||||||
};
|
|
||||||
let core = ObjectX::from_raw(raw);
|
|
||||||
Self { core }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_text(&mut self, text: &str) {
|
|
||||||
let text = CString::new(text).unwrap();
|
|
||||||
unsafe {
|
|
||||||
lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_label_align(&mut self, align: LabelAlign) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_recolor(&mut self, recolor: bool) {
|
|
||||||
unsafe {
|
|
||||||
lvgl_sys::lv_label_set_recolor(self.core.raw().as_mut(), recolor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Themes {
|
pub enum Themes {
|
||||||
Pretty,
|
Pretty,
|
||||||
}
|
}
|
||||||
|
|
18
lvgl/src/widgets/button.rs
Normal file
18
lvgl/src/widgets/button.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use crate::{NativeObject, ObjectX};
|
||||||
|
use core::ptr;
|
||||||
|
|
||||||
|
define_object!(Button);
|
||||||
|
|
||||||
|
impl Button {
|
||||||
|
pub fn new<C>(parent: &mut C) -> Self
|
||||||
|
where
|
||||||
|
C: NativeObject,
|
||||||
|
{
|
||||||
|
let raw = unsafe {
|
||||||
|
let ptr = lvgl_sys::lv_btn_create(parent.raw().as_mut(), ptr::null_mut());
|
||||||
|
ptr::NonNull::new_unchecked(ptr)
|
||||||
|
};
|
||||||
|
let core = ObjectX::from_raw(raw);
|
||||||
|
Self { core }
|
||||||
|
}
|
||||||
|
}
|
51
lvgl/src/widgets/label.rs
Normal file
51
lvgl/src/widgets/label.rs
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
use crate::{NativeObject, ObjectX};
|
||||||
|
use core::ptr;
|
||||||
|
use cstr_core::CString;
|
||||||
|
|
||||||
|
pub enum LabelAlign {
|
||||||
|
Left,
|
||||||
|
Center,
|
||||||
|
Right,
|
||||||
|
Auto,
|
||||||
|
}
|
||||||
|
|
||||||
|
define_object!(Label);
|
||||||
|
|
||||||
|
impl Label {
|
||||||
|
pub fn new<C>(parent: &mut C) -> Self
|
||||||
|
where
|
||||||
|
C: NativeObject,
|
||||||
|
{
|
||||||
|
let raw = unsafe {
|
||||||
|
let ptr = lvgl_sys::lv_label_create(parent.raw().as_mut(), ptr::null_mut());
|
||||||
|
ptr::NonNull::new_unchecked(ptr)
|
||||||
|
};
|
||||||
|
let core = ObjectX::from_raw(raw);
|
||||||
|
Self { core }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_text(&mut self, text: &str) {
|
||||||
|
let text = CString::new(text).unwrap();
|
||||||
|
unsafe {
|
||||||
|
lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_label_align(&mut self, align: LabelAlign) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_recolor(&mut self, recolor: bool) {
|
||||||
|
unsafe {
|
||||||
|
lvgl_sys::lv_label_set_recolor(self.core.raw().as_mut(), recolor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
mod bar;
|
mod bar;
|
||||||
|
mod button;
|
||||||
|
mod label;
|
||||||
|
|
||||||
pub use self::bar::Bar;
|
pub use self::bar::*;
|
||||||
|
pub use self::button::*;
|
||||||
|
pub use self::label::*;
|
||||||
|
|
Loading…
Reference in a new issue