Added embedded-graphics dependency

This commit is contained in:
c3ma 2023-05-24 21:12:18 +02:00
parent 43f193965c
commit bb1551f2a8
2 changed files with 84 additions and 14 deletions

View File

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

View File

@ -1,23 +1,92 @@
use std::net::UdpSocket;
use std::env;
use embedded_graphics::{
image::{Image, ImageRaw},
pixelcolor::{BinaryColor, Rgb565},
prelude::*, primitives::{PrimitiveStyle, Line},
};
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_HEIGHT : u32 = 40;
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]
}
impl OriginDimensions for UdpDisplay {
fn size(&self) -> Size {
Size::new(IMAGE_WIDTH, IMAGE_HEIGHT)
}
}
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>
where
I: IntoIterator<Item = Self::Color>,
{
self.draw_iter(
area.points()
.zip(colors)
.map(|(pos, color)| Pixel(pos, color)),
)
}
fn fill_solid(&mut self, area: &embedded_graphics::primitives::Rectangle, color: Self::Color) -> Result<(), Self::Error> {
self.fill_contiguous(area, core::iter::repeat(color))
}
fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
self.fill_solid(&self.bounding_box(), color)
}
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>> {
todo!()
}
}
const IMAGE_WIDTH_BYTE : usize = (5*32) / 8; /* one byte contains 8 lets, one in each bit */
const IMAGE_HEIGHT_BYTE : usize = 40;
fn send_package(ipaddress: String) {
let mut image = [0; (IMAGE_WIDTH_BYTE*IMAGE_HEIGHT_BYTE)+1];
// Brightnes
image[0] = 0x80;
// row 0, led 0
image[1] = 1;
// row 0, led 8
image[2] = 1;
// row 0, led 16 and 17
image[3] = 3;
// row 0, led 24, 25, 26
image[4] = 7;
let mut package = [0; PACKAGE_LENGTH];
package[0] = 128;
let imageSlice: &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);
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)];::
let socket = UdpSocket::bind("0.0.0.0:4242").expect("couldn't bind to address");
socket.send_to(&image, ipaddress + ":4242").expect("couldn't send data");
socket.send_to(&package, ipaddress + ":4242").expect("couldn't send data");
println!("Packet sent");
}