Browse Source

Add a basic AM service wrapper and an example that uses it

pull/95/head
Maccraft123 2 years ago
parent
commit
044cbeecc9
  1. 37
      ctru-rs/examples/title-info.rs
  2. 43
      ctru-rs/src/services/am.rs
  3. 8
      ctru-rs/src/services/fs.rs
  4. 1
      ctru-rs/src/services/mod.rs

37
ctru-rs/examples/title-info.rs

@ -0,0 +1,37 @@
use ctru::prelude::*;
use ctru::services::am::Am;
use ctru::services::fs::FsMediaType;
fn main() {
ctru::use_panic_handler();
let gfx = Gfx::init().expect("Couldn't obtain GFX controller");
let hid = Hid::init().expect("Couldn't obtain HID controller");
let apt = Apt::init().expect("Couldn't obtain APT controller");
let am = Am::init().expect("Couldn't obtain AM controller");
let _console = Console::init(gfx.top_screen.borrow_mut());
let title_count = am.get_title_count(FsMediaType::Sd).expect("Failed to get title count");
println!("This 3DS has {title_count} titles on its SD Card:");
let title_list = am.get_title_list(FsMediaType::Sd).expect("Failed to get title list");
for id in title_list {
println!("{id:x}");
}
// Main loop
while apt.main_loop() {
//Scan all the inputs. This should be done once for each frame
hid.scan_input();
if hid.keys_down().contains(KeyPad::KEY_START) {
break;
}
// Flush and swap framebuffers
gfx.flush_buffers();
gfx.swap_buffers();
//Wait for VBlank
gfx.wait_for_vblank();
}
}

43
ctru-rs/src/services/am.rs

@ -0,0 +1,43 @@
use crate::error::ResultCode;
use crate::services::fs::FsMediaType;
pub struct Am(());
impl Am {
pub fn init() -> crate::Result<Am> {
unsafe {
ResultCode(ctru_sys::amInit())?;
Ok(Am(()))
}
}
pub fn get_title_count(&self, mediatype: FsMediaType) -> crate::Result<u32> {
unsafe {
let mut count = 0;
ResultCode(ctru_sys::AM_GetTitleCount(mediatype as u32, &mut count))?;
Ok(count)
}
}
pub fn get_title_list(&self, mediatype: FsMediaType) -> crate::Result<Vec<u64>> {
unsafe {
let count = self.get_title_count(mediatype)?;
let mut buf = Vec::with_capacity(count as usize);
let mut read_amount = 0;
ResultCode(ctru_sys::AM_GetTitleList(
&mut read_amount,
mediatype as u32,
count,
buf.as_mut_ptr(),
))?;
buf.set_len(read_amount as usize);
Ok(buf)
}
}
}
impl Drop for Am {
fn drop(&mut self) {
unsafe { ctru_sys::amExit() };
}
}

8
ctru-rs/src/services/fs.rs

@ -43,6 +43,14 @@ bitflags! {
} }
} }
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum FsMediaType {
Nand = ctru_sys::MEDIATYPE_NAND,
Sd = ctru_sys::MEDIATYPE_SD,
GameCard = ctru_sys::MEDIATYPE_GAME_CARD,
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum PathType { pub enum PathType {
Invalid, Invalid,

1
ctru-rs/src/services/mod.rs

@ -6,6 +6,7 @@
//! Some include: button input, audio playback, graphics rendering, built-in cameras, etc. //! Some include: button input, audio playback, graphics rendering, built-in cameras, etc.
pub mod apt; pub mod apt;
pub mod am;
pub mod cam; pub mod cam;
pub mod cfgu; pub mod cfgu;
pub mod fs; pub mod fs;

Loading…
Cancel
Save