Make sweep settings configurable

This commit is contained in:
Gwilym Kuiper 2021-04-15 23:49:17 +01:00 committed by Corwin
parent 71160c7116
commit dda79df12d
2 changed files with 40 additions and 6 deletions

View file

@ -3,16 +3,16 @@
extern crate gba; extern crate gba;
use gba::display; use gba::sound;
#[start] #[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize { fn main(_argc: isize, _argv: *const *const u8) -> isize {
let mut gba = gba::Gba::new(); let mut gba = gba::Gba::new();
let mut bitmap = gba.display.video.bitmap3();
let vblank = gba.display.vblank.get();
gba.sound.enable(); 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 {} loop {}
} }

View file

@ -31,9 +31,43 @@ impl Sound {
#[non_exhaustive] #[non_exhaustive]
pub struct Channel1 {} 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 { impl Channel1 {
pub fn play_sound(&self) { pub fn play_sound(&self, sweep_settings: &SweepSettings) {
CHANNEL_1_SWEEP.set(0b00000000_111_0_010); CHANNEL_1_SWEEP.set(sweep_settings.as_bits());
CHANNEL_1_LENGTH_DUTY_ENVELOPE.set(0b111_1_001_01_111111); CHANNEL_1_LENGTH_DUTY_ENVELOPE.set(0b111_1_001_01_111111);
CHANNEL_1_FREQUENCY_CONTROL.set(0b1_0_000_01000000000); CHANNEL_1_FREQUENCY_CONTROL.set(0b1_0_000_01000000000);
} }