Browse Source

Add Hid struct.

pull/10/head
Ronald Kinard 9 years ago
parent
commit
f10d549562
  1. 158
      src/services/hid.rs
  2. 2
      src/services/mod.rs

158
src/services/hid.rs

@ -1,4 +1,5 @@
use ::Result; use core::convert::Into;
use core::marker::PhantomData;
use ::raw::services::hid; use ::raw::services::hid;
@ -34,101 +35,100 @@ pub enum PadKey {
Right Right
} }
fn to_raw_padkey(key: PadKey) -> u32 { impl From<PadKey> for u32 {
use ::raw::services::hid::PAD_KEY::*; fn from(p: PadKey) -> u32 {
use self::PadKey::*; use ::raw::services::hid::PAD_KEY::*;
use self::PadKey::*;
match key { match p {
Up => KEY_DUP as u32 | KEY_CPAD_UP as u32, Up => KEY_DUP as u32 | KEY_CPAD_UP as u32,
Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32, Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32,
Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32, Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32,
Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32, Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32,
A => KEY_A as u32, A => KEY_A as u32,
B => KEY_B as u32, B => KEY_B as u32,
X => KEY_X as u32, X => KEY_X as u32,
Y => KEY_Y as u32, Y => KEY_Y as u32,
L => KEY_L as u32, L => KEY_L as u32,
R => KEY_R as u32, R => KEY_R as u32,
ZL => KEY_ZL as u32, ZL => KEY_ZL as u32,
ZR => KEY_ZR as u32, ZR => KEY_ZR as u32,
Start => KEY_START as u32, Start => KEY_START as u32,
Select => KEY_SELECT as u32, Select => KEY_SELECT as u32,
Touch => KEY_TOUCH as u32, Touch => KEY_TOUCH as u32,
CSRight => KEY_CSTICK_RIGHT as u32, CSRight => KEY_CSTICK_RIGHT as u32,
CSLeft => KEY_CSTICK_LEFT as u32, CSLeft => KEY_CSTICK_LEFT as u32,
CSUp => KEY_CSTICK_UP as u32, CSUp => KEY_CSTICK_UP as u32,
CSDown => KEY_CSTICK_DOWN as u32, CSDown => KEY_CSTICK_DOWN as u32,
CRight => KEY_CPAD_RIGHT as u32, CRight => KEY_CPAD_RIGHT as u32,
CLeft => KEY_CPAD_LEFT as u32, CLeft => KEY_CPAD_LEFT as u32,
CDown => KEY_CPAD_DOWN as u32, CDown => KEY_CPAD_DOWN as u32,
CUp => KEY_CPAD_UP as u32, CUp => KEY_CPAD_UP as u32,
DPadLeft => KEY_DLEFT as u32, DPadLeft => KEY_DLEFT as u32,
DPadRight => KEY_DRIGHT as u32, DPadRight => KEY_DRIGHT as u32,
DPadUp => KEY_DUP as u32, DPadUp => KEY_DUP as u32,
DPadDown => KEY_DDOWN as u32 DPadDown => KEY_DDOWN as u32
}
} }
} }
pub fn init() -> Result { pub struct Hid {
unsafe { pd: PhantomData<i32>
// TODO allow sharedMem argument?
return hid::hidInit(0 as *mut u32);
}
} }
pub fn exit() -> () { impl Hid {
unsafe { pub fn new() -> Result<Hid, i32> {
hid::hidExit(); unsafe {
let r = hid::hidInit(hid::HID_SHAREDMEM_DEFAULT as *mut u32);
if r < 0 {
Err(r)
} else {
Ok(Hid { pd: PhantomData })
}
}
} }
}
/// Update ctrulib's button states. pub fn scan_input(&mut self) {
/// unsafe { hid::hidScanInput() };
/// # Examples
///
/// ```
/// use ctru::service::apt;
///
/// apt::main_loop(|| {
/// scan_input();
/// if key_down(PadKey::A) {
/// apt::set_status(apt::AppStatus::Exiting);
/// }
/// });
/// ```
pub fn scan_input() -> () {
unsafe {
hid::hidScanInput();
} }
}
pub fn key_down(key: PadKey) -> bool { pub fn key_down(&self, key: PadKey) -> bool {
unsafe { let k: u32 = key.into();
if hid::hidKeysDown() & to_raw_padkey(key) != 0 { unsafe {
return true; if hid::hidKeysDown() & k != 0 {
} else { true
return false; } else {
false
}
} }
} }
}
pub fn key_held(key: PadKey) -> bool { pub fn key_held(&self, key: PadKey) -> bool {
unsafe { let k: u32 = key.into();
if hid::hidKeysHeld() & to_raw_padkey(key) != 0 { unsafe {
return true; if hid::hidKeysHeld() & k != 0 {
} else { true
return false; } else {
false
}
} }
} }
}
pub fn key_up(key: PadKey) -> bool { pub fn key_up(&self, key: PadKey) -> bool {
unsafe { let k: u32 = key.into();
if hid::hidKeysUp() & to_raw_padkey(key) != 0 { unsafe {
return true; if hid::hidKeysUp() & k != 0 {
} else { return true;
return false; } else {
return false;
}
} }
} }
} }
impl Drop for Hid {
fn drop(&mut self) {
unsafe { hid::hidExit() };
}
}

2
src/services/mod.rs

@ -1,3 +1,5 @@
pub mod apt; pub mod apt;
pub mod hid; pub mod hid;
pub mod gsp; pub mod gsp;
pub use self::hid::Hid;

Loading…
Cancel
Save