move DisplayCommandCode into shared lib

This commit is contained in:
Vinzenz Schroeter 2024-05-10 18:43:00 +02:00
parent ae12c82c2e
commit 63520f5708
3 changed files with 54 additions and 73 deletions

View file

@ -1,36 +1,9 @@
use num_derive::{FromPrimitive, ToPrimitive};
use servicepoint2::DisplayCommandCode;
use std::mem::size_of;
use num_derive::{FromPrimitive, ToPrimitive};
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive, Default)]
pub enum DisplayCommandCode {
#[default]
Clear = 0x0002,
Cp437data = 0x0003,
CharBrightness = 0x0005,
Brightness = 0x0007,
HardReset = 0x000b,
FadeOut = 0x000d,
BitmapLegacy = 0x0010,
BitmapLinear = 0x0012,
BitmapLinearWin = 0x0013,
BitmapLinearAnd = 0x0014,
BitmapLinearOr = 0x0015,
BitmapLinearXor = 0x0016,
}
impl DisplayCommandCode {
pub fn from_primitive(value: u16) -> Option<Self> {
num::FromPrimitive::from_u16(value)
}
pub fn to_primitive(&self) -> u16 {
num::ToPrimitive::to_u16(self).unwrap()
}
}
#[repr(C)]
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct HdrWindow {
pub command: DisplayCommandCode,
pub x: u16,
@ -67,38 +40,41 @@ pub enum ReadHeaderError {
InvalidCommand(u16),
}
pub fn read_header(buffer: &[u8]) -> Result<HdrWindow, ReadHeaderError> {
assert_eq!(size_of::<HdrWindow>(), 10, "invalid struct size");
impl HdrWindow {
pub fn from_buffer(buffer: &[u8]) -> Result<HdrWindow, ReadHeaderError> {
assert_eq!(size_of::<HdrWindow>(), 10, "invalid struct size");
if buffer.len() < size_of::<HdrWindow>() {
return Err(ReadHeaderError::BufferTooSmall);
if buffer.len() < size_of::<HdrWindow>() {
return Err(ReadHeaderError::BufferTooSmall);
}
let command_u16 = Self::read_beu16(&buffer[0..=1]);
return match DisplayCommandCode::from_primitive(command_u16) {
Some(command) => Ok(HdrWindow {
command,
x: Self::read_beu16(&buffer[2..=3]),
y: Self::read_beu16(&buffer[4..=5]),
w: Self::read_beu16(&buffer[6..=7]),
h: Self::read_beu16(&buffer[8..=9]),
}),
None => {
let maybe_command =
DisplayCommandCode::from_primitive(u16::swap_bytes(command_u16));
return match maybe_command {
None => Err(ReadHeaderError::InvalidCommand(command_u16)),
Some(command) => Err(ReadHeaderError::WrongCommandEndianness(
command_u16,
command,
)),
};
}
};
}
let command_u16 = read_beu16(&buffer[0..=1]);
return match DisplayCommandCode::from_primitive(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 = DisplayCommandCode::from_primitive(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);
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::font::BitmapFont;
use crate::protocol::{read_header, DisplayCommandCode, HdrWindow, ReadHeaderError};
use crate::protocol::{HdrWindow, ReadHeaderError};
use crate::DISPLAY;
use log::{debug, error, info, warn};
use servicepoint2::{PixelGrid, PIXEL_WIDTH, TILE_SIZE};
use servicepoint2::{PixelGrid, PIXEL_WIDTH, TILE_SIZE, DisplayCommandCode};
use std::io::ErrorKind;
use std::net::{ToSocketAddrs, UdpSocket};
use std::sync::mpsc;
@ -60,7 +60,7 @@ impl UdpThread {
}
fn handle_package(received: &mut [u8], font: &BitmapFont) {
let header = match read_header(&received[..10]) {
let header = match HdrWindow::from_buffer(&received[..10]) {
Err(ReadHeaderError::BufferTooSmall) => {
error!("received a packet that is too small");
return;