From 27f891cd921821400b4ae69e02986bf5ed2687b8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 11 May 2024 14:41:09 +0200 Subject: [PATCH] remove Window, fix examples, add logging --- Cargo.lock | 7 + Cargo.toml | 1 + examples/Cargo.lock | 516 ++++++++++++++++++++++++++++++ examples/Cargo.toml | 8 + examples/announce/Cargo.lock | 7 + examples/announce/Cargo.toml | 2 + examples/announce/src/main.rs | 21 +- examples/game_of_life/Cargo.lock | 1 + examples/game_of_life/Cargo.toml | 1 + examples/game_of_life/src/main.rs | 63 ++-- examples/moving_line/Cargo.lock | 82 +++++ examples/moving_line/Cargo.toml | 4 +- examples/moving_line/src/main.rs | 11 +- src/byte_grid.rs | 2 +- src/command.rs | 32 +- src/connection.rs | 8 +- src/lib.rs | 2 +- 17 files changed, 707 insertions(+), 61 deletions(-) create mode 100644 examples/Cargo.lock create mode 100644 examples/Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 05a1f53..b114eb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + [[package]] name = "num" version = "0.4.3" @@ -114,6 +120,7 @@ dependencies = [ name = "servicepoint2" version = "0.1.0" dependencies = [ + "log", "num", "num-derive", "num-traits", diff --git a/Cargo.toml b/Cargo.toml index 89e2057..4216dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" num = "0.4" num-derive = "0.4" num-traits = "0.2" +log = "0.4.21" diff --git a/examples/Cargo.lock b/examples/Cargo.lock new file mode 100644 index 0000000..b2eb944 --- /dev/null +++ b/examples/Cargo.lock @@ -0,0 +1,516 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "announce" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", + "servicepoint2", +] + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "game_of_life" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", + "rand", + "servicepoint2", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "libc" +version = "0.2.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + +[[package]] +name = "moving_line" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", + "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 = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[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 = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "random_brightness" +version = "0.1.0" +dependencies = [ + "clap", + "env_logger", + "log", + "rand", + "servicepoint2", +] + +[[package]] +name = "regex" +version = "1.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + +[[package]] +name = "servicepoint2" +version = "0.1.0" +dependencies = [ + "log", + "num", + "num-derive", + "num-traits", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f660c3bfcefb88c538776b6685a0c472e3128b51e74d48793dc2a488196e8eb" +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" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 0000000..20a2220 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +resolver = "2" +members = [ + "announce", + "game_of_life", + "moving_line", + "random_brightness", +] \ No newline at end of file diff --git a/examples/announce/Cargo.lock b/examples/announce/Cargo.lock index 561e9d9..f18ddcd 100644 --- a/examples/announce/Cargo.lock +++ b/examples/announce/Cargo.lock @@ -123,6 +123,12 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + [[package]] name = "num" version = "0.4.3" @@ -229,6 +235,7 @@ dependencies = [ name = "servicepoint2" version = "0.1.0" dependencies = [ + "log", "num", "num-derive", "num-traits", diff --git a/examples/announce/Cargo.toml b/examples/announce/Cargo.toml index e37d66e..973851c 100644 --- a/examples/announce/Cargo.toml +++ b/examples/announce/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] clap = { version = "4.5.4", features = ["derive"] } servicepoint2 = { path = "../.." } +log = "0.4.21" +env_logger = "0.11.3" diff --git a/examples/announce/src/main.rs b/examples/announce/src/main.rs index 7194067..2dd90b5 100644 --- a/examples/announce/src/main.rs +++ b/examples/announce/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use servicepoint2::{Command, Connection, Origin, Size, Window}; +use servicepoint2::{ByteGrid, Command, Connection, Origin}; #[derive(Parser, Debug)] struct Cli { @@ -7,16 +7,23 @@ struct Cli { destination: String, #[arg(short, long, num_args = 1.., value_delimiter = '\n')] text: Vec, + #[arg(short, long, default_value_t = true)] + clear: bool, } /// example: `cargo run -- --text "Hallo, /// CCCB"` fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Debug) + .init(); let cli = Cli::parse(); - println!("starting with args: {:?}", &cli); let connection = Connection::open(&cli.destination).unwrap(); + if cli.clear { + connection.send(Command::Clear).unwrap(); + } let mut max_width = 0; for l in cli.text.iter() { @@ -25,7 +32,7 @@ fn main() { } } - let mut data = vec!(0; max_width * cli.text.len()); + let mut chars = ByteGrid::new(max_width, max_width * cli.text.len()); for y in 0..cli.text.len() { let row = &cli.text[y]; for x in 0..max_width { @@ -33,13 +40,9 @@ fn main() { continue; } - data[x + y * max_width] = row.chars().nth(x).unwrap().try_into().unwrap(); + chars.set(x, y, row.chars().nth(x).unwrap().try_into().unwrap()); } } - let window = Window(Origin(0, 0), Size(max_width as u16, cli.text.len() as u16)); - let command = Command::Cp437Data(window, data); - - connection.send(Command::Clear).unwrap(); - connection.send(command).unwrap() + connection.send(Command::Cp437Data(Origin::top_left(), chars)).unwrap(); } diff --git a/examples/game_of_life/Cargo.lock b/examples/game_of_life/Cargo.lock index d4e620d..65a3234 100644 --- a/examples/game_of_life/Cargo.lock +++ b/examples/game_of_life/Cargo.lock @@ -296,6 +296,7 @@ dependencies = [ name = "servicepoint2" version = "0.1.0" dependencies = [ + "log", "num", "num-derive", "num-traits", diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index 8659a98..f0bbfa4 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -8,3 +8,4 @@ servicepoint2 = { path = "../.." } rand = "0.8.5" clap = { version = "4.5.4", features = ["derive"] } log = "0.4.21" +env_logger = "0.11.3" diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index d624aad..b2a2a66 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -14,44 +14,37 @@ struct Cli { } fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .init(); let cli = Cli::parse(); - println!("starting with args: {:?}", &cli); let connection = Connection::open(&cli.destination).unwrap(); + let mut field = make_random_field(cli.probability); - let mut field = PixelGrid::max_sized(); + loop { + connection.send(BitmapLinearWin(Origin::top_left(), field.clone())).expect("could not send"); + thread::sleep(Duration::from_millis(14)); + field = iteration(field); + } +} - let mut rng = rand::thread_rng(); - let d = distributions::Bernoulli::new(cli.probability).unwrap(); +fn iteration(field: PixelGrid) -> PixelGrid { + let mut next = field.clone(); for x in 0..field.width { for y in 0..field.height { - field.set(x, y, rng.sample(d)); + let old_state = field.get(x, y); + let neighbors = count_neighbors(&field, x as i32, y as i32); + let new_state = match (old_state, neighbors) { + (true, 2) => true, + (true, 3) => true, + (false, 3) => true, + _ => false + }; + next.set(x, y, new_state); } } - - let origin = Origin(0, 0); - loop { - connection.send(BitmapLinearWin(origin, field.clone())).expect("could not send"); - thread::sleep(Duration::from_millis(14)); - - let mut next = field.clone(); - - for x in 0..field.width { - for y in 0..field.height { - let old_state = field.get(x, y); - let neighbors = count_neighbors(&field, x as i32, y as i32); - let new_state = match (old_state, neighbors) { - (true, 2) => true, - (true, 3) => true, - (false, 3) => true, - _ => false - }; - next.set(x, y, new_state); - } - } - - field = next; - } + next } fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 { @@ -76,3 +69,15 @@ fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 { return count; } + +fn make_random_field(probability: f64) -> PixelGrid { + let mut field = PixelGrid::max_sized(); + let mut rng = rand::thread_rng(); + let d = distributions::Bernoulli::new(probability).unwrap(); + for x in 0..field.width { + for y in 0..field.height { + field.set(x, y, rng.sample(d)); + } + } + return field; +} diff --git a/examples/moving_line/Cargo.lock b/examples/moving_line/Cargo.lock index e13f60e..ae95755 100644 --- a/examples/moving_line/Cargo.lock +++ b/examples/moving_line/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.14" @@ -103,23 +112,66 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "is_terminal_polyfill" version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + [[package]] name = "moving_line" version = "0.1.0" dependencies = [ "clap", + "env_logger", + "log", "servicepoint2", ] @@ -225,10 +277,40 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + [[package]] name = "servicepoint2" version = "0.1.0" dependencies = [ + "log", "num", "num-derive", "num-traits", diff --git a/examples/moving_line/Cargo.toml b/examples/moving_line/Cargo.toml index 00826a4..df661a8 100644 --- a/examples/moving_line/Cargo.toml +++ b/examples/moving_line/Cargo.toml @@ -5,4 +5,6 @@ edition = "2021" [dependencies] servicepoint2 = { path = "../.." } -clap = { version = "4.5.4", features = ["derive"] } \ No newline at end of file +clap = { version = "4.5.4", features = ["derive"] } +env_logger = "0.11.3" +log = "0.4.21" \ No newline at end of file diff --git a/examples/moving_line/src/main.rs b/examples/moving_line/src/main.rs index 0b5e68d..a7ec100 100644 --- a/examples/moving_line/src/main.rs +++ b/examples/moving_line/src/main.rs @@ -11,11 +11,12 @@ struct Cli { } fn main() { - let cli = Cli::parse(); - println!("starting with args: {:?}", &cli); - let connection = Connection::open(cli.destination).unwrap(); + env_logger::builder() + .filter_level(log::LevelFilter::Debug) + .init(); + + let connection = Connection::open(Cli::parse().destination).unwrap(); - let origin = Origin(0, 0); let mut pixels = PixelGrid::max_sized(); for x_offset in 0..usize::MAX { pixels.fill(false); @@ -23,7 +24,7 @@ fn main() { for y in 0..PIXEL_HEIGHT as usize { pixels.set((y + x_offset) % PIXEL_WIDTH as usize, y, true); } - connection.send(BitmapLinearWin(origin, pixels.clone())).unwrap(); + connection.send(BitmapLinearWin(Origin::top_left(), pixels.clone())).unwrap(); thread::sleep(Duration::from_millis(14)); } } diff --git a/src/byte_grid.rs b/src/byte_grid.rs index ce7b76b..fd702bf 100644 --- a/src/byte_grid.rs +++ b/src/byte_grid.rs @@ -1,4 +1,4 @@ -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ByteGrid { pub width: usize, pub height: usize, diff --git a/src/command.rs b/src/command.rs index 4583fe8..b9f1c5b 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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) -> Packet(Header(command.to_primitive(), offset, payload.len() as u16, 0, 0), payload) } -fn window_and_payload(command: CommandCode, window: Window, payload: Vec) -> Packet { - let Window(Origin(x, y), Size(w, h)) = window; +fn origin_size_payload(command: CommandCode, origin: Origin, size: Size, payload: Vec) -> 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 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 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()) } } } diff --git a/src/connection.rs b/src/connection.rs index f687d5f..bb4bd77 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -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 { + pub fn open(addr: impl ToSocketAddrs + Debug) -> std::io::Result { + 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) -> std::io::Result<()> { + pub fn send(&self, packet: impl Into + Debug) -> std::io::Result<()> { + debug!("sending {packet:?}"); let packet = packet.into(); let data: Vec = packet.into(); self.socket.send(&*data)?; diff --git a/src/lib.rs b/src/lib.rs index 132b1d5..8db1903 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;