From dda79df12d4a6ff34571e6aea271c8774fe5c667 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 15 Apr 2021 23:49:17 +0100 Subject: [PATCH] Make sweep settings configurable --- examples/beep.rs | 8 ++++---- src/sound/mod.rs | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/examples/beep.rs b/examples/beep.rs index da35dfcc..88ac8e9c 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -3,16 +3,16 @@ extern crate gba; -use gba::display; +use gba::sound; #[start] fn main(_argc: isize, _argv: *const *const u8) -> isize { let mut gba = gba::Gba::new(); - let mut bitmap = gba.display.video.bitmap3(); - let vblank = gba.display.vblank.get(); gba.sound.enable(); - gba.sound.channel1().play_sound(); + + let sweep_settings = gba::sound::SweepSettings::new(3, false, 7); + gba.sound.channel1().play_sound(&sweep_settings); loop {} } diff --git a/src/sound/mod.rs b/src/sound/mod.rs index 9fd4bb66..02974c8c 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -31,9 +31,43 @@ impl Sound { #[non_exhaustive] pub struct Channel1 {} +pub struct SweepSettings { + number_of_sweep_shifts: u8, + increase_sweep: bool, + sweep_time: u8, +} + +impl SweepSettings { + pub fn new(number_of_sweep_shifts: u8, increase_sweep: bool, sweep_time: u8) -> Self { + assert!( + number_of_sweep_shifts < 8, + "Number of sweep shifts must be less than 8" + ); + assert!(sweep_time < 8, "Sweep time must be less than 8"); + + SweepSettings { + number_of_sweep_shifts, + increase_sweep, + sweep_time, + } + } + + fn as_bits(&self) -> u16 { + ((self.number_of_sweep_shifts as u16) & 0b111) + | ((self.increase_sweep as u16) << 3) + | ((self.sweep_time as u16) & 0b111) << 4 + } +} + +impl Default for SweepSettings { + fn default() -> Self { + SweepSettings::new(0, true, 0) + } +} + impl Channel1 { - pub fn play_sound(&self) { - CHANNEL_1_SWEEP.set(0b00000000_111_0_010); + pub fn play_sound(&self, sweep_settings: &SweepSettings) { + CHANNEL_1_SWEEP.set(sweep_settings.as_bits()); CHANNEL_1_LENGTH_DUTY_ENVELOPE.set(0b111_1_001_01_111111); CHANNEL_1_FREQUENCY_CONTROL.set(0b1_0_000_01000000000); }