game of life example, bug fixes
This commit is contained in:
parent
a7b6cf2ad0
commit
e6f8afb378
7 changed files with 439 additions and 20 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()),
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue