audio works! almost...

This commit is contained in:
Alex Janka 2023-02-19 19:10:53 +11:00
parent 0f5bcb8e46
commit f2e08c2c1c

View file

@ -128,7 +128,7 @@ pub(super) struct PwmChannel {
impl PwmChannel { impl PwmChannel {
pub(super) fn new(enabled: bool) -> Self { pub(super) fn new(enabled: bool) -> Self {
let wavelength = 0x700; let wavelength = 0x7FF;
Self { Self {
enabled, enabled,
sweep: Sweep::default(), sweep: Sweep::default(),
@ -138,7 +138,7 @@ impl PwmChannel {
envelope: Envelope::default(), envelope: Envelope::default(),
queued_envelope: Envelope::default(), queued_envelope: Envelope::default(),
wavelength, wavelength,
wave_timer: set_wave_timer(wavelength), wave_timer: (2048 - wavelength) * 4,
wave_position: 0, wave_position: 0,
} }
} }
@ -156,6 +156,7 @@ impl PwmChannel {
(self.wave_timer, b) = self.wave_timer.overflowing_sub(1); (self.wave_timer, b) = self.wave_timer.overflowing_sub(1);
if b { if b {
self.wave_position = (self.wave_position + 1) % 8; self.wave_position = (self.wave_position + 1) % 8;
self.set_wave_timer();
} }
self.dac(self.duty_cycle.as_waveform()[self.wave_position]) self.dac(self.duty_cycle.as_waveform()[self.wave_position])
}) })
@ -250,11 +251,13 @@ impl PwmChannel {
pub(super) fn update_wavelength_low(&mut self, data: u8) { pub(super) fn update_wavelength_low(&mut self, data: u8) {
self.wavelength = (self.wavelength & 0xFF00) | (data as u16); self.wavelength = (self.wavelength & 0xFF00) | (data as u16);
self.set_wave_timer();
} }
pub(super) fn update_wavelength_high_and_control(&mut self, data: u8) { pub(super) fn update_wavelength_high_and_control(&mut self, data: u8) {
self.length_enable = get_bit(data, 6); self.length_enable = get_bit(data, 6);
self.wavelength = (self.wavelength & 0xFF) | (((data & 0b111) as u16) << 8); self.wavelength = (self.wavelength & 0xFF) | (((data & 0b111) as u16) << 8);
self.set_wave_timer();
if get_bit(data, 7) { if get_bit(data, 7) {
self.trigger(); self.trigger();
} }
@ -263,10 +266,10 @@ impl PwmChannel {
pub(super) fn get_control(&self) -> u8 { pub(super) fn get_control(&self) -> u8 {
set_or_clear_bit(0, 6, self.length_enable) set_or_clear_bit(0, 6, self.length_enable)
} }
}
fn set_wave_timer(wavelength: u16) -> u16 { fn set_wave_timer(&mut self) {
(2048 - wavelength) * 4 self.wave_timer = (2048 - self.wavelength) * 4;
}
} }
pub(super) struct WaveChannel { pub(super) struct WaveChannel {