Remove some UB

This commit is contained in:
Rafael Caricio 2020-06-02 19:59:41 +02:00
parent 11695e20c1
commit 63e33d052f
9 changed files with 47 additions and 49 deletions

View file

@ -28,7 +28,7 @@ fn main() -> Result<(), String> {
// Create screen and widgets // Create screen and widgets
let mut screen = ui.scr_act(); let mut screen = ui.scr_act();
let mut screen_style = Style::new(); let mut screen_style = Style::default();
screen_style.set_body_main_color(Color::from_rgb((255, 255, 255))); screen_style.set_body_main_color(Color::from_rgb((255, 255, 255)));
screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255))); screen_style.set_body_grad_color(Color::from_rgb((255, 255, 255)));
screen_style.set_body_radius(0); screen_style.set_body_radius(0);
@ -42,13 +42,13 @@ fn main() -> Result<(), String> {
bar.set_value(0, Animation::OFF); 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::new(); let mut ind_style = Style::default();
ind_style.set_body_main_color(Color::from_rgb((100, 245, 0))); ind_style.set_body_main_color(Color::from_rgb((100, 245, 0)));
ind_style.set_body_grad_color(Color::from_rgb((100, 245, 0))); ind_style.set_body_grad_color(Color::from_rgb((100, 245, 0)));
bar.set_bar_style(BarComponent::Indicator, ind_style); bar.set_bar_style(BarComponent::Indicator, ind_style);
// Set the background style for the bar object // Set the background style for the bar object
let mut bg_style = Style::new(); let mut bg_style = Style::default();
bg_style.set_body_grad_color(Color::from_rgb((255, 255, 255))); 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_main_color(Color::from_rgb((255, 255, 255)));
bg_style.set_body_radius(0); bg_style.set_body_radius(0);
@ -61,7 +61,7 @@ fn main() -> Result<(), String> {
loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10); loading_lbl.set_align(&mut bar, Align::OutTopMid, 0, -10);
loading_lbl.set_label_align(LabelAlign::Center); loading_lbl.set_label_align(LabelAlign::Center);
let mut loading_style = Style::new(); let mut loading_style = Style::default();
loading_style.set_text_color(Color::from_rgb((0, 0, 0))); loading_style.set_text_color(Color::from_rgb((0, 0, 0)));
loading_lbl.set_style(loading_style); loading_lbl.set_style(loading_style);

View file

@ -3,8 +3,8 @@ use embedded_graphics::prelude::*;
use embedded_graphics_simulator::{ use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
}; };
use lvgl::widgets::{Bar, Button, Label}; use lvgl::widgets::{Button, Label};
use lvgl::{self, Align, Animation, Color, DisplayDriver, Event, NativeObject, Object, Style, UI}; use lvgl::{self, Align, Color, DisplayDriver, Event, 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;
@ -28,7 +28,7 @@ fn main() -> Result<(), String> {
// Create screen and widgets // Create screen and widgets
let mut screen = ui.scr_act(); let mut screen = ui.scr_act();
let mut screen_style = Style::new(); let mut screen_style = Style::default();
screen_style.set_body_main_color(Color::from_rgb((0, 0, 0))); screen_style.set_body_main_color(Color::from_rgb((0, 0, 0)));
screen_style.set_body_grad_color(Color::from_rgb((0, 0, 0))); screen_style.set_body_grad_color(Color::from_rgb((0, 0, 0)));
screen_style.set_body_radius(0); screen_style.set_body_radius(0);

View file

@ -5,7 +5,7 @@ use embedded_graphics_simulator::{
}; };
use lvgl; use lvgl;
use lvgl::widgets::{Label, LabelAlign}; use lvgl::widgets::{Label, LabelAlign};
use lvgl::{Object, UI}; use lvgl::{Align, Color, 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;
@ -32,24 +32,24 @@ fn main() -> Result<(), String> {
let font_roboto_28 = unsafe { &lvgl_sys::lv_font_roboto_28 }; let font_roboto_28 = unsafe { &lvgl_sys::lv_font_roboto_28 };
let font_noto_sans_numeric_28 = unsafe { &noto_sans_numeric_80 }; let font_noto_sans_numeric_28 = unsafe { &noto_sans_numeric_80 };
let mut screen_style = lvgl::Style::new(); let mut screen_style = Default::default();
screen_style.set_body_main_color(lvgl::Color::from_rgb((0, 0, 0))); screen_style.set_body_main_color(Color::from_rgb((0, 0, 0)));
screen_style.set_body_grad_color(lvgl::Color::from_rgb((0, 0, 0))); screen_style.set_body_grad_color(Color::from_rgb((0, 0, 0)));
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 = Label::new(&mut screen); let mut time = Label::new(&mut screen);
let mut style_time = lvgl::Style::new(); let mut style_time = Default::default();
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)));
time.set_style(style_time); time.set_style(style_time);
time.set_align(&mut screen, lvgl::Align::InLeftMid, 20, 0); time.set_align(&mut screen, Align::InLeftMid, 20, 0);
time.set_text("20:46"); time.set_text("20:46");
time.set_width(240); time.set_width(240);
time.set_height(240); time.set_height(240);
let mut bt = Label::new(&mut screen); let mut bt = Label::new(&mut screen);
let mut style_bt = lvgl::Style::new(); let mut style_bt = Default::default();
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();
bt.set_style(style_bt); bt.set_style(style_bt);
@ -58,7 +58,7 @@ fn main() -> Result<(), String> {
bt.set_recolor(true); bt.set_recolor(true);
bt.set_text("#5794f2 \u{F293}#"); bt.set_text("#5794f2 \u{F293}#");
bt.set_label_align(LabelAlign::Left); bt.set_label_align(LabelAlign::Left);
bt.set_align(&mut screen, lvgl::Align::InTopLeft, 0, 0); bt.set_align(&mut screen, Align::InTopLeft, 0, 0);
let mut power = Label::new(&mut screen); let mut power = Label::new(&mut screen);
power.set_style(style_power); power.set_style(style_power);
@ -67,7 +67,7 @@ fn main() -> Result<(), String> {
power.set_height(20); power.set_height(20);
power.set_text("#fade2a 20%#"); power.set_text("#fade2a 20%#");
power.set_label_align(LabelAlign::Right); power.set_label_align(LabelAlign::Right);
power.set_align(&mut screen, lvgl::Align::InTopRight, 0, 0); power.set_align(&mut screen, Align::InTopRight, 0, 0);
let threaded_ui = Arc::new(Mutex::new(ui)); let threaded_ui = Arc::new(Mutex::new(ui));

View file

@ -1,4 +1,3 @@
use bindgen;
use cc::Build; use cc::Build;
use std::{env, path::Path, path::PathBuf}; use std::{env, path::Path, path::PathBuf};
@ -13,13 +12,12 @@ fn main() {
let vendor_src = vendor.join("lvgl").join("src"); let vendor_src = vendor.join("lvgl").join("src");
let lv_config_dir = { let lv_config_dir = {
let raw_path = env::var(CONFIG_NAME).expect( let raw_path = env::var(CONFIG_NAME).unwrap_or_else(|_| {
format!( panic!(
"The environment variable {} is required to be defined", "The environment variable {} is required to be defined",
CONFIG_NAME CONFIG_NAME
)
.as_str(),
); );
});
let conf_path = PathBuf::from(raw_path); let conf_path = PathBuf::from(raw_path);
if !conf_path.exists() { if !conf_path.exists() {
@ -80,7 +78,6 @@ fn main() {
.use_core() .use_core()
.rustfmt_bindings(true) .rustfmt_bindings(true)
.ctypes_prefix("cty") .ctypes_prefix("cty")
.raw_line("use cty;")
.clang_args(&cc_args) .clang_args(&cc_args)
.generate() .generate()
.expect("Unable to generate bindings") .expect("Unable to generate bindings")

View file

@ -2,6 +2,8 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::redundant_static_lifetimes)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

View file

@ -1,7 +1,6 @@
use crate::Color; use crate::Color;
use alloc::boxed::Box; use alloc::boxed::Box;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use embedded_graphics;
use embedded_graphics::prelude::*; use embedded_graphics::prelude::*;
use embedded_graphics::{drawable, DrawTarget}; use embedded_graphics::{drawable, DrawTarget};
@ -18,8 +17,7 @@ impl DisplayDriver {
{ {
let disp_drv = unsafe { let disp_drv = unsafe {
// Create a display buffer for LittlevGL // Create a display buffer for LittlevGL
let mut display_buffer = let mut display_buffer = MaybeUninit::<lvgl_sys::lv_disp_buf_t>::uninit();
Box::new(MaybeUninit::<lvgl_sys::lv_disp_buf_t>::uninit().assume_init());
// Declare a buffer for the refresh rate // Declare a buffer for the refresh rate
const REFRESH_BUFFER_LEN: usize = 2; const REFRESH_BUFFER_LEN: usize = 2;
@ -40,27 +38,23 @@ impl DisplayDriver {
// Initialize the display buffer // Initialize the display buffer
lvgl_sys::lv_disp_buf_init( lvgl_sys::lv_disp_buf_init(
display_buffer.as_mut(), display_buffer.as_mut_ptr(),
Box::into_raw(refresh_buffer1) as *mut cty::c_void, Box::into_raw(refresh_buffer1) as *mut cty::c_void,
Box::into_raw(refresh_buffer2) as *mut cty::c_void, Box::into_raw(refresh_buffer2) as *mut cty::c_void,
lvgl_sys::LV_HOR_RES_MAX * REFRESH_BUFFER_LEN as u32, lvgl_sys::LV_HOR_RES_MAX * REFRESH_BUFFER_LEN as u32,
); );
let display_buffer = Box::new(display_buffer.assume_init());
// Descriptor of a display driver // Basic initialization of the display driver
let mut disp_drv = MaybeUninit::<lvgl_sys::lv_disp_drv_t>::uninit().assume_init(); let mut disp_drv = MaybeUninit::<lvgl_sys::lv_disp_drv_t>::uninit();
lvgl_sys::lv_disp_drv_init(disp_drv.as_mut_ptr());
// Basic initialization let mut disp_drv = disp_drv.assume_init();
lvgl_sys::lv_disp_drv_init(&mut disp_drv);
// Assign the buffer to the display // Assign the buffer to the display
disp_drv.buffer = Box::into_raw(display_buffer); disp_drv.buffer = Box::into_raw(display_buffer);
// Set your driver function // Set your driver function
disp_drv.flush_cb = Some(display_callback_wrapper::<T, C>); disp_drv.flush_cb = Some(display_callback_wrapper::<T, C>);
// TODO: DrawHandler type here // TODO: DrawHandler type here
disp_drv.user_data = device as *mut _ as *mut cty::c_void; disp_drv.user_data = device as *mut _ as *mut cty::c_void;
disp_drv disp_drv
}; };
Self { raw: disp_drv } Self { raw: disp_drv }
@ -143,5 +137,5 @@ where
.flatten(); .flatten();
// TODO: Maybe find a way to use `draw_image` method on the device instance. // TODO: Maybe find a way to use `draw_image` method on the device instance.
Ok(display.draw_iter(pixels.into_iter())?) Ok(display.draw_iter(pixels)?)
} }

View file

@ -25,7 +25,7 @@ unsafe impl Send for UI {}
impl UI { impl UI {
pub fn init() -> Result<Self, LvError> { pub fn init() -> Result<Self, LvError> {
if LVGL_IN_USE.compare_and_swap(false, true, Ordering::SeqCst) == false { if !LVGL_IN_USE.compare_and_swap(false, true, Ordering::SeqCst) {
unsafe { unsafe {
lvgl_sys::lv_init(); lvgl_sys::lv_init();
} }

View file

@ -4,7 +4,6 @@ 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};
use lvgl_sys;
const PANIC_MESSAGE: &str = "Value was dropped by LittlevGL"; const PANIC_MESSAGE: &str = "Value was dropped by LittlevGL";
@ -33,6 +32,11 @@ impl NativeObject for GenericObject {
pub trait Object: NativeObject { pub trait Object: NativeObject {
type SpecialEvent; type SpecialEvent;
/// Construct an instance of the object from a raw pointer.
///
/// # Safety
/// Provided the LVGL library can allocate memory this should be safe.
///
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 set_pos(&mut self, x: i16, y: i16) { fn set_pos(&mut self, x: i16, y: i16) {
@ -236,15 +240,6 @@ pub struct Style {
} }
impl Style { impl Style {
pub fn new() -> Self {
let raw = unsafe {
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit();
lvgl_sys::lv_style_copy(native_style.as_mut_ptr(), &lvgl_sys::lv_style_pretty);
native_style.assume_init()
};
Self { raw }
}
/// Object's main background color /// Object's main background color
pub fn set_body_main_color(&mut self, color: Color) { pub fn set_body_main_color(&mut self, color: Color) {
self.raw.body.main_color = color.raw; self.raw.body.main_color = color.raw;
@ -311,6 +306,17 @@ impl Style {
} }
} }
impl Default for Style {
fn default() -> Self {
let raw = unsafe {
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit();
lvgl_sys::lv_style_copy(native_style.as_mut_ptr(), &lvgl_sys::lv_style_pretty);
native_style.assume_init()
};
Self { raw }
}
}
impl Clone for Style { impl Clone for Style {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit(); let mut native_style = mem::MaybeUninit::<lvgl_sys::lv_style_t>::uninit();

View file

@ -2,7 +2,6 @@ use crate::support::{Animation, GenericObject, NativeObject, Style};
use crate::Object; use crate::Object;
use alloc::boxed::Box; use alloc::boxed::Box;
use core::ptr; use core::ptr;
use lvgl_sys;
define_object!(Bar); define_object!(Bar);