game of life example, bug fixes

This commit is contained in:
Vinzenz Schroeter 2024-05-10 12:24:07 +02:00
parent a7b6cf2ad0
commit e6f8afb378
7 changed files with 439 additions and 20 deletions

315
examples/game_of_life/Cargo.lock generated Normal file
View file

@ -0,0 +1,315 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
name = "game_of_life"
version = "0.1.0"
dependencies = [
"clap",
"log",
"rand",
"servicepoint2",
]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "libc"
version = "0.2.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "servicepoint2"
version = "0.1.0"
[[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.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
[[package]]
name = "windows_i686_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
[[package]]
name = "windows_i686_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"

View file

@ -0,0 +1,10 @@
[package]
name = "game_of_life"
version = "0.1.0"
edition = "2021"
[dependencies]
servicepoint2 = { path = "../.." }
rand = "0.8.5"
clap = { version = "4.5.4", features = ["derive"] }
log = "0.4.21"

View file

@ -0,0 +1,78 @@
use std::thread;
use std::time::Duration;
use rand::{distributions, Rng};
use clap::Parser;
use servicepoint2::{Connection, Origin, PixelGrid};
use servicepoint2::Command::BitmapLinearWin;
#[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();
println!("starting with args: {:?}", &cli);
let connection = Connection::open(&cli.destination).unwrap();
let mut field = PixelGrid::max_sized();
let mut rng = rand::thread_rng();
let d = distributions::Bernoulli::new(cli.probability).unwrap();
for x in 0..field.width {
for y in 0..field.height {
field.set(x, y, rng.sample(d));
}
}
let origin = Origin(0, 0);
loop {
connection.send(BitmapLinearWin(origin, field.clone())).expect("could not send");
thread::sleep(Duration::from_millis(14));
let mut next = field.clone();
for x in 0..field.width {
for y in 0..field.height {
let old_state = field.get(x, y);
let neighbors = count_neighbors(&field, x as i32, y as i32);
let new_state = match (old_state, neighbors) {
(true, 2) => true,
(true, 3) => true,
(false, 3) => true,
_ => false
};
next.set(x, y, new_state);
}
}
field = next;
}
}
fn count_neighbors(field: &PixelGrid, 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;
}
}
return count;
}

View file

@ -1,17 +1,20 @@
use servicepoint2::{Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid, Size, TILE_WIDTH, Window};
use std::thread;
use std::time::Duration;
use servicepoint2::{Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
use servicepoint2::Command::BitmapLinearWin;
fn main() {
let connection = Connection::open("localhost:2342").unwrap();
for x_offset in 0..usize::MAX {
let origin = Origin(0, 0);
let mut pixels = PixelGrid::max_sized();
for x_offset in 0..usize::MAX {
pixels.fill(false);
for y in 0..PIXEL_HEIGHT as usize {
pixels.set((y + x_offset) % PIXEL_WIDTH as usize, y, true);
}
let window = Window(Origin(0, 0), Size(TILE_WIDTH, PIXEL_HEIGHT));
connection.send(BitmapLinearWin(window, pixels)).unwrap();
connection.send(BitmapLinearWin(origin, pixels.clone())).unwrap();
thread::sleep(Duration::from_millis(14));
}
}

View file

@ -11,9 +11,7 @@ impl BitVec {
}
pub fn set(&mut self, index: usize, value: bool) -> bool {
let byte_index = index / 8;
let bit_in_byte_index = 7 - index % 8;
let bit_mask = 1 << bit_in_byte_index;
let (byte_index, bit_mask) = self.get_indexes(index);
let byte = self.data[byte_index];
let old_value = byte & bit_mask != 0;
@ -21,17 +19,27 @@ impl BitVec {
self.data[byte_index] = if value {
byte | bit_mask
} else {
byte ^ bit_mask
byte & (u8::MAX ^ bit_mask)
};
return old_value;
}
pub fn get(&self, index: usize) -> bool {
let (byte_index, bit_mask) = self.get_indexes(index);
return self.data[byte_index] & bit_mask != 0;
}
pub fn fill(&mut self, value: bool){
let byte: u8 = if value {0xFF} else {0x00};
self.data.fill(byte);
}
fn get_indexes(&self, index: usize) -> (usize, u8) {
let byte_index = index / 8;
let bit_in_byte_index = 7 - index % 8;
let bit_mask = 1 << bit_in_byte_index;
return self.data[byte_index] & bit_mask != 0;
let bit_mask: u8 = 1 << bit_in_byte_index;
return (byte_index, bit_mask);
}
}

View file

@ -1,25 +1,22 @@
use crate::{BitVec, Header, Packet, PixelGrid, ToPacket};
use crate::{BitVec, Header, Packet, PixelGrid, TILE_SIZE, ToPacket};
/// A window
#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct Window(pub Origin, pub Size);
/// An origin marks the top left position of the
/// data sent to the display.
/// A window
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Origin(pub u16, pub u16);
/// Size defines the width and height of a window
/// A window
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Size(pub u16, pub u16);
type Offset = u16;
type Brightness = u8;
#[derive(Debug)]
pub enum Command {
Clear,
@ -32,7 +29,7 @@ pub enum Command {
BitmapLinearOr(Offset, BitVec),
BitmapLinearXor(Offset, BitVec),
Cp437Data(Window, Vec<u8>),
BitmapLinearWin(Window, PixelGrid),
BitmapLinearWin(Origin, PixelGrid),
}
fn offset_and_payload(command: u16, offset: Offset, payload: Vec<u8>) -> Packet {
@ -53,7 +50,11 @@ impl ToPacket for Command {
Command::HardReset => Packet(Header(0x000b, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
Command::FadeOut => Packet(Header(0x000d, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
Command::BitmapLinear(offset, bits) => offset_and_payload(0x0012, offset, bits.into()),
Command::BitmapLinearWin(window, pixels) => window_and_payload(0x0013, window, pixels.into()),
Command::BitmapLinearWin(Origin(pixel_x, pixel_y), pixels) => {
debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0);
Packet(Header(0x0013, pixel_x / TILE_SIZE, pixel_y, pixels.width as u16/ TILE_SIZE,pixels.height as u16), pixels.into())
}
Command::BitmapLinearAnd(offset, bits) => offset_and_payload(0x0014, offset, bits.into()),
Command::BitmapLinearOr(offset, bits) => offset_and_payload(0x0015, offset, bits.into()),
Command::BitmapLinearXor(offset, bits) => offset_and_payload(0x0016, offset, bits.into()),

View file

@ -29,6 +29,10 @@ impl PixelGrid {
pub fn get(&self, x: usize, y: usize) -> bool {
self.bit_vec.get(x + y * self.width)
}
pub fn fill(&mut self, value: bool) {
self.bit_vec.fill(value);
}
}
impl Into<Vec<u8>> for PixelGrid {