hid.h
Go to the documentation of this file.
/**
* @file hid.h
* @brief HID service.
*/
#pragma once
//See also: http://3dbrew.org/wiki/HID_Services http://3dbrew.org/wiki/HID_Shared_Memory
/// Key values.
enum
{
KEY_A = BIT(0), ///< A
KEY_B = BIT(1), ///< B
KEY_SELECT = BIT(2), ///< Select
KEY_START = BIT(3), ///< Start
KEY_DRIGHT = BIT(4), ///< D-Pad Right
KEY_DLEFT = BIT(5), ///< D-Pad Left
KEY_DUP = BIT(6), ///< D-Pad Up
KEY_DDOWN = BIT(7), ///< D-Pad Down
KEY_R = BIT(8), ///< R
KEY_L = BIT(9), ///< L
KEY_X = BIT(10), ///< X
KEY_Y = BIT(11), ///< Y
KEY_ZL = BIT(14), ///< ZL (New 3DS only)
KEY_ZR = BIT(15), ///< ZR (New 3DS only)
KEY_TOUCH = BIT(20), ///< Touch (Not actually provided by HID)
KEY_CSTICK_RIGHT = BIT(24), ///< C-Stick Right (New 3DS only)
KEY_CSTICK_LEFT = BIT(25), ///< C-Stick Left (New 3DS only)
KEY_CSTICK_UP = BIT(26), ///< C-Stick Up (New 3DS only)
KEY_CSTICK_DOWN = BIT(27), ///< C-Stick Down (New 3DS only)
KEY_CPAD_RIGHT = BIT(28), ///< Circle Pad Right
KEY_CPAD_LEFT = BIT(29), ///< Circle Pad Left
KEY_CPAD_UP = BIT(30), ///< Circle Pad Up
KEY_CPAD_DOWN = BIT(31), ///< Circle Pad Down
// Generic catch-all directions
KEY_UP = KEY_DUP | KEY_CPAD_UP, ///< D-Pad Up or Circle Pad Up
KEY_DOWN = KEY_DDOWN | KEY_CPAD_DOWN, ///< D-Pad Down or Circle Pad Down
KEY_LEFT = KEY_DLEFT | KEY_CPAD_LEFT, ///< D-Pad Left or Circle Pad Left
KEY_RIGHT = KEY_DRIGHT | KEY_CPAD_RIGHT, ///< D-Pad Right or Circle Pad Right
};
/// Touch position.
typedef struct
{
u16 px; ///< Touch X
u16 py; ///< Touch Y
} touchPosition;
/// Circle Pad position.
typedef struct
{
s16 dx; ///< Pad X
s16 dy; ///< Pad Y
} circlePosition;
/// Accelerometer vector.
typedef struct
{
s16 x; ///< Accelerometer X
s16 y; ///< Accelerometer Y
s16 z; ///< Accelerometer Z
} accelVector;
/// Gyroscope angular rate.
typedef struct
{
s16 x; ///< Roll
s16 z; ///< Yaw
s16 y; ///< Pitch
} angularRate;
/// HID events.
typedef enum
{
HIDEVENT_PAD0 = 0, ///< Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was updated.
HIDEVENT_PAD1, ///< Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was updated.
HIDEVENT_Accel, ///< Event signaled by HID-module, when the sharedmem accelerometer state was updated.
HIDEVENT_Gyro, ///< Event signaled by HID-module, when the sharedmem gyroscope state was updated.
HIDEVENT_DebugPad, ///< Event signaled by HID-module, when the sharedmem DebugPad state was updated.
HIDEVENT_MAX, ///< Used to know how many events there are.
} HID_Event;
extern Handle hidMemHandle; ///< HID shared memory handle.
extern vu32* hidSharedMem; ///< HID shared memory.
/// Initializes HID.
Result hidInit(void);
/// Exits HID.
void hidExit(void);
/**
* @brief Sets the key repeat parameters for @ref hidKeysRepeat.
* @param delay Initial delay.
* @param interval Repeat interval.
*/
void hidSetRepeatParameters(u32 delay, u32 interval);
/// Scans HID for input data.
void hidScanInput(void);
/**
* @brief Returns a bitmask of held buttons.
* Individual buttons can be extracted using binary AND.
* @return 32-bit bitmask of held buttons (1+ frames).
*/
u32 hidKeysHeld(void);
/**
* @brief Returns a bitmask of newly pressed buttons, this frame.
* Individual buttons can be extracted using binary AND.
* @return 32-bit bitmask of newly pressed buttons.
*/
u32 hidKeysDown(void);
/**
* @brief Returns a bitmask of newly pressed or repeated buttons, this frame.
* Individual buttons can be extracted using binary AND.
* @return 32-bit bitmask of newly pressed or repeated buttons.
*/
u32 hidKeysDownRepeat(void);
/**
* @brief Returns a bitmask of newly released buttons, this frame.
* Individual buttons can be extracted using binary AND.
* @return 32-bit bitmask of newly released buttons.
*/
u32 hidKeysUp(void);
/**
* @brief Reads the current touch position.
* @param pos Pointer to output the touch position to.
*/
void hidTouchRead(touchPosition* pos);
/**
* @brief Reads the current circle pad position.
* @param pos Pointer to output the circle pad position to.
*/
void hidCircleRead(circlePosition* pos);
/**
* @brief Reads the current accelerometer data.
* @param vector Pointer to output the accelerometer data to.
*/
void hidAccelRead(accelVector* vector);
/**
* @brief Reads the current gyroscope data.
* @param rate Pointer to output the gyroscope data to.
*/
void hidGyroRead(angularRate* rate);
/**
* @brief Waits for an HID event.
* @param id ID of the event.
* @param nextEvent Whether to discard the current event and wait for the next event.
*/
void hidWaitForEvent(HID_Event id, bool nextEvent);
/**
* @brief Waits for any HID or IRRST event.
* @param nextEvents Whether to discard the current events and wait for the next events.
* @param cancelEvent Optional additional handle to wait on, otherwise 0.
* @param timeout Timeout.
*/
Result hidWaitForAnyEvent(bool nextEvents, Handle cancelEvent, s64 timeout);
/// Compatibility macro for hidScanInput.
#define scanKeys hidScanInput
/// Compatibility macro for hidKeysHeld.
#define keysHeld hidKeysHeld
/// Compatibility macro for hidKeysDown.
#define keysDown hidKeysDown
/// Compatibility macro for hidKeysUp.
#define keysUp hidKeysUp
/// Compatibility macro for hidTouchRead.
#define touchRead hidTouchRead
/// Compatibility macro for hidCircleRead.
#define circleRead hidCircleRead
/**
* @brief Gets the handles for HID operation.
* @param outMemHandle Pointer to output the shared memory handle to.
* @param eventpad0 Pointer to output the pad 0 event handle to.
* @param eventpad1 Pointer to output the pad 1 event handle to.
* @param eventaccel Pointer to output the accelerometer event handle to.
* @param eventgyro Pointer to output the gyroscope event handle to.
* @param eventdebugpad Pointer to output the debug pad event handle to.
*/
Result HIDUSER_GetHandles(Handle* outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad);
/// Enables the accelerometer.
Result HIDUSER_EnableAccelerometer(void);
/// Disables the accelerometer.
Result HIDUSER_DisableAccelerometer(void);
/// Enables the gyroscope.
Result HIDUSER_EnableGyroscope(void);
/// Disables the gyroscope.
Result HIDUSER_DisableGyroscope(void);
/**
* @brief Gets the gyroscope raw to dps coefficient.
* @param coeff Pointer to output the coefficient to.
*/
Result HIDUSER_GetGyroscopeRawToDpsCoefficient(float *coeff);
/**
* @brief Gets the current volume slider value. (0-63)
* @param volume Pointer to write the volume slider value to.
*/
Result HIDUSER_GetSoundVolume(u8 *volume);
HIDUSER_GetGyroscopeRawToDpsCoefficient
Result HIDUSER_GetGyroscopeRawToDpsCoefficient(float *coeff)
Gets the gyroscope raw to dps coefficient.
HIDUSER_DisableAccelerometer
Result HIDUSER_DisableAccelerometer(void)
Disables the accelerometer.
hidMemHandle
Handle hidMemHandle
HID shared memory handle.
HIDUSER_EnableAccelerometer
Result HIDUSER_EnableAccelerometer(void)
Enables the accelerometer.
hidInit
Result hidInit(void)
Initializes HID.
hidGyroRead
void hidGyroRead(angularRate *rate)
Reads the current gyroscope data.
KEY_TOUCH
@ KEY_TOUCH
Touch (Not actually provided by HID)
Definition: hid.h:26
KEY_UP
@ KEY_UP
D-Pad Up or Circle Pad Up.
Definition: hid.h:37
KEY_DDOWN
@ KEY_DDOWN
D-Pad Down.
Definition: hid.h:19
KEY_B
@ KEY_B
B.
Definition: hid.h:13
KEY_CSTICK_UP
@ KEY_CSTICK_UP
C-Stick Up (New 3DS only)
Definition: hid.h:29
KEY_CPAD_RIGHT
@ KEY_CPAD_RIGHT
Circle Pad Right.
Definition: hid.h:31
KEY_SELECT
@ KEY_SELECT
Select.
Definition: hid.h:14
KEY_DRIGHT
@ KEY_DRIGHT
D-Pad Right.
Definition: hid.h:16
KEY_X
@ KEY_X
X.
Definition: hid.h:22
KEY_ZR
@ KEY_ZR
ZR (New 3DS only)
Definition: hid.h:25
KEY_CSTICK_RIGHT
@ KEY_CSTICK_RIGHT
C-Stick Right (New 3DS only)
Definition: hid.h:27
KEY_Y
@ KEY_Y
Y.
Definition: hid.h:23
KEY_LEFT
@ KEY_LEFT
D-Pad Left or Circle Pad Left.
Definition: hid.h:39
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
KEY_RIGHT
@ KEY_RIGHT
D-Pad Right or Circle Pad Right.
Definition: hid.h:40
KEY_DUP
@ KEY_DUP
D-Pad Up.
Definition: hid.h:18
KEY_ZL
@ KEY_ZL
ZL (New 3DS only)
Definition: hid.h:24
KEY_R
@ KEY_R
R.
Definition: hid.h:20
KEY_A
@ KEY_A
A.
Definition: hid.h:12
KEY_DOWN
@ KEY_DOWN
D-Pad Down or Circle Pad Down.
Definition: hid.h:38
KEY_CPAD_UP
@ KEY_CPAD_UP
Circle Pad Up.
Definition: hid.h:33
KEY_CSTICK_LEFT
@ KEY_CSTICK_LEFT
C-Stick Left (New 3DS only)
Definition: hid.h:28
KEY_L
@ KEY_L
L.
Definition: hid.h:21
KEY_CPAD_DOWN
@ KEY_CPAD_DOWN
Circle Pad Down.
Definition: hid.h:34
KEY_DLEFT
@ KEY_DLEFT
D-Pad Left.
Definition: hid.h:17
KEY_CSTICK_DOWN
@ KEY_CSTICK_DOWN
C-Stick Down (New 3DS only)
Definition: hid.h:30
KEY_CPAD_LEFT
@ KEY_CPAD_LEFT
Circle Pad Left.
Definition: hid.h:32
hidCircleRead
void hidCircleRead(circlePosition *pos)
Reads the current circle pad position.
HIDUSER_DisableGyroscope
Result HIDUSER_DisableGyroscope(void)
Disables the gyroscope.
HIDUSER_GetSoundVolume
Result HIDUSER_GetSoundVolume(u8 *volume)
Gets the current volume slider value.
hidKeysHeld
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
HIDUSER_GetHandles
Result HIDUSER_GetHandles(Handle *outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad)
Gets the handles for HID operation.
hidWaitForAnyEvent
Result hidWaitForAnyEvent(bool nextEvents, Handle cancelEvent, s64 timeout)
Waits for any HID or IRRST event.
hidKeysDownRepeat
u32 hidKeysDownRepeat(void)
Returns a bitmask of newly pressed or repeated buttons, this frame.
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
HID_Event
HID_Event
HID events.
Definition: hid.h:75
HIDEVENT_Gyro
@ HIDEVENT_Gyro
Event signaled by HID-module, when the sharedmem gyroscope state was updated.
Definition: hid.h:79
HIDEVENT_DebugPad
@ HIDEVENT_DebugPad
Event signaled by HID-module, when the sharedmem DebugPad state was updated.
Definition: hid.h:80
HIDEVENT_Accel
@ HIDEVENT_Accel
Event signaled by HID-module, when the sharedmem accelerometer state was updated.
Definition: hid.h:78
HIDEVENT_MAX
@ HIDEVENT_MAX
Used to know how many events there are.
Definition: hid.h:82
HIDEVENT_PAD1
@ HIDEVENT_PAD1
Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was upd...
Definition: hid.h:77
HIDEVENT_PAD0
@ HIDEVENT_PAD0
Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was upd...
Definition: hid.h:76
hidScanInput
void hidScanInput(void)
Scans HID for input data.
hidTouchRead
void hidTouchRead(touchPosition *pos)
Reads the current touch position.
hidSharedMem
vu32 * hidSharedMem
HID shared memory.
hidKeysUp
u32 hidKeysUp(void)
Returns a bitmask of newly released buttons, this frame.
hidExit
void hidExit(void)
Exits HID.
hidAccelRead
void hidAccelRead(accelVector *vector)
Reads the current accelerometer data.
HIDUSER_EnableGyroscope
Result HIDUSER_EnableGyroscope(void)
Enables the gyroscope.
hidWaitForEvent
void hidWaitForEvent(HID_Event id, bool nextEvent)
Waits for an HID event.
hidSetRepeatParameters
void hidSetRepeatParameters(u32 delay, u32 interval)
Sets the key repeat parameters for hidKeysRepeat.
accelVector
Accelerometer vector.
Definition: hid.h:59
accelVector::x
s16 x
Accelerometer X.
Definition: hid.h:60
accelVector::y
s16 y
Accelerometer Y.
Definition: hid.h:61
accelVector::z
s16 z
Accelerometer Z.
Definition: hid.h:62
angularRate
Gyroscope angular rate.
Definition: hid.h:67
angularRate::y
s16 y
Pitch.
Definition: hid.h:70
angularRate::z
s16 z
Yaw.
Definition: hid.h:69
angularRate::x
s16 x
Roll.
Definition: hid.h:68
circlePosition
Circle Pad position.
Definition: hid.h:52
circlePosition::dx
s16 dx
Pad X.
Definition: hid.h:53
circlePosition::dy
s16 dy
Pad Y.
Definition: hid.h:54
touchPosition
Touch position.
Definition: hid.h:45
touchPosition::px
u16 px
Touch X.
Definition: hid.h:46
touchPosition::py
u16 py
Touch Y.
Definition: hid.h:47
s64
int64_t s64
64-bit signed integer
Definition: types.h:29
BIT
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
u8
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
s16
int16_t s16
16-bit signed integer
Definition: types.h:27
Handle
u32 Handle
Resource handle.
Definition: types.h:41
vu32
volatile u32 vu32
32-bit volatile unsigned integer.
Definition: types.h:33
Result
s32 Result
Function result.
Definition: types.h:42
u16
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23