remove Window, fix examples, add logging

This commit is contained in:
Vinzenz Schroeter 2024-05-11 14:41:09 +02:00
parent 40de106f46
commit 27f891cd92
17 changed files with 707 additions and 61 deletions

View file

@ -1,4 +1,4 @@
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ByteGrid {
pub width: usize,
pub height: usize,

View file

@ -1,14 +1,17 @@
use crate::{BitVec, ByteGrid, CommandCode, Header, Packet, PixelGrid, TILE_SIZE};
/// A window
#[derive(Debug, Copy, Clone)]
pub struct Window(pub Origin, pub Size);
use crate::{BitVec, ByteGrid, Header, Packet, PixelGrid, TILE_SIZE};
use crate::command_codes::CommandCode;
/// An origin marks the top left position of the
/// data sent to the display.
#[derive(Debug, Clone, Copy)]
pub struct Origin(pub u16, pub u16);
impl Origin {
pub fn top_left() -> Self {
Self(0, 0)
}
}
/// Size defines the width and height of a window
#[derive(Debug, Clone, Copy)]
pub struct Size(pub u16, pub u16);
@ -38,8 +41,9 @@ fn offset_and_payload(command: CommandCode, offset: Offset, payload: Vec<u8>) ->
Packet(Header(command.to_primitive(), offset, payload.len() as u16, 0, 0), payload)
}
fn window_and_payload(command: CommandCode, window: Window, payload: Vec<u8>) -> Packet {
let Window(Origin(x, y), Size(w, h)) = window;
fn origin_size_payload(command: CommandCode, origin: Origin, size: Size, payload: Vec<u8>) -> Packet {
let Origin(x, y) = origin;
let Size(w, h) = size;
Packet(Header(command.to_primitive(), x, y, w, h), payload.into())
}
@ -57,9 +61,10 @@ impl Into<Packet> for Command {
Command::BitmapLegacy => command_code_only(CommandCode::BitmapLegacy),
Command::CharBrightness(origin, grid) => {
window_and_payload(CommandCode::CharBrightness,
Window(origin, Size(grid.width as u16, grid.height as u16)),
grid.into())
origin_size_payload(CommandCode::CharBrightness,
origin,
Size(grid.width as u16, grid.height as u16),
grid.into())
}
Command::Brightness(brightness) => {
Packet(Header(CommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness))
@ -89,9 +94,10 @@ impl Into<Packet> for Command {
offset_and_payload(CommandCode::BitmapLinearXor, offset, bits.into())
}
Command::Cp437Data(origin, grid) => {
window_and_payload(CommandCode::Cp437Data,
Window(origin, Size(grid.width as u16, grid.height as u16)),
grid.into())
origin_size_payload(CommandCode::Cp437Data,
origin,
Size(grid.width as u16, grid.height as u16),
grid.into())
}
}
}

View file

@ -1,4 +1,6 @@
use std::fmt::Debug;
use std::net::{ToSocketAddrs, UdpSocket};
use log::{debug, info};
use crate::Packet;
pub struct Connection {
@ -7,14 +9,16 @@ pub struct Connection {
impl Connection {
/// Open a new UDP socket and create a display instance
pub fn open(addr: impl ToSocketAddrs) -> std::io::Result<Self> {
pub fn open(addr: impl ToSocketAddrs + Debug) -> std::io::Result<Self> {
info!("connecting to {addr:?}");
let socket = UdpSocket::bind("0.0.0.0:0")?;
socket.connect(addr)?;
Ok(Self { socket })
}
/// Send a command to the display
pub fn send(&self, packet: impl Into<Packet>) -> std::io::Result<()> {
pub fn send(&self, packet: impl Into<Packet> + Debug) -> std::io::Result<()> {
debug!("sending {packet:?}");
let packet = packet.into();
let data: Vec<u8> = packet.into();
self.socket.send(&*data)?;

View file

@ -10,7 +10,7 @@ pub use crate::connection::Connection;
pub use crate::pixel_grid::PixelGrid;
pub use crate::bit_vec::BitVec;
pub use crate::packet::{Packet, Header, Payload};
pub use crate::command::{Command, Size, Origin, Window};
pub use crate::command::{Command, Size, Origin};
pub use crate::command_codes::CommandCode;
pub use crate::byte_grid::ByteGrid;