write image

This commit is contained in:
Empire 2023-05-25 21:27:13 +02:00
parent bb1551f2a8
commit 1c12173b77
2 changed files with 59 additions and 48 deletions

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bit = "0.1.1"
embedded-graphics = "0.8.0" embedded-graphics = "0.8.0"

View File

@ -1,37 +1,41 @@
use std::net::UdpSocket; use bit::BitIndex;
use std::env;
use embedded_graphics::{ use embedded_graphics::{
image::{Image, ImageRaw}, image::{Image, ImageRaw},
pixelcolor::{BinaryColor, Rgb565}, pixelcolor::{BinaryColor, Rgb565},
prelude::*, primitives::{PrimitiveStyle, Line}, prelude::*,
primitives::{Line, PrimitiveStyle},
}; };
use std::env;
use std::net::UdpSocket;
const IMAGE_SIZE_BYTE: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; /* one byte contains 8 LEDs, one in each bit */
const IMAGE_WIDTH: u32 = 5 * 32;
const IMAGE_WIDTH_BYTE: u32 = IMAGE_WIDTH / 8; /* one byte contains 8 LEDs, one in each bit */
const IMAGE_SIZE_BYTE : usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT) as usize; /* one byte contains 8 LEDs, one in each bit */ const IMAGE_HEIGHT: u32 = 40;
const IMAGE_WIDTH : u32 = 5*32; const IMAGE_HEIGHT_BYTE: u32 = 40;
const IMAGE_WIDTH_BYTE : u32 = IMAGE_WIDTH / 8; /* one byte contains 8 LEDs, one in each bit */ const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize;
const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) as usize;
const IMAGE_HEIGHT : u32 = 40; struct UdpDisplay<'a> {
const IMAGE_HEIGHT_BYTE : u32 = 40; image_slice: &'a mut[u8; IMAGE_SIZE_BYTE],
const IMAGE_LENGTH : usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize;
const PACKAGE_LENGTH : usize = (IMAGE_LENGTH + 1) as usize;
struct UdpDisplay {
imageSlice: [u8; IMAGE_SIZE_BYTE]
} }
impl OriginDimensions for UdpDisplay { impl OriginDimensions for UdpDisplay<'_> {
fn size(&self) -> Size { fn size(&self) -> Size {
Size::new(IMAGE_WIDTH, IMAGE_HEIGHT) Size::new(IMAGE_WIDTH, IMAGE_HEIGHT)
} }
} }
impl DrawTarget for UdpDisplay { impl DrawTarget for UdpDisplay<'_> {
type Color = BinaryColor; type Color = BinaryColor;
type Error = core::convert::Infallible; type Error = core::convert::Infallible;
fn fill_contiguous<I>(&mut self, area: &embedded_graphics::primitives::Rectangle, colors: I) -> Result<(), Self::Error> fn fill_contiguous<I>(
&mut self,
area: &embedded_graphics::primitives::Rectangle,
colors: I,
) -> Result<(), Self::Error>
where where
I: IntoIterator<Item = Self::Color>, I: IntoIterator<Item = Self::Color>,
{ {
@ -42,7 +46,11 @@ impl DrawTarget for UdpDisplay {
) )
} }
fn fill_solid(&mut self, area: &embedded_graphics::primitives::Rectangle, color: Self::Color) -> Result<(), Self::Error> { fn fill_solid(
&mut self,
area: &embedded_graphics::primitives::Rectangle,
color: Self::Color,
) -> Result<(), Self::Error> {
self.fill_contiguous(area, core::iter::repeat(color)) self.fill_contiguous(area, core::iter::repeat(color))
} }
@ -52,48 +60,50 @@ impl DrawTarget for UdpDisplay {
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error> fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where where
I: IntoIterator<Item = Pixel<Self::Color>> { I: IntoIterator<Item = Pixel<Self::Color>>,
todo!() {
for pixel in pixels {
let x = pixel.0.x as u32;
let y = pixel.0.y as u32;
let v = pixel.1.is_on();
let offset: usize = (x + y * IMAGE_WIDTH) as usize;
let subbit: usize = (offset % 8).into();
let byte_offset: usize = (offset / 8).into();
let current = &mut self.image_slice[byte_offset];
current.set_bit(subbit, v);
}
return Ok(());
} }
} }
fn send_package(ipaddress: String) { fn send_package(ipaddress: String) {
let mut package = [0; PACKAGE_LENGTH]; let mut package = [0; PACKAGE_LENGTH];
// Brightness
package[0] = 128; package[0] = 128;
let imageSlice: &mut[u8;IMAGE_SIZE_BYTE] = &mut package[1..PACKAGE_LENGTH].try_into().unwrap(); let image_slice: &mut [u8; IMAGE_SIZE_BYTE] =
&mut package[1..PACKAGE_LENGTH].try_into().unwrap();
let mut display = UdpDisplay { image_slice };
let mut display = UdpDisplay {
imageSlice
};
const IMAGE: &[u8] = &[0 as u8; ((IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize)];
let raw_image = ImageRaw::<BinaryColor>::new(IMAGE, IMAGE_WIDTH);
Line::new(Point::new(50, 20), Point::new(60, 35)) Line::new(Point::new(50, 20), Point::new(60, 35))
.into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1)) .into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
.draw(&mut raw_image)?; .draw(&mut display).unwrap();
// Brightness
imageSlice.copy_from_slice(&IMAGE);
// TODO convert RgbImage into image buffer:
// let pixel = img[(100, 100)];::
let socket = UdpSocket::bind("0.0.0.0:4242").expect("couldn't bind to address"); let socket = UdpSocket::bind("0.0.0.0:4242").expect("couldn't bind to address");
socket.send_to(&package, ipaddress + ":4242").expect("couldn't send data"); socket
.send_to(&package, ipaddress + ":4242")
.expect("couldn't send data");
println!("Packet sent"); println!("Packet sent");
} }
fn help() { fn help() {
println!("usage: println!(
LEDboardClient <ip address>"); "usage:
println!("one argument necessary!"); LEDboardClient <ip address>"
);
println!("one argument necessary!");
println!("<ip address>"); println!("<ip address>");
} }
@ -105,12 +115,12 @@ fn main() {
1 => { 1 => {
// show a help message // show a help message
help(); help();
}, }
// one argument passed // one argument passed
2 => { 2 => {
let ip = &args[1]; let ip = &args[1];
send_package(ip.to_string()); send_package(ip.to_string());
}, }
// all the other cases // all the other cases
_ => { _ => {
// show a help message // show a help message