diff --git a/Cargo.lock b/Cargo.lock index 2e82da8f0013332cb72bf78b88bdae71099d65cd..c585c61db207c673087496cc5ef5889fee7f77d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,6 +99,7 @@ name = "card10-l0dable" version = "0.1.1" dependencies = [ "card10-sys 0.1.0", + "embedded-graphics 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -161,6 +162,20 @@ dependencies = [ "volatile-register 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "draw-image" +version = "0.0.0" +dependencies = [ + "card10-l0dable 0.1.1", + "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-graphics 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "embedded-graphics" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "env_logger" version = "0.6.2" @@ -442,6 +457,7 @@ dependencies = [ "checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cortex-m 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3df5de9a9829f2ccb7defa8945fa020c6614cd2f6ba9b5f33db9241dcc01985e" +"checksum embedded-graphics 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "704679bfcb66cc748ec185ae62ccd5b03311a53793372e2abf1ff9a7e9875ff7" "checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" diff --git a/Cargo.toml b/Cargo.toml index 04452aca4443b62859cf007943fba31377ebfaf8..7b1b7a0378038db1a435d04dbdbbb39fbaf75b24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,11 +5,13 @@ members = [ "card10-l0dable", "example", "rkanoid", + "draw-image", ] default-members = [ "example", "rkanoid", + "draw-image", ] [profile.release] diff --git a/README.md b/README.md index 6896bb875245b1372ca311244292397517108228..bc8f8078610db724fbf80475e2f5c0ab89c28811 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ extension (e.g `example` must be renamed as `example.elf`). | card10-l0dable | High-level crate for building l0dables | | example | l0dable example | | rkanoid | Arkanoid clone | +| draw-image | Example of drawing a static image to the display | ## Misc diff --git a/card10-l0dable/Cargo.toml b/card10-l0dable/Cargo.toml index 96880ced0aa0403563f51380d63f891871e40e49..d6c27f2fd8585e03c36875b9a6e4e09446e04b10 100644 --- a/card10-l0dable/Cargo.toml +++ b/card10-l0dable/Cargo.toml @@ -18,4 +18,5 @@ description = "make l0dables for the Card10 (CCCamp 2019) badge" [dependencies] card10-sys = { path = "../card10-sys", version = "^0.1" } +embedded-graphics = "0.5.2" diff --git a/card10-l0dable/src/framebuffer/mod.rs b/card10-l0dable/src/framebuffer/mod.rs index 7d1d07d93800a3dd1d30eac1368e79235281162a..56faa3b24dbc7c2bb94bb4fd4e6ef1429da75a00 100644 --- a/card10-l0dable/src/framebuffer/mod.rs +++ b/card10-l0dable/src/framebuffer/mod.rs @@ -3,6 +3,10 @@ use card10_sys::*; use core::mem::{transmute, uninitialized}; use core::ops::{Index, IndexMut}; +use embedded_graphics::pixelcolor::Rgb565; +use embedded_graphics::prelude::Pixel; +use embedded_graphics::Drawing; + mod font; pub use font::*; mod text; @@ -71,6 +75,23 @@ impl<'d> IndexMut<(u16, u16)> for FrameBuffer<'d> { } } +impl<'d> Drawing<Rgb565> for FrameBuffer<'d> { + fn draw<T>(&mut self, item: T) + where + T: IntoIterator<Item = Pixel<Rgb565>>, + { + for Pixel(coord, color) in item { + let x = coord[0] as u16; + let y = coord[1] as u16; + + if x >= Display::W || y >= Display::H { + continue; + } + self[(x, y)] = RawColor::rgb8(color.r(), color.g(), color.b()); + } + } +} + #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct RawColor([u8; 2]); diff --git a/card10-l0dable/src/framebuffer/text.rs b/card10-l0dable/src/framebuffer/text.rs index 118e7754d8dbd8dfa4e06a6bb5563405c9d06ca9..cb30d1f451a886a970b29b7cd29e966f6e0faaa1 100644 --- a/card10-l0dable/src/framebuffer/text.rs +++ b/card10-l0dable/src/framebuffer/text.rs @@ -1,6 +1,6 @@ +use super::{Font, FrameBuffer, RawColor}; +use crate::Display; use core::fmt::Write; -use super::{FrameBuffer, Font, RawColor}; -use crate::{Display}; pub struct TextRenderer<'a, 'd, 'f> { pub framebuffer: &'a mut FrameBuffer<'d>, diff --git a/draw-image/Cargo.toml b/draw-image/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..c7423842586732a67e4ef9efdb98693f4223194e --- /dev/null +++ b/draw-image/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "draw-image" +version = "0.0.0" +authors = ["Rafael Caricio <crates.rs@caric.io>"] +edition = "2018" + +[dependencies] +card10-l0dable = { path = "../card10-l0dable" } +embedded-graphics = { version = "0.5.2" } + +[build-dependencies] +cc = "1.0" + +[[bin]] +name = "draw-image" +path = "src/main.rs" diff --git a/draw-image/README.md b/draw-image/README.md new file mode 100644 index 0000000000000000000000000000000000000000..65f12dbcfd53da9a12893b735a53a9ae24b37e39 --- /dev/null +++ b/draw-image/README.md @@ -0,0 +1,16 @@ +Draw Image +========== + +Images need to be converted to a bitmap of 16BPP for inclusion with `include_bytes!()`. You can convert an image using: + +``` +convert image.png -flip -type truecolor -define bmp:subtype=RGB565 -depth 16 -strip image.bmp +``` + +then + +``` +tail -c $bytes image.bmp > image.raw // where $bytes is w * h * 2 +``` + +This will remove the BMP header leaving the raw pixel data E.g 160x80 image will have 160 * 80 * 2 bytes of raw data. \ No newline at end of file diff --git a/draw-image/src/applewatch-160x80.raw b/draw-image/src/applewatch-160x80.raw new file mode 100644 index 0000000000000000000000000000000000000000..824710533766f2ce8eba4b8fabf5432f0e06b006 Binary files /dev/null and b/draw-image/src/applewatch-160x80.raw differ diff --git a/draw-image/src/main.rs b/draw-image/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..5b377cf2334aa80069cc55451251ab30d1a5426a --- /dev/null +++ b/draw-image/src/main.rs @@ -0,0 +1,26 @@ +#![no_std] +#![no_main] + +use card10_l0dable::*; +use embedded_graphics::prelude::*; + +use embedded_graphics::image::Image16BPP; +use embedded_graphics::pixelcolor::Rgb565; + +main!(main); +fn main() { + let display = Display::open(); + let mut framebuffer = display.framebuffer(); + + let image: Image16BPP<Rgb565> = + Image16BPP::new(include_bytes!("applewatch-160x80.raw"), 160, 80); + framebuffer.draw(&image); + framebuffer.send(); + + loop { + let buttons = Buttons::read(); + if buttons.left_top() { + break; + } + } +}