Use SoundDirection rather than a boolean

This commit is contained in:
Gwilym Kuiper 2021-04-15 23:53:49 +01:00 committed by Corwin
parent dda79df12d
commit 82fd9ce120
2 changed files with 47 additions and 29 deletions

View file

@ -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 {}

View file

@ -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)
}
}