Integrate embeddedgraphics #2
1 changed files with 18 additions and 29 deletions
|
@ -4,34 +4,27 @@ use embedded_graphics;
|
|||
use embedded_graphics::prelude::*;
|
||||
use embedded_graphics::{drawable, DrawTarget};
|
||||
use core::mem::MaybeUninit;
|
||||
use embedded_graphics::pixelcolor::{PixelColor, raw::RawData, raw::RawU32};
|
||||
use embedded_graphics::pixelcolor::PixelColor;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
|
||||
struct DisplayDriver<F, C>
|
||||
struct DisplayDriver<'a, T, C>
|
||||
where
|
||||
F: FnMut(drawable::Pixel<C>),
|
||||
T: DrawTarget<C>,
|
||||
|
||||
C: PixelColor
|
||||
{
|
||||
pub raw: lvgl_sys::lv_disp_drv_t,
|
||||
callback: F,
|
||||
display_buffer: MaybeUninit<lvgl_sys::lv_disp_buf_t>,
|
||||
refresh_buffer: [MaybeUninit<lvgl_sys::lv_color_t>; lvgl_sys::LV_HOR_RES_MAX as usize * 10],
|
||||
phantom: PhantomData<C>,
|
||||
phantom: &'a PhantomData<T>,
|
||||
phantom1: PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<F, C> DisplayDriver<F, C> where
|
||||
F: FnMut(drawable::Pixel<C>),
|
||||
C: PixelColor {
|
||||
|
||||
pub fn new<T>(mut device: T) -> Self
|
||||
where
|
||||
T: DrawTarget<C>
|
||||
{
|
||||
let mut callback = move |pixel: drawable::Pixel<C>| {
|
||||
let _ = device.draw_pixel(pixel);
|
||||
};
|
||||
|
||||
impl<'a, T, C> DisplayDriver<'a, T, C> where
|
||||
T: DrawTarget<C>,
|
||||
C: PixelColor
|
||||
{
|
||||
pub fn new(device: &'a mut T) -> Self {
|
||||
// Create a display buffer for LittlevGL
|
||||
let mut display_buffer = MaybeUninit::<lvgl_sys::lv_disp_buf_t>::uninit();
|
||||
// Declare a buffer for 10 lines
|
||||
|
@ -53,8 +46,8 @@ impl<F, C> DisplayDriver<F, C> where
|
|||
// Basic initialization
|
||||
lvgl_sys::lv_disp_drv_init(&mut disp_drv);
|
||||
// Set your driver function
|
||||
disp_drv.flush_cb = Some(display_callback_wrapper::<F, C>);
|
||||
disp_drv.user_data = &mut callback as *mut _ as *mut cty::c_void;
|
||||
disp_drv.flush_cb = Some(display_callback_wrapper::<T, C>);
|
||||
disp_drv.user_data = device as *mut _ as *mut cty::c_void;
|
||||
disp_drv
|
||||
};
|
||||
// Assign the buffer to the display
|
||||
|
@ -65,10 +58,10 @@ impl<F, C> DisplayDriver<F, C> where
|
|||
}
|
||||
Self {
|
||||
raw: disp_drv,
|
||||
callback,
|
||||
display_buffer,
|
||||
refresh_buffer,
|
||||
phantom: PhantomData,
|
||||
phantom: &PhantomData,
|
||||
phantom1: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,12 +70,12 @@ impl<F, C> DisplayDriver<F, C> where
|
|||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn display_callback_wrapper<F, C>(
|
||||
unsafe extern "C" fn display_callback_wrapper<T, C>(
|
||||
disp_drv: *mut lvgl_sys::lv_disp_drv_t,
|
||||
area: *const lvgl_sys::lv_area_t,
|
||||
color_p: *mut lvgl_sys::lv_color_t,
|
||||
) where
|
||||
F: FnMut(drawable::Pixel<C>),
|
||||
T: DrawTarget<C>,
|
||||
C: PixelColor
|
||||
{
|
||||
// We need to make sure panics can't escape across the FFI boundary.
|
||||
|
@ -91,19 +84,15 @@ unsafe extern "C" fn display_callback_wrapper<F, C>(
|
|||
let disp = *disp_drv;
|
||||
|
||||
// Rust code closure reference
|
||||
let closure = &mut *(disp.user_data as *mut F);
|
||||
let device = &mut *(disp.user_data as *mut T);
|
||||
|
||||
for y in (*area).y1..=(*area).y2 {
|
||||
I just could not figure out to make it more generic because of this line. We need a concrete type to instantiate the color. I just could not figure out to make it more generic because of this line. We need a concrete type to instantiate the color.
|
||||
for x in (*area).x1..=(*area).x2 {
|
||||
// Convert C color representation to high-level Rust
|
||||
let raw_color = *color_p.add(i);
|
||||
// let color = Rgb888::new(raw_color.ch.red,
|
||||
// raw_color.ch.green,
|
||||
// raw_color.ch.blue);
|
||||
let color = C::Raw::from_u32(raw_color.full);
|
||||
i = i + 1;
|
||||
// Callback the Rust closure to flush the new points to the screen
|
||||
closure(drawable::Pixel(Point::new(x as i32, y as i32), color));
|
||||
let _ = device.draw_pixel(drawable::Pixel(Point::new(x as i32, y as i32), raw_color.into()));
|
||||
}
|
||||
}
|
||||
// Indicate to LittlevGL that you are ready with the flushing
|
||||
|
|
Loading…
Reference in a new issue
Would be nice to have this type more generic. Not all embedded systems will support that.