Skip to content
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

Fix typos in code for embedded_graphics crate in chapter 3 #1269

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
13 changes: 8 additions & 5 deletions blog/content/edition-3/posts/03-screen-output/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,24 @@ Fortunately, there is the nice `no_std`-compatible [`embedded-graphics`] crate,

```rust ,hl_lines=3
// in kernel/src/framebuffer.rs
use embedded_graphics::pixelcolor::Rgb888;

pub struct Display {
framebuffer: Framebuffer,
framebuffer: FrameBuffer,
}

impl Display {
pub fn new(framebuffer: Framebuffer) -> Display {
pub fn new(framebuffer: FrameBuffer) -> Display {
Self { framebuffer }
}

fn draw_pixel(&mut self, pixel: Pixel) {
fn draw_pixel(&mut self, Pixel(coordinates, color): Pixel<Rgb888>) {
// ignore any pixels that are out of bounds.
let (width, height) = {
let info = self.framebuffer.info();
(info.width, info.height)
}

if let Ok((x @ 0..width, y @ 0..height)) = coordinates.try_into() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still won't compile on account of the fact that the Point type apparently doesn't implement Into<(usize, usize)>. Attempting to change the type just results in more issues because the range values must be of the same integer type and have to be constants. The checks could of course be moved down but that would look much worse

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should also note that this syntax requires you to enable the experimental exclusive_range_pattern feature in order for it to be valid in the first place

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is already a good improvement, so let's merge this. I try my best to continue working on finishing the post soon.

let color = Color { red: color.r(), green: color.g(), blue: color.b()};
set_pixel_in(&mut self.framebuffer, Position { x, y }, color);
Expand All @@ -336,7 +338,7 @@ impl Display {
}

impl embedded_graphics::draw_target::DrawTarget for Display {
type Color = embedded_graphics::pixelcolor::Rgb888;
type Color = Rgb888;

/// Drawing operations can never fail.
type Error = core::convert::Infallible;
Expand All @@ -345,9 +347,10 @@ impl embedded_graphics::draw_target::DrawTarget for Display {
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for Pixel(coordinates, color) in pixels.into_iter() {
for pixel in pixels.into_iter() {
self.draw_pixel(pixel);
}

Ok(())
}
}
Expand Down
Loading