add command code type

This commit is contained in:
Vinzenz Schroeter 2024-05-10 18:33:51 +02:00
parent a23ca55f60
commit 8b33389203
7 changed files with 421 additions and 15 deletions

View file

@ -1,4 +1,5 @@
use crate::{BitVec, Header, Packet, PixelGrid, TILE_SIZE, ToPacket};
use crate::command_codes::DisplayCommandCode;
/// A window
#[derive(Debug, Copy, Clone)]
@ -32,33 +33,53 @@ pub enum Command {
BitmapLinearWin(Origin, PixelGrid),
}
fn offset_and_payload(command: u16, offset: Offset, payload: Vec<u8>) -> Packet {
Packet(Header(command, offset, payload.len() as u16, 0, 0), payload)
fn offset_and_payload(command: DisplayCommandCode, offset: Offset, payload: Vec<u8>) -> Packet {
Packet(Header(command.to_primitive(), offset, payload.len() as u16, 0, 0), payload)
}
fn window_and_payload(command: u16, window: Window, payload: Vec<u8>) -> Packet {
fn window_and_payload(command: DisplayCommandCode, window: Window, payload: Vec<u8>) -> Packet {
let Window(Origin(x, y), Size(w, h)) = window;
Packet(Header(command, x, y, w, h), payload.into())
Packet(Header(command.to_primitive(), x, y, w, h), payload.into())
}
fn command_code_only(code: DisplayCommandCode) -> Packet {
Packet(Header(code.to_primitive(), 0x0000, 0x0000, 0x0000, 0x0000), vec!())
}
impl ToPacket for Command {
fn to_packet(self) -> Packet {
match self {
Command::Clear => Packet(Header(0x0002, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
Command::CharBrightness(window, payload) => window_and_payload(0x0005, window, payload),
Command::Brightness(brightness) => Packet(Header(0x0007, 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness)),
Command::HardReset => Packet(Header(0x000b, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
Command::FadeOut => Packet(Header(0x000d, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
Command::BitmapLinear(offset, bits) => offset_and_payload(0x0012, offset, bits.into()),
Command::Clear => command_code_only(DisplayCommandCode::Clear),
Command::FadeOut => command_code_only(DisplayCommandCode::FadeOut),
Command::HardReset => command_code_only(DisplayCommandCode::HardReset),
Command::CharBrightness(window, payload) => {
window_and_payload(DisplayCommandCode::CharBrightness, window, payload)
}
Command::Brightness(brightness) => {
Packet(Header(DisplayCommandCode::Brightness.to_primitive(), 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness))
}
Command::BitmapLinear(offset, bits) => {
offset_and_payload(DisplayCommandCode::BitmapLinear, offset, bits.into())
}
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => {
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
Packet(Header(0x0013, pixel_x / TILE_SIZE, pixel_y, pixels.width as u16/ TILE_SIZE,pixels.height as u16), pixels.into())
Packet(Header(0x0013, pixel_x / TILE_SIZE, pixel_y, pixels.width as u16 / TILE_SIZE, pixels.height as u16), pixels.into())
}
Command::BitmapLinearAnd(offset, bits) => {
offset_and_payload(DisplayCommandCode::BitmapLinearAnd, offset, bits.into())
}
Command::BitmapLinearOr(offset, bits) => {
offset_and_payload(DisplayCommandCode::BitmapLinearOr, offset, bits.into())
}
Command::BitmapLinearXor(offset, bits) => {
offset_and_payload(DisplayCommandCode::BitmapLinearXor, offset, bits.into())
}
Command::Cp437Data(window, payload) => {
window_and_payload(DisplayCommandCode::Cp437data, window, payload)
}
Command::BitmapLinearAnd(offset, bits) => offset_and_payload(0x0014, offset, bits.into()),
Command::BitmapLinearOr(offset, bits) => offset_and_payload(0x0015, offset, bits.into()),
Command::BitmapLinearXor(offset, bits) => offset_and_payload(0x0016, offset, bits.into()),
Command::Cp437Data(window, payload) => window_and_payload(0x0003, window, payload),
}
}
}

29
src/command_codes.rs Normal file
View file

@ -0,0 +1,29 @@
use num::{FromPrimitive, ToPrimitive};
use num_derive::{FromPrimitive, ToPrimitive};
#[repr(u16)]
#[derive(Debug, FromPrimitive, ToPrimitive)]
pub enum DisplayCommandCode {
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> {
FromPrimitive::from_u16(value)
}
pub fn to_primitive(&self) -> u16 {
ToPrimitive::to_u16(self).unwrap()
}
}

View file

@ -3,12 +3,14 @@ mod pixel_grid;
mod bit_vec;
mod packet;
mod command;
mod command_codes;
pub use crate::connection::{Connection, ToPacket};
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_codes::{DisplayCommandCode};
pub const TILE_SIZE: u16 = 8;
pub const TILE_WIDTH: u16 = 56;