mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 17:41:33 +11:00
Improve timer API and use it
This commit is contained in:
parent
d65fd5d740
commit
3a419b05de
|
@ -8,7 +8,7 @@ use super::{SoundChannel, SoundPriority};
|
||||||
use crate::{
|
use crate::{
|
||||||
fixnum::Num,
|
fixnum::Num,
|
||||||
interrupt::free,
|
interrupt::free,
|
||||||
interrupt::{add_interrupt_handler, Interrupt, InterruptHandler},
|
interrupt::{add_interrupt_handler, InterruptHandler},
|
||||||
timer::Divider,
|
timer::Divider,
|
||||||
timer::Timer,
|
timer::Timer,
|
||||||
};
|
};
|
||||||
|
@ -63,13 +63,14 @@ impl Mixer {
|
||||||
/// You are still required to call `frame`
|
/// You are still required to call `frame`
|
||||||
pub fn setup_interrupt_handler(&self) -> InterruptHandler<'_> {
|
pub fn setup_interrupt_handler(&self) -> InterruptHandler<'_> {
|
||||||
let mut timer1 = unsafe { Timer::new(1) };
|
let mut timer1 = unsafe { Timer::new(1) };
|
||||||
timer1.set_cascade(true);
|
timer1
|
||||||
timer1.set_divider(Divider::Divider1);
|
.set_cascade(true)
|
||||||
timer1.set_interrupt(true);
|
.set_divider(Divider::Divider1)
|
||||||
timer1.set_overflow_amount(constants::SOUND_BUFFER_SIZE as u16);
|
.set_interrupt(true)
|
||||||
timer1.set_enabled(true);
|
.set_overflow_amount(constants::SOUND_BUFFER_SIZE as u16)
|
||||||
|
.set_enabled(true);
|
||||||
|
|
||||||
add_interrupt_handler(Interrupt::Timer1, move |cs| self.buffer.swap(cs))
|
add_interrupt_handler(timer1.interrupt(), move |cs| self.buffer.swap(cs))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame(&mut self) {
|
pub fn frame(&mut self) {
|
||||||
|
|
|
@ -62,32 +62,37 @@ impl Timer {
|
||||||
new_timer
|
new_timer
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_overflow_amount(&mut self, n: u16) {
|
pub fn set_overflow_amount(&mut self, n: u16) -> &mut Self {
|
||||||
let count_up_value = 0u16.wrapping_sub(n);
|
let count_up_value = 0u16.wrapping_sub(n);
|
||||||
self.data_register().set(count_up_value);
|
self.data_register().set(count_up_value);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> u16 {
|
pub fn value(&self) -> u16 {
|
||||||
self.data_register().get()
|
self.data_register().get()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_divider(&mut self, divider: Divider) {
|
pub fn set_divider(&mut self, divider: Divider) -> &mut Self {
|
||||||
self.control_register().set_bits(divider.as_bits(), 2, 0);
|
self.control_register().set_bits(divider.as_bits(), 2, 0);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_enabled(&mut self, enabled: bool) {
|
pub fn set_enabled(&mut self, enabled: bool) -> &mut Self {
|
||||||
let bit = if enabled { 1 } else { 0 };
|
let bit = if enabled { 1 } else { 0 };
|
||||||
self.control_register().set_bits(bit, 1, 7);
|
self.control_register().set_bits(bit, 1, 7);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cascade(&mut self, cascade: bool) {
|
pub fn set_cascade(&mut self, cascade: bool) -> &mut Self {
|
||||||
let bit = if cascade { 1 } else { 0 };
|
let bit = if cascade { 1 } else { 0 };
|
||||||
self.control_register().set_bits(bit, 1, 2);
|
self.control_register().set_bits(bit, 1, 2);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_interrupt(&mut self, interrupt: bool) {
|
pub fn set_interrupt(&mut self, interrupt: bool) -> &mut Self {
|
||||||
let bit = interrupt as u16;
|
let bit = interrupt as u16;
|
||||||
self.control_register().set_bits(bit, 1, 6);
|
self.control_register().set_bits(bit, 1, 6);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn data_register(&self) -> MemoryMapped<u16> {
|
fn data_register(&self) -> MemoryMapped<u16> {
|
||||||
|
|
Loading…
Reference in a new issue