diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index c0a55f5..26836ac 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -17,7 +17,7 @@ fn main() -> Result<(), String> { )); let output_settings = OutputSettingsBuilder::new().scale(4).build(); - let mut window = Window::new("Hello World", &output_settings); + let mut window = Window::new("PineTime", &output_settings); let mut ui = UI::init().unwrap(); @@ -42,7 +42,7 @@ fn main() -> Result<(), String> { style_time.set_text_color(lvgl::Color::from_rgb((255, 255, 255))); time.set_style(style_time); time.set_align(&mut screen, lvgl::Align::InLeftMid, 20, 0); - time.set_text("20:46\0"); + time.set_text("20:46"); time.set_width(240); time.set_height(240); @@ -54,7 +54,7 @@ fn main() -> Result<(), String> { bt.set_width(50); bt.set_height(80); bt.set_recolor(true); - bt.set_text("#5794f2 \u{F293}#\0"); + bt.set_text("#5794f2 \u{F293}#"); bt.set_label_align(lvgl::LabelAlign::Left); bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0); @@ -63,7 +63,7 @@ fn main() -> Result<(), String> { power.set_recolor(true); power.set_width(80); power.set_height(20); - power.set_text("#fade2a 20%#\0"); + power.set_text("#fade2a 20%#"); power.set_label_align(lvgl::LabelAlign::Right); power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0); @@ -86,7 +86,7 @@ fn main() -> Result<(), String> { if i > 59 { i = 0; } - time.set_text(format!("21:{:02}\0", i).as_str()); + time.set_text(format!("21:{:02}", i).as_str()); i = 1 + i; sleep(Duration::from_millis( @@ -112,6 +112,13 @@ fn main() -> Result<(), String> { } // Reference to native font for LittlevGL, defined in the file: "fonts_noto_sans_numeric_80.c" +// TODO: Create a macro for definiting a safe wrapper for fonts. +// Maybe sometihng like: +// +// font! { +// NotoSansNumeric80 = noto_sans_numeric_80; +// }; +// extern "C" { pub static mut noto_sans_numeric_80: lvgl_sys::lv_font_t; } diff --git a/lvgl/Cargo.toml b/lvgl/Cargo.toml index db865c1..2165e32 100644 --- a/lvgl/Cargo.toml +++ b/lvgl/Cargo.toml @@ -16,3 +16,4 @@ keywords = ["littlevgl", "lvgl", "graphical_interfaces"] lvgl-sys = {path="../lvgl-sys", version="0.1"} cty = "0.2" embedded-graphics = "0.6" +cstr_core = { version = "0.2", default-features = false, features = ["alloc"] } diff --git a/lvgl/src/global.rs b/lvgl/src/global.rs index d47b6f1..521af32 100644 --- a/lvgl/src/global.rs +++ b/lvgl/src/global.rs @@ -15,8 +15,6 @@ pub enum LvError { AlreadyInUse, } -type LvResult = Result; - pub struct UI { // LittlevGL is not thread-safe by default. _not_sync: PhantomData<*mut ()>, diff --git a/lvgl/src/support.rs b/lvgl/src/support.rs index de15044..bb545d2 100644 --- a/lvgl/src/support.rs +++ b/lvgl/src/support.rs @@ -1,7 +1,7 @@ use alloc::boxed::Box; use core::mem; use core::ptr; -use cty; +use cstr_core::CString; use embedded_graphics::pixelcolor::{Rgb565, Rgb888}; use lvgl_sys; @@ -170,11 +170,9 @@ impl Label { } 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() as *const cty::c_char, - ); + lvgl_sys::lv_label_set_text(self.core.raw().as_mut(), text.as_ptr()); } } @@ -234,10 +232,6 @@ impl Style { pub fn set_text_font(&mut self, font: &lvgl_sys::lv_font_t) { self.raw.text.font = font; } - - fn raw(&mut self) -> *const lvgl_sys::lv_style_t { - &mut self.raw - } } impl Clone for Style {