Skip to content

fix: use correct display coordinates #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -49,23 +49,16 @@
//! .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]

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<u8>) -> 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.
Expand All @@ -77,10 +70,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);
Expand Down Expand Up @@ -119,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] =
Expand All @@ -131,7 +129,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(),
);
Expand Down