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
[dependencies]
bit = "0.1.1"
embedded-graphics = "0.8.0"

View File

@ -1,11 +1,12 @@
use std::net::UdpSocket;
use std::env;
use bit::BitIndex;
use embedded_graphics::{
image::{Image, ImageRaw},
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;
@ -16,22 +17,25 @@ const IMAGE_HEIGHT_BYTE : u32 = 40;
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]
struct UdpDisplay<'a> {
image_slice: &'a mut[u8; IMAGE_SIZE_BYTE],
}
impl OriginDimensions for UdpDisplay {
impl OriginDimensions for UdpDisplay<'_> {
fn size(&self) -> Size {
Size::new(IMAGE_WIDTH, IMAGE_HEIGHT)
}
}
impl DrawTarget for UdpDisplay {
impl DrawTarget for UdpDisplay<'_> {
type Color = BinaryColor;
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
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))
}
@ -52,47 +60,49 @@ impl DrawTarget for UdpDisplay {
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>> {
todo!()
}
I: IntoIterator<Item = Pixel<Self::Color>>,
{
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) {
let mut package = [0; PACKAGE_LENGTH];
// Brightness
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 {
imageSlice
};
const IMAGE: &[u8] = &[0 as u8; ((IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize)];
let raw_image = ImageRaw::<BinaryColor>::new(IMAGE, IMAGE_WIDTH);
let mut display = UdpDisplay { image_slice };
Line::new(Point::new(50, 20), Point::new(60, 35))
.into_styled(PrimitiveStyle::with_stroke(BinaryColor::On, 1))
.draw(&mut raw_image)?;
// Brightness
imageSlice.copy_from_slice(&IMAGE);
// TODO convert RgbImage into image buffer:
// let pixel = img[(100, 100)];::
.draw(&mut display).unwrap();
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");
}
fn help() {
println!("usage:
LEDboardClient <ip address>");
println!(
"usage:
LEDboardClient <ip address>"
);
println!("one argument necessary!");
println!("<ip address>");
}
@ -105,12 +115,12 @@ fn main() {
1 => {
// show a help message
help();
},
}
// one argument passed
2 => {
let ip = &args[1];
send_package(ip.to_string());
},
}
// all the other cases
_ => {
// show a help message