diff --git a/examples/beep.rs b/examples/beep.rs index 88ac8e9c..f0dffc49 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -11,7 +11,7 @@ fn main(_argc: isize, _argv: *const *const u8) -> isize { gba.sound.enable(); - let sweep_settings = gba::sound::SweepSettings::new(3, false, 7); + let sweep_settings = sound::SweepSettings::new(3, sound::SoundDirection::Decrease, 7); gba.sound.channel1().play_sound(&sweep_settings); loop {} diff --git a/src/sound/mod.rs b/src/sound/mod.rs index 02974c8c..a408fe03 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -31,37 +31,17 @@ impl Sound { #[non_exhaustive] pub struct Channel1 {} -pub struct SweepSettings { - number_of_sweep_shifts: u8, - increase_sweep: bool, - sweep_time: u8, +pub enum SoundDirection { + Increase, + Decrease, } -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, - } - } - +impl SoundDirection { 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) + match &self { + SoundDirection::Increase => 1, + SoundDirection::Decrease => 0, + } } } @@ -72,3 +52,41 @@ impl Channel1 { CHANNEL_1_FREQUENCY_CONTROL.set(0b1_0_000_01000000000); } } + +pub struct SweepSettings { + number_of_sweep_shifts: u8, + sound_direction: SoundDirection, + sweep_time: u8, +} + +impl SweepSettings { + pub fn new( + number_of_sweep_shifts: u8, + sound_direction: SoundDirection, + 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, + sound_direction, + sweep_time, + } + } + + fn as_bits(&self) -> u16 { + ((self.number_of_sweep_shifts as u16) & 0b111) + | (self.sound_direction.as_bits() << 3) + | ((self.sweep_time as u16) & 0b111) << 4 + } +} + +impl Default for SweepSettings { + fn default() -> Self { + SweepSettings::new(0, SoundDirection::Increase, 0) + } +}