Compare commits

...

No commits in common. "main" and "servicepoint1" have entirely different histories.

77 changed files with 763 additions and 8488 deletions

View file

@ -1,44 +0,0 @@
name: Rust
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
# Make sure CI fails on all warnings, including Clippy lints
RUSTFLAGS: "-Dwarnings"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Update repos
run: sudo apt-get update -qq
- name: Install rust toolchain
run: sudo apt-get install -qy cargo rust-clippy
- name: install lzma
run: sudo apt-get update && sudo apt-get install -y liblzma-dev
- name: Run Clippy
run: cargo clippy --all-features
- name: no features -- test (without doctest)
run: cargo test --lib --no-default-features
- name: default features -- test
run: cargo test --all
- name: default features -- examples
run: cargo build --examples
- name: all features -- test
run: cargo test --all --all-features
- name: all features -- examples
run: cargo build --examples --all-features

11
.gitignore vendored
View file

@ -1,8 +1,3 @@
target
.idea
out
.direnv
.envrc
result
mutants.*
tarpaulin-report.html
*.swo
*.swp
target/

View file

@ -1,31 +0,0 @@
# Contributing
Contributions are accepted in any form (issues, documentation, feature requests, code, review, ...).
All creatures welcome.
If you have access, please contribute on the [CCCB Forgejo](https://git.berlin.ccc.de/servicepoint/servicepoint).
Contributions on GitHub will be copied over and merged there.
## Pull requests
Feel free to create a PR, even if your change is not done yet.
Mark your PR as a draft as long as you do not want it to be merged.
The main branch is supposed to be a working version, including language bindings,
which means sometimes your PR may be merged into a temporary development branch.
Unit tests and documentation are required for the core library.
## Language bindings
Pull requests for your preferred language will be accepted.
If there is no code generator, it should call the C ABI methods provided by `servicepoint_binding_c`.
It should be able to send most of the basic commands in a way the simulator accepts, receiving is
not required for the merge.
It is okay for the feature set of a language binding to lag behind the one of the rust crate.
This also means you do not have to expose a feature to all the language bindings when adding something to the core.
If your change may break other language bindings, please note that in your PR description so someone can check them.

566
Cargo.lock generated
View file

@ -1,566 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "bzip2"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49ecfb22d906f800d4fe833b6282cf4dc1c298f5057ca0b5445e5c209735ca47"
dependencies = [
"bzip2-sys",
]
[[package]]
name = "bzip2-sys"
version = "0.1.13+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225bff33b2141874fe80d71e07d6eec4f85c5c216453dd96388240f96e1acc14"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "cc"
version = "1.2.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc"
dependencies = [
"jobserver",
"libc",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "clap"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "crc32fast"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
[[package]]
name = "flate2"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a3d7db9596fecd151c5f638c0ee5d5bd487b6e0ea232e5dc96d5250f6f94b1d"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "jobserver"
version = "0.1.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a"
dependencies = [
"getrandom",
"libc",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "log"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "rust-lzma"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d62915608f6cee1d7f2fc00f28b4f058ff79d6e4ec3c2fe0006b09b52437c84"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]]
name = "servicepoint"
version = "0.16.0"
dependencies = [
"bitvec",
"bzip2",
"clap",
"flate2",
"log",
"once_cell",
"rand",
"rust-lzma",
"thiserror",
"zstd",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[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.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
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.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zstd"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "7.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.0.15+zstd.1.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237"
dependencies = [
"cc",
"pkg-config",
]

View file

@ -1,101 +0,0 @@
[package]
name = "servicepoint"
version = "0.16.0"
publish = true
edition = "2021"
license = "GPL-3.0-or-later"
description = "A rust library for the CCCB Service Point Display."
homepage = "https://docs.rs/crate/servicepoint"
repository = "https://git.berlin.ccc.de/servicepoint/servicepoint"
readme = "README.md"
keywords = ["cccb", "cccb-servicepoint"]
rust-version = "1.70.0"
[lib]
crate-type = ["rlib"]
[dependencies]
log = "0.4"
bitvec = "1.0"
flate2 = { version = "1.0", optional = true }
bzip2 = { version = "0.5", optional = true }
zstd = { version = "0.13", optional = true }
rust-lzma = { version = "0.6", optional = true }
rand = { version = "0.9", optional = true }
once_cell = { version = "1.20", optional = true }
thiserror = "2.0"
[features]
default = ["compression_lzma", "cp437"]
compression_zlib = ["dep:flate2"]
compression_bzip2 = ["dep:bzip2"]
compression_lzma = ["dep:rust-lzma"]
compression_zstd = ["dep:zstd"]
all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"]
rand = ["dep:rand"]
cp437 = ["dep:once_cell"]
[[example]]
name = "random_brightness"
required-features = ["rand"]
[[example]]
name = "game_of_life"
required-features = ["rand"]
[[example]]
name = "own_command"
required-features = ["rand"]
[dev-dependencies]
# for examples
clap = { version = "4.5", features = ["derive"] }
[lints.rust]
missing-docs = "warn"
deprecated-safe = "warn"
future-incompatible = "warn"
keyword-idents = "warn"
let-underscore = "warn"
nonstandard-style = "warn"
refining_impl_trait_reachable = "warn"
rust-2024-compatibility = "warn"
[lints.clippy]
## Categories
complexity = {level = "warn", priority = -1 }
perf = {level = "warn", priority = -1 }
style = {level = "warn", priority = -1 }
pedantic = {level = "warn", priority = -1 }
## Blacklist
unwrap_used = "warn"
expect_used = "warn"
panic = "warn"
incompatible_msrv = "forbid"
allow_attributes_without_reason = "warn"
## Whitelist
# Too many false positives as often a module only contains one struct that is re-exported at top-level
module_name_repetitions = "allow"
# The pretty detailed exception types should be enough for now
missing_errors_doc = "allow"
# The few places where a panic is triggered in code are inspected and should never panic
missing_panics_doc = "allow"
# Does not work for all types, but should probably be fixed at some point
iter_without_into_iter = "allow"
[lints.rustdoc]
private_doc_tests = "warn"
unescaped_backticks = "warn"
[package.metadata.docs.rs]
all-features = true
[profile.size-optimized]
inherits = "release"
opt-level = 'z' # Optimize for size
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary

674
LICENSE
View file

@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

145
README.md
View file

@ -1,145 +0,0 @@
# servicepoint
[![Release](https://git.berlin.ccc.de/servicepoint/servicepoint/badges/release.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint/releases)
[![crates.io](https://img.shields.io/crates/v/servicepoint.svg)](https://crates.io/crates/servicepoint)
[![Crates.io Total Downloads](https://img.shields.io/crates/d/servicepoint)](https://crates.io/crates/servicepoint)
[![docs.rs](https://img.shields.io/docsrs/servicepoint)](https://docs.rs/servicepoint/latest/servicepoint/)
[![GPLv3 licensed](https://img.shields.io/crates/l/servicepoint)](./LICENSE)
[![CI](https://git.berlin.ccc.de/servicepoint/servicepoint/badges/workflows/rust.yml/badge.svg)](https://git.berlin.ccc.de/servicepoint/servicepoint)
In [CCCB](https://berlin.ccc.de/), there is a big pixel matrix hanging on the wall. It is called "Service Point
Display" or "Airport Display".
This crate contains a library for parsing, encoding and sending packets to this display via UDP.
The library itself is written in Rust, but can be used from multiple languages
via [language bindings](#supported-language-bindings).
## Examples
```rust no_run
use std::net::UdpSocket;
// everything you need is in the top-level
use servicepoint::{ClearCommand, UdpSocketExt};
fn main() {
// this should be the IP of the real display @CCCB
let destination = "172.23.42.29:2342";
// establish connection
let connection = UdpSocket::bind_connect(destination).expect("connection failed");
// clear screen content using the UdpSocketExt
connection.send_command(ClearCommand).expect("send failed");
}
```
More examples are available in the crate.
Execute `cargo run --example` for a list of available examples and `cargo run --example <name>` to run one.
## Installation
```bash
cargo add servicepoint
```
or
```toml
[dependencies]
servicepoint = "0.15.2"
```
## Note on stability
This library can be used for creative project or just to play around with the display.
A decent coverage by unit tests prevents major problems and I also test this with my own projects, which mostly use
up-to-date versions.
That being said, the API is still being worked on.
Expect breaking changes with every minor version bump.
There should be no breaking changes in patch releases, but there may also be features hiding in those.
All of this means for you: please specify the full version including patch in your Cargo.toml until 1.0 is released.
Release notes are published [here](https://git.berlin.ccc.de/servicepoint/servicepoint/releases), please check them before updating.
Currently, this crate requires Rust [v1.70](https://releases.rs/docs/1.70.0/) from June 2023.
## Features
This library has multiple optional dependencies.
You can choose to (not) include them by toggling the related features.
| Name | Default | Description | Dependencies |
|-------------------|---------|----------------------------------------------|-------------------------------------------------|
| cp437 | true | Conversion to and from CP-437 | [once_cell](https://crates.io/crates/once_cell) |
| compression_lzma | true | Enable additional compression algorithm | [rust-lzma](https://crates.io/crates/rust-lzma) |
| compression_zlib | false | Enable additional compression algorithm | [flate2](https://crates.io/crates/flate2) |
| compression_bzip2 | false | Enable additional compression algorithm | [bzip2](https://crates.io/crates/bzip2) |
| compression_zstd | false | Enable additional compression algorithm | [zstd](https://crates.io/crates/zstd) |
| rand | false | `impl Distribution<Brightness> for Standard` | [rand](https://crates.io/crates/rand) |
Es an example, if you only want zlib compression:
```toml
[dependencies]
servicepoint = { version = "0.15.2", default-features = false, features = ["compression_zlib"] }
```
If you are looking at features to minimize binary size: take a look at the `tiny_announce`-example!
## Supported language bindings
| Language | Support level | Repo |
|-----------|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| .NET (C#) | Full | [servicepoint-binding-csharp](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-csharp) contains bindings and a `.csproj` to reference |
| C | Full | [servicepoint-binding-c](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c) contains a header and a library to link against |
| Ruby | Working | [servicepoint-binding-ruby](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-ruby) contains bindings |
| Python | Unsupported | bindings can be generated from [servicepoint-binding-uniffi](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-uniffi), tested once |
| Go | Unsupported | bindings can be generated from [servicepoint-binding-uniffi](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-uniffi) |
| Kotlin | Unsupported | bindings can be generated from [servicepoint-binding-uniffi](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-uniffi) |
| Swift | Unsupported | bindings can be generated from [servicepoint-binding-uniffi](https://git.berlin.ccc.de/servicepoint/servicepoint-binding-uniffi) |
## Projects using the library
- [servicepoint-simulator](https://git.berlin.ccc.de/servicepoint/servicepoint-simulator): a screen simulator written in rust
- [servicepoint-tanks](https://git.berlin.ccc.de/vinzenz/servicepoint-tanks): a multiplayer game written in C# with a second screen in the browser written in React/Typescript
- [servicepoint-life](https://git.berlin.ccc.de/vinzenz/servicepoint-life): a cellular automata slideshow written in rust
- [servicepoint-cli](https://git.berlin.ccc.de/servicepoint/servicepoint-cli): a CLI that can:
- share (stream) your screen
- send image files with dithering
- clear the display
- ...
To add yourself to the list, open a pull request.
You can also check out [awesome-servicepoint](https://github.com/stars/kaesaecracker/lists/awesome-servicepoint) for a
bigger collection of projects, including some not related to this library.
If you have access, there is even more software linked in [the wiki](https://wiki.berlin.ccc.de/LED-Riesendisplay).
Some more related projects:
- [cccb-servicepoint-browser](https://github.com/SamuelScheit/cccb-servicepoint-browser): a partial typescript implementation inspired by this library and browser stream
- [arfst23/ServicePoint](https://github.com/arfst23/ServicePoint): a bunch of projects in C that [used to](https://zerforschen.plus/posts/tiny-binaries-rust/) use the C bindings
- a CLI tool to display image files on the display or use the display as a TTY
- a BSD games robots clone
- a split-flap-display simulator
- animations that play on the display
## Contributing
You are welcome to contribute, see [CONTRIBUTING.md](CONTRIBUTING.md).
## History
### Move to Forgejo
This project moved
to [git.berlin.ccc.de/servicepoint/servicepoint](https://git.berlin.ccc.de/servicepoint/servicepoint).
The [GitHub repository](https://github.com/cccb/servicepoint) remains available as a mirror.
### What happened to servicepoint2?
`servicepoint2` was a fork of `servicepoint`. Since `servicepoint2` has been merged into `servicepoint`, `servicepoint2` did not get any updates.

View file

@ -1,41 +0,0 @@
# About the display
- Resolution: 352x160=56,320 pixels
- Pixels are grouped into 44x20=880 tiles (8x8=64 pixels each)
- Smallest addressable unit: row of pixels inside of a tile (8 pixels = 1 byte)
- The brightness can only be set per tile
- Screen content can be changed using a simple UDP protocol
- Between each row of tiles, there is a gap of around 4 pixels size. This gap changes the aspect ratio of the display.
### Binary format
A UDP package sent to the display has a header size of 10 bytes.
Each header value has a size of two bytes (unsigned 16 bit integer).
Depending on the command, there can be a payload following the header.
To change screen contents, these commands are the most relevant:
1. Clear screen
- command: `0x0002`
- (rest does not matter)
2. Send CP437 data: render specified text into rectangular region
- command: `0x0003`
- top left tile x
- top left tile y
- width in tiles
- height in tiles
- payload: (width in tiles * height in tiles) bytes
- 1 byte = 1 character
- each character is rendered into one tile (mono-spaced)
- characters are encoded using code page 437
3. Send bitmap window: set pixel states for a rectangular region
- command: `0x0013`
- top left tile x
- top left _pixel_ y
- width in tiles
- height in _pixels_
- payload: (width in tiles * height in pixels) bytes
- network byte order
- 1 bit = 1 pixel
There are other commands implemented as well, e.g. for changing the brightness.

2
airportdisplay/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

View file

@ -0,0 +1,7 @@
[package]
name = "airportdisplay"
version = "0.1.0"
edition = "2021"
[dependencies]
codepage-437 = "0.1.0"

View file

@ -0,0 +1,49 @@
use std::convert::From;
use super::{
graphics::Graphics,
luminance::Luminance,
text::{Buffer as TextBuffer, Raw as TextRaw, Text},
};
/// Display Commands
pub enum Command {
Reset,
Clear,
Reboot,
Fadeout,
Text(Text),
Luminance(Luminance),
Graphics(Graphics),
}
/// Directly converty a raw text into a command which
/// can be sent to the display.
impl From<TextRaw> for Command {
fn from(raw: TextRaw) -> Self {
Command::Text(Text::Raw(raw))
}
}
/// Shortcut to directly convert a text buffer into
/// a commmand which can be sent to the display.
impl From<TextBuffer> for Command {
fn from(buffer: TextBuffer) -> Self {
Command::Text(Text::Buffer(buffer))
}
}
/// Shortcut to convert a luminance window
/// to a command.
impl From<Luminance> for Command {
fn from(luminance: Luminance) -> Self {
Command::Luminance(luminance)
}
}
/// Shortcut for graphics
impl From<Graphics> for Command {
fn from(gfx: Graphics) -> Self {
Command::Graphics(gfx)
}
}

View file

@ -0,0 +1,30 @@
use std::io::Result;
use std::net::UdpSocket;
use super::{commands::Command, protocol::Data};
pub struct Display {
addr: String,
socket: UdpSocket,
}
impl Display {
/// Open a new UDP socket and create a display instance
pub fn connect(addr: String) -> Result<Self> {
let socket = UdpSocket::bind("0.0.0.0:0")?;
Ok(Self {
addr: addr,
socket: socket,
})
}
/// Send a command to the display
pub fn send(&self, cmd: Command) -> Result<()> {
let data: Data = cmd.into();
for frame in data {
self.socket
.send_to(frame.as_slice(), self.addr.clone().as_str())?;
}
Ok(())
}
}

View file

@ -0,0 +1,26 @@
pub const COLUMNS: usize = 56;
pub const ROWS: usize = 20;
/// An origin marks the top left position of the
/// data sent to the display.
#[derive(Default)]
pub struct Origin(pub u16, pub u16);
/// Size defines the width and height of a window
pub struct Size(pub u16, pub u16);
impl Default for Size {
fn default() -> Self {
Self(1, 1)
}
}
/// A window
#[derive(Default)]
pub struct Window(pub Origin, pub Size);
impl Window {
pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
Window(Origin(x, y), Size(w, h))
}
}

View file

@ -0,0 +1,51 @@
use super::geometry::{COLUMNS, ROWS};
const FB_WIDTH: usize = COLUMNS * 8;
const FB_HEIGHT: usize = ROWS * 8;
pub enum Graphics {
/// Raw is a series
Raw(Raw),
}
/// Raw: Offset + Raw pixel content.
/// Pixels content: series of byte-sized 8 pixel
/// horizontal blocks. highest bit is the top left pixel.
pub struct Raw(pub u16, pub Vec<u8>);
/// A framebuffer holds 8bit pixel data.
/// The value of each pixel encodes the luminance,
/// unfortunatley this can only be set per block - so the average
/// across 8 pixels is used.
///
/// There are 56 segments and 20 rows, with 8x8 pixels per segment.
pub struct Framebuffer {
data: Vec<u8>,
}
impl Framebuffer {
pub fn new() -> Self {
Self {
data: Vec::with_capacity(FB_WIDTH * FB_HEIGHT),
}
}
/// Convert to pixel data. (Not convinced this is correct...)
pub fn into_bitmap(&self) -> Vec<u8> {
let mut bitmap = Vec::with_capacity(COLUMNS * ROWS);
for (i, v) in self.data.iter().enumerate() {
let offset = i / 8;
let pixel = i % 8;
let shift = 7 - pixel;
if *v > 0 {
bitmap[offset] |= 1 << shift
} else {
bitmap[offset] &= !(1 << shift)
}
}
bitmap
}
// Convert to luminance map
// ... TODO
}

11
airportdisplay/src/lib.rs Normal file
View file

@ -0,0 +1,11 @@
mod commands;
mod display;
pub mod geometry;
pub mod graphics;
pub mod luminance;
mod protocol;
pub mod text;
pub use commands::Command;
pub use display::Display;
pub use protocol::Data;

View file

@ -0,0 +1,4 @@
use super::geometry::Window;
/// Set luminance of [0..5??] at window
pub struct Luminance(pub Window, pub u16);

View file

@ -0,0 +1,207 @@
use std::convert::From;
use codepage_437::{ToCp437, CP437_WINGDINGS};
use super::{
commands::Command,
geometry::{Origin, Size, Window, COLUMNS, ROWS},
graphics::{Graphics, Raw as GraphicsRaw},
luminance::Luminance,
text::{Buffer as TextBuffer, Raw as TextRaw, Text},
};
const CMD_TEXT_RAW: &'static [u8] = &[0x00, 0x03];
const CMD_LUM_RAW: &'static [u8] = &[0x00, 0x05];
const CMD_GFX_RAW: &'static [u8] = &[0x00, 0x12];
/// A frame holds a single encoded display command,
/// like set text at pos x, y.
pub type Frame = Vec<u8>;
/// Data is a list of commands to be sent to the display.
pub type Data = Vec<Frame>;
fn encode_u16(v: u16) -> Frame {
vec![(v >> 8) as u8, v as u8]
}
/// Encode position data as big endian
impl From<Origin> for Frame {
fn from(Origin(x, y): Origin) -> Self {
[encode_u16(x), encode_u16(y)].concat()
}
}
/// Encode size as big endian
impl From<Size> for Frame {
fn from(Size(w, h): Size) -> Self {
[encode_u16(w), encode_u16(h)].concat()
}
}
/// Encode window data
impl From<Window> for Frame {
fn from(Window(origin, size): Window) -> Self {
[Frame::from(origin), Frame::from(size)].concat()
}
}
/// Encode raw graphics
impl From<GraphicsRaw> for Data {
fn from(raw: GraphicsRaw) -> Self {
let GraphicsRaw(offset, data) = raw;
vec![[
CMD_GFX_RAW.into(),
encode_u16(offset),
encode_u16(data.len() as u16),
encode_u16(0),
encode_u16(0),
data.into(),
]
.concat()]
}
}
/// Encode raw text byte command
impl From<TextRaw> for Data {
fn from(TextRaw(origin, bytes): TextRaw) -> Data {
let mut bytes = bytes.clone();
bytes.truncate(COLUMNS);
let size = Size(bytes.len() as u16, 1);
vec![[CMD_TEXT_RAW.into(), origin.into(), size.into(), bytes].concat()]
}
}
/// Encode luminance
impl From<Luminance> for Data {
fn from(luminance: Luminance) -> Data {
let Luminance(window, value) = luminance;
vec![[CMD_LUM_RAW.into(), Vec::from(window), encode_u16(value)].concat()]
}
}
/// Encode a text buffer as a series of commands (data).
impl From<TextBuffer> for Data {
fn from(TextBuffer(Origin(x, y), text): TextBuffer) -> Data {
let mut lines: Vec<&str> = text.split("\n").collect();
lines.truncate(ROWS);
let mut data = vec![];
for (i, line) in lines.iter().enumerate() {
// Convert utf8 to codepage 437
if let Ok(bytes) = line.to_cp437(&CP437_WINGDINGS) {
let mut bytes: Frame = bytes.into();
bytes.truncate(COLUMNS);
let len = bytes.len() as u16;
let pos = Origin(x, y + i as u16);
let size = Size(len, 1);
data.push(
[
Frame::from(CMD_TEXT_RAW),
pos.into(),
size.into(),
bytes.into(),
]
.concat(),
);
}
}
data
}
}
/// Encode text command
impl From<Text> for Data {
fn from(text: Text) -> Data {
match text {
Text::Raw(raw) => raw.into(),
Text::Buffer(buffer) => buffer.into(),
}
}
}
impl From<Graphics> for Data {
fn from(gfx: Graphics) -> Data {
match gfx {
Graphics::Raw(raw) => raw.into(),
}
}
}
/// Encode command
impl From<Command> for Data {
fn from(cmd: Command) -> Self {
match cmd {
Command::Reset => vec![vec![0x00, 0x08]],
Command::Clear => vec![vec![0x00, 0x02]],
Command::Reboot => vec![vec![0x00, 0x0b]],
Command::Fadeout => vec![vec![0x00, 0x0d]],
Command::Text(text) => text.into(),
Command::Luminance(lum) => lum.into(),
Command::Graphics(gfx) => gfx.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_origin_big_endian() {
let frame: Frame = Origin(23, 42).into();
assert_eq!(frame[0], 0);
assert_eq!(frame[1], 23);
assert_eq!(frame[2], 0);
assert_eq!(frame[3], 42);
let frame: Frame = Origin(0xabcd, 0xcdef).into();
assert_eq!(frame[0], 0xab);
assert_eq!(frame[1], 0xcd);
assert_eq!(frame[2], 0xcd);
assert_eq!(frame[3], 0xef);
}
#[test]
fn encode_size_big_endian() {
let frame: Frame = Size(23, 42).into();
assert_eq!(frame[0], 0);
assert_eq!(frame[1], 23);
assert_eq!(frame[2], 0);
assert_eq!(frame[3], 42);
let frame: Frame = Size(0xabcd, 0xcdef).into();
assert_eq!(frame[0], 0xab);
assert_eq!(frame[1], 0xcd);
assert_eq!(frame[2], 0xcd);
assert_eq!(frame[3], 0xef);
}
#[test]
fn data_from_raw_text() {
let bytes: Vec<u8> = "text123".into();
let len: u8 = bytes.len() as u8;
let cmd: Command = text::Raw::from(bytes).into();
let data: Data = cmd.into();
println!("data: {:?}\n", data);
assert_eq!(data[0][1], 0x03); // TEXT
assert_eq!(data[0][7], len);
}
#[test]
fn data_from_text_buffer() {
let text: String = "hello\ndisplay!".into();
let cmd: Command = text::Buffer::from(text).into();
let data: Data = cmd.into();
println!("data: {:?}\n", data);
assert_eq!(data.len(), 2); // 2 commands
assert_eq!(data[0][1], 0x03); // TEXT
assert_eq!(data[0][7], 5); // len(hallo)
assert_eq!(data[1][7], 8); // len(display!)
}
}

View file

@ -0,0 +1,62 @@
use std::convert::From;
use super::geometry::Origin;
/// TextRaw holds bytes and a window
pub struct Raw(pub Origin, pub Vec<u8>);
/// Convert from bytes
impl From<Vec<u8>> for Raw {
fn from(bytes: Vec<u8>) -> Self {
Self(Origin::default(), bytes)
}
}
/// TextBuffer holds a multiline block of utf8 text
/// data and a origin.
pub struct Buffer(pub Origin, pub String);
impl Buffer {
pub fn at(x: u16, y: u16, text: String) -> Self {
Self(Origin(x, y), text)
}
}
/// Implement convert from trait for String
impl From<String> for Buffer {
fn from(text: String) -> Self {
Self(Origin::default(), text)
}
}
/// Text Commands
pub enum Text {
Raw(Raw),
Buffer(Buffer),
}
impl From<Raw> for Text {
fn from(raw: Raw) -> Self {
Text::Raw(raw)
}
}
impl From<Buffer> for Text {
fn from(buffer: Buffer) -> Self {
Text::Buffer(buffer)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn buffer_from_string() {
let buf: Buffer = String::from("hej there!").into();
let Buffer(Origin(x, y), text) = buf;
assert_eq!(x, 0);
assert_eq!(y, 0);
assert_eq!(text, "hej there!");
}
}

View file

@ -1,51 +0,0 @@
//! An example for how to send text to the display.
use clap::Parser;
use servicepoint::{
CharGrid, CharGridCommand, ClearCommand, UdpSocketExt, TILE_WIDTH,
};
use std::net::UdpSocket;
#[derive(Parser, Debug)]
struct Cli {
#[arg(
short,
long,
default_value = "localhost:2342",
help = "Address of the display"
)]
destination: String,
#[arg(short, long, num_args = 1.., value_delimiter = '\n',
help = "Text to send - specify multiple times for multiple lines")]
text: Vec<String>,
#[arg(
short,
long,
default_value_t = true,
help = "Clear screen before sending text"
)]
clear: bool,
}
/// example: `cargo run -- --text "Hallo" --text "CCCB"`
fn main() {
let mut cli = Cli::parse();
if cli.text.is_empty() {
cli.text.push("Hello, CCCB!".to_string());
}
let connection = UdpSocket::bind_connect(&cli.destination)
.expect("could not connect to display");
if cli.clear {
connection
.send_command(ClearCommand)
.expect("sending clear failed");
}
let text = cli.text.join("\n");
let command: CharGridCommand = CharGrid::wrap_str(TILE_WIDTH, &text).into();
connection
.send_command(command)
.expect("sending text failed");
}

View file

@ -1,37 +0,0 @@
//! Show a brightness level test pattern on screen
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
DataRef, GridMut, UdpSocketExt, TILE_HEIGHT, TILE_WIDTH,
};
use std::net::UdpSocket;
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
}
fn main() {
let cli = Cli::parse();
let connection = UdpSocket::bind_connect(cli.destination)
.expect("could not connect to display");
let mut bitmap = Bitmap::max_sized();
bitmap.fill(true);
connection
.send_command(BitmapCommand::from(bitmap))
.expect("send failed");
let max_brightness: u8 = Brightness::MAX.into();
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
for (index, byte) in brightnesses.data_ref_mut().iter_mut().enumerate() {
let level = (index % u8::MAX as usize) as u8 % max_brightness;
*byte = Brightness::try_from(level).unwrap();
}
let command: BrightnessGridCommand = brightnesses.into();
connection.send_command(command).expect("send failed");
}

View file

@ -1,54 +0,0 @@
//! An example on how to modify the image on screen without knowing the current content.
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, GridMut, Origin, Packet,
UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT, PIXEL_WIDTH,
};
use std::{net::UdpSocket, thread, time::Duration};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long = "duration-ms", default_value_t = 5000)]
time: u64,
}
fn main() {
let cli = Cli::parse();
let sleep_duration = Duration::max(
FRAME_PACING,
Duration::from_millis(cli.time / PIXEL_WIDTH as u64),
);
let connection = UdpSocket::bind_connect(cli.destination)
.expect("could not connect to display");
let mut command = BitmapCommand {
compression: CompressionCode::Uncompressed,
bitmap: Bitmap::max_sized(),
origin: Origin::ZERO,
};
command.bitmap.fill(true);
let mut buf = [0u8; 10000];
for x_offset in 0..PIXEL_WIDTH {
for y in 0..PIXEL_HEIGHT {
command.bitmap.set((x_offset + y) % PIXEL_WIDTH, y, false);
}
let packet: Packet = Packet::try_from(&command)
.expect("could not turn command into packet");
let size = packet
.serialize_to(&mut buf)
.expect("failed to serialize packet");
connection
.send(&buf[..size])
.expect("could not send command to display");
thread::sleep(sleep_duration);
}
}

View file

@ -1,84 +0,0 @@
//! A simple game of life implementation to show how to render graphics to the display.
use clap::Parser;
use rand::Rng;
use servicepoint::{
Bitmap, BitmapCommand, Grid, GridMut, UdpSocketExt, FRAME_PACING,
};
use std::{net::UdpSocket, thread};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long, default_value_t = 0.5f64)]
probability: f64,
}
fn main() {
let cli = Cli::parse();
let connection = UdpSocket::bind_connect(&cli.destination)
.expect("could not connect to display");
let mut command = BitmapCommand::from(make_random_field(cli.probability));
loop {
connection.send_command(&command).expect("could not send");
thread::sleep(FRAME_PACING);
command.bitmap = iteration(command.bitmap);
}
}
fn iteration(field: Bitmap) -> Bitmap {
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 =
matches!((old_state, neighbors), (true, 2 | 3) | (false, 3));
next.set(x, y, new_state);
}
}
next
}
fn count_neighbors(field: &Bitmap, x: i32, y: i32) -> i32 {
let mut count = 0;
for nx in x - 1..=x + 1 {
for ny in y - 1..=y + 1 {
if nx == x && ny == y {
continue; // the cell itself does not count
}
if nx < 0
|| ny < 0
|| nx >= field.width() as i32
|| ny >= field.height() as i32
{
continue; // pixels outside the grid do not count
}
if !field.get(nx as usize, ny as usize) {
continue; // dead cells do not count
}
count += 1;
}
}
count
}
fn make_random_field(probability: f64) -> Bitmap {
let mut field = Bitmap::max_sized();
let mut rng = rand::rng();
let d = rand::distr::Bernoulli::new(probability).unwrap();
for x in 0..field.width() {
for y in 0..field.height() {
field.set(x, y, rng.sample(d));
}
}
field
}

View file

@ -1,33 +0,0 @@
//! A simple example for how to send pixel data to the display.
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, GridMut, UdpSocketExt,
FRAME_PACING, PIXEL_HEIGHT, PIXEL_WIDTH,
};
use std::{net::UdpSocket, thread};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
}
fn main() {
let connection = UdpSocket::bind_connect(Cli::parse().destination)
.expect("could not connect to display");
let mut bitmap = Bitmap::max_sized();
for x_offset in 0..usize::MAX {
bitmap.fill(false);
for y in 0..PIXEL_HEIGHT {
bitmap.set((y + x_offset) % PIXEL_WIDTH, y, true);
}
let mut command = BitmapCommand::from(bitmap.clone());
command.compression = CompressionCode::Uncompressed;
connection.send_command(command).expect("send failed");
thread::sleep(FRAME_PACING);
}
}

View file

@ -1,49 +0,0 @@
//! An example on how to use the provided infrastructure to implement custom commands.
use rand::Rng;
use servicepoint::{
Brightness, GlobalBrightnessCommand, Header, Packet, UdpSocketExt,
};
use std::{fmt::Debug, net::UdpSocket};
/// Command that sets the brightness to zero globally.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct ZeroBrightnessCommand;
impl Into<Packet> for ZeroBrightnessCommand {
fn into(self) -> Packet {
GlobalBrightnessCommand::from(Brightness::MIN).into()
}
}
/// Command that turns into a random packet.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct FuzzyCommand;
impl TryInto<Packet> for FuzzyCommand {
type Error = ();
fn try_into(self) -> Result<Packet, Self::Error> {
let mut rng = rand::rng();
Ok(Packet {
payload: None,
header: Header {
command_code: rng.random(),
a: rng.random(),
b: rng.random(),
c: rng.random(),
d: rng.random(),
},
})
}
}
fn main() {
let connection = UdpSocket::bind_connect("172.23.42.29:2342")
.expect("could not connect to display");
for _ in 0..100 {
connection.send_command(FuzzyCommand).unwrap()
}
connection.send_command(ZeroBrightnessCommand).unwrap();
}

View file

@ -1,67 +0,0 @@
//! A simple example for how to set brightnesses for tiles on the screen.
//! Continuously changes the tiles in a random window to random brightnesses.
use clap::Parser;
use rand::Rng;
use servicepoint::{
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
GlobalBrightnessCommand, GridMut, Origin, UdpSocketExt, TILE_HEIGHT,
TILE_WIDTH,
};
use std::{net::UdpSocket, time::Duration};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long, default_value_t = true)]
enable_all: bool,
#[arg(short, long, default_value_t = 100, allow_negative_numbers = false)]
wait_ms: u64,
}
fn main() {
let cli = Cli::parse();
let connection = UdpSocket::bind_connect(cli.destination)
.expect("could not connect to display");
let wait_duration = Duration::from_millis(cli.wait_ms);
// put all pixels in on state
if cli.enable_all {
let mut filled_grid = Bitmap::max_sized();
filled_grid.fill(true);
let command = BitmapCommand::from(filled_grid);
connection.send_command(command).expect("send failed");
}
// set all pixels to the same random brightness
let mut rng = rand::rng();
let command: GlobalBrightnessCommand = rng.random::<Brightness>().into();
connection.send_command(command).unwrap();
// continuously update random windows to new random brightness
loop {
let min_size = 1;
let x = rng.random_range(0..TILE_WIDTH - min_size);
let y = rng.random_range(0..TILE_HEIGHT - min_size);
let w = rng.random_range(min_size..=TILE_WIDTH - x);
let h = rng.random_range(min_size..=TILE_HEIGHT - y);
let origin = Origin::new(x, y);
let mut luma = BrightnessGrid::new(w, h);
for y in 0..h {
for x in 0..w {
luma.set(x, y, rng.random());
}
}
connection
.send_command(BrightnessGridCommand { origin, grid: luma })
.unwrap();
std::thread::sleep(wait_duration);
}
}

View file

@ -1,47 +0,0 @@
//! An example for how to send text to the display - but optimized for minimal binary size.
//!
//! See [zerforschen.plus/posts/tiny-binaries-rust](https://zerforschen.plus/posts/tiny-binaries-rust/)
//! for details.
//!
//! The bulk of optimizations are compiler options, though there are some code changes that together
//! make a huge difference.
//!
//! To build this example inside this repository for the smallest possible size, you can run:
//! ```sh
//! RUSTFLAGS="-Zlocation-detail=none -Zfmt-debug=none" \
//! cargo build \
//! --example=tiny_announce \
//! --profile=size-optimized \
//! --no-default-features --features=protocol_udp \
//! -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \
//! -Zbuild-std-features="panic_immediate_abort"
//!```
//!
//! This requires unstable rust.
#![no_main]
use servicepoint::{CharGrid, CharGridCommand, ClearCommand, UdpSocketExt};
use std::net::{SocketAddr, UdpSocket};
/// This is the entry point of the example.
/// `#![no_main]` is used to remove the default rust main
/// Because we use `#![no_main]`, this is a C-style main function.
#[unsafe(no_mangle)]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
let addr = SocketAddr::from(([172, 23, 42, 29], 2342));
let connection = UdpSocket::bind_connect(addr).unwrap();
connection.send_command(ClearCommand).unwrap();
let grid = CharGrid::from_vec(
5,
vec!['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'],
)
.unwrap();
connection
.send_command(CharGridCommand::from(grid))
.unwrap();
0
}

View file

@ -1,43 +0,0 @@
//! An example on how to modify the image on screen without knowing the current content.
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, GridMut, UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT,
PIXEL_WIDTH,
};
use std::{net::UdpSocket, thread, time::Duration};
#[derive(Parser, Debug)]
struct Cli {
#[arg(short, long, default_value = "localhost:2342")]
destination: String,
#[arg(short, long = "duration-ms", default_value_t = 5000)]
time: u64,
}
fn main() {
let cli = Cli::parse();
let sleep_duration = Duration::max(
FRAME_PACING,
Duration::from_millis(cli.time / PIXEL_WIDTH as u64),
);
let connection = UdpSocket::bind_connect(cli.destination)
.expect("could not connect to display");
let mut enabled_pixels = Bitmap::max_sized();
enabled_pixels.fill(true);
for x_offset in 0..PIXEL_WIDTH {
for y in 0..PIXEL_HEIGHT {
enabled_pixels.set(x_offset % PIXEL_WIDTH, y, false);
}
let command = BitmapCommand::from(enabled_pixels.clone());
connection
.send_command(command)
.expect("could not send command to display");
thread::sleep(sleep_duration);
}
}

48
flake.lock generated
View file

@ -1,48 +0,0 @@
{
"nodes": {
"naersk": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1745925850,
"narHash": "sha256-cyAAMal0aPrlb1NgzMxZqeN1mAJ2pJseDhm2m6Um8T0=",
"owner": "nix-community",
"repo": "naersk",
"rev": "38bc60bbc157ae266d4a0c96671c6c742ee17a5f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1750838302,
"narHash": "sha256-aVkL3/yu50oQzi2YuKo0ceiCypVZpZXYd2P2p1FMJM4=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "7284e2decc982b81a296ab35aa46e804baaa1cfe",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-25.05",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"naersk": "naersk",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

155
flake.nix
View file

@ -1,155 +0,0 @@
{
description = "Flake for the servicepoint library.";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
inputs@{
self,
nixpkgs,
naersk,
}:
let
lib = nixpkgs.lib;
supported-systems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
];
forAllSystems =
f:
lib.genAttrs supported-systems (
system:
f rec {
pkgs = nixpkgs.legacyPackages.${system};
inherit system;
}
);
in
rec {
packages = forAllSystems (
{ pkgs, ... }:
let
naersk' = pkgs.callPackage naersk { };
nativeBuildInputs = with pkgs; [
pkg-config
makeWrapper
];
buildInputs = with pkgs; [
xe
xz
];
makeExample =
{
package,
example,
features ? "",
}:
naersk'.buildPackage {
pname = example;
cargoBuildOptions =
x:
x
++ [
"--package"
package
];
src = ./.;
inherit nativeBuildInputs buildInputs;
strictDeps = true;
gitSubmodules = true;
overrideMain = old: {
preConfigure = ''
cargo_build_options="$cargo_build_options --example ${example} ${
if features == "" then "" else "--features " + features
}"
'';
};
};
makePackage =
package:
let
package-param = [
"--package"
package
];
in
naersk'.buildPackage {
pname = package;
cargoBuildOptions = x: x ++ package-param;
cargoTestOptions = x: x ++ package-param;
src = ./.;
doCheck = true;
strictDeps = true;
inherit nativeBuildInputs buildInputs;
};
in
rec {
servicepoint = makePackage "servicepoint";
announce = makeExample {
package = "servicepoint";
example = "announce";
};
game-of-life = makeExample {
package = "servicepoint";
example = "game_of_life";
features = "rand";
};
moving-line = makeExample {
package = "servicepoint";
example = "moving_line";
};
random-brightness = makeExample {
package = "servicepoint";
example = "random_brightness";
features = "rand";
};
wiping-clear = makeExample {
package = "servicepoint";
example = "wiping_clear";
};
}
);
legacyPackages = packages;
devShells = forAllSystems (
{ pkgs, system }:
{
default = pkgs.mkShell rec {
inputsFrom = [ self.packages.${system}.servicepoint ];
packages = with pkgs; [
(pkgs.symlinkJoin {
name = "rust-toolchain";
paths = with pkgs; [
rustc
cargo
rustPlatform.rustcSrc
rustfmt
clippy
cargo-expand
cargo-tarpaulin
cargo-semver-checks
cargo-show-asm
cargo-flamegraph
];
})
];
LD_LIBRARY_PATH = "${pkgs.lib.makeLibraryPath (builtins.concatMap (d: d.buildInputs) inputsFrom)}";
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
RUST_LOG = "all";
RUST_BACKTRACE = "1";
};
}
);
formatter = forAllSystems ({ pkgs, ... }: pkgs.nixfmt-rfc-style);
};
}

View file

@ -1,2 +0,0 @@
#/usr/bin/env bash
cargo tarpaulin --out Html --all-features

1
hello_display/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

103
hello_display/Cargo.lock generated Normal file
View file

@ -0,0 +1,103 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "airportdisplay"
version = "0.1.0"
dependencies = [
"codepage-437",
]
[[package]]
name = "anyhow"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305"
[[package]]
name = "bstr"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
"serde",
]
[[package]]
name = "codepage-437"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e40c1169585d8d08e5675a39f2fc056cd19a258fc4cba5e3bbf4a9c1026de535"
dependencies = [
"csv",
]
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "hello_display"
version = "0.1.0"
dependencies = [
"airportdisplay",
"anyhow",
]
[[package]]
name = "itoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"

11
hello_display/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "hello_display"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1"
airportdisplay = { path = "../airportdisplay" }

View file

@ -0,0 +1 @@
max_width = 80

18
hello_display/src/main.rs Normal file
View file

@ -0,0 +1,18 @@
use airportdisplay::{text::Buffer, Command, Display};
use anyhow::Result;
fn main() -> Result<()> {
println!("Sending hello display...");
let display = Display::connect("172.23.42.29:2342".into())?;
let text: String =
"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥\n♥ mauuu ♥\n♥ mau mauuuunz! ♥\n♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥".into();
println!("{}", text);
display.send(Command::Clear)?;
display.send(Buffer::from(text).into())?;
Ok(())
}

103
send_text/Cargo.lock generated Normal file
View file

@ -0,0 +1,103 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "airportdisplay"
version = "0.1.0"
dependencies = [
"codepage-437",
]
[[package]]
name = "anyhow"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305"
[[package]]
name = "bstr"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
"serde",
]
[[package]]
name = "codepage-437"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e40c1169585d8d08e5675a39f2fc056cd19a258fc4cba5e3bbf4a9c1026de535"
dependencies = [
"csv",
]
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "itoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "send_text"
version = "0.1.0"
dependencies = [
"airportdisplay",
"anyhow",
]
[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"

9
send_text/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "send_text"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1"
airportdisplay = { path = "../airportdisplay" }

21
send_text/examples/cc.txt Normal file
View file

@ -0,0 +1,21 @@
,cCCCCCCCCCCCCCCCCCCCCCc,
CC' `\, C `CC
CC \\ C CC
CC `\, \================\, \ \ \
CC \===================\\ \ \ \
CC ,/====================\| |/|/\\
CC ,// /=================\====/////
CC // C CC .=====/ / /
CC, // B ,CC /-=-/ //
`CCCCCCCCCCCCCCCCCCCCCCC' / .\|/\/
X \
/ \ \____/
Willkommen im / | |
Catzen Computer Club Berlin / / |
| /
----|-`
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥ |
♥ mau mau mau ♥ |
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥ |
\
`

View file

@ -0,0 +1,20 @@
,cCCCCCCCCCCCCCCCCCCCCCc,
CC' `\, C `CC
CC \\ C CC
CC `\, \================\, \ \ \
CC \===================\\ \ \ \
CC ,/====================\| |/|/\\
CC ,// /=================\====/////
CC // C CC .=====/ / /
CC, // B ,CC /-=-/ //
`CCCCCCCCCCCCCCCCCCCCCCC' / .\|/\/
X \
/ \ \____/
Willkommen im / | |
Chaos Computer Club Berlin / / |
| /
----|-`
|
\
\
`

1
send_text/rustfmt.toml Normal file
View file

@ -0,0 +1 @@
max_width = 80

23
send_text/src/main.rs Normal file
View file

@ -0,0 +1,23 @@
use anyhow::Result;
use airportdisplay::{text::Buffer, Command, Display};
use std::io;
/// Send text read from stdio to the display
fn main() -> Result<()> {
// Read stdin into buffer
let mut text = String::new();
for line in io::stdin().lines() {
text.push_str(line?.as_str());
text.push('\n');
}
// Send content to display
let display = Display::connect("172.23.42.29:2342".into())?;
display.send(Command::Clear)?;
display.send(Buffer::from(text).into())?;
Ok(())
}

View file

@ -1,120 +0,0 @@
#[cfg(feature = "rand")]
use rand::{
distr::{Distribution, StandardUniform},
Rng,
};
/// A display brightness value, checked for correct value range
///
/// # Examples
///
/// ```
/// # use servicepoint::*;
/// let b = Brightness::MAX;
/// let val: u8 = b.into();
///
/// let b = Brightness::try_from(7).unwrap();
/// # let connection = FakeConnection;
/// let result = connection.send_command(GlobalBrightnessCommand::from(b));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Brightness(u8);
impl From<Brightness> for u8 {
fn from(brightness: Brightness) -> Self {
Self::from(&brightness)
}
}
impl From<&Brightness> for u8 {
fn from(brightness: &Brightness) -> Self {
brightness.0
}
}
impl TryFrom<u8> for Brightness {
type Error = u8;
fn try_from(value: u8) -> Result<Self, Self::Error> {
if value > Self::MAX.0 {
Err(value)
} else {
Ok(Brightness(value))
}
}
}
impl Brightness {
/// highest possible brightness value, 11
pub const MAX: Brightness = Brightness(11);
/// lowest possible brightness value, 0
pub const MIN: Brightness = Brightness(0);
/// Create a brightness value without returning an error for brightnesses above [`Brightness::MAX`].
///
/// returns: the specified value as a [Brightness], or [`Brightness::MAX`].
#[must_use]
pub fn saturating_from(value: u8) -> Brightness {
if value > Brightness::MAX.into() {
Brightness::MAX
} else {
Brightness(value)
}
}
}
impl Default for Brightness {
fn default() -> Self {
Self::MAX
}
}
#[cfg(feature = "rand")]
impl Distribution<Brightness> for StandardUniform {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Brightness {
Brightness(rng.random_range(Brightness::MIN.0..=Brightness::MAX.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn brightness_from_u8() {
assert_eq!(Err(100), Brightness::try_from(100));
assert_eq!(Ok(Brightness(1)), Brightness::try_from(1));
}
#[test]
#[cfg(feature = "rand")]
fn rand_brightness() {
let mut rng = rand::rng();
for _ in 0..100 {
let _: Brightness = rng.random();
}
}
#[test]
fn saturating_convert() {
assert_eq!(Brightness::MAX, Brightness::saturating_from(100));
assert_eq!(Brightness(5), Brightness::saturating_from(5));
}
#[test]
#[cfg(feature = "rand")]
fn test() {
let mut rng = rand::rng();
// more so random failure is less likely
assert_ne!(
[
rng.random::<Brightness>(),
rng.random(),
rng.random(),
rng.random()
],
[rng.random(), rng.random(), rng.random(), rng.random()]
);
}
}

View file

@ -1,252 +0,0 @@
/// The u16 command codes used for the [`crate::Command`]s.
#[repr(u16)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum CommandCode {
Clear = 0x0002,
Cp437Data = 0x0003,
CharBrightness = 0x0005,
Brightness = 0x0007,
HardReset = 0x000b,
FadeOut = 0x000d,
#[deprecated]
BitmapLegacy = 0x0010,
BitmapLinear = 0x0012,
BitmapLinearWinUncompressed = 0x0013,
BitmapLinearAnd = 0x0014,
BitmapLinearOr = 0x0015,
BitmapLinearXor = 0x0016,
#[cfg(feature = "compression_zlib")]
BitmapLinearWinZlib = 0x0017,
#[cfg(feature = "compression_bzip2")]
BitmapLinearWinBzip2 = 0x0018,
#[cfg(feature = "compression_lzma")]
BitmapLinearWinLzma = 0x0019,
Utf8Data = 0x0020,
#[cfg(feature = "compression_zstd")]
BitmapLinearWinZstd = 0x001A,
}
impl From<CommandCode> for u16 {
/// returns the u16 command code corresponding to the enum value
fn from(value: CommandCode) -> Self {
value as u16
}
}
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
#[error("The command code {0} is not known.")]
pub struct InvalidCommandCodeError(pub u16);
impl TryFrom<u16> for CommandCode {
type Error = InvalidCommandCodeError;
/// Returns the enum value for the specified `u16` or `Error` if the code is unknown.
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
value if value == CommandCode::Clear as u16 => {
Ok(CommandCode::Clear)
}
value if value == CommandCode::Cp437Data as u16 => {
Ok(CommandCode::Cp437Data)
}
value if value == CommandCode::CharBrightness as u16 => {
Ok(CommandCode::CharBrightness)
}
value if value == CommandCode::Brightness as u16 => {
Ok(CommandCode::Brightness)
}
value if value == CommandCode::HardReset as u16 => {
Ok(CommandCode::HardReset)
}
value if value == CommandCode::FadeOut as u16 => {
Ok(CommandCode::FadeOut)
}
#[allow(deprecated)]
value if value == CommandCode::BitmapLegacy as u16 => {
Ok(CommandCode::BitmapLegacy)
}
value if value == CommandCode::BitmapLinear as u16 => {
Ok(CommandCode::BitmapLinear)
}
value
if value == CommandCode::BitmapLinearWinUncompressed as u16 =>
{
Ok(CommandCode::BitmapLinearWinUncompressed)
}
value if value == CommandCode::BitmapLinearAnd as u16 => {
Ok(CommandCode::BitmapLinearAnd)
}
value if value == CommandCode::BitmapLinearOr as u16 => {
Ok(CommandCode::BitmapLinearOr)
}
value if value == CommandCode::BitmapLinearXor as u16 => {
Ok(CommandCode::BitmapLinearXor)
}
#[cfg(feature = "compression_zstd")]
value if value == CommandCode::BitmapLinearWinZstd as u16 => {
Ok(CommandCode::BitmapLinearWinZstd)
}
#[cfg(feature = "compression_lzma")]
value if value == CommandCode::BitmapLinearWinLzma as u16 => {
Ok(CommandCode::BitmapLinearWinLzma)
}
#[cfg(feature = "compression_zlib")]
value if value == CommandCode::BitmapLinearWinZlib as u16 => {
Ok(CommandCode::BitmapLinearWinZlib)
}
#[cfg(feature = "compression_bzip2")]
value if value == CommandCode::BitmapLinearWinBzip2 as u16 => {
Ok(CommandCode::BitmapLinearWinBzip2)
}
value if value == CommandCode::Utf8Data as u16 => {
Ok(CommandCode::Utf8Data)
}
_ => Err(InvalidCommandCodeError(value)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clear() {
assert_eq!(CommandCode::try_from(0x0002), Ok(CommandCode::Clear));
assert_eq!(u16::from(CommandCode::Clear), 0x0002);
}
#[test]
fn cp437_data() {
assert_eq!(CommandCode::try_from(0x0003), Ok(CommandCode::Cp437Data));
assert_eq!(u16::from(CommandCode::Cp437Data), 0x0003);
}
#[test]
fn char_brightness() {
assert_eq!(
CommandCode::try_from(0x0005),
Ok(CommandCode::CharBrightness)
);
assert_eq!(u16::from(CommandCode::CharBrightness), 0x0005);
}
#[test]
fn brightness() {
assert_eq!(CommandCode::try_from(0x0007), Ok(CommandCode::Brightness));
assert_eq!(u16::from(CommandCode::Brightness), 0x0007);
}
#[test]
fn hard_reset() {
assert_eq!(CommandCode::try_from(0x000b), Ok(CommandCode::HardReset));
assert_eq!(u16::from(CommandCode::HardReset), 0x000b);
}
#[test]
fn fade_out() {
assert_eq!(CommandCode::try_from(0x000d), Ok(CommandCode::FadeOut));
assert_eq!(u16::from(CommandCode::FadeOut), 0x000d);
}
#[test]
#[allow(deprecated)]
fn bitmap_legacy() {
assert_eq!(
CommandCode::try_from(0x0010),
Ok(CommandCode::BitmapLegacy)
);
assert_eq!(u16::from(CommandCode::BitmapLegacy), 0x0010);
}
#[test]
fn linear() {
assert_eq!(
CommandCode::try_from(0x0012),
Ok(CommandCode::BitmapLinear)
);
assert_eq!(u16::from(CommandCode::BitmapLinear), 0x0012);
}
#[test]
fn linear_and() {
assert_eq!(
CommandCode::try_from(0x0014),
Ok(CommandCode::BitmapLinearAnd)
);
assert_eq!(u16::from(CommandCode::BitmapLinearAnd), 0x0014);
}
#[test]
fn linear_xor() {
assert_eq!(
CommandCode::try_from(0x0016),
Ok(CommandCode::BitmapLinearXor)
);
assert_eq!(u16::from(CommandCode::BitmapLinearXor), 0x0016);
}
#[test]
#[cfg(feature = "compression_zlib")]
fn bitmap_win_zlib() {
assert_eq!(
CommandCode::try_from(0x0017),
Ok(CommandCode::BitmapLinearWinZlib)
);
assert_eq!(u16::from(CommandCode::BitmapLinearWinZlib), 0x0017);
}
#[test]
#[cfg(feature = "compression_bzip2")]
fn bitmap_win_bzip2() {
assert_eq!(
CommandCode::try_from(0x0018),
Ok(CommandCode::BitmapLinearWinBzip2)
);
assert_eq!(u16::from(CommandCode::BitmapLinearWinBzip2), 0x0018);
}
#[test]
#[cfg(feature = "compression_lzma")]
fn bitmap_win_lzma() {
assert_eq!(
CommandCode::try_from(0x0019),
Ok(CommandCode::BitmapLinearWinLzma)
);
assert_eq!(u16::from(CommandCode::BitmapLinearWinLzma), 0x0019);
}
#[test]
#[cfg(feature = "compression_zstd")]
fn bitmap_win_zstd() {
assert_eq!(
CommandCode::try_from(0x001A),
Ok(CommandCode::BitmapLinearWinZstd)
);
assert_eq!(u16::from(CommandCode::BitmapLinearWinZstd), 0x001A);
}
#[test]
fn bitmap_win_uncompressed() {
assert_eq!(
CommandCode::try_from(0x0013),
Ok(CommandCode::BitmapLinearWinUncompressed)
);
assert_eq!(u16::from(CommandCode::BitmapLinearWinUncompressed), 0x0013);
}
#[test]
fn utf8_data() {
assert_eq!(CommandCode::try_from(0x0020), Ok(CommandCode::Utf8Data));
assert_eq!(u16::from(CommandCode::Utf8Data), 0x0020);
}
#[test]
fn linear_or() {
assert_eq!(
CommandCode::try_from(0x0015),
Ok(CommandCode::BitmapLinearOr)
);
assert_eq!(u16::from(CommandCode::BitmapLinearOr), 0x0015);
}
}

View file

@ -1,342 +0,0 @@
use crate::{
command_code::{CommandCode, InvalidCommandCodeError},
commands::errors::{TryFromPacketError, TryIntoPacketError},
compression::{compress, decompress, CompressionError},
Bitmap, CompressionCode, DataRef, Grid, Header, Origin, Packet, Pixels,
TypedCommand, TILE_SIZE,
};
/// Overwrites a rectangular region of pixels.
///
/// Origin coordinates must be divisible by 8.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// #
/// let mut bitmap = Bitmap::max_sized();
/// // draw something to the pixels here
/// # bitmap.set(2, 5, true);
///
/// // create command to send pixels
/// let command = BitmapCommand {
/// bitmap,
/// origin: Origin::ZERO,
/// compression: CompressionCode::Uncompressed
/// };
///
/// connection.send_command(command).expect("send failed");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BitmapCommand {
/// the pixels to send
pub bitmap: Bitmap,
/// where to start drawing the pixels
pub origin: Origin<Pixels>,
/// how to compress the command when converting to packet
pub compression: CompressionCode,
}
impl TryFrom<&BitmapCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &BitmapCommand) -> Result<Self, Self::Error> {
let tile_x = (value.origin.x / TILE_SIZE).try_into()?;
let tile_w = (value.bitmap.width() / TILE_SIZE).try_into()?;
let pixel_h = value.bitmap.height().try_into()?;
let command =
BitmapCommand::command_code_for_compression(value.compression);
let data_ref = value.bitmap.data_ref();
let payload = match compress(value.compression, data_ref) {
Ok(payload) => payload,
Err(CompressionError::NoCompression) => data_ref.to_vec(),
Err(_) => return Err(TryIntoPacketError::CompressionFailed),
};
Ok(Packet {
header: Header {
command_code: command.into(),
a: tile_x,
b: value.origin.y.try_into()?,
c: tile_w,
d: pixel_h,
},
payload: Some(payload),
})
}
}
impl TryFrom<BitmapCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: BitmapCommand) -> Result<Self, Self::Error> {
Packet::try_from(&value)
}
}
impl TryFrom<Packet> for BitmapCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let code = CommandCode::try_from(packet.header.command_code)?;
let compression = BitmapCommand::compression_for_command_code(code)
.ok_or(InvalidCommandCodeError(packet.header.command_code))?;
let Packet {
header:
Header {
command_code: _,
a: tiles_x,
b: pixels_y,
c: tile_w,
d: pixel_h,
},
payload,
} = packet;
let expected = tile_w as usize * pixel_h as usize;
let payload =
payload.ok_or(TryFromPacketError::UnexpectedPayloadSize {
actual: 0,
expected,
})?;
let payload = match decompress(compression, &payload) {
Ok(payload) => payload,
Err(CompressionError::NoCompression) => payload,
Err(_) => return Err(TryFromPacketError::DecompressionFailed),
};
let bitmap = Bitmap::load(
tile_w as usize * TILE_SIZE,
pixel_h as usize,
&payload,
)?;
let origin =
Origin::new(tiles_x as usize * TILE_SIZE, pixels_y as usize);
Ok(Self {
bitmap,
origin,
compression,
})
}
}
impl From<BitmapCommand> for TypedCommand {
fn from(command: BitmapCommand) -> Self {
Self::Bitmap(command)
}
}
impl From<Bitmap> for BitmapCommand {
fn from(bitmap: Bitmap) -> Self {
Self {
bitmap,
origin: Origin::default(),
compression: CompressionCode::default(),
}
}
}
impl BitmapCommand {
fn command_code_for_compression(
compression_code: CompressionCode,
) -> CommandCode {
match compression_code {
CompressionCode::Uncompressed => {
CommandCode::BitmapLinearWinUncompressed
}
#[cfg(feature = "compression_zlib")]
CompressionCode::Zlib => CommandCode::BitmapLinearWinZlib,
#[cfg(feature = "compression_bzip2")]
CompressionCode::Bzip2 => CommandCode::BitmapLinearWinBzip2,
#[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => CommandCode::BitmapLinearWinLzma,
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => CommandCode::BitmapLinearWinZstd,
}
}
fn compression_for_command_code(
command_code: CommandCode,
) -> Option<CompressionCode> {
Some(match command_code {
CommandCode::BitmapLinearWinUncompressed => {
CompressionCode::Uncompressed
}
#[cfg(feature = "compression_zlib")]
CommandCode::BitmapLinearWinZlib => CompressionCode::Zlib,
#[cfg(feature = "compression_bzip2")]
CommandCode::BitmapLinearWinBzip2 => CompressionCode::Bzip2,
#[cfg(feature = "compression_lzma")]
CommandCode::BitmapLinearWinLzma => CompressionCode::Lzma,
#[cfg(feature = "compression_zstd")]
CommandCode::BitmapLinearWinZstd => CompressionCode::Zstd,
_ => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
command_code::CommandCode, commands::tests::TestImplementsCommand,
GridMut,
};
impl TestImplementsCommand for BitmapCommand {}
#[test]
fn command_code() {
assert_eq!(
BitmapCommand::try_from(Packet {
payload: None,
header: Header {
command_code: CommandCode::Brightness.into(),
..Default::default()
}
}),
Err(InvalidCommandCodeError(CommandCode::Brightness.into()).into())
);
}
#[test]
fn error_decompression_failed_win() {
for compression in CompressionCode::ALL {
let p: Packet = BitmapCommand {
origin: Origin::new(16, 8),
bitmap: Bitmap::new(8, 8).unwrap(),
compression: *compression,
}
.try_into()
.unwrap();
let Packet { header, payload } = p;
let mut payload = payload.unwrap();
// mangle it
for byte in &mut payload {
*byte -= *byte / 2;
}
let p = Packet {
header,
payload: Some(payload),
};
let result = TypedCommand::try_from(p);
if *compression != CompressionCode::Uncompressed {
assert_eq!(
result,
Err(TryFromPacketError::DecompressionFailed)
);
} else {
assert!(result.is_ok());
}
}
}
#[test]
fn into_command() {
let mut bitmap = Bitmap::max_sized();
bitmap.fill(true);
assert_eq!(
BitmapCommand::from(bitmap.clone()),
BitmapCommand {
bitmap,
origin: Origin::default(),
compression: CompressionCode::default()
},
)
}
#[test]
fn into_packet_out_of_range() {
let mut cmd = BitmapCommand::from(Bitmap::max_sized());
cmd.origin.x = usize::MAX;
assert!(matches!(
Packet::try_from(cmd),
Err(TryIntoPacketError::ConversionError(_))
));
}
#[test]
fn into_packet_invalid_alignment() {
let cmd = BitmapCommand {
bitmap: Bitmap::max_sized(),
compression: CompressionCode::Uncompressed,
origin: Origin::new(5, 0),
};
let packet = Packet::try_from(cmd).unwrap();
assert_eq!(
packet.header,
Header {
command_code: 19,
a: 0,
b: 0,
c: 56,
d: 160
}
);
let cmd = BitmapCommand {
bitmap: Bitmap::max_sized(),
compression: CompressionCode::Uncompressed,
origin: Origin::new(11, 0),
};
let packet = Packet::try_from(cmd).unwrap();
assert_eq!(
packet.header,
Header {
command_code: 19,
a: 1,
b: 0,
c: 56,
d: 160
}
);
}
#[test]
fn round_trip() {
for compression in CompressionCode::ALL {
crate::commands::tests::round_trip(
BitmapCommand {
origin: Origin::ZERO,
bitmap: Bitmap::new(8, 2).unwrap(),
compression: *compression,
}
.into(),
);
}
}
#[test]
fn round_trip_ref() {
for compression in CompressionCode::ALL {
crate::commands::tests::round_trip_ref(
&BitmapCommand {
origin: Origin::ZERO,
bitmap: Bitmap::max_sized(),
compression: *compression,
}
.into(),
);
}
}
#[test]
fn invalid_y() {
let command = BitmapCommand {
bitmap: Bitmap::new(8, 3).unwrap(),
origin: Origin::new(4, u16::MAX as usize + 1),
compression: CompressionCode::Uncompressed,
};
assert!(matches!(
Packet::try_from(command),
Err(TryIntoPacketError::ConversionError(_)),
))
}
}

View file

@ -1,87 +0,0 @@
#![allow(deprecated, reason = "this implements the deprecated functionality")]
use crate::{
command_code::CommandCode, commands::check_command_code_only,
commands::errors::TryFromPacketError, Packet, TypedCommand,
};
use std::fmt::Debug;
/// Legacy command code, gets ignored by the real display.
///
/// Might be useful as a noop package.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// // this sends a packet that does nothing
/// connection.send_command(BitmapLegacyCommand).unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[deprecated]
pub struct BitmapLegacyCommand;
impl TryFrom<Packet> for BitmapLegacyCommand {
type Error = TryFromPacketError;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
if let Some(e) =
check_command_code_only(value, CommandCode::BitmapLegacy)
{
Err(e)
} else {
Ok(Self)
}
}
}
impl From<BitmapLegacyCommand> for Packet {
fn from(value: BitmapLegacyCommand) -> Self {
Packet::from(&value)
}
}
impl From<&BitmapLegacyCommand> for Packet {
fn from(_: &BitmapLegacyCommand) -> Self {
Packet::command_code_only(CommandCode::BitmapLegacy)
}
}
impl From<BitmapLegacyCommand> for TypedCommand {
fn from(command: BitmapLegacyCommand) -> Self {
Self::BitmapLegacy(command)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{commands::tests::TestImplementsCommand, Header};
impl TestImplementsCommand for BitmapLegacyCommand {}
#[test]
fn invalid_fields() {
assert_eq!(
BitmapLegacyCommand::try_from(Packet {
header: Header {
command_code: CommandCode::BitmapLegacy.into(),
a: 1,
..Default::default()
},
payload: None,
}),
Err(TryFromPacketError::ExtraneousHeaderValues)
);
}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(BitmapLegacyCommand.into());
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(&BitmapLegacyCommand.into());
}
}

View file

@ -1,431 +0,0 @@
use crate::{
command_code::{CommandCode, InvalidCommandCodeError},
commands::errors::TryFromPacketError,
compression::{compress, decompress, CompressionError},
CompressionCode, DisplayBitVec, Header, Offset, Packet, TryIntoPacketError,
TypedCommand,
};
/// Binary operations for use with the [`BitVecCommand`] command.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
#[repr(u8)]
pub enum BinaryOperation {
/// r := a
#[default]
Overwrite,
/// r := a && b
And,
/// r := a || b
Or,
/// r := (a || b) && (a != b)
Xor,
}
/// Set pixel data starting at the pixel offset on screen.
///
/// The screen will continuously overwrite more pixel data without regarding the offset, meaning
/// once the starting row is full, overwriting will continue on column 0.
///
/// The [`BinaryOperation`] will be applied on the display comparing old and sent bit.
///
/// `new_bit = old_bit op sent_bit`
///
/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels.
///
/// The contained [`DisplayBitVec`] is always uncompressed.
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
pub struct BitVecCommand {
/// the pixels to send to the display as one long row
pub bitvec: DisplayBitVec,
/// where to start overwriting pixel data
pub offset: Offset,
/// The operation to apply on the display per bit comparing old and new state.
pub operation: BinaryOperation,
/// how to compress the command when converting to packet
pub compression: CompressionCode,
}
impl TryFrom<BitVecCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: BitVecCommand) -> Result<Self, Self::Error> {
Packet::try_from(&value)
}
}
impl TryFrom<&BitVecCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &BitVecCommand) -> Result<Self, Self::Error> {
let command_code = match value.operation {
BinaryOperation::Overwrite => CommandCode::BitmapLinear,
BinaryOperation::And => CommandCode::BitmapLinearAnd,
BinaryOperation::Or => CommandCode::BitmapLinearOr,
BinaryOperation::Xor => CommandCode::BitmapLinearXor,
};
let data_ref = value.bitvec.as_raw_slice();
let length = data_ref.len().try_into()?;
let payload = match compress(value.compression, data_ref) {
Ok(payload) => payload,
Err(CompressionError::NoCompression) => data_ref.to_vec(),
Err(_) => return Err(TryIntoPacketError::CompressionFailed),
};
Ok(Packet {
header: Header {
command_code: command_code.into(),
a: value.offset.try_into()?,
b: length,
c: value.compression.into(),
d: 0,
},
payload: Some(payload),
})
}
}
impl TryFrom<Packet> for BitVecCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet {
header:
Header {
command_code,
a: offset,
b: expected_len,
c: sub,
d: reserved,
..
},
payload,
} = packet;
let command_code = CommandCode::try_from(command_code)?;
let operation = match command_code {
CommandCode::BitmapLinear => BinaryOperation::Overwrite,
CommandCode::BitmapLinearAnd => BinaryOperation::And,
CommandCode::BitmapLinearOr => BinaryOperation::Or,
CommandCode::BitmapLinearXor => BinaryOperation::Xor,
_ => {
return Err(InvalidCommandCodeError(command_code.into()).into());
}
};
if reserved != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
let compression = CompressionCode::try_from(sub)?;
let payload =
payload.ok_or(TryFromPacketError::UnexpectedPayloadSize {
expected: expected_len as usize,
actual: 0,
})?;
let payload = match decompress(compression, &payload) {
Ok(payload) => payload,
Err(CompressionError::NoCompression) => payload.clone(),
Err(_) => return Err(TryFromPacketError::DecompressionFailed),
};
if payload.len() != expected_len as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected: expected_len as usize,
actual: payload.len(),
});
}
Ok(Self {
offset: offset as Offset,
bitvec: DisplayBitVec::from_vec(payload),
compression,
operation,
})
}
}
impl From<BitVecCommand> for TypedCommand {
fn from(command: BitVecCommand) -> Self {
Self::BitVec(command)
}
}
impl From<DisplayBitVec> for BitVecCommand {
fn from(bitvec: DisplayBitVec) -> Self {
Self {
bitvec,
operation: BinaryOperation::default(),
offset: Offset::default(),
compression: CompressionCode::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
commands, commands::tests::TestImplementsCommand,
compression_code::InvalidCompressionCodeError, PIXEL_WIDTH,
};
impl TestImplementsCommand for BitVecCommand {}
#[test]
fn command_code() {
assert_eq!(
BitVecCommand::try_from(Packet {
payload: None,
header: Header {
command_code: CommandCode::Brightness.into(),
..Default::default()
}
}),
Err(InvalidCommandCodeError(CommandCode::Brightness.into()).into())
);
}
#[test]
fn round_trip() {
for compression in CompressionCode::ALL {
for operation in [
BinaryOperation::Overwrite,
BinaryOperation::And,
BinaryOperation::Or,
BinaryOperation::Xor,
] {
crate::commands::tests::round_trip(
BitVecCommand {
offset: 23,
bitvec: DisplayBitVec::repeat(false, 40),
compression: *compression,
operation,
}
.into(),
);
}
}
}
#[test]
fn round_trip_ref() {
for compression in CompressionCode::ALL {
for operation in [
BinaryOperation::Overwrite,
BinaryOperation::And,
BinaryOperation::Or,
BinaryOperation::Xor,
] {
crate::commands::tests::round_trip(
BitVecCommand {
offset: 23,
bitvec: DisplayBitVec::repeat(false, 40),
compression: *compression,
operation,
}
.into(),
);
}
}
}
#[test]
fn error_decompression_failed_and() {
for compression in CompressionCode::ALL {
let p: Packet = commands::BitVecCommand {
offset: 0,
bitvec: DisplayBitVec::repeat(false, 8),
compression: *compression,
operation: BinaryOperation::Overwrite,
}
.try_into()
.unwrap();
let Packet { header, payload } = p;
let mut payload = payload.unwrap();
// mangle it
for byte in &mut payload {
*byte -= *byte / 2;
}
let p = Packet {
header,
payload: Some(payload),
};
let result = TypedCommand::try_from(p);
if *compression != CompressionCode::Uncompressed {
assert_eq!(
result,
Err(TryFromPacketError::DecompressionFailed)
);
} else {
// when not compressing, there is no way to detect corrupted data
assert!(result.is_ok());
}
}
}
#[test]
fn error_reserved_used() {
let Packet { header, payload } = commands::BitVecCommand {
offset: 0,
bitvec: DisplayBitVec::repeat(false, 8),
compression: CompressionCode::Uncompressed,
operation: BinaryOperation::Or,
}
.try_into()
.unwrap();
let Header {
command_code: command,
a: offset,
b: length,
c: sub,
d: _reserved,
} = header;
let p = Packet {
header: Header {
command_code: command,
a: offset,
b: length,
c: sub,
d: 69,
},
payload,
};
assert_eq!(
TypedCommand::try_from(p),
Err(TryFromPacketError::ExtraneousHeaderValues)
);
}
#[test]
fn error_invalid_compression() {
let Packet { header, payload } = commands::BitVecCommand {
offset: 0,
bitvec: DisplayBitVec::repeat(false, 8),
compression: CompressionCode::Uncompressed,
operation: BinaryOperation::And,
}
.try_into()
.unwrap();
let Header {
command_code: command,
a: offset,
b: length,
c: _sub,
d: reserved,
} = header;
let p = Packet {
header: Header {
command_code: command,
a: offset,
b: length,
c: 42,
d: reserved,
},
payload,
};
assert_eq!(
TypedCommand::try_from(p),
Err(InvalidCompressionCodeError(42).into())
);
}
#[test]
fn error_unexpected_size() {
let Packet { header, payload } = commands::BitVecCommand {
offset: 0,
bitvec: DisplayBitVec::repeat(false, 8),
compression: CompressionCode::Uncompressed,
operation: BinaryOperation::Xor,
}
.try_into()
.unwrap();
let Header {
command_code: command,
a: offset,
b: length,
c: compression,
d: reserved,
} = header;
let p = Packet {
header: Header {
command_code: command,
a: offset,
b: 420,
c: compression,
d: reserved,
},
payload,
};
assert_eq!(
TypedCommand::try_from(p),
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 420,
actual: length as usize,
})
);
}
#[test]
fn into_command() {
let mut bitvec = DisplayBitVec::repeat(true, PIXEL_WIDTH);
bitvec.fill(true);
assert_eq!(
BitVecCommand::from(bitvec.clone()),
BitVecCommand {
bitvec,
offset: 0,
compression: CompressionCode::default(),
operation: BinaryOperation::Overwrite,
},
)
}
#[test]
fn into_packet_invalid_alignment() {
let mut cmd = BitVecCommand::from(DisplayBitVec::repeat(false, 32));
cmd.offset = 5;
cmd.compression = CompressionCode::Uncompressed;
let packet = Packet::try_from(cmd).unwrap();
assert_eq!(
packet.header,
Header {
command_code: 18,
a: 5,
b: 4,
c: 0,
d: 0
}
);
let cmd = BitVecCommand {
bitvec: DisplayBitVec::repeat(false, 32),
offset: 11,
operation: BinaryOperation::Overwrite,
compression: CompressionCode::Uncompressed,
};
let packet = Packet::try_from(cmd).unwrap();
assert_eq!(
packet.header,
Header {
command_code: 18,
a: 11,
b: 4,
c: 0,
d: 0
}
);
}
#[test]
fn invalid_offset() {
let command = BitVecCommand {
bitvec: DisplayBitVec::repeat(false, 16),
offset: u16::MAX as usize + 1,
operation: BinaryOperation::Or,
compression: CompressionCode::Uncompressed,
};
assert!(matches!(
Packet::try_from(command),
Err(TryIntoPacketError::ConversionError(_)),
))
}
}

View file

@ -1,201 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code,
commands::errors::TryFromPacketError, BrightnessGrid, ByteGrid, Header,
Origin, Packet, Tiles, TryIntoPacketError, TypedCommand,
};
/// Set the brightness of individual tiles in a rectangular area of the display.
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
pub struct BrightnessGridCommand {
/// the brightness values per tile
pub grid: BrightnessGrid,
/// which tile the brightness rectangle should start
pub origin: Origin<Tiles>,
}
impl TryFrom<BrightnessGridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: BrightnessGridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_to_packet(
value.origin,
value.grid,
CommandCode::CharBrightness,
)?)
}
}
impl TryFrom<&BrightnessGridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &BrightnessGridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_as_packet(
value.origin,
&value.grid,
CommandCode::CharBrightness,
)?)
}
}
impl From<BrightnessGrid> for BrightnessGridCommand {
fn from(grid: BrightnessGrid) -> Self {
Self {
grid,
origin: Origin::default(),
}
}
}
impl TryFrom<Packet> for BrightnessGridCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet {
header:
Header {
command_code,
a: x,
b: y,
c: width,
d: height,
},
payload,
} = packet;
check_command_code(command_code, CommandCode::CharBrightness)?;
let expected_size = width as usize * height as usize;
let payload = match payload {
None => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 0,
expected: expected_size,
})
}
Some(payload) if payload.len() != expected_size => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
actual: payload.len(),
expected: expected_size,
})
}
Some(payload) => payload,
};
let grid = ByteGrid::from_raw_parts_unchecked(
width as usize,
height as usize,
payload,
);
let grid = match BrightnessGrid::try_from(grid) {
Ok(grid) => grid,
Err(val) => return Err(TryFromPacketError::InvalidBrightness(val)),
};
Ok(Self {
grid,
origin: Origin::new(x as usize, y as usize),
})
}
}
impl From<BrightnessGridCommand> for TypedCommand {
fn from(command: BrightnessGridCommand) -> Self {
Self::BrightnessGrid(command)
}
}
#[cfg(test)]
mod tests {
use crate::{
commands::{errors::TryFromPacketError, tests::TestImplementsCommand},
Brightness, BrightnessGrid, BrightnessGridCommand, Origin, Packet,
TypedCommand,
};
impl TestImplementsCommand for BrightnessGridCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(
BrightnessGridCommand {
origin: Origin::new(5, 2),
grid: BrightnessGrid::new(7, 5),
}
.into(),
);
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(
&BrightnessGridCommand {
origin: Origin::new(5, 2),
grid: BrightnessGrid::new(7, 5),
}
.into(),
);
}
#[test]
fn packet_into_char_brightness_invalid() {
let grid = BrightnessGrid::new(2, 2);
let command = BrightnessGridCommand {
origin: Origin::ZERO,
grid,
};
let mut packet: Packet = command.try_into().unwrap();
let slot = packet.payload.as_mut().unwrap().get_mut(1).unwrap();
*slot = 23;
assert_eq!(
TypedCommand::try_from(packet),
Err(TryFromPacketError::InvalidBrightness(23))
);
}
#[test]
fn into_command() {
let mut grid = BrightnessGrid::new(2, 3);
grid.iter_mut().enumerate().for_each(|(index, value)| {
*value = Brightness::saturating_from(index as u8)
});
assert_eq!(
BrightnessGridCommand::from(grid.clone()),
BrightnessGridCommand {
grid,
origin: Origin::default(),
},
)
}
#[test]
fn invalid_size() {
let command: BrightnessGridCommand = BrightnessGrid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: Some(packet.payload.as_ref().unwrap()[..5].to_vec()),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
BrightnessGridCommand::try_from(packet)
);
}
#[test]
fn missing_payload() {
let command: BrightnessGridCommand = BrightnessGrid::new(2, 3).into();
let mut packet: Packet = command.try_into().unwrap();
packet.payload = None;
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 0,
expected: 6
}),
BrightnessGridCommand::try_from(packet)
);
}
}

View file

@ -1,192 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code,
commands::errors::TryFromPacketError, CharGrid, Header, Origin, Packet,
Tiles, TryIntoPacketError, TypedCommand,
};
/// Show text on the screen.
///
/// The text is sent in the form of a 2D grid of UTF-8 encoded characters (the default encoding in rust).
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// let grid = CharGrid::from("Hello,\nWorld!");
/// connection.send_command(CharGridCommand { origin: Origin::ZERO, grid }).expect("send failed");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CharGridCommand {
/// the text to send to the display
pub grid: CharGrid,
/// which tile the text should start on
pub origin: Origin<Tiles>,
}
impl TryFrom<CharGridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: CharGridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_to_packet(
value.origin,
value.grid,
CommandCode::Utf8Data,
)?)
}
}
impl TryFrom<&CharGridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &CharGridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_as_packet(
value.origin,
&value.grid,
CommandCode::Utf8Data,
)?)
}
}
impl TryFrom<Packet> for CharGridCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet {
header:
Header {
command_code,
a: origin_x,
b: origin_y,
c: width,
d: height,
},
payload,
} = packet;
check_command_code(command_code, CommandCode::Utf8Data)?;
let expected = width as usize * height as usize;
let payload = match payload {
None => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: 0,
})
}
Some(payload) if payload.len() != expected => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: payload.len(),
})
}
Some(payload) => {
String::from_utf8(payload.clone())?.chars().collect()
}
};
Ok(Self {
origin: Origin::new(origin_x as usize, origin_y as usize),
grid: CharGrid::from_raw_parts_unchecked(
width as usize,
height as usize,
payload,
),
})
}
}
impl From<CharGridCommand> for TypedCommand {
fn from(command: CharGridCommand) -> Self {
Self::CharGrid(command)
}
}
impl From<CharGrid> for CharGridCommand {
fn from(grid: CharGrid) -> Self {
Self {
grid,
origin: Origin::default(),
}
}
}
#[cfg(test)]
mod tests {
use crate::{
commands::tests::TestImplementsCommand, CharGrid, CharGridCommand,
Origin, Packet, TryFromPacketError,
};
impl TestImplementsCommand for CharGridCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(
CharGridCommand {
origin: Origin::new(5, 2),
grid: CharGrid::new(7, 5),
}
.into(),
);
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(
&CharGridCommand {
origin: Origin::new(5, 2),
grid: CharGrid::new(7, 5),
}
.into(),
);
}
#[test]
#[cfg(feature = "cp437")]
fn into_command() {
let mut grid = CharGrid::new(2, 3);
grid.iter_mut().enumerate().for_each(|(index, value)| {
*value = crate::cp437::cp437_to_char(index as u8)
});
assert_eq!(
CharGridCommand::from(grid.clone()),
CharGridCommand {
grid,
origin: Origin::default(),
},
)
}
#[test]
fn invalid_size() {
let command: CharGridCommand = CharGrid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: Some(packet.payload.as_ref().unwrap()[..5].to_vec()),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
CharGridCommand::try_from(packet)
);
}
#[test]
fn missing_payload() {
let command: CharGridCommand = CharGrid::new(2, 3).into();
let mut packet: Packet = command.try_into().unwrap();
packet.payload = None;
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 0,
expected: 6
}),
CharGridCommand::try_from(packet)
);
}
}

View file

@ -1,105 +0,0 @@
use crate::{
command_code::CommandCode,
commands::{check_command_code_only, errors::TryFromPacketError},
Packet, TypedCommand,
};
use std::fmt::Debug;
/// Set all pixels to the off state. Does not affect brightness.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// connection.send_command(ClearCommand).unwrap();
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// ```
pub struct ClearCommand;
impl TryFrom<Packet> for ClearCommand {
type Error = TryFromPacketError;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
if let Some(e) = check_command_code_only(value, CommandCode::Clear) {
Err(e)
} else {
Ok(Self)
}
}
}
impl From<ClearCommand> for Packet {
fn from(value: ClearCommand) -> Self {
Packet::from(&value)
}
}
impl From<&ClearCommand> for Packet {
fn from(_: &ClearCommand) -> Self {
Packet::command_code_only(CommandCode::Clear)
}
}
impl From<ClearCommand> for TypedCommand {
fn from(command: ClearCommand) -> Self {
Self::Clear(command)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command_code::InvalidCommandCodeError;
use crate::commands::tests::TestImplementsCommand;
use crate::Header;
impl TestImplementsCommand for ClearCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(ClearCommand.into());
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(&ClearCommand.into());
}
#[test]
fn extraneous_header_values() {
let p = Packet {
header: Header {
command_code: CommandCode::Clear.into(),
a: 0x05,
b: 0x00,
c: 0x00,
d: 0x00,
},
payload: None,
};
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
));
}
#[test]
fn invalid_command_code() {
let p = Packet {
header: Header {
command_code: CommandCode::HardReset.into(),
a: 0x00,
b: 0x00,
c: 0x00,
d: 0x00,
},
payload: None,
};
assert_eq!(
Err(InvalidCommandCodeError(CommandCode::HardReset.into()).into()),
ClearCommand::try_from(p)
);
}
}

View file

@ -1,200 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code,
commands::errors::TryFromPacketError, Cp437Grid, Header, Origin, Packet,
Tiles, TryIntoPacketError, TypedCommand,
};
/// Show text on the screen.
///
/// The text is sent in the form of a 2D grid of [CP-437] encoded characters.
///
/// <div class="warning">You probably want to use [Command::Utf8Data] instead</div>
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// let grid = CharGrid::from("Hello,\nWorld!");
/// # #[cfg(feature = "cp437")] {
/// let grid = Cp437Grid::from(&grid);
/// connection.send_command(Cp437GridCommand{ origin: Origin::ZERO, grid }).expect("send failed");
/// # }
/// ```
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// let grid = Cp437Grid::load_ascii("Hello\nWorld", 5, false).unwrap();
/// connection.send_command(Cp437GridCommand{ origin: Origin::new(2, 2), grid }).unwrap();
/// ```
/// [CP-437]: https://en.wikipedia.org/wiki/Code_page_437
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cp437GridCommand {
/// the text to send to the display
pub grid: Cp437Grid,
/// which tile the text should start
pub origin: Origin<Tiles>,
}
impl TryFrom<Cp437GridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: Cp437GridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_to_packet(
value.origin,
value.grid,
CommandCode::Cp437Data,
)?)
}
}
impl TryFrom<&Cp437GridCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &Cp437GridCommand) -> Result<Self, Self::Error> {
Ok(Packet::origin_grid_as_packet(
value.origin,
&value.grid,
CommandCode::Cp437Data,
)?)
}
}
impl TryFrom<Packet> for Cp437GridCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet {
header:
Header {
command_code,
a: origin_x,
b: origin_y,
c: width,
d: height,
},
payload,
} = packet;
check_command_code(command_code, CommandCode::Cp437Data)?;
let expected = width as usize * height as usize;
let payload = match payload {
None => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: 0,
})
}
Some(payload) if payload.len() != expected => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected,
actual: payload.len(),
})
}
Some(payload) => payload,
};
Ok(Self {
origin: Origin::new(origin_x as usize, origin_y as usize),
grid: Cp437Grid::from_raw_parts_unchecked(
width as usize,
height as usize,
payload,
),
})
}
}
impl From<Cp437GridCommand> for TypedCommand {
fn from(command: Cp437GridCommand) -> Self {
Self::Cp437Grid(command)
}
}
impl From<Cp437Grid> for Cp437GridCommand {
fn from(grid: Cp437Grid) -> Self {
Self {
grid,
origin: Origin::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::commands::tests::TestImplementsCommand;
impl TestImplementsCommand for Cp437GridCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(
Cp437GridCommand {
origin: Origin::new(5, 2),
grid: Cp437Grid::new(7, 5),
}
.into(),
);
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(
&Cp437GridCommand {
origin: Origin::new(5, 2),
grid: Cp437Grid::new(7, 5),
}
.into(),
);
}
#[test]
fn into_command() {
let mut grid = Cp437Grid::new(2, 3);
grid.iter_mut()
.enumerate()
.for_each(|(index, value)| *value = index as u8);
assert_eq!(
Cp437GridCommand::from(grid.clone()),
Cp437GridCommand {
grid,
origin: Origin::default(),
},
)
}
#[test]
fn invalid_size() {
let command: Cp437GridCommand = Cp437Grid::new(2, 3).into();
let packet: Packet = command.try_into().unwrap();
let packet = Packet {
header: packet.header,
payload: Some(packet.payload.as_ref().unwrap()[..5].to_vec()),
};
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 5,
expected: 6
}),
Cp437GridCommand::try_from(packet)
);
}
#[test]
fn missing_payload() {
let command: Cp437GridCommand = Cp437Grid::new(2, 3).into();
let mut packet: Packet = command.try_into().unwrap();
packet.payload = None;
assert_eq!(
Err(TryFromPacketError::UnexpectedPayloadSize {
actual: 0,
expected: 6
}),
Cp437GridCommand::try_from(packet)
);
}
}

View file

@ -1,54 +0,0 @@
use crate::{
command_code::InvalidCommandCodeError,
compression_code::InvalidCompressionCodeError, LoadBitmapError,
};
use std::num::TryFromIntError;
/// Err values for [`crate::TypedCommand::try_from`].
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum TryFromPacketError {
/// the contained command code does not correspond to a known command
#[error(transparent)]
InvalidCommand(#[from] InvalidCommandCodeError),
/// the expected payload size was n, but size m was found
#[error(
"the expected payload size was {actual}, but size {expected} was found"
)]
UnexpectedPayloadSize {
/// size of the provided payload
actual: usize,
/// expected size according to command or header values
expected: usize,
},
/// Header fields not needed for the command have been used.
///
/// Note that these commands would usually still work on the actual display.
#[error("Header fields not needed for the command have been used")]
ExtraneousHeaderValues,
/// The contained compression code is not known. This could be of disabled features.
#[error(transparent)]
InvalidCompression(#[from] InvalidCompressionCodeError),
/// Decompression of the payload failed. This can be caused by corrupted packets.
#[error("The decompression of the payload failed")]
DecompressionFailed,
/// The given brightness value is out of bounds
#[error("The given brightness value {0} is out of bounds.")]
InvalidBrightness(u8),
/// Some provided text was not valid UTF-8.
#[error(transparent)]
InvalidUtf8(#[from] std::string::FromUtf8Error),
/// The bitmap contained in the payload could not be loaded
#[error(transparent)]
LoadBitmapFailed(#[from] LoadBitmapError),
}
/// An error that can occur when parsing a raw packet as a command
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum TryIntoPacketError {
/// Compression of the payload failed.
#[error("The compression of the payload failed")]
CompressionFailed,
/// Conversion (probably to u16) failed
#[error(transparent)]
ConversionError(#[from] TryFromIntError),
}

View file

@ -1,111 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code_only,
commands::errors::TryFromPacketError, Packet, TypedCommand,
};
use std::fmt::Debug;
/// <div class="warning">Untested</div>
///
/// Slowly decrease brightness until off or something like that?
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// connection.send_command(FadeOutCommand).unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FadeOutCommand;
impl TryFrom<Packet> for FadeOutCommand {
type Error = TryFromPacketError;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
if let Some(e) = check_command_code_only(value, CommandCode::FadeOut) {
Err(e)
} else {
Ok(Self)
}
}
}
impl From<FadeOutCommand> for Packet {
fn from(value: FadeOutCommand) -> Self {
Packet::from(&value)
}
}
impl From<&FadeOutCommand> for Packet {
fn from(_: &FadeOutCommand) -> Self {
Packet::command_code_only(CommandCode::FadeOut)
}
}
impl From<FadeOutCommand> for TypedCommand {
fn from(command: FadeOutCommand) -> Self {
Self::FadeOut(command)
}
}
#[cfg(test)]
mod tests {
use crate::{
command_code::CommandCode,
commands::{errors::TryFromPacketError, tests::TestImplementsCommand},
FadeOutCommand, Header, Packet, TypedCommand,
};
impl TestImplementsCommand for FadeOutCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(FadeOutCommand.into());
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(&FadeOutCommand.into());
}
#[test]
fn error_extraneous_header_fade_out() {
let p = Packet {
header: Header {
command_code: CommandCode::FadeOut.into(),
a: 0x10,
b: 0x00,
c: 0x00,
d: 0x01,
},
payload: None,
};
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
));
}
#[test]
fn error_unexpected_payload() {
let p = Packet {
header: Header {
command_code: CommandCode::FadeOut.into(),
a: 0x00,
b: 0x00,
c: 0x00,
d: 0x00,
},
payload: Some(vec![5, 7]),
};
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 0,
actual: 2
})
));
}
}

View file

@ -1,220 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code,
commands::errors::TryFromPacketError, Brightness, Header, Packet,
TypedCommand,
};
/// Set the brightness of all tiles to the same value.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// let command = GlobalBrightnessCommand { brightness: Brightness::MAX };
/// connection.send_command(command).unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GlobalBrightnessCommand {
/// the brightness to set all pixels to
pub brightness: Brightness,
}
impl From<GlobalBrightnessCommand> for Packet {
fn from(value: GlobalBrightnessCommand) -> Self {
Packet::from(&value)
}
}
impl From<&GlobalBrightnessCommand> for Packet {
fn from(command: &GlobalBrightnessCommand) -> Self {
Self {
header: Header {
command_code: CommandCode::Brightness.into(),
a: 0x00000,
b: 0x0000,
c: 0x0000,
d: 0x0000,
},
payload: Some(vec![command.brightness.into()]),
}
}
}
impl TryFrom<Packet> for GlobalBrightnessCommand {
type Error = TryFromPacketError;
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
let Packet {
header:
Header {
command_code,
a,
b,
c,
d,
},
payload,
} = packet;
check_command_code(command_code, CommandCode::Brightness)?;
if a != 0 || b != 0 || c != 0 || d != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues);
}
let brightness = match payload {
None => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: 0,
})
}
Some(payload) if payload.len() == 1 => payload[0],
Some(payload) => {
return Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: payload.len(),
});
}
};
match Brightness::try_from(brightness) {
Ok(brightness) => Ok(Self { brightness }),
Err(_) => Err(TryFromPacketError::InvalidBrightness(brightness)),
}
}
}
impl From<GlobalBrightnessCommand> for TypedCommand {
fn from(command: GlobalBrightnessCommand) -> Self {
Self::Brightness(command)
}
}
impl From<Brightness> for GlobalBrightnessCommand {
fn from(brightness: Brightness) -> Self {
GlobalBrightnessCommand { brightness }
}
}
#[cfg(test)]
mod tests {
use crate::{
command_code::CommandCode,
commands::{errors::TryFromPacketError, tests::TestImplementsCommand},
Brightness, GlobalBrightnessCommand, Header, Packet, TypedCommand,
};
impl TestImplementsCommand for GlobalBrightnessCommand {}
#[test]
fn brightness_as_command() {
assert_eq!(
GlobalBrightnessCommand {
brightness: Brightness::MAX
},
Brightness::MAX.into()
);
}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(
GlobalBrightnessCommand {
brightness: Brightness::try_from(6).unwrap(),
}
.into(),
);
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(
&GlobalBrightnessCommand {
brightness: Brightness::try_from(6).unwrap(),
}
.into(),
);
}
#[test]
fn error_extraneous_header_values() {
let p = Packet {
header: Header {
command_code: CommandCode::Brightness.into(),
a: 0x00,
b: 0x13,
c: 0x37,
d: 0x00,
},
payload: Some(vec![5]),
};
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
));
}
#[test]
fn unexpected_payload_size_brightness() {
assert_eq!(
TypedCommand::try_from(Packet {
header: Header {
command_code: CommandCode::Brightness.into(),
a: 0,
b: 0,
c: 0,
d: 0,
},
payload: None
}),
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: 0
})
);
assert_eq!(
TypedCommand::try_from(Packet {
header: Header {
command_code: CommandCode::Brightness.into(),
a: 0,
b: 0,
c: 0,
d: 0,
},
payload: Some(vec!(0, 0))
}),
Err(TryFromPacketError::UnexpectedPayloadSize {
expected: 1,
actual: 2
})
);
}
#[test]
fn packet_into_brightness_invalid() {
let mut packet: Packet = GlobalBrightnessCommand {
brightness: Brightness::MAX,
}
.into();
let slot = packet.payload.as_mut().unwrap().get_mut(0).unwrap();
*slot = 42;
assert_eq!(
TypedCommand::try_from(packet),
Err(TryFromPacketError::InvalidBrightness(42))
);
}
#[test]
fn into_command() {
assert_eq!(
GlobalBrightnessCommand::from(Brightness::MIN),
GlobalBrightnessCommand {
brightness: Brightness::MIN,
},
)
}
}

View file

@ -1,88 +0,0 @@
use crate::{
command_code::CommandCode, commands::check_command_code_only,
commands::errors::TryFromPacketError, Packet, TypedCommand,
};
use std::fmt::Debug;
/// Kills the udp daemon on the display, which usually results in a restart.
///
/// Please do not send this in your normal program flow.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// connection.send_command(HardResetCommand).unwrap();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HardResetCommand;
impl TryFrom<Packet> for HardResetCommand {
type Error = TryFromPacketError;
fn try_from(value: Packet) -> Result<Self, Self::Error> {
if let Some(e) = check_command_code_only(value, CommandCode::HardReset)
{
Err(e)
} else {
Ok(Self)
}
}
}
impl From<HardResetCommand> for Packet {
fn from(value: HardResetCommand) -> Self {
Packet::from(&value)
}
}
impl From<&HardResetCommand> for Packet {
fn from(_: &HardResetCommand) -> Self {
Packet::command_code_only(CommandCode::HardReset)
}
}
impl From<HardResetCommand> for TypedCommand {
fn from(command: HardResetCommand) -> Self {
Self::HardReset(command)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::commands::tests::TestImplementsCommand;
use crate::Header;
impl TestImplementsCommand for HardResetCommand {}
#[test]
fn round_trip() {
crate::commands::tests::round_trip(HardResetCommand.into());
}
#[test]
fn round_trip_ref() {
crate::commands::tests::round_trip_ref(&HardResetCommand.into());
}
#[test]
fn error_extraneous_header() {
let p = Packet {
header: Header {
command_code: CommandCode::HardReset.into(),
a: 0x00,
b: 0x00,
c: 0x00,
d: 0x01,
},
payload: None,
};
let result = TypedCommand::try_from(p);
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
));
}
}

View file

@ -1,156 +0,0 @@
mod bitmap;
mod bitmap_legacy;
mod bitvec;
mod brightness_grid;
mod char_grid;
mod clear;
mod cp437_grid;
mod errors;
mod fade_out;
mod global_brightness;
mod hard_reset;
mod typed;
use crate::{
command_code::{CommandCode, InvalidCommandCodeError},
Header, Packet,
};
pub use bitmap::*;
pub use bitmap_legacy::*;
pub use bitvec::*;
pub use brightness_grid::*;
pub use char_grid::*;
pub use clear::*;
pub use cp437_grid::*;
pub use errors::*;
pub use fade_out::*;
pub use global_brightness::*;
pub use hard_reset::*;
use std::{fmt::Debug, hash::Hash};
pub use typed::*;
/// This trait represents a command that can be sent to the display.
///
/// To send a [Command], use a [connection][std::net::UdpSocket].
///
/// # Available commands
///
/// To send text, take a look at [`Cp437GridCommand`].
///
/// To draw pixels, the easiest command to use is [`BitmapCommand`].
///
/// The other BitmapLinear-Commands operate on a region of pixel memory directly.
/// [`BitVecCommand`] overwrites a region or applies a logical operation per pixel with [`BinaryOperation`].
///
/// Out of bounds operations may be truncated or ignored by the display.
///
/// # Compression
///
/// Some commands can contain compressed payloads.
/// To get started, use `CompressionCode::default()`.
///
/// If you want to archive the best performance (e.g. latency),
/// you can try the different compression algorithms for your hardware and use case.
///
/// In memory, the payload is not compressed in the [Command].
/// Payload (de-)compression happens when converting the [Command] into a [Packet] or vice versa.
///
/// # Examples
///
/// ```rust
/// use servicepoint::*;
///
/// // create command
/// let command = GlobalBrightnessCommand{ brightness: Brightness::MAX };
///
/// // turn command into Packet
/// let packet: Packet = command.clone().into();
///
/// // read command from packet
/// let round_tripped = TypedCommand::try_from(packet).unwrap();
///
/// // round tripping produces exact copy
/// assert_eq!(round_tripped, TypedCommand::from(command.clone()));
///
/// // send command
/// # let connection = FakeConnection;
/// connection.send_command(command).unwrap();
/// ```
pub trait Command:
Debug + Clone + Eq + TryInto<Packet> + TryFrom<Packet> + Hash
{
}
impl<T: Debug + Clone + Eq + TryInto<Packet> + TryFrom<Packet> + Hash> Command
for T
{
}
fn check_command_code_only(
packet: Packet,
code: CommandCode,
) -> Option<TryFromPacketError> {
let Packet {
header:
Header {
command_code: _,
a,
b,
c,
d,
},
payload,
} = packet;
if packet.header.command_code != u16::from(code) {
Some(InvalidCommandCodeError(packet.header.command_code).into())
} else if let Some(payload) = payload {
Some(TryFromPacketError::UnexpectedPayloadSize {
expected: 0,
actual: payload.len(),
})
} else if a != 0 || b != 0 || c != 0 || d != 0 {
Some(TryFromPacketError::ExtraneousHeaderValues)
} else {
None
}
}
fn check_command_code(
actual: u16,
expected: CommandCode,
) -> Result<(), InvalidCommandCodeError> {
if actual == u16::from(expected) {
Ok(())
} else {
Err(InvalidCommandCodeError(actual))
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[allow(
unused,
reason = "false positive, used in submodules that check if structs impl Command"
)]
pub(crate) trait TestImplementsCommand: Command {}
pub(crate) fn round_trip(original: TypedCommand) {
let packet: Packet = original.clone().try_into().unwrap();
let copy: TypedCommand = match TypedCommand::try_from(packet) {
Ok(command) => command,
Err(err) => panic!("could not reload {original:?}: {err:?}"),
};
assert_eq!(copy, original);
}
pub(crate) fn round_trip_ref(original: &TypedCommand) {
let packet: Packet = original.try_into().unwrap();
let copy: TypedCommand = match TypedCommand::try_from(packet) {
Ok(command) => command,
Err(err) => panic!("could not reload {original:?}: {err:?}"),
};
assert_eq!(&copy, original);
}
}

View file

@ -1,153 +0,0 @@
use crate::{
command_code::CommandCode, commands::errors::TryFromPacketError,
BitVecCommand, BitmapCommand, BrightnessGridCommand, CharGridCommand,
ClearCommand, Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand,
HardResetCommand, Packet, TryIntoPacketError,
};
/// This enum contains all commands provided by the library.
/// This is useful in case you want one data type for all kinds of commands without using `dyn`.
///
/// Please look at the contained structs for documentation per command.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
#[allow(deprecated)]
pub enum TypedCommand {
Clear(ClearCommand),
CharGrid(CharGridCommand),
Cp437Grid(Cp437GridCommand),
Bitmap(BitmapCommand),
Brightness(GlobalBrightnessCommand),
BrightnessGrid(BrightnessGridCommand),
BitVec(BitVecCommand),
HardReset(HardResetCommand),
FadeOut(FadeOutCommand),
#[deprecated]
BitmapLegacy(crate::BitmapLegacyCommand),
}
impl TryFrom<Packet> for TypedCommand {
type Error = TryFromPacketError;
/// Try to interpret the [Packet] as one containing a [`TypedCommand`]
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
Ok(match CommandCode::try_from(packet.header.command_code)? {
CommandCode::Clear => {
TypedCommand::Clear(crate::ClearCommand::try_from(packet)?)
}
CommandCode::Brightness => TypedCommand::Brightness(
crate::GlobalBrightnessCommand::try_from(packet)?,
),
CommandCode::HardReset => TypedCommand::HardReset(
crate::HardResetCommand::try_from(packet)?,
),
CommandCode::FadeOut => {
TypedCommand::FadeOut(crate::FadeOutCommand::try_from(packet)?)
}
CommandCode::Cp437Data => TypedCommand::Cp437Grid(
crate::Cp437GridCommand::try_from(packet)?,
),
CommandCode::CharBrightness => TypedCommand::BrightnessGrid(
crate::BrightnessGridCommand::try_from(packet)?,
),
CommandCode::Utf8Data => TypedCommand::CharGrid(
crate::CharGridCommand::try_from(packet)?,
),
#[allow(deprecated)]
CommandCode::BitmapLegacy => TypedCommand::BitmapLegacy(
crate::BitmapLegacyCommand::try_from(packet)?,
),
CommandCode::BitmapLinear
| CommandCode::BitmapLinearOr
| CommandCode::BitmapLinearAnd
| CommandCode::BitmapLinearXor => {
TypedCommand::BitVec(crate::BitVecCommand::try_from(packet)?)
}
CommandCode::BitmapLinearWinUncompressed => {
TypedCommand::Bitmap(crate::BitmapCommand::try_from(packet)?)
}
#[cfg(feature = "compression_zlib")]
CommandCode::BitmapLinearWinZlib => {
TypedCommand::Bitmap(crate::BitmapCommand::try_from(packet)?)
}
#[cfg(feature = "compression_bzip2")]
CommandCode::BitmapLinearWinBzip2 => {
TypedCommand::Bitmap(crate::BitmapCommand::try_from(packet)?)
}
#[cfg(feature = "compression_lzma")]
CommandCode::BitmapLinearWinLzma => {
TypedCommand::Bitmap(crate::BitmapCommand::try_from(packet)?)
}
#[cfg(feature = "compression_zstd")]
CommandCode::BitmapLinearWinZstd => {
TypedCommand::Bitmap(crate::BitmapCommand::try_from(packet)?)
}
})
}
}
impl TryFrom<TypedCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: TypedCommand) -> Result<Self, Self::Error> {
Ok(match value {
TypedCommand::Clear(c) => c.into(),
TypedCommand::CharGrid(c) => c.try_into()?,
TypedCommand::Cp437Grid(c) => c.try_into()?,
TypedCommand::Bitmap(c) => c.try_into()?,
TypedCommand::Brightness(c) => c.into(),
TypedCommand::BrightnessGrid(c) => c.try_into()?,
TypedCommand::BitVec(c) => c.try_into()?,
TypedCommand::HardReset(c) => c.into(),
TypedCommand::FadeOut(c) => c.into(),
#[allow(deprecated)]
TypedCommand::BitmapLegacy(c) => c.into(),
})
}
}
impl TryFrom<&TypedCommand> for Packet {
type Error = TryIntoPacketError;
fn try_from(value: &TypedCommand) -> Result<Self, Self::Error> {
Ok(match value {
TypedCommand::Clear(c) => c.into(),
TypedCommand::CharGrid(c) => c.try_into()?,
TypedCommand::Cp437Grid(c) => c.try_into()?,
TypedCommand::Bitmap(c) => c.try_into()?,
TypedCommand::Brightness(c) => c.into(),
TypedCommand::BrightnessGrid(c) => c.try_into()?,
TypedCommand::BitVec(c) => c.try_into()?,
TypedCommand::HardReset(c) => c.into(),
TypedCommand::FadeOut(c) => c.into(),
#[allow(deprecated)]
TypedCommand::BitmapLegacy(c) => c.into(),
})
}
}
#[cfg(test)]
mod tests {
use crate::{
command_code::InvalidCommandCodeError,
commands::tests::TestImplementsCommand, Header, Packet, TypedCommand,
};
impl TestImplementsCommand for TypedCommand {}
#[test]
fn error_invalid_command() {
let p = Packet {
header: Header {
command_code: 0xFF,
a: 0x00,
b: 0x00,
c: 0x00,
d: 0x00,
},
payload: None,
};
let result = TypedCommand::try_from(p);
assert_eq!(result, Err(InvalidCommandCodeError(0xFF).into()));
}
}

View file

@ -1,177 +0,0 @@
#[cfg(feature = "compression_bzip2")]
use bzip2::read::{BzDecoder, BzEncoder};
#[cfg(feature = "compression_zlib")]
use flate2::{FlushCompress, FlushDecompress, Status};
#[allow(unused, reason = "used depending on enabled features")]
use log::error;
#[allow(unused, reason = "used depending on enabled features")]
use std::io::{Read, Write};
#[cfg(feature = "compression_zstd")]
use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder};
use crate::{CompressionCode, Payload};
#[derive(thiserror::Error, Debug, PartialEq)]
pub(crate) enum CompressionError {
#[error("Could not compress or decompress as no compression is used.")]
NoCompression,
#[error("Could not initialize compression library")]
#[allow(unused, reason = "depends on features")]
LibraryError,
#[error("Compression/decompression operation failed")]
#[allow(unused, reason = "depends on features")]
CompressionFailed,
}
pub(crate) fn decompress(
kind: CompressionCode,
#[allow(unused, reason = "depends on features")] payload: &[u8],
) -> Result<Payload, CompressionError> {
match kind {
CompressionCode::Uncompressed => Err(CompressionError::NoCompression),
#[cfg(feature = "compression_zlib")]
CompressionCode::Zlib => {
let mut decompress = flate2::Decompress::new(true);
let mut buffer = [0u8; 10000];
match decompress.decompress(
payload,
&mut buffer,
FlushDecompress::Finish,
) {
Ok(Status::Ok) => {
error!("input not big enough");
Err(CompressionError::CompressionFailed)
}
Ok(Status::BufError) => {
error!("output buffer is too small");
Err(CompressionError::CompressionFailed)
}
Ok(Status::StreamEnd) =>
{
#[allow(
clippy::cast_possible_truncation,
reason = "can never be larger than the fixed buffer size"
)]
Ok(buffer[..decompress.total_out() as usize].to_owned())
}
Err(e) => {
error!("failed to decompress data: {e}");
Err(CompressionError::CompressionFailed)
}
}
}
#[cfg(feature = "compression_bzip2")]
CompressionCode::Bzip2 => {
let mut decoder = BzDecoder::new(payload);
let mut decompressed = vec![];
match decoder.read_to_end(&mut decompressed) {
Ok(_) => Ok(decompressed),
Err(e) => {
error!("failed to decompress data: {e}");
Err(CompressionError::CompressionFailed)
}
}
}
#[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => lzma::decompress(payload).map_err(|e| {
error!("failed to decompress data: {e}");
CompressionError::CompressionFailed
}),
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(payload) {
Ok(value) => value,
Err(e) => {
error!("failed to create zstd decoder: {e}");
return Err(CompressionError::LibraryError);
}
};
let mut decompressed = vec![];
match decoder.read_to_end(&mut decompressed) {
Err(e) => {
error!("failed to decompress data: {e}");
Err(CompressionError::CompressionFailed)
}
Ok(_) => Ok(decompressed),
}
}
}
}
pub(crate) fn compress(
kind: CompressionCode,
#[allow(unused, reason = "depends on features")] payload: &[u8],
) -> Result<Payload, CompressionError> {
match kind {
CompressionCode::Uncompressed => Err(CompressionError::NoCompression),
#[cfg(feature = "compression_zlib")]
CompressionCode::Zlib => {
let mut compress =
flate2::Compress::new(flate2::Compression::fast(), true);
let mut buffer = [0u8; 10000];
match compress.compress(payload, &mut buffer, FlushCompress::Finish)
{
Ok(Status::Ok) => {
error!("output buffer not big enough");
Err(CompressionError::CompressionFailed)
}
Ok(Status::BufError) => {
error!("Could not compress with buffer error");
Err(CompressionError::CompressionFailed)
}
Ok(Status::StreamEnd) =>
{
#[allow(
clippy::cast_possible_truncation,
reason = "can never be larger than the fixed buffer size"
)]
Ok(buffer[..compress.total_out() as usize].to_owned())
}
Err(e) => {
error!("failed to compress data: {e}");
Err(CompressionError::CompressionFailed)
}
}
}
#[cfg(feature = "compression_bzip2")]
CompressionCode::Bzip2 => {
let mut encoder =
BzEncoder::new(payload, bzip2::Compression::fast());
let mut compressed = vec![];
match encoder.read_to_end(&mut compressed) {
Ok(_) => Ok(compressed),
Err(e) => {
error!("failed to compress data: {e}");
Err(CompressionError::CompressionFailed)
}
}
}
#[cfg(feature = "compression_lzma")]
CompressionCode::Lzma => lzma::compress(payload, 6).map_err(|e| {
error!("failed to compress data: {e}");
CompressionError::CompressionFailed
}),
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => {
let buf = Vec::with_capacity(payload.len());
let mut encoder =
ZstdEncoder::new(buf, zstd::DEFAULT_COMPRESSION_LEVEL)
.map_err(|e| {
error!("failed to create zstd encoder: {e}");
CompressionError::LibraryError
})?;
if let Err(e) = encoder.write_all(payload) {
error!("failed to compress data: {e}");
return Err(CompressionError::CompressionFailed);
}
encoder.finish().map_err(|e| {
error!("failed to finish compression: {e}");
CompressionError::CompressionFailed
})
}
}
}

View file

@ -1,175 +0,0 @@
/// Specifies the kind of compression to use. Availability depends on features.
///
/// # Examples
///
/// create command without payload compression
/// ```rust
/// # use servicepoint::*;
/// # let pixels = Bitmap::max_sized();
/// _ = BitmapCommand {
/// origin: Origin::ZERO,
/// bitmap: pixels,
/// compression: CompressionCode::Uncompressed
/// };
/// ```
///
/// create command with payload compressed with lzma and appropriate header flags
/// ```rust
/// # use servicepoint::*;
/// # let pixels = Bitmap::max_sized();
/// # #[cfg(feature = "compression_lzma")] {
/// _ = BitmapCommand {
/// origin: Origin::ZERO,
/// bitmap: pixels,
/// compression: CompressionCode::Lzma
/// };
/// # }
/// ```
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CompressionCode {
/// no compression
Uncompressed = 0x0,
#[cfg(feature = "compression_zlib")]
/// compress using flate2 with zlib header
Zlib = 0x677a,
#[cfg(feature = "compression_bzip2")]
/// compress using bzip2
Bzip2 = 0x627a,
#[cfg(feature = "compression_lzma")]
/// compress using lzma
Lzma = 0x6c7a,
#[cfg(feature = "compression_zstd")]
/// compress using Zstandard
Zstd = 0x7a73,
}
impl CompressionCode {
/// All available compression codes (depending on features).
pub const ALL: &'static [CompressionCode] = &[
Self::Uncompressed,
#[cfg(feature = "compression_zlib")]
Self::Zlib,
#[cfg(feature = "compression_bzip2")]
Self::Bzip2,
#[cfg(feature = "compression_lzma")]
Self::Lzma,
#[cfg(feature = "compression_zstd")]
Self::Zstd,
];
}
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
#[error("The compression code {0} is not known.")]
pub struct InvalidCompressionCodeError(pub u16);
impl From<CompressionCode> for u16 {
fn from(value: CompressionCode) -> Self {
value as u16
}
}
impl TryFrom<u16> for CompressionCode {
type Error = InvalidCompressionCodeError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
value if value == CompressionCode::Uncompressed as u16 => {
Ok(CompressionCode::Uncompressed)
}
#[cfg(feature = "compression_zlib")]
value if value == CompressionCode::Zlib as u16 => {
Ok(CompressionCode::Zlib)
}
#[cfg(feature = "compression_bzip2")]
value if value == CompressionCode::Bzip2 as u16 => {
Ok(CompressionCode::Bzip2)
}
#[cfg(feature = "compression_lzma")]
value if value == CompressionCode::Lzma as u16 => {
Ok(CompressionCode::Lzma)
}
#[cfg(feature = "compression_zstd")]
value if value == CompressionCode::Zstd as u16 => {
Ok(CompressionCode::Zstd)
}
_ => Err(InvalidCompressionCodeError(value)),
}
}
}
impl Default for CompressionCode {
#[cfg(feature = "compression_lzma")]
fn default() -> Self {
CompressionCode::Lzma
}
#[cfg(not(feature = "compression_lzma"))]
fn default() -> Self {
CompressionCode::Uncompressed
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn uncompressed() {
assert_eq!(
CompressionCode::try_from(0x0000),
Ok(CompressionCode::Uncompressed)
);
assert_eq!(u16::from(CompressionCode::Uncompressed), 0x0000);
}
#[test]
#[cfg(feature = "compression_zlib")]
fn zlib() {
assert_eq!(
CompressionCode::try_from(0x677a),
Ok(CompressionCode::Zlib)
);
assert_eq!(u16::from(CompressionCode::Zlib), 0x677a);
}
#[test]
#[cfg(feature = "compression_bzip2")]
fn bzip2() {
assert_eq!(
CompressionCode::try_from(0x627a),
Ok(CompressionCode::Bzip2)
);
assert_eq!(u16::from(CompressionCode::Bzip2), 0x627a);
}
#[test]
#[cfg(feature = "compression_lzma")]
fn lzma() {
assert_eq!(
CompressionCode::try_from(0x6c7a),
Ok(CompressionCode::Lzma)
);
assert_eq!(u16::from(CompressionCode::Lzma), 0x6c7a);
}
#[test]
#[cfg(feature = "compression_zstd")]
fn zstd() {
assert_eq!(
CompressionCode::try_from(0x7a73),
Ok(CompressionCode::Zstd)
);
assert_eq!(u16::from(CompressionCode::Zstd), 0x7a73);
}
#[test]
#[cfg(feature = "compression_lzma")]
fn default_lzma() {
assert_eq!(CompressionCode::default(), CompressionCode::Lzma);
}
#[test]
#[cfg(not(feature = "compression_lzma"))]
fn default_uncompressed() {
assert_eq!(CompressionCode::default(), CompressionCode::Uncompressed);
}
}

View file

@ -1,40 +0,0 @@
use crate::Packet;
use std::net::{Ipv4Addr, ToSocketAddrs};
use std::{convert::TryInto, net::UdpSocket};
/// Provides servicepoint specific extensions for `UdpSocket`
pub trait UdpSocketExt {
/// Creates a `UdpSocket` that can be used so send to the specified addr.
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket>;
/// Serializes the command and sends it through the socket
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()>;
}
impl UdpSocketExt for UdpSocket {
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket> {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
socket.connect(addr)?;
Ok(socket)
}
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
let packet = command.try_into().ok()?;
let vec: Vec<_> = packet.into();
self.send(&vec).ok()?;
Some(())
}
}
/// A fake connection for testing that does not actually send anything.
pub struct FakeConnection;
impl FakeConnection {
/// Serializes the command, but does not actually send it as this is the fake connection
pub fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
_ = self; // suppress unused warning
let packet = command.try_into().ok()?;
drop(Vec::from(packet));
Some(())
}
}

View file

@ -1,75 +0,0 @@
use std::time::Duration;
/// size of a single tile in one dimension
pub const TILE_SIZE: usize = 8;
/// Display tile count in the x-direction
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
/// let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
/// ```
pub const TILE_WIDTH: usize = 56;
/// Display tile count in the y-direction
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{Cp437Grid, TILE_HEIGHT, TILE_WIDTH};
/// let grid = Cp437Grid::new(TILE_WIDTH, TILE_HEIGHT);
/// ```
pub const TILE_HEIGHT: usize = 20;
/// Display width in pixels
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
/// let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_WIDTH: usize = TILE_WIDTH * TILE_SIZE;
/// Display height in pixels
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{PIXEL_HEIGHT, PIXEL_WIDTH, Bitmap};
/// let grid = Bitmap::new(PIXEL_WIDTH, PIXEL_HEIGHT);
/// ```
pub const PIXEL_HEIGHT: usize = TILE_HEIGHT * TILE_SIZE;
/// pixel count on whole screen
pub const PIXEL_COUNT: usize = PIXEL_WIDTH * PIXEL_HEIGHT;
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
///
/// # Examples
///
/// ```rust
/// # use std::time::Instant;
/// # use servicepoint::*;
/// # let connection = FakeConnection;
/// # let pixels = Bitmap::max_sized();
/// loop {
/// let start = Instant::now();
///
/// // Change pixels here
///
/// connection.send_command(BitmapCommand {
/// origin: Origin::new(0,0),
/// bitmap: pixels,
/// compression: CompressionCode::default()
/// })
/// .expect("send failed");
///
/// // warning: will crash if resulting duration is negative, e.g. when resuming from standby
/// std::thread::sleep(FRAME_PACING - start.elapsed());
/// # break; // prevent doctest from hanging
/// }
/// ```
pub const FRAME_PACING: Duration = Duration::from_millis(30);

View file

@ -1,10 +0,0 @@
/// A byte-packed vector of booleans.
///
/// The implementation is provided by [bitvec].
/// This is an alias for the specific type of [`bitvec::BitVec`] used in this crate.
pub type DisplayBitVec = bitvec::BitVec<u8, bitvec::Msb0>;
pub mod bitvec {
//! Re-export of the used library [`::bitvec`].
pub use ::bitvec::prelude::*;
}

View file

@ -1,509 +0,0 @@
use crate::{
containers::absolute_bounds_to_abs_range, DataRef, DisplayBitVec, Grid,
GridMut, Payload, ValueGrid, Window, WindowMut, PIXEL_HEIGHT, PIXEL_WIDTH,
};
use ::bitvec::{order::Msb0, prelude::BitSlice, slice::IterMut};
use std::ops::RangeBounds;
/// A fixed-size 2D grid of booleans.
///
/// The values are stored in packed bytes (8 values per byte) in the same order as used by the display for storing pixels.
/// This means that no conversion is necessary for sending the data to the display.
/// The downside is that the width has to be a multiple of 8.
///
/// # Examples
///
/// ```rust
/// use servicepoint::Bitmap;
/// let mut bitmap = Bitmap::new(8, 2);
///
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Bitmap {
width: usize,
height: usize,
bit_vec: DisplayBitVec,
}
impl Bitmap {
/// Creates a new [`Bitmap`] with the specified dimensions.
/// The initial state of the contained pixels is false.
///
/// The width has to be a multiple of [`crate::TILE_SIZE`], otherwise this function returns None.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
#[must_use]
pub fn new(width: usize, height: usize) -> Option<Self> {
assert!(width < isize::MAX as usize);
assert!(height < isize::MAX as usize);
if width % 8 != 0 {
return None;
}
Some(Self::new_unchecked(width, height))
}
/// Creates a new pixel grid with the size of the whole screen.
#[must_use]
pub fn max_sized() -> Self {
Self::new_unchecked(PIXEL_WIDTH, PIXEL_HEIGHT)
}
#[must_use]
fn new_unchecked(width: usize, height: usize) -> Self {
Self {
width,
height,
bit_vec: DisplayBitVec::repeat(false, width * height),
}
}
/// Loads a [Bitmap] with the specified dimensions from the provided data.
///
/// The data cannot be loaded on the following cases:
/// - when the dimensions and data size do not match exactly.
/// - when the width is not dividable by 8
///
/// In those cases, an Err is returned.
///
/// Otherwise, this returns a [Bitmap] that contains a copy of the provided data
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
pub fn load(
width: usize,
height: usize,
data: &[u8],
) -> Result<Self, LoadBitmapError> {
assert!(width < isize::MAX as usize);
assert!(height < isize::MAX as usize);
if width % 8 != 0 {
return Err(LoadBitmapError::InvalidWidth);
}
if data.len() != height * width / 8 {
return Err(LoadBitmapError::InvalidDataSize);
}
Ok(Self {
width,
height,
bit_vec: DisplayBitVec::from_slice(data),
})
}
/// Creates a [Bitmap] with the specified width from the provided [`DisplayBitVec`] without copying it.
///
/// The data cannot be loaded on the following cases:
/// - when the data size is not divisible by the width (incomplete rows)
/// - when the width is not dividable by 8
///
/// In those cases, an Err is returned.
/// Otherwise, this returns a [Bitmap] that contains the provided data.
pub fn from_bitvec(
width: usize,
bit_vec: DisplayBitVec,
) -> Result<Self, LoadBitmapError> {
if width % 8 != 0 {
return Err(LoadBitmapError::InvalidWidth);
}
let len = bit_vec.len();
let height = len / width;
assert!(width < isize::MAX as usize);
assert!(height < isize::MAX as usize);
if len % width != 0 {
return Err(LoadBitmapError::InvalidDataSize);
}
Ok(Self {
width,
height,
bit_vec,
})
}
/// Iterate over all cells in [Bitmap].
///
/// Order is equivalent to the following loop:
/// ```
/// # use servicepoint::{Bitmap, Grid};
/// # let grid = Bitmap::new(8, 2).unwrap();
/// for y in 0..grid.height() {
/// for x in 0..grid.width() {
/// grid.get(x, y);
/// }
/// }
/// ```
pub fn iter(&self) -> impl Iterator<Item = &bool> {
self.bit_vec.iter().by_refs()
}
/// Iterate over all cells in [Bitmap] mutably.
///
/// Order is equivalent to the following loop:
/// ```
/// # use servicepoint::{Bitmap, Grid, GridMut};
/// # let mut grid = Bitmap::new(8, 2).unwrap();
/// # let value = false;
/// for y in 0..grid.height() {
/// for x in 0..grid.width() {
/// grid.set(x, y, value);
/// }
/// }
/// ```
///
/// # Example
/// ```
/// # use servicepoint::{Bitmap, Grid};
/// # let mut grid = Bitmap::new(8, 2).unwrap();
/// # let value = false;
/// for (index, mut pixel) in grid.iter_mut().enumerate() {
/// pixel.set(index % 2 == 0)
/// }
/// ```
#[must_use]
#[allow(clippy::iter_without_into_iter)]
pub fn iter_mut(&mut self) -> IterMut<'_, u8, Msb0> {
self.bit_vec.iter_mut()
}
/// Iterate over all rows in [Bitmap] top to bottom.
pub fn iter_rows(&self) -> impl Iterator<Item = &BitSlice<u8, Msb0>> {
IterRows {
bitmap: self,
row: 0,
}
}
/// Creates a window into the bitmap.
///
/// Returns None in case the window does not fit.
#[must_use]
pub fn window(
&self,
xs: impl RangeBounds<usize>,
ys: impl RangeBounds<usize>,
) -> Option<Window<'_, bool, Self>> {
let xs = absolute_bounds_to_abs_range(xs, self.width)?;
let ys = absolute_bounds_to_abs_range(ys, self.height)?;
Window::new(self, xs, ys)
}
/// Creates a mutable window into the bitmap.
///
/// Returns None in case the window does not fit.
pub fn window_mut(
&mut self,
xs: impl RangeBounds<usize>,
ys: impl RangeBounds<usize>,
) -> Option<WindowMut<'_, bool, Self>> {
let xs = absolute_bounds_to_abs_range(xs, self.width)?;
let ys = absolute_bounds_to_abs_range(ys, self.height)?;
WindowMut::new(self, xs, ys)
}
}
impl Grid<bool> for Bitmap {
fn get_optional(&self, x: usize, y: usize) -> Option<bool> {
let index = x + y * self.width;
if self.is_in_bounds(x, y) {
Some(self.bit_vec[index])
} else {
None
}
}
fn width(&self) -> usize {
self.width
}
fn height(&self) -> usize {
self.height
}
}
impl GridMut<bool> for Bitmap {
/// Sets the value of the specified position in the [Bitmap].
///
/// # Arguments
///
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// returns: old value of the cell
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
fn set_optional(&mut self, x: usize, y: usize, value: bool) -> bool {
if self.is_in_bounds(x, y) {
self.bit_vec.set(x + y * self.width, value);
true
} else {
false
}
}
/// Sets the state of all pixels in the [Bitmap].
///
/// # Arguments
///
/// - `this`: instance to write to
/// - `value`: the value to set all pixels to
fn fill(&mut self, value: bool) {
self.bit_vec.fill(value);
}
}
impl DataRef<u8> for Bitmap {
fn data_ref_mut(&mut self) -> &mut [u8] {
self.bit_vec.as_raw_mut_slice()
}
fn data_ref(&self) -> &[u8] {
self.bit_vec.as_raw_slice()
}
}
impl From<Bitmap> for Vec<u8> {
/// Turns a [Bitmap] into the underlying [`Vec<u8>`].
fn from(value: Bitmap) -> Self {
value.bit_vec.into()
}
}
impl From<Bitmap> for DisplayBitVec {
/// Turns a [Bitmap] into the underlying [`DisplayBitVec`].
fn from(value: Bitmap) -> Self {
value.bit_vec
}
}
impl TryFrom<&ValueGrid<bool>> for Bitmap {
type Error = ();
fn try_from(value: &ValueGrid<bool>) -> Result<Self, Self::Error> {
let mut result = Self::new(value.width(), value.height()).ok_or(())?;
result.deref_assign(value);
Ok(result)
}
}
impl<T: Grid<bool>> TryFrom<&Window<'_, bool, T>> for Bitmap {
type Error = ();
fn try_from(value: &Window<bool, T>) -> Result<Self, Self::Error> {
let mut result = Self::new(value.width(), value.height()).ok_or(())?;
result.deref_assign(value);
Ok(result)
}
}
impl From<&Bitmap> for Payload {
fn from(value: &Bitmap) -> Self {
value.bit_vec.as_raw_slice().into()
}
}
#[must_use]
struct IterRows<'t> {
bitmap: &'t Bitmap,
row: usize,
}
impl<'t> Iterator for IterRows<'t> {
type Item = &'t BitSlice<u8, Msb0>;
fn next(&mut self) -> Option<Self::Item> {
if self.row >= self.bitmap.height {
return None;
}
let start = self.row * self.bitmap.width;
let end = start + self.bitmap.width;
self.row += 1;
Some(&self.bitmap.bit_vec[start..end])
}
}
/// Errors that can happen when loading a bitmap.
#[derive(thiserror::Error, Debug, PartialEq)]
pub enum LoadBitmapError {
/// The provided width is not divisible by 8.
#[error("The provided width is not divisible by 8.")]
InvalidWidth,
/// The provided data has an incorrect size for the provided dimensions.
#[error(
"The provided data has an incorrect size for the provided dimensions."
)]
InvalidDataSize,
}
#[cfg(test)]
mod tests {
use crate::{
Bitmap, DataRef, DisplayBitVec, Grid, GridMut, LoadBitmapError,
ValueGrid,
};
#[test]
fn fill() {
let mut grid = Bitmap::new(8, 2).unwrap();
assert_eq!(grid.data_ref(), [0x00, 0x00]);
grid.fill(true);
assert_eq!(grid.data_ref(), [0xFF, 0xFF]);
grid.fill(false);
assert_eq!(grid.data_ref(), [0x00, 0x00]);
}
#[test]
fn get_set() {
let mut grid = Bitmap::new(8, 2).unwrap();
assert!(!grid.get(0, 0));
assert!(!grid.get(1, 1));
grid.set(5, 0, true);
grid.set(1, 1, true);
assert_eq!(grid.data_ref(), [0x04, 0x40]);
assert!(grid.get(5, 0));
assert!(grid.get(1, 1));
assert!(!grid.get(1, 0));
}
#[test]
fn load() {
let mut grid = Bitmap::new(8, 3).unwrap();
for x in 0..grid.width {
for y in 0..grid.height {
grid.set(x, y, (x + y) % 2 == 0);
}
}
assert_eq!(grid.data_ref(), [0xAA, 0x55, 0xAA]);
let data: Vec<u8> = grid.into();
let grid = Bitmap::load(8, 3, &data).unwrap();
assert_eq!(grid.data_ref(), [0xAA, 0x55, 0xAA]);
}
#[test]
#[should_panic]
fn out_of_bounds_x() {
let vec = Bitmap::new(8, 2).unwrap();
_ = vec.get(8, 1);
}
#[test]
#[should_panic]
fn out_of_bounds_y() {
let mut vec = Bitmap::new(8, 2).unwrap();
vec.set(1, 2, false);
}
#[test]
fn iter() {
let grid = Bitmap::new(8, 2).unwrap();
assert_eq!(16, grid.iter().count());
}
#[test]
fn iter_rows() {
let grid = Bitmap::load(8, 2, &[0x04, 0x40]).unwrap();
let mut iter = grid.iter_rows();
assert_eq!(iter.next().unwrap().count_ones(), 1);
assert_eq!(iter.next().unwrap().count_ones(), 1);
assert_eq!(None, iter.next());
}
#[test]
fn iter_mut() {
let mut grid = Bitmap::new(8, 2).unwrap();
for (index, mut pixel) in grid.iter_mut().enumerate() {
pixel.set(index % 2 == 0);
}
assert_eq!(grid.data_ref(), [0xAA, 0xAA]);
}
#[test]
fn data_ref_mut() {
let mut grid = Bitmap::new(8, 2).unwrap();
let data = grid.data_ref_mut();
data[1] = 0x0F;
assert!(grid.get(7, 1));
}
#[test]
fn to_bitvec() {
let mut grid = Bitmap::new(8, 2).unwrap();
grid.set(0, 0, true);
let bitvec: DisplayBitVec = grid.into();
assert_eq!(bitvec.as_raw_slice(), [0x80, 0x00]);
}
#[test]
fn from_bool_grid() {
let original = ValueGrid::load(
8,
1,
&[true, false, true, false, true, false, true, false],
)
.unwrap();
let converted = Bitmap::try_from(&original).unwrap();
let reconverted = ValueGrid::from(&converted);
assert_eq!(original, reconverted);
}
#[test]
fn load_invalid_width() {
let data = DisplayBitVec::repeat(false, 7 * 3).into_vec();
assert_eq!(
Bitmap::load(7, 3, &data),
Err(LoadBitmapError::InvalidWidth)
);
}
#[test]
fn load_invalid_size() {
let data = DisplayBitVec::repeat(false, 8 * 4).into_vec();
assert_eq!(
Bitmap::load(8, 3, &data),
Err(LoadBitmapError::InvalidDataSize)
);
}
#[test]
fn from_vec_invalid_width() {
let data = DisplayBitVec::repeat(false, 7 * 3);
assert_eq!(
Bitmap::from_bitvec(7, data),
Err(LoadBitmapError::InvalidWidth)
);
}
#[test]
fn from_vec_invalid_size() {
let data = DisplayBitVec::repeat(false, 7 * 4);
assert_eq!(
Bitmap::from_bitvec(8, data),
Err(LoadBitmapError::InvalidDataSize)
);
}
#[test]
fn from_vec() {
let orig = Bitmap::new(8, 3).unwrap();
assert_eq!(Bitmap::from_bitvec(8, orig.bit_vec.clone()).unwrap(), orig);
}
#[test]
fn new_invalid_width() {
assert_eq!(Bitmap::new(7, 2), None);
}
}

View file

@ -1,124 +0,0 @@
use crate::{Brightness, ByteGrid, Grid, ValueGrid};
/// A grid containing brightness values.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// let mut grid = BrightnessGrid::new(2,2);
/// grid.set(0, 0, Brightness::MIN);
/// grid.set(1, 1, Brightness::MIN);
///
/// # let connection = FakeConnection;
/// connection.send_command(BrightnessGridCommand {
/// origin: Origin::new(3, 7),
/// grid
/// }).unwrap()
/// ```
pub type BrightnessGrid = ValueGrid<Brightness>;
impl BrightnessGrid {
/// Like [`Self::load`], but ignoring any out-of-range brightness values
#[must_use]
pub fn saturating_load(
width: usize,
height: usize,
data: &[u8],
) -> Option<Self> {
ValueGrid::load(width, height, data)
.map(move |grid| grid.map(Brightness::saturating_from))
}
}
impl From<&BrightnessGrid> for Vec<u8> {
fn from(value: &BrightnessGrid) -> Self {
value
.iter()
.map(|brightness| (*brightness).into())
.collect()
}
}
impl From<BrightnessGrid> for Vec<u8> {
fn from(value: BrightnessGrid) -> Self {
// look mom, zero copies!
let mut vec =
std::mem::ManuallyDrop::new(Vec::<Brightness>::from(value));
// Safety: this is safe because Brightness is repr(transparent) and only contains one u8.
// Also see https://doc.rust-lang.org/std/mem/fn.transmute.html
unsafe {
// this makes sure the operation is safe at compile time
const _: () = assert!(
size_of::<Brightness>() == size_of::<u8>()
&& align_of::<Brightness>() == align_of::<u8>()
);
Vec::from_raw_parts(
vec.as_mut_ptr().cast(),
vec.len(),
vec.capacity(),
)
}
}
}
impl From<&BrightnessGrid> for ByteGrid {
fn from(value: &BrightnessGrid) -> Self {
let u8s = value
.iter()
.map(|brightness| (*brightness).into())
.collect::<Vec<u8>>();
Self::from_raw_parts_unchecked(value.width(), value.height(), u8s)
}
}
impl TryFrom<ByteGrid> for BrightnessGrid {
type Error = u8;
fn try_from(value: ByteGrid) -> Result<Self, Self::Error> {
let brightnesses = value
.iter()
.map(|b| Brightness::try_from(*b))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self::from_raw_parts_unchecked(
value.width(),
value.height(),
brightnesses,
))
}
}
#[cfg(test)]
mod tests {
use crate::{Brightness, BrightnessGrid, DataRef, GridMut, ValueGrid};
#[test]
fn to_u8_grid() {
let mut grid = BrightnessGrid::new(2, 2);
grid.set(1, 0, Brightness::MIN);
grid.set(0, 1, Brightness::MAX);
let actual: ValueGrid<u8> = ValueGrid::from(&grid);
assert_eq!(actual.data_ref(), &[11, 0, 11, 11]);
}
#[test]
fn saturating_load() {
assert_eq!(
BrightnessGrid::load(
2,
2,
&[
Brightness::MAX,
Brightness::MAX,
Brightness::MIN,
Brightness::MAX
]
)
.unwrap(),
BrightnessGrid::saturating_load(2, 2, &[255u8, 23, 0, 42]).unwrap()
);
}
}

View file

@ -1,4 +0,0 @@
use crate::ValueGrid;
/// A 2d grid of bytes - see [`ValueGrid`].
pub type ByteGrid = ValueGrid<u8>;

View file

@ -1,190 +0,0 @@
use crate::{CharGridMutExt, GridMut, ValueGrid};
/// A grid containing UTF-8 characters.
///
/// To send a `CharGrid` to the display, use a [`crate::CharGridCommand`].
///
/// Also see [`ValueGrid`] for the non-specialized operations and examples.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::*;
/// let grid = CharGrid::from("You can\nload multiline\nstrings directly");
/// assert_eq!(grid.get_row_str(1), Some("load multiline\0\0".to_string()));
///
/// # let connection = FakeConnection;
/// let command = CharGridCommand { origin: Origin::ZERO, grid };
/// connection.send_command(command).unwrap()
/// ```
pub type CharGrid = ValueGrid<char>;
impl CharGrid {
/// Loads a [`CharGrid`] with the specified width from the provided text, wrapping to as many rows as needed.
///
/// The passed rows are extended with '\0' if needed.
///
/// returns: [`CharGrid`] that contains a copy of the provided data.
///
/// # Examples
///
/// ```
/// # use servicepoint::CharGrid;
/// let grid = CharGrid::wrap_str(2, "abc\ndef");
/// ```
#[must_use]
pub fn wrap_str(width: usize, text: &str) -> Self {
let lines = text
.split('\n')
.flat_map(move |x| {
x.chars()
.collect::<Vec<char>>()
.chunks(width)
.map(|c| {
let mut s = String::from_iter(c);
s.push_str(&" ".repeat(width - s.chars().count()));
s
})
.collect::<Vec<String>>()
})
.collect::<Vec<String>>();
let height = lines.len();
let mut result = Self::new(width, height);
for (row, text_line) in lines.iter().enumerate() {
#[allow(clippy::unwrap_used)]
// we calculated the width before setting
result.set_row_str(row, text_line).unwrap();
}
result
}
}
impl From<&str> for CharGrid {
fn from(value: &str) -> Self {
let value = value.replace("\r\n", "\n");
let mut lines = value.split('\n').collect::<Vec<_>>();
let width = lines
.iter()
.fold(0, move |a, x| std::cmp::max(a, x.chars().count()));
while lines.last().is_some_and(move |line| line.is_empty()) {
_ = lines.pop();
}
let mut grid = Self::new(width, lines.len());
for (y, line) in lines.iter().enumerate() {
for (x, char) in line.chars().enumerate() {
grid.set(x, y, char);
}
}
grid
}
}
impl From<String> for CharGrid {
fn from(value: String) -> Self {
CharGrid::from(&*value)
}
}
impl From<CharGrid> for String {
fn from(grid: CharGrid) -> Self {
String::from(&grid)
}
}
impl From<&CharGrid> for String {
/// Converts a [`CharGrid`] into a [String].
///
/// Rows are separated by '\n'.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::CharGrid;
/// let grid = CharGrid::from("ab\ncd");
/// let string = String::from(grid);
/// let grid = CharGrid::from(string);
/// ```
fn from(value: &CharGrid) -> Self {
value
.iter_rows()
.map(String::from_iter)
.collect::<Vec<String>>()
.join("\n")
}
}
impl From<&CharGrid> for Vec<u8> {
/// Converts a [`CharGrid`] into a [`Vec<u8>`].
///
/// Rows are not separated.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{CharGrid, Grid};
/// let grid = CharGrid::from("ab\ncd");
/// let height = grid.height();
/// let width = grid.width();
/// let bytes = Vec::<u8>::from(grid);
/// ```
fn from(value: &CharGrid) -> Self {
value.iter().collect::<String>().into_bytes()
}
}
impl From<CharGrid> for Vec<u8> {
/// See [`From<&CharGrid>::from`].
fn from(value: CharGrid) -> Self {
Self::from(&value)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::Grid;
#[test]
fn str_to_char_grid() {
// conversion with .to_string() covers one more line
let original = "Hello\r\nWorld!\n...\n".to_string();
let grid = CharGrid::from(original);
assert_eq!(3, grid.height());
assert_eq!("Hello\0\nWorld!\n...\0\0\0", String::from(grid));
}
#[test]
fn round_trip_bytes() {
let grid = CharGrid::from("Hello\0\nWorld!\n...\0\0\0");
let bytes: Vec<u8> = grid.clone().into();
let copy = CharGrid::load(
grid.width(),
grid.height(),
&String::from_utf8(bytes)
.unwrap()
.chars()
.collect::<Vec<_>>(),
)
.unwrap();
assert_eq!(grid, copy);
}
#[test]
fn round_trip_string() {
let grid = CharGrid::from("Hello \nWorld!\n... ");
let str: String = grid.clone().into();
let copy = CharGrid::from(str);
assert_eq!(grid, copy);
}
#[test]
fn wrap_str() {
let grid = CharGrid::wrap_str(2, "abc\ndef");
assert_eq!(4, grid.height());
assert_eq!(2, grid.width());
assert_eq!("ab\nc \nde\nf ", String::from(grid));
}
}

View file

@ -1,173 +0,0 @@
use crate::{Grid, GridMut, SetValueSeriesError};
/// Extension methods for any [`Grid<char>`]
pub trait CharGridExt {
/// Copies a column from the grid as a String.
///
/// Returns [None] if x is out of bounds.
///
/// # Examples
///
/// ```
/// # use servicepoint::{CharGrid, CharGridExt};
/// let grid = CharGrid::from("ab\ncd");
/// let col = grid.get_col_str(0).unwrap(); // "ac"
/// ```
#[must_use]
fn get_col_str(&self, x: usize) -> Option<String>;
/// Copies a row from the grid as a String.
///
/// Returns [None] if y is out of bounds.
///
/// # Examples
///
/// ```
/// # use servicepoint::{CharGrid, CharGridExt};
/// let grid = CharGrid::from("ab\ncd");
/// let row = grid.get_row_str(0).unwrap(); // "ab"
/// ```
#[must_use]
fn get_row_str(&self, y: usize) -> Option<String>;
}
/// Extension methods for any [`GridMut<char>`].
pub trait CharGridMutExt {
/// Overwrites a row in the grid with a str.
///
/// Returns [`SetValueSeriesError`] if y is out of bounds or `row` is not of the correct size.
///
/// # Examples
///
/// ```
/// # use servicepoint::{CharGrid, CharGridMutExt};
/// let mut grid = CharGrid::from("ab\ncd");
/// grid.set_row_str(0, "ef").unwrap();
/// ```
fn set_row_str(
&mut self,
y: usize,
value: &str,
) -> Result<(), SetValueSeriesError>;
/// Overwrites a column in the grid with a str.
///
/// Returns [`SetValueSeriesError`] if y is out of bounds or `row` is not of the correct size.
///
/// # Examples
///
/// ```
/// # use servicepoint::{CharGrid, CharGridMutExt};
/// let mut grid = CharGrid::from("ab\ncd");
/// grid.set_col_str(0, "ef").unwrap();
/// ```
fn set_col_str(
&mut self,
x: usize,
value: &str,
) -> Result<(), SetValueSeriesError>;
}
impl<G: Grid<char>> CharGridExt for G {
fn get_col_str(&self, x: usize) -> Option<String> {
Some(String::from_iter(self.get_col(x)?))
}
fn get_row_str(&self, y: usize) -> Option<String> {
Some(String::from_iter(self.get_row(y)?))
}
}
impl<G: GridMut<char>> CharGridMutExt for G {
fn set_row_str(
&mut self,
y: usize,
value: &str,
) -> Result<(), SetValueSeriesError> {
let width = self.width();
let len = value.len();
if len > width {
return Err(SetValueSeriesError::InvalidLength {
actual: len,
expected: width,
});
}
let height = self.height();
if y >= height {
return Err(SetValueSeriesError::OutOfBounds {
index: y,
size: height,
});
}
let chars = value.chars().take(width);
for (x, c) in chars.enumerate() {
self.set(x, y, c);
}
Ok(())
}
fn set_col_str(
&mut self,
x: usize,
value: &str,
) -> Result<(), SetValueSeriesError> {
let height = self.height();
let len = value.len();
if len > height {
return Err(SetValueSeriesError::InvalidLength {
actual: len,
expected: height,
});
}
let width = self.width();
if x >= width {
return Err(SetValueSeriesError::OutOfBounds {
index: x,
size: width,
});
}
let chars = value.chars().take(height);
for (y, c) in chars.enumerate() {
self.set(x, y, c);
}
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::{CharGrid, CharGridExt, CharGridMutExt, SetValueSeriesError};
#[test]
fn col_str() {
let mut grid = CharGrid::new(2, 3);
assert_eq!(grid.get_col_str(2), None);
assert_eq!(grid.get_col_str(1), Some(String::from("\0\0\0")));
assert_eq!(grid.set_col_str(1, "abc"), Ok(()));
assert_eq!(grid.get_col_str(1), Some(String::from("abc")));
}
#[test]
fn row_str() {
let mut grid = CharGrid::new(2, 3);
assert_eq!(grid.get_row_str(3), None);
assert_eq!(grid.get_row_str(1), Some(String::from("\0\0")));
assert_eq!(
grid.set_row_str(1, "abc"),
Err(SetValueSeriesError::InvalidLength {
expected: 2,
actual: 3
})
);
assert_eq!(grid.set_row_str(1, "ab"), Ok(()));
assert_eq!(grid.get_row_str(1), Some(String::from("ab")));
}
}

View file

@ -1,117 +0,0 @@
use crate::{GridMut, ValueGrid};
/// A grid containing codepage 437 characters.
///
/// The encoding is currently not enforced.
pub type Cp437Grid = ValueGrid<u8>;
/// The error occurring when loading an invalid character
#[derive(Debug, PartialEq, thiserror::Error)]
#[error(
"The character {char:?} at position {index} is not a valid CP437 character"
)]
pub struct InvalidCharError {
/// invalid character is at this position in input
index: usize,
/// the invalid character
char: char,
}
impl Cp437Grid {
/// Load an ASCII-only [&str] into a [`Cp437Grid`] of specified width.
///
/// # Panics
///
/// - for width == 0
/// - on empty strings
pub fn load_ascii(
value: &str,
width: usize,
wrap: bool,
) -> Result<Self, InvalidCharError> {
assert!(width > 0);
assert!(!value.is_empty());
let mut chars = {
let mut x = 0;
let mut y = 0;
for (index, char) in value.chars().enumerate() {
if !char.is_ascii() {
return Err(InvalidCharError { index, char });
}
let is_lf = char == '\n';
if is_lf || (wrap && x == width) {
y += 1;
x = 0;
if is_lf {
continue;
}
}
x += 1;
}
Cp437Grid::new(width, y + 1)
};
let mut x = 0;
let mut y = 0;
for char in value.chars().map(move |c| c as u8) {
let is_lf = char == b'\n';
if is_lf || (wrap && x == width) {
y += 1;
x = 0;
if is_lf {
continue;
}
}
if wrap || x < width {
chars.set(x, y, char);
}
x += 1;
}
Ok(chars)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn load_ascii_nowrap() {
let chars = ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
.map(move |c| c as u8);
let expected = Cp437Grid::load(5, 2, &chars).unwrap();
let actual = Cp437Grid::load_ascii("Hello,\nWorld!", 5, false).unwrap();
// comma will be removed because line is too long and wrap is off
assert_eq!(actual, expected);
}
#[test]
fn load_ascii_wrap() {
let chars = ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
.map(move |c| c as u8);
let expected = Cp437Grid::load(5, 2, &chars).unwrap();
let actual = Cp437Grid::load_ascii("HelloWorld", 5, true).unwrap();
// line break will be added
assert_eq!(actual, expected);
}
#[test]
fn load_ascii_invalid() {
assert_eq!(
Err(InvalidCharError {
char: '🥶',
index: 2
}),
Cp437Grid::load_ascii("?#🥶42", 3, false)
);
}
}

View file

@ -1,14 +0,0 @@
/// A trait for getting the underlying raw byte slices of data containers.
///
/// The expectation is that you can create an equal instance with this data given the additional
/// metadata needed.
pub trait DataRef<T> {
/// Get the underlying bytes writable.
///
/// Note that depending on the struct this is implemented on, writing invalid values here might
/// lead to panics later in the lifetime of the program or on the receiving side.
fn data_ref_mut(&mut self) -> &mut [T];
/// Get the underlying bytes read-only.
fn data_ref(&self) -> &[T];
}

View file

@ -1,271 +0,0 @@
use crate::SetValueSeriesError;
/// A two-dimensional readonly grid of `T`
pub trait Grid<T> {
/// Get the current value at the specified position
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
#[must_use]
fn get(&self, x: usize, y: usize) -> T {
#[allow(clippy::panic, reason = "This is the version that panics - _optional does not")]
self.get_optional(x, y)
.unwrap_or_else(|| panic!("Cannot access index ({x}, {y}) because it is out of bounds for a grid of dimension {}x{}", self.width(), self.height()))
}
/// Get the current value at the specified position if the position is inside of bounds
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// returns: Value at position or None
#[must_use]
fn get_optional(&self, x: usize, y: usize) -> Option<T>;
/// the size in x-direction
#[must_use]
fn width(&self) -> usize;
/// the height in y-direction
#[must_use]
fn height(&self) -> usize;
/// Checks whether the specified signed position is in grid bounds
#[must_use]
fn is_in_bounds(&self, x: usize, y: usize) -> bool {
x < self.width() && y < self.height()
}
/// Asserts that the specified unsigned position is in grid bounds.
///
/// # Panics
///
/// When the specified position is out of bounds for this grid.
fn assert_in_bounds(&self, x: usize, y: usize) {
let width = self.width();
assert!(x < width, "cannot access index [{x}, {y}] because x is outside of bounds [0..{width})");
let height = self.height();
assert!(y < height, "cannot access index [{x}, {y}] because y is outside of bounds [0..{height})");
}
/// Copies a row from the grid.
///
/// Returns [None] if y is out of bounds.
#[must_use]
fn get_row(&self, y: usize) -> Option<Vec<T>> {
if y >= self.height() {
return None;
}
let width = self.width();
let mut row = Vec::with_capacity(width);
for x in 0..width {
row.push(self.get(x, y));
}
Some(row)
}
/// Copies a column from the grid.
///
/// Returns [None] if x is out of bounds.
#[must_use]
fn get_col(&self, x: usize) -> Option<Vec<T>> {
if x >= self.width() {
return None;
}
let height = self.height();
let mut col = Vec::with_capacity(height);
for y in 0..height {
col.push(self.get(x, y));
}
Some(col)
}
}
/// A two-dimensional mutable grid of `T`
pub trait GridMut<T: Clone>: Grid<T> {
/// Sets the value at the specified position
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// When accessing `x` or `y` out of bounds.
fn set(&mut self, x: usize, y: usize, value: T) {
#[allow(
clippy::expect_used,
reason = "This is the version that panics - _optional does not"
)]
let worked = self.set_optional(x, y, value);
assert!(worked, "Cannot access index ({x}, {y}) because it is out of bounds for a grid of dimension {}x{}", self.width(), self.height());
}
/// Sets the value at the specified position if the position is inside of bounds
///
/// # Arguments
///
/// - `x` and `y`: position of the cell to read
///
/// returns: true if the value has been set
#[must_use]
fn set_optional(&mut self, x: usize, y: usize, value: T) -> bool;
/// Sets all cells in the grid to the specified value
fn fill(&mut self, value: T);
/// Fills the grid with the values from the provided grid.
///
/// The grids have to match in size exactly.
///
/// For 1D slices the equivalent would be `*slice = other_slice`.
fn deref_assign<O: Grid<T>>(&mut self, other: &O) {
let width = self.width();
let height = self.height();
assert_eq!(
width,
other.width(),
"Cannot assign grid of width {} to a window of width {}",
other.width(),
self.width()
);
assert_eq!(
height,
other.height(),
"Cannot assign grid of height {} to a height of width {}",
other.height(),
self.height()
);
for y in 0..height {
for x in 0..width {
self.set(x, y, other.get(x, y));
}
}
}
/// Overwrites a column in the grid.
///
/// Returns [Err] if x is out of bounds or `col` is not of the correct size.
fn set_col(
&mut self,
x: usize,
col: &[T],
) -> Result<(), SetValueSeriesError> {
let height = self.height();