libctru  v1.6.0
hid.h
Go to the documentation of this file.
1 /**
2  * @file hid.h
3  * @brief HID service.
4  */
5 #pragma once
6 
7 //See also: http://3dbrew.org/wiki/HID_Services http://3dbrew.org/wiki/HID_Shared_Memory
8 
9 /// Key values.
10 enum
11 {
12  KEY_A = BIT(0), ///< A
13  KEY_B = BIT(1), ///< B
14  KEY_SELECT = BIT(2), ///< Select
15  KEY_START = BIT(3), ///< Start
16  KEY_DRIGHT = BIT(4), ///< D-Pad Right
17  KEY_DLEFT = BIT(5), ///< D-Pad Left
18  KEY_DUP = BIT(6), ///< D-Pad Up
19  KEY_DDOWN = BIT(7), ///< D-Pad Down
20  KEY_R = BIT(8), ///< R
21  KEY_L = BIT(9), ///< L
22  KEY_X = BIT(10), ///< X
23  KEY_Y = BIT(11), ///< Y
24  KEY_ZL = BIT(14), ///< ZL (New 3DS only)
25  KEY_ZR = BIT(15), ///< ZR (New 3DS only)
26  KEY_TOUCH = BIT(20), ///< Touch (Not actually provided by HID)
27  KEY_CSTICK_RIGHT = BIT(24), ///< C-Stick Right (New 3DS only)
28  KEY_CSTICK_LEFT = BIT(25), ///< C-Stick Left (New 3DS only)
29  KEY_CSTICK_UP = BIT(26), ///< C-Stick Up (New 3DS only)
30  KEY_CSTICK_DOWN = BIT(27), ///< C-Stick Down (New 3DS only)
31  KEY_CPAD_RIGHT = BIT(28), ///< Circle Pad Right
32  KEY_CPAD_LEFT = BIT(29), ///< Circle Pad Left
33  KEY_CPAD_UP = BIT(30), ///< Circle Pad Up
34  KEY_CPAD_DOWN = BIT(31), ///< Circle Pad Down
35 
36  // Generic catch-all directions
37  KEY_UP = KEY_DUP | KEY_CPAD_UP, ///< D-Pad Up or Circle Pad Up
38  KEY_DOWN = KEY_DDOWN | KEY_CPAD_DOWN, ///< D-Pad Down or Circle Pad Down
39  KEY_LEFT = KEY_DLEFT | KEY_CPAD_LEFT, ///< D-Pad Left or Circle Pad Left
40  KEY_RIGHT = KEY_DRIGHT | KEY_CPAD_RIGHT, ///< D-Pad Right or Circle Pad Right
41 };
42 
43 /// Touch position.
44 typedef struct
45 {
46  u16 px; ///< Touch X
47  u16 py; ///< Touch Y
49 
50 /// Circle Pad position.
51 typedef struct
52 {
53  s16 dx; ///< Pad X
54  s16 dy; ///< Pad Y
56 
57 /// Accelerometer vector.
58 typedef struct
59 {
60  s16 x; ///< Accelerometer X
61  s16 y; ///< Accelerometer Y
62  s16 z; ///< Accelerometer Z
63 } accelVector;
64 
65 /// Gyroscope angular rate.
66 typedef struct
67 {
68  s16 x; ///< Roll
69  s16 z; ///< Yaw
70  s16 y; ///< Pitch
71 } angularRate;
72 
73 /// HID events.
74 typedef enum
75 {
76  HIDEVENT_PAD0 = 0, ///< Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was updated.
77  HIDEVENT_PAD1, ///< Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was updated.
78  HIDEVENT_Accel, ///< Event signaled by HID-module, when the sharedmem accelerometer state was updated.
79  HIDEVENT_Gyro, ///< Event signaled by HID-module, when the sharedmem gyroscope state was updated.
80  HIDEVENT_DebugPad, ///< Event signaled by HID-module, when the sharedmem DebugPad state was updated.
81 
82  HIDEVENT_MAX, ///< Used to know how many events there are.
83 } HID_Event;
84 
85 extern Handle hidMemHandle; ///< HID shared memory handle.
86 extern vu32* hidSharedMem; ///< HID shared memory.
87 
88 /// Initializes HID.
89 Result hidInit(void);
90 
91 /// Exits HID.
92 void hidExit(void);
93 
94 /// Scans HID for input data.
95 void hidScanInput(void);
96 
97 /**
98  * @brief Returns a bitmask of held buttons.
99  * Individual buttons can be extracted using binary AND.
100  * @return 32-bit bitmask of held buttons (1+ frames).
101  */
102 u32 hidKeysHeld(void);
103 
104 /**
105  * @brief Returns a bitmask of newly pressed buttons, this frame.
106  * Individual buttons can be extracted using binary AND.
107  * @return 32-bit bitmask of newly pressed buttons.
108  */
109 u32 hidKeysDown(void);
110 
111 /**
112 * @brief Returns a bitmask of newly released buttons, this frame.
113  * Individual buttons can be extracted using binary AND.
114  * @return 32-bit bitmask of newly released buttons.
115  */
116 u32 hidKeysUp(void);
117 
118 /**
119  * @brief Reads the current touch position.
120  * @param pos Pointer to output the touch position to.
121  */
122 void hidTouchRead(touchPosition* pos);
123 
124 /**
125  * @brief Reads the current circle pad position.
126  * @param pos Pointer to output the circle pad position to.
127  */
128 void hidCircleRead(circlePosition* pos);
129 
130 /**
131  * @brief Reads the current accelerometer data.
132  * @param vector Pointer to output the accelerometer data to.
133  */
134 void hidAccelRead(accelVector* vector);
135 
136 /**
137  * @brief Reads the current gyroscope data.
138  * @param rate Pointer to output the gyroscope data to.
139  */
140 void hidGyroRead(angularRate* rate);
141 
142 /**
143  * @brief Waits for an HID event.
144  * @param id ID of the event.
145  * @param nextEvent Whether to discard the current event and wait for the next event.
146  */
147 void hidWaitForEvent(HID_Event id, bool nextEvent);
148 
149 /// Compatibility macro for hidScanInput.
150 #define scanKeys hidScanInput
151 /// Compatibility macro for hidKeysHeld.
152 #define keysHeld hidKeysHeld
153 /// Compatibility macro for hidKeysDown.
154 #define keysDown hidKeysDown
155 /// Compatibility macro for hidKeysUp.
156 #define keysUp hidKeysUp
157 /// Compatibility macro for hidTouchRead.
158 #define touchRead hidTouchRead
159 /// Compatibility macro for hidCircleRead.
160 #define circleRead hidCircleRead
161 
162 /**
163  * @brief Gets the handles for HID operation.
164  * @param outMemHandle Pointer to output the shared memory handle to.
165  * @param eventpad0 Pointer to output the pad 0 event handle to.
166  * @param eventpad1 Pointer to output the pad 1 event handle to.
167  * @param eventaccel Pointer to output the accelerometer event handle to.
168  * @param eventgyro Pointer to output the gyroscope event handle to.
169  * @param eventdebugpad Pointer to output the debug pad event handle to.
170  */
171 Result HIDUSER_GetHandles(Handle* outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad);
172 
173 /// Enables the accelerometer.
175 
176 /// Disables the accelerometer.
178 
179 /// Enables the gyroscope.
181 
182 /// Disables the gyroscope.
184 
185 /**
186  * @brief Gets the gyroscope raw to dps coefficient.
187  * @param coeff Pointer to output the coefficient to.
188  */
190 
191 /**
192  * @brief Gets the current volume slider value. (0-63)
193  * @param volume Pointer to write the volume slider value to.
194  */
Accelerometer vector.
Definition: hid.h:58
void hidScanInput(void)
Scans HID for input data.
s16 dx
Pad X.
Definition: hid.h:53
volatile u32 vu32
32-bit volatile unsigned integer.
Definition: types.h:33
C-Stick Left (New 3DS only)
Definition: hid.h:28
s32 Result
Function result.
Definition: types.h:42
Result hidInit(void)
Initializes HID.
D-Pad Left or Circle Pad Left.
Definition: hid.h:39
s16 y
Pitch.
Definition: hid.h:70
L.
Definition: hid.h:21
X.
Definition: hid.h:22
Result HIDUSER_DisableGyroscope(void)
Disables the gyroscope.
Event signaled by HID-module, when the sharedmem DebugPad state was updated.
Definition: hid.h:80
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
Select.
Definition: hid.h:14
Circle Pad Left.
Definition: hid.h:32
Result HIDUSER_EnableAccelerometer(void)
Enables the accelerometer.
Gyroscope angular rate.
Definition: hid.h:66
D-Pad Down.
Definition: hid.h:19
s16 z
Accelerometer Z.
Definition: hid.h:62
void hidWaitForEvent(HID_Event id, bool nextEvent)
Waits for an HID event.
u32 Handle
Resource handle.
Definition: types.h:41
Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was upd...
Definition: hid.h:76
D-Pad Up.
Definition: hid.h:18
u16 py
Touch Y.
Definition: hid.h:47
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
D-Pad Right.
Definition: hid.h:16
Start.
Definition: hid.h:15
Circle Pad position.
Definition: hid.h:51
Event signaled by HID-module, when the sharedmem accelerometer state was updated.
Definition: hid.h:78
u16 px
Touch X.
Definition: hid.h:46
void hidTouchRead(touchPosition *pos)
Reads the current touch position.
Circle Pad Down.
Definition: hid.h:34
void hidAccelRead(accelVector *vector)
Reads the current accelerometer data.
Event signaled by HID-module, when the sharedmem gyroscope state was updated.
Definition: hid.h:79
ZR (New 3DS only)
Definition: hid.h:25
C-Stick Right (New 3DS only)
Definition: hid.h:27
uint32_t u32
32-bit unsigned integer
Definition: types.h:23
void hidCircleRead(circlePosition *pos)
Reads the current circle pad position.
s16 x
Roll.
Definition: hid.h:68
s16 x
Accelerometer X.
Definition: hid.h:60
Result HIDUSER_GetHandles(Handle *outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad)
Gets the handles for HID operation.
D-Pad Down or Circle Pad Down.
Definition: hid.h:38
HID_Event
HID events.
Definition: hid.h:74
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
Touch position.
Definition: hid.h:44
s16 y
Accelerometer Y.
Definition: hid.h:61
C-Stick Up (New 3DS only)
Definition: hid.h:29
Used to know how many events there are.
Definition: hid.h:82
B.
Definition: hid.h:13
R.
Definition: hid.h:20
ZL (New 3DS only)
Definition: hid.h:24
A.
Definition: hid.h:12
D-Pad Right or Circle Pad Right.
Definition: hid.h:40
void hidGyroRead(angularRate *rate)
Reads the current gyroscope data.
u32 hidKeysUp(void)
Returns a bitmask of newly released buttons, this frame.
s16 z
Yaw.
Definition: hid.h:69
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
Handle hidMemHandle
HID shared memory handle.
D-Pad Up or Circle Pad Up.
Definition: hid.h:37
Result HIDUSER_EnableGyroscope(void)
Enables the gyroscope.
Circle Pad Right.
Definition: hid.h:31
vu32 * hidSharedMem
HID shared memory.
Y.
Definition: hid.h:23
void hidExit(void)
Exits HID.
Result HIDUSER_DisableAccelerometer(void)
Disables the accelerometer.
C-Stick Down (New 3DS only)
Definition: hid.h:30
Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was upd...
Definition: hid.h:77
Touch (Not actually provided by HID)
Definition: hid.h:26
Result HIDUSER_GetGyroscopeRawToDpsCoefficient(float *coeff)
Gets the gyroscope raw to dps coefficient.
Result HIDUSER_GetSoundVolume(u8 *volume)
Gets the current volume slider value.
s16 dy
Pad Y.
Definition: hid.h:54
D-Pad Left.
Definition: hid.h:17
Circle Pad Up.
Definition: hid.h:33
int16_t s16
16-bit signed integer
Definition: types.h:27