Upgrade to lvgl v7.10.1

- Clean up demo
- Remove warning from rustc
- Improve test of custom Box implementation
- Upgrade dep versions
This commit is contained in:
Rafael Caricio 2021-05-24 22:27:23 +02:00 committed by Rafael Carício
parent 3791b211b8
commit 348963b80d
7 changed files with 59 additions and 45 deletions

View file

@ -8,8 +8,8 @@ publish = false
[dev-dependencies]
lvgl = { path = "../lvgl" }
lvgl-sys = { path = "../lvgl-sys" }
embedded-graphics = "0.6"
embedded-graphics-simulator = "0.2.0"
embedded-graphics = "0.6.2"
embedded-graphics-simulator = "0.2.1"
heapless = "0.5.5"
cstr_core = { version = "0.2.0", features = ["alloc"] }

View file

@ -1,4 +1,4 @@
use cstr_core::{CStr, CString};
use cstr_core::CString;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{
@ -52,20 +52,6 @@ fn main() -> Result<(), LvError> {
bt.set_label_align(LabelAlign::Left)?;
bt.set_align(&mut screen, Align::InTopLeft, 0, 0)?;
fn set_text<S>(text: S) -> Result<(), ()>
where
S: AsRef<cstr_core::CStr>,
{
let _v: *const i8 = text.as_ref().as_ptr();
Ok(())
}
let mut t: heapless::String<heapless::consts::U8> = heapless::String::from("test");
t.push('\0').unwrap();
set_text(CStr::from_bytes_with_nul(t.as_bytes()).unwrap()).unwrap();
set_text(cstr_core::CStr::from_bytes_with_nul("test\0".as_bytes()).unwrap()).unwrap();
set_text(cstr_core::CString::new("test").unwrap().as_c_str()).unwrap();
let mut power = Label::new(&mut screen)?;
power.set_recolor(true)?;
power.set_width(80)?;
@ -80,8 +66,8 @@ fn main() -> Result<(), LvError> {
if i > 59 {
i = 0;
}
let val = format!("21:{:02}", i);
time.set_text(CString::new(val.as_str()).unwrap().as_c_str())?;
let val = CString::new(format!("21:{:02}", i)).unwrap();
time.set_text(&val)?;
i = 1 + i;
ui.task_handler();

View file

@ -12,7 +12,7 @@ repository = "https://github.com/rafaelcaricio/lvgl-rs"
regex = "1.4.3"
quote = "1.0.9"
lazy_static = "1.4.0"
proc-macro2 = "1.0.24"
proc-macro2 = "1.0.27"
Inflector = "0.11.4"
syn = { version = "1.0.62", features = ["full"]}
syn = { version = "1.0.72", features = ["full"]}

View file

@ -20,5 +20,5 @@ name = "lvgl_sys"
cty = "0.2.1"
[build-dependencies]
cc = "1.0.67"
bindgen = "0.57.0"
cc = "1.0.68"
bindgen = "0.58.1"

View file

@ -29,21 +29,21 @@ fn main() {
});
if !conf_path.exists() {
panic!(format!(
panic!(
"Directory {} referenced by {} needs to exist",
conf_path.to_string_lossy(),
CONFIG_NAME
));
);
}
if !conf_path.is_dir() {
panic!(format!("{} needs to be a directory", CONFIG_NAME));
panic!("{} needs to be a directory", CONFIG_NAME);
}
if !conf_path.join("lv_conf.h").exists() {
panic!(format!(
panic!(
"Directory {} referenced by {} needs to contain a file called lv_conf.h",
conf_path.to_string_lossy(),
CONFIG_NAME
));
);
}
println!(

View file

@ -3,11 +3,16 @@ use core::mem;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
/// Places `T` into LVGL memory.
/// Places a sized `T` into LVGL memory.
///
/// This is useful for cases when we need to allocate memory on Rust side
/// and handover the management of that memory to LVGL. May also be used in cases we
/// want to use dynamic memory in the Rust side.
pub(crate) struct Box<T>(NonNull<T>);
impl<T> Box<T> {
pub fn new(inner: T) -> LvResult<Box<T>> {
/// Allocate memory using LVGL memory API and place `T` in the LVGL tracked memory.
pub fn new(value: T) -> LvResult<Box<T>> {
let size = mem::size_of::<T>();
let inner = unsafe {
let ptr = lvgl_sys::lv_mem_alloc(size as lvgl_sys::size_t) as *mut T;
@ -19,17 +24,14 @@ impl<T> Box<T> {
"Memory address not aligned!"
);
match NonNull::new(ptr) {
Some(v) => {
// Move `T` to LVGL managed memory
// It will panic if LVGL memory is not aligned
ptr.write(inner);
Ok(v)
}
None => Err(LvError::LvOOMemory),
}
NonNull::new(ptr)
.map(|p| {
p.as_ptr().write(value);
p
})
.ok_or(LvError::LvOOMemory)?
};
Ok(Box(inner?))
Ok(Box(inner))
}
pub fn into_raw(self) -> *mut T {
@ -71,6 +73,7 @@ mod test {
use super::*;
use core::mem::MaybeUninit;
use std::sync::Once;
use std::vec::Vec;
static INIT_LVGL: Once = Once::new();
@ -105,6 +108,10 @@ mod test {
disp: i32,
}
print_mem_info();
let total_mem_available_initially = get_mem_info().free_size;
let mut keep = Vec::new();
for i in 0..100 {
let p = Point {
x: i,
@ -128,17 +135,35 @@ mod test {
println!("{:?}", point);
}
assert_eq!(point.x, i);
print_mem_info();
keep.push(b);
}
drop(keep);
print_mem_info();
unsafe {
lvgl_sys::lv_mem_defrag();
}
print_mem_info();
// If this fails, we are leaking memory! BOOM! \o/
assert_eq!(total_mem_available_initially, get_mem_info().free_size)
}
fn print_mem_info() {
let mut info = MaybeUninit::uninit();
fn get_mem_info() -> lvgl_sys::lv_mem_monitor_t {
let mut info: MaybeUninit<lvgl_sys::lv_mem_monitor_t> = MaybeUninit::uninit();
unsafe {
lvgl_sys::lv_mem_monitor(info.as_mut_ptr());
}
if !info.as_ptr().is_null() {
let info = unsafe { info.assume_init() };
println!("mem info: {:?}", info);
unsafe { info.assume_init() }
} else {
panic!("Could not get memory info from LVGL! :(");
}
}
fn print_mem_info() {
println!("mem info: {:?}", get_mem_info());
}
}

View file

@ -43,7 +43,10 @@ where
C: PixelColor + From<Color>,
{
pub fn init() -> LvResult<Self> {
if !LVGL_IN_USE.compare_and_swap(false, true, Ordering::SeqCst) {
if LVGL_IN_USE
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
unsafe {
lvgl_sys::lv_init();
}