remove shared code, fix bugs

This commit is contained in:
Vinzenz Schroeter 2024-05-10 12:36:42 +02:00
parent acbe9de219
commit 05c7b4cb71
8 changed files with 119 additions and 37 deletions

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "pixel-shared-rs"]
path = pixel-shared-rs
url = https://github.com/kaesaecracker/pixel-shared-rs.git

17
Cargo.lock generated
View file

@ -1281,9 +1281,9 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]]
name = "num"
version = "0.4.2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3135b08af27d103b0a51f2ae0f8632117b7b185ccf931445affa8df530576a41"
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
dependencies = [
"num-bigint",
"num-complex",
@ -1561,19 +1561,12 @@ dependencies = [
"env_logger",
"image",
"log",
"pixel-shared-rs",
"pixels",
"raw-window-handle 0.6.1",
"winit",
]
[[package]]
name = "pixel-shared-rs"
version = "0.1.0"
dependencies = [
"num",
"num-derive",
"num-traits",
"pixels",
"raw-window-handle 0.6.1",
"winit",
]
[[package]]

View file

@ -12,4 +12,6 @@ winit = { version = "0.30", features = ["rwh_05"] }
log = "0.4"
raw-window-handle = "0.6"
env_logger = "0.11"
pixel-shared-rs = { path = "pixel-shared-rs" }
num = "0.4"
num-derive = "0.4"
num-traits = "0.2"

@ -1 +0,0 @@
Subproject commit 6e46694d877faa61647fae104afe6603c1718a06

View file

@ -1,6 +1,6 @@
use crate::protocol::{PIXEL_HEIGHT, PIXEL_WIDTH};
use crate::DISPLAY;
use log::{debug, warn};
use pixel_shared_rs::{PIXEL_HEIGHT, PIXEL_WIDTH};
use log::{trace, warn};
use pixels::wgpu::TextureFormat;
use pixels::{Pixels, PixelsBuilder, SurfaceTexture};
use winit::application::ApplicationHandler;
@ -40,7 +40,7 @@ impl ApplicationHandler for App {
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
debug!("event {:?}", event);
trace!("event {:?}", event);
match event {
WindowEvent::CloseRequested => {
warn!("The close button was pressed; stopping");
@ -64,8 +64,6 @@ impl ApplicationHandler for App {
i += 1;
}
debug!("drawn {} pixels", i);
pixels.render().expect("could not render");
window.request_redraw();
}

View file

@ -2,6 +2,7 @@
mod gui;
mod upd_loop;
mod protocol;
use std::default::Default;
use std::sync::mpsc;
@ -10,12 +11,12 @@ use crate::gui::App;
use crate::upd_loop::start_udp_thread;
use clap::Parser;
use log::info;
use pixel_shared_rs::PIXEL_COUNT;
use protocol::PIXEL_COUNT;
use winit::event_loop::{ControlFlow, EventLoop};
#[derive(Parser, Debug)]
struct Cli {
#[arg(long = "bind", default_value = "0.0.0.0:2342")]
#[arg(long, default_value = "0.0.0.0:2342")]
bind: String,
}

99
src/protocol.rs Normal file
View file

@ -0,0 +1,99 @@
use num_derive::{FromPrimitive, ToPrimitive};
use std::mem::size_of;
#[derive(Debug, FromPrimitive, ToPrimitive, Default)]
pub enum DisplayCommand {
#[default]
CmdClear = 0x0002,
CmdCp437data = 0x0003,
CmdCharBrightness = 0x0005,
CmdBrightness = 0x0007,
CmdHardReset = 0x000b,
CmdFadeOut = 0x000d,
CmdBitmapLegacy = 0x0010,
CmdBitmapLinear = 0x0012,
CmdBitmapLinearWin = 0x0013,
CmdBitmapLinearAnd = 0x0014,
CmdBitmapLinearOr = 0x0015,
CmdBitmapLinearXor = 0x0016,
}
#[repr(C)]
#[derive(Debug, Default)]
pub struct HdrWindow {
pub command: DisplayCommand,
pub x: u16,
pub y: u16,
pub w: u16,
pub h: u16,
}
/*
#[repr(C)]
pub struct HdrBitmap {
pub command: DisplayCommand,
pub offset: u16,
pub length: u16,
pub subcommand: DisplaySubcommand,
reserved: u16,
}
*/
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive)]
pub enum DisplaySubcommand {
SubCmdBitmapNormal = 0x0,
SubCmdBitmapCompressZ = 0x677a,
SubCmdBitmapCompressBz = 0x627a,
SubCmdBitmapCompressLz = 0x6c7a,
SubCmdBitmapCompressZs = 0x7a73,
}
pub const TILE_SIZE: u16 = 8;
pub const TILE_WIDTH: u16 = 56;
pub const TILE_HEIGHT: u16 = 20;
pub const PIXEL_WIDTH: u16 = TILE_WIDTH * TILE_SIZE;
pub const PIXEL_HEIGHT: u16 = TILE_HEIGHT * TILE_SIZE;
pub const PIXEL_COUNT: usize = PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize;
#[derive(Debug)]
pub enum ReadHeaderError {
BufferTooSmall,
WrongCommandEndianness(u16, DisplayCommand),
InvalidCommand(u16),
}
pub fn read_header(buffer: &[u8]) -> Result<HdrWindow, ReadHeaderError> {
if buffer.len() < size_of::<HdrWindow>() {
return Err(ReadHeaderError::BufferTooSmall);
}
let command_u16 = read_beu16(&buffer[0..=1]);
return match num::FromPrimitive::from_u16(command_u16) {
Some(command) => Ok(HdrWindow {
command,
x: read_beu16(&buffer[2..=3]),
y: read_beu16(&buffer[4..=5]),
w: read_beu16(&buffer[6..=7]),
h: read_beu16(&buffer[8..=9]),
}),
None => {
let maybe_command: Option<DisplayCommand> =
num::FromPrimitive::from_u16(u16::swap_bytes(command_u16));
return match maybe_command {
None => Err(ReadHeaderError::InvalidCommand(command_u16)),
Some(command) => Err(ReadHeaderError::WrongCommandEndianness(
command_u16,
command,
)),
};
}
};
}
fn read_beu16(buffer: &[u8]) -> u16 {
let buffer: [u8; 2] = buffer
.try_into()
.expect("cannot read u16 from buffer with size != 2");
return u16::from_be_bytes(buffer);
}

View file

@ -1,8 +1,8 @@
use crate::DISPLAY;
use log::{error, info, warn};
use pixel_shared_rs::{
use crate::protocol::{
read_header, DisplayCommand, HdrWindow, ReadHeaderError, PIXEL_WIDTH, TILE_SIZE,
};
use crate::DISPLAY;
use log::{error, info, warn};
use std::io::ErrorKind;
use std::mem::size_of;
use std::net::UdpSocket;
@ -109,7 +109,7 @@ fn check_payload_size(buf: &[u8], expected: usize) -> bool {
}
fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) {
if !check_payload_size(payload, (header.w * header.h) as usize) {
if !check_payload_size(payload, header.w as usize * header.h as usize) {
return;
}
@ -118,19 +118,15 @@ fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) {
header.x, header.y
);
let mut text_repr = String::new();
for y in 0..header.h {
for byte_x in 0..header.w {
let byte_index = (y * header.w + byte_x) as usize;
let byte = payload[byte_index];
for pixel_x in 1u8..=8u8 {
let bit_index = 8 - pixel_x;
for pixel_x in 0u8..8u8 {
let bit_index = 7 - pixel_x;
let bitmask = 1 << bit_index;
let is_set = byte & bitmask != 0;
let char = if is_set { '█' } else { ' ' };
text_repr.push(char);
let x = byte_x * TILE_SIZE + pixel_x as u16;
@ -143,10 +139,7 @@ fn print_bitmap_linear_win(header: &HdrWindow, payload: &[u8]) {
}
}
}
text_repr.push('\n');
}
info!("{}", text_repr);
}
// TODO: actually convert from CP437