From 8b333892030cea01e79bb3546419bf6217f8bfb3 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 10 May 2024 18:33:51 +0200 Subject: [PATCH] add command code type --- Cargo.lock | 129 +++++++++++++++++++++++++++++++ Cargo.toml | 2 + examples/game_of_life/Cargo.lock | 94 ++++++++++++++++++++++ examples/moving_line/Cargo.lock | 129 +++++++++++++++++++++++++++++++ src/command.rs | 51 ++++++++---- src/command_codes.rs | 29 +++++++ src/lib.rs | 2 + 7 files changed, 421 insertions(+), 15 deletions(-) create mode 100644 src/command_codes.rs diff --git a/Cargo.lock b/Cargo.lock index c55f3b7..6656233 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,135 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "servicepoint2" version = "0.1.0" +dependencies = [ + "num", + "num-derive", +] + +[[package]] +name = "syn" +version = "2.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml index 2684a6e..7d0959f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +num = "0.4" +num-derive = "0.4" diff --git a/examples/game_of_life/Cargo.lock b/examples/game_of_life/Cargo.lock index 62a5011..bf66681 100644 --- a/examples/game_of_life/Cargo.lock +++ b/examples/game_of_life/Cargo.lock @@ -51,6 +51,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + [[package]] name = "cfg-if" version = "1.0.0" @@ -148,6 +154,90 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -205,6 +295,10 @@ dependencies = [ [[package]] name = "servicepoint2" version = "0.1.0" +dependencies = [ + "num", + "num-derive", +] [[package]] name = "strsim" diff --git a/examples/moving_line/Cargo.lock b/examples/moving_line/Cargo.lock index f8fad2d..ad4e807 100644 --- a/examples/moving_line/Cargo.lock +++ b/examples/moving_line/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + [[package]] name = "moving_line" version = "0.1.0" @@ -9,6 +15,129 @@ dependencies = [ "servicepoint2", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "servicepoint2" version = "0.1.0" +dependencies = [ + "num", + "num-derive", +] + +[[package]] +name = "syn" +version = "2.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/src/command.rs b/src/command.rs index b3d965e..86ec9a6 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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) -> Packet { - Packet(Header(command, offset, payload.len() as u16, 0, 0), payload) +fn offset_and_payload(command: DisplayCommandCode, offset: Offset, payload: Vec) -> Packet { + Packet(Header(command.to_primitive(), offset, payload.len() as u16, 0, 0), payload) } -fn window_and_payload(command: u16, window: Window, payload: Vec) -> Packet { +fn window_and_payload(command: DisplayCommandCode, window: Window, payload: Vec) -> 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), } } } diff --git a/src/command_codes.rs b/src/command_codes.rs new file mode 100644 index 0000000..bd4199a --- /dev/null +++ b/src/command_codes.rs @@ -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 { + FromPrimitive::from_u16(value) + } + + pub fn to_primitive(&self) -> u16 { + ToPrimitive::to_u16(self).unwrap() + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 55ba617..a3bfff3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;