From d28e9e48d4c1ff6124359498fb941088e37bf25d Mon Sep 17 00:00:00 2001 From: zabackary <137591653+zabackary@users.noreply.github.com> Date: Fri, 18 Apr 2025 18:32:30 +0900 Subject: [PATCH 1/2] fix: use correct display coordinates --- src/lib.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 20dae43..0640367 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,44 +1,44 @@ //! [`embedded-graphics`] Driver for the VEX V5 -//! +//! //! [`embedded-graphics`]: https://crates.io/crates/embedded-graphics -//! +//! //! This crate provides a [`DrawTarget`] implementation for the VEX V5 brain display, //! allowing you to draw to the display using the `embedded-graphics` ecosystem. -//! +//! //! # Usage -//! +//! //! To begin, turn your `display` peripheral into a [`DisplayDriver`]: -//! +//! //! ``` //! #![no_std] //! #![no_main] -//! +//! //! use vexide::prelude::*; //! use vexide_embedded_graphics::DisplayDriver; -//! +//! //! #[vexide::main] //! async fn main(peripherals: Peripherals) { //! let mut display = DisplayDriver::new(peripherals.display); //! } //! ``` -//! +//! //! [`DisplayDriver`] is a [`DrawTarget`] that the `embedded-graphics` crate is //! able to draw to. -//! +//! //! ``` //! #![no_std] //! #![no_main] -//! +//! //! use vexide::prelude::*; //! use vexide_embedded_graphics::DisplayDriver; -//! +//! //! use embedded_graphics::{ //! mono_font::{ascii::FONT_6X10, MonoTextStyle}, //! pixelcolor::Rgb888, //! prelude::*, //! text::Text, //! }; -//! +//! //! #[vexide::main] //! async fn main(peripherals: Peripherals) { //! let mut display = DisplayDriver::new(peripherals.display); @@ -49,9 +49,9 @@ //! .unwrap(); //! } //! ``` -//! +//! //! Check out the [`embedded-graphics` docs] for more examples. -//! +//! //! [`embedded-graphics` docs]: https://docs.rs/embedded-graphics/latest/embedded_graphics/examples/index.html #![no_std] @@ -77,10 +77,15 @@ pub struct DisplayDriver { impl DisplayDriver { /// Create a new [`DisplayDriver`] from a [`Display`]. - /// + /// /// The display peripheral must be moved into this struct, /// as it is used to render the display and having multiple /// mutable references to it is unsafe. + /// + /// It is recommended to use a frame buffer like [`embedded_graphics_framebuf`] + /// in order to reduce flickering. + /// + /// [`embedded_graphics_framebuf`]: https://crates.io/crates/embedded-graphics-framebuf #[must_use] pub fn new(mut display: Display) -> Self { display.set_render_mode(vexide::devices::display::RenderMode::DoubleBuffered); @@ -131,7 +136,7 @@ impl DrawTarget for DisplayDriver { 0, 0x20, Display::HORIZONTAL_RESOLUTION.into(), - Display::VERTICAL_RESOLUTION.into(), + 0x20 + i32::from(Display::VERTICAL_RESOLUTION), self.triple_buffer.as_mut_ptr(), Display::HORIZONTAL_RESOLUTION.into(), ); From fdcc83980355ad94bb2f38db1585e7c437a31421 Mon Sep 17 00:00:00 2001 From: zabackary <137591653+zabackary@users.noreply.github.com> Date: Fri, 18 Apr 2025 18:35:50 +0900 Subject: [PATCH 2/2] perf: use `into_storage` instead of another fn it's probably being inlined anyway but using `into_storage` removes a layer of unnecessary indirection. --- src/lib.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0640367..ae0efb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,14 +58,7 @@ use core::convert::Infallible; use embedded_graphics_core::{pixelcolor::Rgb888, prelude::*}; -use vexide::devices::{ - display::{Display, TouchEvent}, - rgb::Rgb, -}; - -fn rgb_into_raw(rgb: Rgb) -> u32 { - (u32::from(rgb.r) << 16) + (u32::from(rgb.g) << 8) + u32::from(rgb.b) -} +use vexide::devices::display::{Display, TouchEvent}; /// An embedded-graphics draw target for the V5 Brain display /// Currently, this does not support touch detection like the regular [`Display`] API. @@ -124,7 +117,7 @@ impl DrawTarget for DisplayDriver { { pixels .into_iter() - .map(|p| (p.0, rgb_into_raw(Rgb::new(p.1.r(), p.1.g(), p.1.b())))) + .map(|p| (p.0, p.1.into_storage())) .for_each(|(pos, col)| { self.triple_buffer [pos.y as usize * Display::HORIZONTAL_RESOLUTION as usize + pos.x as usize] =