USB_Host_Shield_2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hidjoystickrptparser.cpp
Go to the documentation of this file.
1 #include "hidjoystickrptparser.h"
2 
4  joyEvents(evt),
5  oldHat(0xDE),
6  oldButtons(0)
7 {
8  for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
9  oldPad[i] = 0xD;
10 }
11 
12 void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
13 {
14  bool match = true;
15 
16  // Checking if there are changes in report since the method was last called
17  for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
18  if (buf[i] != oldPad[i])
19  {
20  match = false;
21  break;
22  }
23 
24  // Calling Game Pad event handler
25  if (!match && joyEvents)
26  {
27  joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
28 
29  for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
30  }
31 
32  uint8_t hat = (buf[5] & 0xF);
33 
34  // Calling Hat Switch event handler
35  if (hat != oldHat && joyEvents)
36  {
37  joyEvents->OnHatSwitch(hat);
38  oldHat = hat;
39  }
40 
41  uint16_t buttons = (0x0000 | buf[6]);
42  buttons <<= 4;
43  buttons |= (buf[5] >> 4);
44  uint16_t changes = (buttons ^ oldButtons);
45 
46  // Calling Button Event Handler for every button changed
47  if (changes)
48  {
49  for (uint8_t i=0; i<0x0C; i++)
50  {
51  uint16_t mask = (0x0001 << i);
52 
53  if (((mask & changes) > 0) && joyEvents)
54  if ((buttons & mask) > 0)
55  joyEvents->OnButtonDn(i+1);
56  else
57  joyEvents->OnButtonUp(i+1);
58  }
59  oldButtons = buttons;
60  }
61 }
62 
64 {
65  Serial.print("X: ");
66  PrintHex<uint8_t>(evt->X);
67  Serial.print("\tY: ");
68  PrintHex<uint8_t>(evt->Y);
69  Serial.print("\tZ: ");
70  PrintHex<uint8_t>(evt->Z1);
71  Serial.print("\tZ: ");
72  PrintHex<uint8_t>(evt->Z2);
73  Serial.print("\tRz: ");
74  PrintHex<uint8_t>(evt->Rz);
75  Serial.println("");
76 }
77 
78 void JoystickEvents::OnHatSwitch(uint8_t hat)
79 {
80  Serial.print("Hat Switch: ");
81  PrintHex<uint8_t>(hat);
82  Serial.println("");
83 }
84 
85 void JoystickEvents::OnButtonUp(uint8_t but_id)
86 {
87  Serial.print("Up: ");
88  Serial.println(but_id, DEC);
89 }
90 
91 void JoystickEvents::OnButtonDn(uint8_t but_id)
92 {
93  Serial.print("Dn: ");
94  Serial.println(but_id, DEC);
95 }