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 @@ @@ -1,4 +1,5 @@
use ::Result;
use core::convert::Into;
use core::marker::PhantomData;
use ::raw::services::hid;
@ -34,101 +35,100 @@ pub enum PadKey { @@ -34,101 +35,100 @@ pub enum PadKey {
Right
}
fn to_raw_padkey(key: PadKey) -> u32 {
use ::raw::services::hid::PAD_KEY::*;
use self::PadKey::*;
impl From<PadKey> for u32 {
fn from(p: PadKey) -> u32 {
use ::raw::services::hid::PAD_KEY::*;
use self::PadKey::*;
match key {
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,
Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32,
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,
Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32,
A => KEY_A as u32,
B => KEY_B as u32,
X => KEY_X as u32,
Y => KEY_Y as u32,
L => KEY_L as u32,
R => KEY_R as u32,
ZL => KEY_ZL as u32,
ZR => KEY_ZR as u32,
Start => KEY_START as u32,
Select => KEY_SELECT as u32,
Touch => KEY_TOUCH as u32,
CSRight => KEY_CSTICK_RIGHT as u32,
CSLeft => KEY_CSTICK_LEFT as u32,
CSUp => KEY_CSTICK_UP as u32,
CSDown => KEY_CSTICK_DOWN as u32,
CRight => KEY_CPAD_RIGHT as u32,
CLeft => KEY_CPAD_LEFT as u32,
CDown => KEY_CPAD_DOWN as u32,
CUp => KEY_CPAD_UP as u32,
DPadLeft => KEY_DLEFT as u32,
DPadRight => KEY_DRIGHT as u32,
DPadUp => KEY_DUP as u32,
DPadDown => KEY_DDOWN as u32
A => KEY_A as u32,
B => KEY_B as u32,
X => KEY_X as u32,
Y => KEY_Y as u32,
L => KEY_L as u32,
R => KEY_R as u32,
ZL => KEY_ZL as u32,
ZR => KEY_ZR as u32,
Start => KEY_START as u32,
Select => KEY_SELECT as u32,
Touch => KEY_TOUCH as u32,
CSRight => KEY_CSTICK_RIGHT as u32,
CSLeft => KEY_CSTICK_LEFT as u32,
CSUp => KEY_CSTICK_UP as u32,
CSDown => KEY_CSTICK_DOWN as u32,
CRight => KEY_CPAD_RIGHT as u32,
CLeft => KEY_CPAD_LEFT as u32,
CDown => KEY_CPAD_DOWN as u32,
CUp => KEY_CPAD_UP as u32,
DPadLeft => KEY_DLEFT as u32,
DPadRight => KEY_DRIGHT as 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() -> () {
unsafe {
hid::hidExit();
impl Hid {
pub fn new() -> Result<Hid, i32> {
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.
///
/// # 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 {
unsafe {
if hid::hidKeysDown() & to_raw_padkey(key) != 0 {
return true;
} else {
return false;
pub fn key_down(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
if hid::hidKeysDown() & k != 0 {
true
} else {
false
}
}
}
}
pub fn key_held(key: PadKey) -> bool {
unsafe {
if hid::hidKeysHeld() & to_raw_padkey(key) != 0 {
return true;
} else {
return false;
pub fn key_held(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
if hid::hidKeysHeld() & k != 0 {
true
} else {
false
}
}
}
}
pub fn key_up(key: PadKey) -> bool {
unsafe {
if hid::hidKeysUp() & to_raw_padkey(key) != 0 {
return true;
} else {
return false;
pub fn key_up(&self, key: PadKey) -> bool {
let k: u32 = key.into();
unsafe {
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