aboutsummaryrefslogtreecommitdiff
path: root/lib/usbhost/USB_Host_Shield_2.0/examples/HID
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-08-18 18:20:25 +1000
committerGitHub <noreply@github.com>2021-08-18 18:20:25 +1000
commitb16091659cc9a724a8800f77e631643b4ab089ad (patch)
treee44933472c6d100bd4fc5d8a693d9d21e3c32f6f /lib/usbhost/USB_Host_Shield_2.0/examples/HID
parentcf5e40c25139ff64ff246f1c6280e983ef75551c (diff)
downloadqmk_firmware-b16091659cc9a724a8800f77e631643b4ab089ad.tar.gz
qmk_firmware-b16091659cc9a724a8800f77e631643b4ab089ad.zip
Move USB Host Shield and Arduino core to `lib/` (#13973)
Diffstat (limited to 'lib/usbhost/USB_Host_Shield_2.0/examples/HID')
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino129
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino178
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino83
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino38
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp84
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h33
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino77
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h52
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino42
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp43
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h42
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale.ino51
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp150
-rw-r--r--lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h55
14 files changed, 1057 insertions, 0 deletions
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino
new file mode 100644
index 000000000..48b33abfd
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino
@@ -0,0 +1,129 @@
1#include <hidboot.h>
2#include <usbhub.h>
3
4// Satisfy the IDE, which needs to see the include statment in the ino too.
5#ifdef dobogusinclude
6#include <spi4teensy3.h>
7#include <SPI.h>
8#endif
9
10class KbdRptParser : public KeyboardReportParser
11{
12 void PrintKey(uint8_t mod, uint8_t key);
13
14 protected:
15 void OnControlKeysChanged(uint8_t before, uint8_t after);
16
17 void OnKeyDown (uint8_t mod, uint8_t key);
18 void OnKeyUp (uint8_t mod, uint8_t key);
19 void OnKeyPressed(uint8_t key);
20};
21
22void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
23{
24 MODIFIERKEYS mod;
25 *((uint8_t*)&mod) = m;
26 Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
27 Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
28 Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
29 Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
30
31 Serial.print(" >");
32 PrintHex<uint8_t>(key, 0x80);
33 Serial.print("< ");
34
35 Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
36 Serial.print((mod.bmRightShift == 1) ? "S" : " ");
37 Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
38 Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
39};
40
41void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
42{
43 Serial.print("DN ");
44 PrintKey(mod, key);
45 uint8_t c = OemToAscii(mod, key);
46
47 if (c)
48 OnKeyPressed(c);
49}
50
51void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
52
53 MODIFIERKEYS beforeMod;
54 *((uint8_t*)&beforeMod) = before;
55
56 MODIFIERKEYS afterMod;
57 *((uint8_t*)&afterMod) = after;
58
59 if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
60 Serial.println("LeftCtrl changed");
61 }
62 if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
63 Serial.println("LeftShift changed");
64 }
65 if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
66 Serial.println("LeftAlt changed");
67 }
68 if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
69 Serial.println("LeftGUI changed");
70 }
71
72 if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
73 Serial.println("RightCtrl changed");
74 }
75 if (beforeMod.bmRightShift != afterMod.bmRightShift) {
76 Serial.println("RightShift changed");
77 }
78 if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
79 Serial.println("RightAlt changed");
80 }
81 if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
82 Serial.println("RightGUI changed");
83 }
84
85}
86
87void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
88{
89 Serial.print("UP ");
90 PrintKey(mod, key);
91}
92
93void KbdRptParser::OnKeyPressed(uint8_t key)
94{
95 Serial.print("ASCII: ");
96 Serial.println((char)key);
97};
98
99USB Usb;
100//USBHub Hub(&Usb);
101HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
102
103uint32_t next_time;
104
105KbdRptParser Prs;
106
107void setup()
108{
109 Serial.begin( 115200 );
110#if !defined(__MIPSEL__)
111 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
112#endif
113 Serial.println("Start");
114
115 if (Usb.Init() == -1)
116 Serial.println("OSC did not start.");
117
118 delay( 200 );
119
120 next_time = millis() + 5000;
121
122 HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
123}
124
125void loop()
126{
127 Usb.Task();
128}
129
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino
new file mode 100644
index 000000000..5fc8c96fc
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino
@@ -0,0 +1,178 @@
1#include <hidboot.h>
2#include <usbhub.h>
3
4// Satisfy IDE, which only needs to see the include statment in the ino.
5#ifdef dobogusinclude
6#include <spi4teensy3.h>
7#include <SPI.h>
8#endif
9
10class MouseRptParser : public MouseReportParser
11{
12 protected:
13 void OnMouseMove(MOUSEINFO *mi);
14 void OnLeftButtonUp(MOUSEINFO *mi);
15 void OnLeftButtonDown(MOUSEINFO *mi);
16 void OnRightButtonUp(MOUSEINFO *mi);
17 void OnRightButtonDown(MOUSEINFO *mi);
18 void OnMiddleButtonUp(MOUSEINFO *mi);
19 void OnMiddleButtonDown(MOUSEINFO *mi);
20};
21void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
22{
23 Serial.print("dx=");
24 Serial.print(mi->dX, DEC);
25 Serial.print(" dy=");
26 Serial.println(mi->dY, DEC);
27};
28void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
29{
30 Serial.println("L Butt Up");
31};
32void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
33{
34 Serial.println("L Butt Dn");
35};
36void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
37{
38 Serial.println("R Butt Up");
39};
40void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
41{
42 Serial.println("R Butt Dn");
43};
44void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
45{
46 Serial.println("M Butt Up");
47};
48void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
49{
50 Serial.println("M Butt Dn");
51};
52
53class KbdRptParser : public KeyboardReportParser
54{
55 void PrintKey(uint8_t mod, uint8_t key);
56
57 protected:
58 void OnControlKeysChanged(uint8_t before, uint8_t after);
59 void OnKeyDown (uint8_t mod, uint8_t key);
60 void OnKeyUp (uint8_t mod, uint8_t key);
61 void OnKeyPressed(uint8_t key);
62};
63
64void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
65{
66 MODIFIERKEYS mod;
67 *((uint8_t*)&mod) = m;
68 Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
69 Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
70 Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
71 Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
72
73 Serial.print(" >");
74 PrintHex<uint8_t>(key, 0x80);
75 Serial.print("< ");
76
77 Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
78 Serial.print((mod.bmRightShift == 1) ? "S" : " ");
79 Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
80 Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
81};
82
83void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
84{
85 Serial.print("DN ");
86 PrintKey(mod, key);
87 uint8_t c = OemToAscii(mod, key);
88
89 if (c)
90 OnKeyPressed(c);
91}
92
93void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
94
95 MODIFIERKEYS beforeMod;
96 *((uint8_t*)&beforeMod) = before;
97
98 MODIFIERKEYS afterMod;
99 *((uint8_t*)&afterMod) = after;
100
101 if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
102 Serial.println("LeftCtrl changed");
103 }
104 if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
105 Serial.println("LeftShift changed");
106 }
107 if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
108 Serial.println("LeftAlt changed");
109 }
110 if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
111 Serial.println("LeftGUI changed");
112 }
113
114 if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
115 Serial.println("RightCtrl changed");
116 }
117 if (beforeMod.bmRightShift != afterMod.bmRightShift) {
118 Serial.println("RightShift changed");
119 }
120 if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
121 Serial.println("RightAlt changed");
122 }
123 if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
124 Serial.println("RightGUI changed");
125 }
126
127}
128
129void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
130{
131 Serial.print("UP ");
132 PrintKey(mod, key);
133}
134
135void KbdRptParser::OnKeyPressed(uint8_t key)
136{
137 Serial.print("ASCII: ");
138 Serial.println((char)key);
139};
140
141USB Usb;
142USBHub Hub(&Usb);
143
144HIDBoot < HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE > HidComposite(&Usb);
145HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
146HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb);
147
148//uint32_t next_time;
149
150KbdRptParser KbdPrs;
151MouseRptParser MousePrs;
152
153void setup()
154{
155 Serial.begin( 115200 );
156#if !defined(__MIPSEL__)
157 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
158#endif
159 Serial.println("Start");
160
161 if (Usb.Init() == -1)
162 Serial.println("OSC did not start.");
163
164 delay( 200 );
165
166 //next_time = millis() + 5000;
167
168 HidComposite.SetReportParser(0, (HIDReportParser*)&KbdPrs);
169 HidComposite.SetReportParser(1, (HIDReportParser*)&MousePrs);
170 HidKeyboard.SetReportParser(0, (HIDReportParser*)&KbdPrs);
171 HidMouse.SetReportParser(0, (HIDReportParser*)&MousePrs);
172}
173
174void loop()
175{
176 Usb.Task();
177}
178
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino
new file mode 100644
index 000000000..53102512b
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDBootMouse/USBHIDBootMouse.ino
@@ -0,0 +1,83 @@
1#include <hidboot.h>
2#include <usbhub.h>
3
4// Satisfy the IDE, which needs to see the include statment in the ino too.
5#ifdef dobogusinclude
6#include <spi4teensy3.h>
7#include <SPI.h>
8#endif
9
10class MouseRptParser : public MouseReportParser
11{
12protected:
13 void OnMouseMove (MOUSEINFO *mi);
14 void OnLeftButtonUp (MOUSEINFO *mi);
15 void OnLeftButtonDown (MOUSEINFO *mi);
16 void OnRightButtonUp (MOUSEINFO *mi);
17 void OnRightButtonDown (MOUSEINFO *mi);
18 void OnMiddleButtonUp (MOUSEINFO *mi);
19 void OnMiddleButtonDown (MOUSEINFO *mi);
20};
21void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
22{
23 Serial.print("dx=");
24 Serial.print(mi->dX, DEC);
25 Serial.print(" dy=");
26 Serial.println(mi->dY, DEC);
27};
28void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
29{
30 Serial.println("L Butt Up");
31};
32void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
33{
34 Serial.println("L Butt Dn");
35};
36void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
37{
38 Serial.println("R Butt Up");
39};
40void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
41{
42 Serial.println("R Butt Dn");
43};
44void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
45{
46 Serial.println("M Butt Up");
47};
48void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
49{
50 Serial.println("M Butt Dn");
51};
52
53USB Usb;
54USBHub Hub(&Usb);
55HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb);
56
57uint32_t next_time;
58
59MouseRptParser Prs;
60
61void setup()
62{
63 Serial.begin( 115200 );
64#if !defined(__MIPSEL__)
65 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
66#endif
67 Serial.println("Start");
68
69 if (Usb.Init() == -1)
70 Serial.println("OSC did not start.");
71
72 delay( 200 );
73
74 next_time = millis() + 5000;
75
76 HidMouse.SetReportParser(0,(HIDReportParser*)&Prs);
77}
78
79void loop()
80{
81 Usb.Task();
82}
83
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino
new file mode 100644
index 000000000..956441d67
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/USBHIDJoystick.ino
@@ -0,0 +1,38 @@
1#include <hid.h>
2#include <hiduniversal.h>
3#include <usbhub.h>
4
5// Satisfy IDE, which only needs to see the include statment in the ino.
6#ifdef dobogusinclude
7#include <spi4teensy3.h>
8#include <SPI.h>
9#endif
10
11#include "hidjoystickrptparser.h"
12
13USB Usb;
14USBHub Hub(&Usb);
15HIDUniversal Hid(&Usb);
16JoystickEvents JoyEvents;
17JoystickReportParser Joy(&JoyEvents);
18
19void setup() {
20 Serial.begin(115200);
21#if !defined(__MIPSEL__)
22 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
23#endif
24 Serial.println("Start");
25
26 if (Usb.Init() == -1)
27 Serial.println("OSC did not start.");
28
29 delay(200);
30
31 if (!Hid.SetReportParser(0, &Joy))
32 ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);
33}
34
35void loop() {
36 Usb.Task();
37}
38
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
new file mode 100644
index 000000000..083b95cac
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.cpp
@@ -0,0 +1,84 @@
1#include "hidjoystickrptparser.h"
2
3JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
4joyEvents(evt),
5oldHat(0xDE),
6oldButtons(0) {
7 for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)
8 oldPad[i] = 0xD;
9}
10
11void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
12 bool match = true;
13
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]) {
17 match = false;
18 break;
19 }
20
21 // Calling Game Pad event handler
22 if (!match && joyEvents) {
23 joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
24
25 for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
26 }
27
28 uint8_t hat = (buf[5] & 0xF);
29
30 // Calling Hat Switch event handler
31 if (hat != oldHat && joyEvents) {
32 joyEvents->OnHatSwitch(hat);
33 oldHat = hat;
34 }
35
36 uint16_t buttons = (0x0000 | buf[6]);
37 buttons <<= 4;
38 buttons |= (buf[5] >> 4);
39 uint16_t changes = (buttons ^ oldButtons);
40
41 // Calling Button Event Handler for every button changed
42 if (changes) {
43 for (uint8_t i = 0; i < 0x0C; i++) {
44 uint16_t mask = (0x0001 << i);
45
46 if (((mask & changes) > 0) && joyEvents)
47 if ((buttons & mask) > 0)
48 joyEvents->OnButtonDn(i + 1);
49 else
50 joyEvents->OnButtonUp(i + 1);
51 }
52 oldButtons = buttons;
53 }
54}
55
56void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
57 Serial.print("X1: ");
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);
67 Serial.println("");
68}
69
70void JoystickEvents::OnHatSwitch(uint8_t hat) {
71 Serial.print("Hat Switch: ");
72 PrintHex<uint8_t > (hat, 0x80);
73 Serial.println("");
74}
75
76void JoystickEvents::OnButtonUp(uint8_t but_id) {
77 Serial.print("Up: ");
78 Serial.println(but_id, DEC);
79}
80
81void JoystickEvents::OnButtonDn(uint8_t but_id) {
82 Serial.print("Dn: ");
83 Serial.println(but_id, DEC);
84}
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h
new file mode 100644
index 000000000..733b8f8da
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHIDJoystick/hidjoystickrptparser.h
@@ -0,0 +1,33 @@
1#if !defined(__HIDJOYSTICKRPTPARSER_H__)
2#define __HIDJOYSTICKRPTPARSER_H__
3
4#include <hid.h>
5
6struct GamePadEventData {
7 uint8_t X, Y, Z1, Z2, Rz;
8};
9
10class JoystickEvents {
11public:
12 virtual void OnGamePadChanged(const GamePadEventData *evt);
13 virtual void OnHatSwitch(uint8_t hat);
14 virtual void OnButtonUp(uint8_t but_id);
15 virtual void OnButtonDn(uint8_t but_id);
16};
17
18#define RPT_GEMEPAD_LEN 5
19
20class JoystickReportParser : public HIDReportParser {
21 JoystickEvents *joyEvents;
22
23 uint8_t oldPad[RPT_GEMEPAD_LEN];
24 uint8_t oldHat;
25 uint16_t oldButtons;
26
27public:
28 JoystickReportParser(JoystickEvents *evt);
29
30 virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
31};
32
33#endif // __HIDJOYSTICKRPTPARSER_H__
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino
new file mode 100644
index 000000000..85cfc19a2
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/USBHID_desc.ino
@@ -0,0 +1,77 @@
1#include <hid.h>
2#include <hiduniversal.h>
3#include <hidescriptorparser.h>
4#include <usbhub.h>
5#include "pgmstrings.h"
6
7// Satisfy the IDE, which needs to see the include statment in the ino too.
8#ifdef dobogusinclude
9#include <spi4teensy3.h>
10#include <SPI.h>
11#endif
12
13class HIDUniversal2 : public HIDUniversal
14{
15public:
16 HIDUniversal2(USB *usb) : HIDUniversal(usb) {};
17
18protected:
19 uint8_t OnInitSuccessful();
20};
21
22uint8_t HIDUniversal2::OnInitSuccessful()
23{
24 uint8_t rcode;
25
26 HexDumper<USBReadParser, uint16_t, uint16_t> Hex;
27 ReportDescParser Rpt;
28
29 if ((rcode = GetReportDescr(0, &Hex)))
30 goto FailGetReportDescr1;
31
32 if ((rcode = GetReportDescr(0, &Rpt)))
33 goto FailGetReportDescr2;
34
35 return 0;
36
37FailGetReportDescr1:
38 USBTRACE("GetReportDescr1:");
39 goto Fail;
40
41FailGetReportDescr2:
42 USBTRACE("GetReportDescr2:");
43 goto Fail;
44
45Fail:
46 Serial.println(rcode, HEX);
47 Release();
48 return rcode;
49}
50
51USB Usb;
52//USBHub Hub(&Usb);
53HIDUniversal2 Hid(&Usb);
54UniversalReportParser Uni;
55
56void setup()
57{
58 Serial.begin( 115200 );
59#if !defined(__MIPSEL__)
60 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
61#endif
62 Serial.println("Start");
63
64 if (Usb.Init() == -1)
65 Serial.println("OSC did not start.");
66
67 delay( 200 );
68
69 if (!Hid.SetReportParser(0, &Uni))
70 ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 );
71}
72
73void loop()
74{
75 Usb.Task();
76}
77
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h
new file mode 100644
index 000000000..bdb0077ec
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/USBHID_desc/pgmstrings.h
@@ -0,0 +1,52 @@
1#if !defined(__PGMSTRINGS_H__)
2#define __PGMSTRINGS_H__
3
4#define LOBYTE(x) ((char*)(&(x)))[0]
5#define HIBYTE(x) ((char*)(&(x)))[1]
6#define BUFSIZE 256 //buffer size
7
8
9/* Print strings in Program Memory */
10const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t";
11const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: ";
12const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t";
13const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t";
14const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t";
15const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t";
16const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t";
17const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t";
18const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t";
19const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t";
20const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t";
21const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t";
22const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t";
23const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t";
24const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t";
25const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t";
26const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes";
27const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:";
28const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t";
29const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t";
30const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t";
31const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t";
32const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t";
33const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t";
34const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:";
35const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t";
36const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t";
37const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t";
38const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t";
39const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t";
40const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t";
41const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t";
42const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:";
43const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t";
44const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t";
45const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t";
46const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t";
47const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:";
48const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t";
49const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t";
50const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t";
51
52#endif // __PGMSTRINGS_H__ \ No newline at end of file
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino
new file mode 100644
index 000000000..837d7f5a7
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp.ino
@@ -0,0 +1,42 @@
1/* Simplified Logitech Extreme 3D Pro Joystick Report Parser */
2
3#include <hid.h>
4#include <hiduniversal.h>
5#include <usbhub.h>
6
7#include "le3dp_rptparser.h"
8
9// Satisfy the IDE, which needs to see the include statment in the ino too.
10#ifdef dobogusinclude
11#include <spi4teensy3.h>
12#include <SPI.h>
13#endif
14
15USB Usb;
16USBHub Hub(&Usb);
17HIDUniversal Hid(&Usb);
18JoystickEvents JoyEvents;
19JoystickReportParser Joy(&JoyEvents);
20
21void setup()
22{
23 Serial.begin( 115200 );
24#if !defined(__MIPSEL__)
25 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
26#endif
27 Serial.println("Start");
28
29 if (Usb.Init() == -1)
30 Serial.println("OSC did not start.");
31
32 delay( 200 );
33
34 if (!Hid.SetReportParser(0, &Joy))
35 ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 );
36}
37
38void loop()
39{
40 Usb.Task();
41}
42
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp
new file mode 100644
index 000000000..baece13b2
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.cpp
@@ -0,0 +1,43 @@
1#include "le3dp_rptparser.h"
2
3JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
4 joyEvents(evt)
5{}
6
7void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
8{
9 bool match = true;
10
11 // Checking if there are changes in report since the method was last called
12 for (uint8_t i=0; i<RPT_GAMEPAD_LEN; i++) {
13 if( buf[i] != oldPad[i] ) {
14 match = false;
15 break;
16 }
17 }
18 // Calling Game Pad event handler
19 if (!match && joyEvents) {
20 joyEvents->OnGamePadChanged((const GamePadEventData*)buf);
21
22 for (uint8_t i=0; i<RPT_GAMEPAD_LEN; i++) oldPad[i] = buf[i];
23 }
24}
25
26void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
27{
28 Serial.print("X: ");
29 PrintHex<uint16_t>(evt->x, 0x80);
30 Serial.print(" Y: ");
31 PrintHex<uint16_t>(evt->y, 0x80);
32 Serial.print(" Hat Switch: ");
33 PrintHex<uint8_t>(evt->hat, 0x80);
34 Serial.print(" Twist: ");
35 PrintHex<uint8_t>(evt->twist, 0x80);
36 Serial.print(" Slider: ");
37 PrintHex<uint8_t>(evt->slider, 0x80);
38 Serial.print(" Buttons A: ");
39 PrintHex<uint8_t>(evt->buttons_a, 0x80);
40 Serial.print(" Buttons B: ");
41 PrintHex<uint8_t>(evt->buttons_b, 0x80);
42 Serial.println("");
43}
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h
new file mode 100644
index 000000000..2400364e6
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/le3dp/le3dp_rptparser.h
@@ -0,0 +1,42 @@
1#if !defined(__HIDJOYSTICKRPTPARSER_H__)
2#define __HIDJOYSTICKRPTPARSER_H__
3
4#include <hid.h>
5
6struct GamePadEventData
7{
8 union { //axes and hut switch
9 uint32_t axes;
10 struct {
11 uint32_t x : 10;
12 uint32_t y : 10;
13 uint32_t hat : 4;
14 uint32_t twist : 8;
15 };
16 };
17 uint8_t buttons_a;
18 uint8_t slider;
19 uint8_t buttons_b;
20};
21
22class JoystickEvents
23{
24public:
25 virtual void OnGamePadChanged(const GamePadEventData *evt);
26};
27
28#define RPT_GAMEPAD_LEN sizeof(GamePadEventData)/sizeof(uint8_t)
29
30class JoystickReportParser : public HIDReportParser
31{
32 JoystickEvents *joyEvents;
33
34 uint8_t oldPad[RPT_GAMEPAD_LEN];
35
36public:
37 JoystickReportParser(JoystickEvents *evt);
38
39 virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
40};
41
42#endif // __HIDJOYSTICKRPTPARSER_H__
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale.ino b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale.ino
new file mode 100644
index 000000000..f26ff964d
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale.ino
@@ -0,0 +1,51 @@
1/* Digital Scale Output. Written for Stamps.com Model 510 */
2/* 5lb Digital Scale; any HID scale with Usage page 0x8d should work */
3
4#include <hid.h>
5#include <hiduniversal.h>
6#include <usbhub.h>
7
8#include "scale_rptparser.h"
9
10// Satisfy the IDE, which needs to see the include statment in the ino too.
11#ifdef dobogusinclude
12#include <spi4teensy3.h>
13#include <SPI.h>
14#endif
15
16USB Usb;
17USBHub Hub(&Usb);
18HIDUniversal Hid(&Usb);
19Max_LCD LCD(&Usb);
20ScaleEvents ScaleEvents(&LCD);
21ScaleReportParser Scale(&ScaleEvents);
22
23void setup()
24{
25 Serial.begin( 115200 );
26#if !defined(__MIPSEL__)
27 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
28#endif
29 Serial.println("Start");
30
31 if (Usb.Init() == -1)
32 Serial.println("OSC did not start.");
33
34 // set up the LCD's number of rows and columns:
35 LCD.begin(16, 2);
36 LCD.clear();
37 LCD.home();
38 LCD.setCursor(0,0);
39 LCD.write('R');
40
41 delay( 200 );
42
43 if (!Hid.SetReportParser(0, &Scale))
44 ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 );
45}
46
47void loop()
48{
49 Usb.Task();
50}
51
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp
new file mode 100644
index 000000000..01ed980cf
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.cpp
@@ -0,0 +1,150 @@
1/* Parser for standard HID scale (usage page 0x8d) data input report (ID 3) */
2#include "scale_rptparser.h"
3
4const char* UNITS[13] = {
5 "units", // unknown unit
6 "mg", // milligram
7 "g", // gram
8 "kg", // kilogram
9 "cd", // carat
10 "taels", // lian
11 "gr", // grain
12 "dwt", // pennyweight
13 "tonnes", // metric tons
14 "tons", // avoir ton
15 "ozt", // troy ounce
16 "oz", // ounce
17 "lbs" // pound
18};
19
20ScaleReportParser::ScaleReportParser(ScaleEvents *evt) :
21 scaleEvents(evt)
22{}
23
24void ScaleReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
25{
26 bool match = true;
27
28 // Checking if there are changes in report since the method was last called
29 for (uint8_t i=0; i<RPT_SCALE_LEN; i++) {
30 if( buf[i] != oldScale[i] ) {
31 match = false;
32 break;
33 }
34 }
35 // Calling Game Pad event handler
36 if (!match && scaleEvents) {
37 scaleEvents->OnScaleChanged((const ScaleEventData*)buf);
38
39 for (uint8_t i=0; i<RPT_SCALE_LEN; i++) oldScale[i] = buf[i];
40 }
41}
42
43ScaleEvents::ScaleEvents( Max_LCD* pLCD ) :
44
45 pLcd( pLCD )
46
47{}
48
49void ScaleEvents::LcdPrint( const char* str )
50{
51
52 while( *str ) {
53
54 pLcd->write( *str++ );
55
56 }
57}
58
59void ScaleEvents::OnScaleChanged(const ScaleEventData *evt)
60{
61
62 pLcd->clear();
63 pLcd->home();
64 pLcd->setCursor(0,0);
65
66 if( evt->reportID != 3 ) {
67
68 const char inv_report[]="Invalid report!";
69
70 Serial.println(inv_report);
71 LcdPrint(inv_report);
72
73 return;
74
75 }//if( evt->reportID != 3...
76
77 switch( evt->status ) {
78
79 case REPORT_FAULT:
80 Serial.println(F("Report fault"));
81 break;
82
83 case ZEROED:
84 Serial.println(F("Scale zero set"));
85 break;
86
87 case WEIGHING: {
88
89 const char progress[] = "Weighing...";
90 Serial.println(progress);
91 LcdPrint(progress);
92 break;
93 }
94
95 case WEIGHT_VALID: {
96
97 char buf[10];
98 double weight = evt->weight * pow( 10, evt->exp );
99
100
101
102 Serial.print(F("Weight: "));
103 Serial.print( weight );
104 Serial.print(F(" "));
105 Serial.println( UNITS[ evt->unit ]);
106
107 LcdPrint("Weight: ");
108 dtostrf( weight, 4, 2, buf );
109 LcdPrint( buf );
110 LcdPrint( UNITS[ evt->unit ]);
111
112 break;
113
114 }//case WEIGHT_VALID...
115
116 case WEIGHT_NEGATIVE: {
117
118 const char negweight[] = "Negative weight";
119 Serial.println(negweight);
120 LcdPrint(negweight);
121 break;
122 }
123
124 case OVERWEIGHT: {
125
126 const char overweight[] = "Max.weight reached";
127 Serial.println(overweight);
128 LcdPrint( overweight );
129 break;
130 }
131
132 case CALIBRATE_ME:
133
134 Serial.println(F("Scale calibration required"));
135 break;
136
137 case ZERO_ME:
138
139 Serial.println(F("Scale zeroing required"));
140 break;
141
142 default:
143
144 Serial.print(F("Undefined status code: "));
145 Serial.println( evt->status );
146 break;
147
148 }//switch( evt->status...
149
150}
diff --git a/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h
new file mode 100644
index 000000000..57fbb033b
--- /dev/null
+++ b/lib/usbhost/USB_Host_Shield_2.0/examples/HID/scale/scale_rptparser.h
@@ -0,0 +1,55 @@
1#if !defined(__SCALERPTPARSER_H__)
2#define __SCALERPTPARSER_H__
3
4#include <max_LCD.h>
5#include <hid.h>
6
7/* Scale status constants */
8#define REPORT_FAULT 0x01
9#define ZEROED 0x02
10#define WEIGHING 0x03
11#define WEIGHT_VALID 0x04
12#define WEIGHT_NEGATIVE 0x05
13#define OVERWEIGHT 0x06
14#define CALIBRATE_ME 0x07
15#define ZERO_ME 0x08
16
17/* input data report */
18struct ScaleEventData
19{
20 uint8_t reportID; //must be 3
21 uint8_t status;
22 uint8_t unit;
23 int8_t exp; //scale factor for the weight
24 uint16_t weight; //
25};
26
27class ScaleEvents
28{
29
30 Max_LCD* pLcd;
31
32 void LcdPrint( const char* str );
33
34public:
35
36 ScaleEvents( Max_LCD* pLCD );
37
38 virtual void OnScaleChanged(const ScaleEventData *evt);
39};
40
41#define RPT_SCALE_LEN sizeof(ScaleEventData)/sizeof(uint8_t)
42
43class ScaleReportParser : public HIDReportParser
44{
45 ScaleEvents *scaleEvents;
46
47 uint8_t oldScale[RPT_SCALE_LEN];
48
49public:
50 ScaleReportParser(ScaleEvents *evt);
51
52 virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
53};
54
55#endif // __SCALERPTPARSER_H__