use log::trace; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)] pub struct SoundTimer { counter: i32, } impl Default for SoundTimer { fn default() -> Self { Self::new() } } impl SoundTimer { pub fn current(&self) -> i32 { self.counter } pub fn new() -> Self { SoundTimer { counter: 0 } } pub fn set_timer(&mut self, new_value: i32) { trace!("SETTING SOUND TIMER TO {new_value}"); self.counter = new_value } pub fn tick(&mut self) { trace!( "TICKING SOUND FROM {} to {}", self.counter, self.counter - 1 ); if self.counter > 0 { self.counter -= 1; /* todo: this breaks tests. maybe its wrong? if let good_thread = thread::spawn(|| { beep(440).expect("Unable to beep"); thread::sleep(time::Duration::from_millis(500)); }).join().unwrap(). */ } } pub fn reset(&mut self) { self.counter = 0x00; } }