chip8/src/periph.rs

84 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-25 13:13:09 -05:00
#[derive(Copy, Clone)]
pub struct Display {
pub buf: [u8; Self::SCREEN_SIZE],
}
impl Display {
const SCREEN_SIZE: usize = 64 * 32;
pub fn clear(&mut self) {
self.buf = [0; Self::SCREEN_SIZE]
}
}
impl Default for Display {
fn default() -> Self {
Display {
buf: [0; Self::SCREEN_SIZE],
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Timer {
remaining: u8,
enabled: bool,
}
impl Default for Timer {
fn default() -> Self {
Timer {
remaining: 0,
enabled: false,
}
}
}
impl Timer {
pub fn set(&mut self, secs: u8) {
self.remaining = secs;
self.enabled = true;
}
pub fn get(&self) -> u8 {
self.remaining
}
pub fn tick(&mut self) {
if self.enabled {
self.remaining -= 1;
if self.remaining == 0 {
println!("Beep!");
self.enabled = false;
}
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Keypad {
keys: [bool; 16],
}
impl Default for Keypad {
fn default() -> Self {
Self { keys: [false; 16] }
}
}
impl Keypad {
pub fn get_pressed(&self) -> Option<u8> {
let mut found: Option<u8> = None;
for (index, key) in self.keys.iter().enumerate() {
2020-06-26 15:54:18 -05:00
if *key {
2020-06-25 13:13:09 -05:00
found = Some(index as u8);
break;
}
}
found
}
}