libctru  v2.3.1
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.
90 
91 /// Exits HID.
92 void hidExit(void);
93 
94 /**
95  * @brief Sets the key repeat parameters for @ref hidKeysRepeat.
96  * @param delay Initial delay.
97  * @param interval Repeat interval.
98  */
99 void hidSetRepeatParameters(u32 delay, u32 interval);
100 
101 /// Scans HID for input data.
102 void hidScanInput(void);
103 
104 /**
105  * @brief Returns a bitmask of held buttons.
106  * Individual buttons can be extracted using binary AND.
107  * @return 32-bit bitmask of held buttons (1+ frames).
108  */
110 
111 /**
112  * @brief Returns a bitmask of newly pressed buttons, this frame.
113  * Individual buttons can be extracted using binary AND.
114  * @return 32-bit bitmask of newly pressed buttons.
115  */
117 
118 /**
119  * @brief Returns a bitmask of newly pressed or repeated buttons, this frame.
120  * Individual buttons can be extracted using binary AND.
121  * @return 32-bit bitmask of newly pressed or repeated buttons.
122  */
124 
125 /**
126  * @brief Returns a bitmask of newly released buttons, this frame.
127  * Individual buttons can be extracted using binary AND.
128  * @return 32-bit bitmask of newly released buttons.
129  */
131 
132 /**
133  * @brief Reads the current touch position.
134  * @param pos Pointer to output the touch position to.
135  */
137 
138 /**
139  * @brief Reads the current circle pad position.
140  * @param pos Pointer to output the circle pad position to.
141  */
143 
144 /**
145  * @brief Reads the current accelerometer data.
146  * @param vector Pointer to output the accelerometer data to.
147  */
148 void hidAccelRead(accelVector* vector);
149 
150 /**
151  * @brief Reads the current gyroscope data.
152  * @param rate Pointer to output the gyroscope data to.
153  */
155 
156 /**
157  * @brief Waits for an HID event.
158  * @param id ID of the event.
159  * @param nextEvent Whether to discard the current event and wait for the next event.
160  */
161 void hidWaitForEvent(HID_Event id, bool nextEvent);
162 
163 /**
164  * @brief Waits for any HID or IRRST event.
165  * @param nextEvents Whether to discard the current events and wait for the next events.
166  * @param cancelEvent Optional additional handle to wait on, otherwise 0.
167  * @param timeout Timeout.
168  */
169 Result hidWaitForAnyEvent(bool nextEvents, Handle cancelEvent, s64 timeout);
170 
171 /// Compatibility macro for hidScanInput.
172 #define scanKeys hidScanInput
173 /// Compatibility macro for hidKeysHeld.
174 #define keysHeld hidKeysHeld
175 /// Compatibility macro for hidKeysDown.
176 #define keysDown hidKeysDown
177 /// Compatibility macro for hidKeysUp.
178 #define keysUp hidKeysUp
179 /// Compatibility macro for hidTouchRead.
180 #define touchRead hidTouchRead
181 /// Compatibility macro for hidCircleRead.
182 #define circleRead hidCircleRead
183 
184 /**
185  * @brief Gets the handles for HID operation.
186  * @param outMemHandle Pointer to output the shared memory handle to.
187  * @param eventpad0 Pointer to output the pad 0 event handle to.
188  * @param eventpad1 Pointer to output the pad 1 event handle to.
189  * @param eventaccel Pointer to output the accelerometer event handle to.
190  * @param eventgyro Pointer to output the gyroscope event handle to.
191  * @param eventdebugpad Pointer to output the debug pad event handle to.
192  */
193 Result HIDUSER_GetHandles(Handle* outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad);
194 
195 /// Enables the accelerometer.
197 
198 /// Disables the accelerometer.
200 
201 /// Enables the gyroscope.
203 
204 /// Disables the gyroscope.
206 
207 /**
208  * @brief Gets the gyroscope raw to dps coefficient.
209  * @param coeff Pointer to output the coefficient to.
210  */
212 
213 /**
214  * @brief Gets the current volume slider value. (0-63)
215  * @param volume Pointer to write the volume slider value to.
216  */
Result HIDUSER_GetGyroscopeRawToDpsCoefficient(float *coeff)
Gets the gyroscope raw to dps coefficient.
Result HIDUSER_DisableAccelerometer(void)
Disables the accelerometer.
@ KEY_TOUCH
Touch (Not actually provided by HID)
Definition: hid.h:26
@ KEY_UP
D-Pad Up or Circle Pad Up.
Definition: hid.h:37
@ KEY_DDOWN
D-Pad Down.
Definition: hid.h:19
@ KEY_B
B.
Definition: hid.h:13
@ KEY_CSTICK_UP
C-Stick Up (New 3DS only)
Definition: hid.h:29
@ KEY_CPAD_RIGHT
Circle Pad Right.
Definition: hid.h:31
@ KEY_SELECT
Select.
Definition: hid.h:14
@ KEY_DRIGHT
D-Pad Right.
Definition: hid.h:16
@ KEY_X
X.
Definition: hid.h:22
@ KEY_ZR
ZR (New 3DS only)
Definition: hid.h:25
@ KEY_CSTICK_RIGHT
C-Stick Right (New 3DS only)
Definition: hid.h:27
@ KEY_Y
Y.
Definition: hid.h:23
@ KEY_LEFT
D-Pad Left or Circle Pad Left.
Definition: hid.h:39
@ KEY_START
Start.
Definition: hid.h:15
@ KEY_RIGHT
D-Pad Right or Circle Pad Right.
Definition: hid.h:40
@ KEY_DUP
D-Pad Up.
Definition: hid.h:18
@ KEY_ZL
ZL (New 3DS only)
Definition: hid.h:24
@ KEY_R
R.
Definition: hid.h:20
@ KEY_A
A.
Definition: hid.h:12
@ KEY_DOWN
D-Pad Down or Circle Pad Down.
Definition: hid.h:38
@ KEY_CPAD_UP
Circle Pad Up.
Definition: hid.h:33
@ KEY_CSTICK_LEFT
C-Stick Left (New 3DS only)
Definition: hid.h:28
@ KEY_L
L.
Definition: hid.h:21
@ KEY_CPAD_DOWN
Circle Pad Down.
Definition: hid.h:34
@ KEY_DLEFT
D-Pad Left.
Definition: hid.h:17
@ KEY_CSTICK_DOWN
C-Stick Down (New 3DS only)
Definition: hid.h:30
@ KEY_CPAD_LEFT
Circle Pad Left.
Definition: hid.h:32
Handle hidMemHandle
HID shared memory handle.
Result HIDUSER_EnableAccelerometer(void)
Enables the accelerometer.
Result hidInit(void)
Initializes HID.
void hidGyroRead(angularRate *rate)
Reads the current gyroscope data.
void hidCircleRead(circlePosition *pos)
Reads the current circle pad position.
Result HIDUSER_DisableGyroscope(void)
Disables the gyroscope.
Result HIDUSER_GetSoundVolume(u8 *volume)
Gets the current volume slider value.
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
Result HIDUSER_GetHandles(Handle *outMemHandle, Handle *eventpad0, Handle *eventpad1, Handle *eventaccel, Handle *eventgyro, Handle *eventdebugpad)
Gets the handles for HID operation.
Result hidWaitForAnyEvent(bool nextEvents, Handle cancelEvent, s64 timeout)
Waits for any HID or IRRST event.
u32 hidKeysDownRepeat(void)
Returns a bitmask of newly pressed or repeated buttons, this frame.
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
HID_Event
HID events.
Definition: hid.h:75
@ HIDEVENT_Gyro
Event signaled by HID-module, when the sharedmem gyroscope state was updated.
Definition: hid.h:79
@ HIDEVENT_DebugPad
Event signaled by HID-module, when the sharedmem DebugPad state was updated.
Definition: hid.h:80
@ HIDEVENT_Accel
Event signaled by HID-module, when the sharedmem accelerometer state was updated.
Definition: hid.h:78
@ HIDEVENT_MAX
Used to know how many events there are.
Definition: hid.h:82
@ 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
Event signaled by HID-module, when the sharedmem+0(PAD/circle-pad)/+0xA8(touch-screen) region was upd...
Definition: hid.h:76
void hidScanInput(void)
Scans HID for input data.
void hidTouchRead(touchPosition *pos)
Reads the current touch position.
vu32 * hidSharedMem
HID shared memory.
u32 hidKeysUp(void)
Returns a bitmask of newly released buttons, this frame.
void hidExit(void)
Exits HID.
void hidAccelRead(accelVector *vector)
Reads the current accelerometer data.
Result HIDUSER_EnableGyroscope(void)
Enables the gyroscope.
void hidWaitForEvent(HID_Event id, bool nextEvent)
Waits for an HID event.
void hidSetRepeatParameters(u32 delay, u32 interval)
Sets the key repeat parameters for hidKeysRepeat.
Accelerometer vector.
Definition: hid.h:59
s16 x
Accelerometer X.
Definition: hid.h:60
s16 y
Accelerometer Y.
Definition: hid.h:61
s16 z
Accelerometer Z.
Definition: hid.h:62
Gyroscope angular rate.
Definition: hid.h:67
s16 y
Pitch.
Definition: hid.h:70
s16 z
Yaw.
Definition: hid.h:69
s16 x
Roll.
Definition: hid.h:68
Circle Pad position.
Definition: hid.h:52
s16 dx
Pad X.
Definition: hid.h:53
s16 dy
Pad Y.
Definition: hid.h:54
Touch position.
Definition: hid.h:45
u16 px
Touch X.
Definition: hid.h:46
u16 py
Touch Y.
Definition: hid.h:47
int64_t s64
64-bit signed integer
Definition: types.h:29
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
int16_t s16
16-bit signed integer
Definition: types.h:27
u32 Handle
Resource handle.
Definition: types.h:41
volatile u32 vu32
32-bit volatile unsigned integer.
Definition: types.h:33
s32 Result
Function result.
Definition: types.h:42
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23