]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
1 #include "hidjoystickrptparser.h"
3 JoystickReportParser::JoystickReportParser(JoystickEvents
*evt
) :
7 for (uint8_t i
= 0; i
< RPT_GEMEPAD_LEN
; i
++)
11 void JoystickReportParser::Parse(HID
*hid
, bool is_rpt_id
, uint8_t len
, uint8_t *buf
) {
14 // Checking if there are changes in report since the method was last called
15 for (uint8_t i
= 0; i
< RPT_GEMEPAD_LEN
; i
++)
16 if (buf
[i
] != oldPad
[i
]) {
21 // Calling Game Pad event handler
22 if (!match
&& joyEvents
) {
23 joyEvents
->OnGamePadChanged((const GamePadEventData
*)buf
);
25 for (uint8_t i
= 0; i
< RPT_GEMEPAD_LEN
; i
++) oldPad
[i
] = buf
[i
];
28 uint8_t hat
= (buf
[5] & 0xF);
30 // Calling Hat Switch event handler
31 if (hat
!= oldHat
&& joyEvents
) {
32 joyEvents
->OnHatSwitch(hat
);
36 uint16_t buttons
= (0x0000 | buf
[6]);
38 buttons
|= (buf
[5] >> 4);
39 uint16_t changes
= (buttons
^ oldButtons
);
41 // Calling Button Event Handler for every button changed
43 for (uint8_t i
= 0; i
< 0x0C; i
++) {
44 uint16_t mask
= (0x0001 << i
);
46 if (((mask
& changes
) > 0) && joyEvents
)
47 if ((buttons
& mask
) > 0)
48 joyEvents
->OnButtonDn(i
+ 1);
50 joyEvents
->OnButtonUp(i
+ 1);
56 void JoystickEvents::OnGamePadChanged(const GamePadEventData
*evt
) {
58 PrintHex
<uint8_t > (evt
->X
, 0x80);
59 Serial
.print("\tY1: ");
60 PrintHex
<uint8_t > (evt
->Y
, 0x80);
61 Serial
.print("\tX2: ");
62 PrintHex
<uint8_t > (evt
->Z1
, 0x80);
63 Serial
.print("\tY2: ");
64 PrintHex
<uint8_t > (evt
->Z2
, 0x80);
65 Serial
.print("\tRz: ");
66 PrintHex
<uint8_t > (evt
->Rz
, 0x80);
70 void JoystickEvents::OnHatSwitch(uint8_t hat
) {
71 Serial
.print("Hat Switch: ");
72 PrintHex
<uint8_t > (hat
, 0x80);
76 void JoystickEvents::OnButtonUp(uint8_t but_id
) {
78 Serial
.println(but_id
, DEC
);
81 void JoystickEvents::OnButtonDn(uint8_t but_id
) {
83 Serial
.println(but_id
, DEC
);