Browse Source

Add Hid struct.

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

82
src/services/hid.rs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
use ::Result;
use core::convert::Into;
use core::marker::PhantomData;
use ::raw::services::hid;
@ -34,11 +35,12 @@ pub enum PadKey { @@ -34,11 +35,12 @@ pub enum PadKey {
Right
}
fn to_raw_padkey(key: PadKey) -> u32 {
impl From<PadKey> for u32 {
fn from(p: PadKey) -> u32 {
use ::raw::services::hid::PAD_KEY::*;
use self::PadKey::*;
match key {
match p {
Up => KEY_DUP as u32 | KEY_CPAD_UP as u32,
Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32,
Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32,
@ -68,67 +70,65 @@ fn to_raw_padkey(key: PadKey) -> u32 { @@ -68,67 +70,65 @@ fn to_raw_padkey(key: PadKey) -> u32 {
DPadUp => KEY_DUP as u32,
DPadDown => KEY_DDOWN as u32
}
}
}
pub fn init() -> Result {
unsafe {
// TODO allow sharedMem argument?
return hid::hidInit(0 as *mut u32);
}
pub struct Hid {
pd: PhantomData<i32>
}
pub fn exit() -> () {
impl Hid {
pub fn new() -> Result<Hid, i32> {
unsafe {
hid::hidExit();
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.
///
/// # 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 scan_input(&mut self) {
unsafe { hid::hidScanInput() };
}
}
pub fn key_down(key: PadKey) -> bool {
pub fn key_down(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
if hid::hidKeysDown() & to_raw_padkey(key) != 0 {
return true;
if hid::hidKeysDown() & k != 0 {
true
} else {
return false;
false
}
}
}
}
pub fn key_held(key: PadKey) -> bool {
pub fn key_held(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
if hid::hidKeysHeld() & to_raw_padkey(key) != 0 {
return true;
if hid::hidKeysHeld() & k != 0 {
true
} else {
return false;
false
}
}
}
}
pub fn key_up(key: PadKey) -> bool {
pub fn key_up(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
if hid::hidKeysUp() & to_raw_padkey(key) != 0 {
if hid::hidKeysUp() & k != 0 {
return true;
} else {
return false;
}
}
}
}
impl Drop for Hid {
fn drop(&mut self) {
unsafe { hid::hidExit() };
}
}

2
src/services/mod.rs

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

Loading…
Cancel
Save