diff options
| author | Ryan <fauxpark@gmail.com> | 2021-08-18 18:20:25 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-18 18:20:25 +1000 |
| commit | b16091659cc9a724a8800f77e631643b4ab089ad (patch) | |
| tree | e44933472c6d100bd4fc5d8a693d9d21e3c32f6f /lib/usbhost/USB_Host_Shield_2.0/examples/HID | |
| parent | cf5e40c25139ff64ff246f1c6280e983ef75551c (diff) | |
| download | qmk_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')
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 | |||
| 10 | class 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 | |||
| 22 | void 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 | |||
| 41 | void 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 | |||
| 51 | void 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 | |||
| 87 | void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) | ||
| 88 | { | ||
| 89 | Serial.print("UP "); | ||
| 90 | PrintKey(mod, key); | ||
| 91 | } | ||
| 92 | |||
| 93 | void KbdRptParser::OnKeyPressed(uint8_t key) | ||
| 94 | { | ||
| 95 | Serial.print("ASCII: "); | ||
| 96 | Serial.println((char)key); | ||
| 97 | }; | ||
| 98 | |||
| 99 | USB Usb; | ||
| 100 | //USBHub Hub(&Usb); | ||
| 101 | HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); | ||
| 102 | |||
| 103 | uint32_t next_time; | ||
| 104 | |||
| 105 | KbdRptParser Prs; | ||
| 106 | |||
| 107 | void 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 | |||
| 125 | void 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 | |||
| 10 | class 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 | }; | ||
| 21 | void 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 | }; | ||
| 28 | void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) | ||
| 29 | { | ||
| 30 | Serial.println("L Butt Up"); | ||
| 31 | }; | ||
| 32 | void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) | ||
| 33 | { | ||
| 34 | Serial.println("L Butt Dn"); | ||
| 35 | }; | ||
| 36 | void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) | ||
| 37 | { | ||
| 38 | Serial.println("R Butt Up"); | ||
| 39 | }; | ||
| 40 | void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) | ||
| 41 | { | ||
| 42 | Serial.println("R Butt Dn"); | ||
| 43 | }; | ||
| 44 | void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) | ||
| 45 | { | ||
| 46 | Serial.println("M Butt Up"); | ||
| 47 | }; | ||
| 48 | void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) | ||
| 49 | { | ||
| 50 | Serial.println("M Butt Dn"); | ||
| 51 | }; | ||
| 52 | |||
| 53 | class 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 | |||
| 64 | void 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 | |||
| 83 | void 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 | |||
| 93 | void 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 | |||
| 129 | void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) | ||
| 130 | { | ||
| 131 | Serial.print("UP "); | ||
| 132 | PrintKey(mod, key); | ||
| 133 | } | ||
| 134 | |||
| 135 | void KbdRptParser::OnKeyPressed(uint8_t key) | ||
| 136 | { | ||
| 137 | Serial.print("ASCII: "); | ||
| 138 | Serial.println((char)key); | ||
| 139 | }; | ||
| 140 | |||
| 141 | USB Usb; | ||
| 142 | USBHub Hub(&Usb); | ||
| 143 | |||
| 144 | HIDBoot < HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE > HidComposite(&Usb); | ||
| 145 | HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); | ||
| 146 | HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb); | ||
| 147 | |||
| 148 | //uint32_t next_time; | ||
| 149 | |||
| 150 | KbdRptParser KbdPrs; | ||
| 151 | MouseRptParser MousePrs; | ||
| 152 | |||
| 153 | void 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 | |||
| 174 | void 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 | |||
| 10 | class 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 | }; | ||
| 21 | void 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 | }; | ||
| 28 | void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi) | ||
| 29 | { | ||
| 30 | Serial.println("L Butt Up"); | ||
| 31 | }; | ||
| 32 | void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi) | ||
| 33 | { | ||
| 34 | Serial.println("L Butt Dn"); | ||
| 35 | }; | ||
| 36 | void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi) | ||
| 37 | { | ||
| 38 | Serial.println("R Butt Up"); | ||
| 39 | }; | ||
| 40 | void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi) | ||
| 41 | { | ||
| 42 | Serial.println("R Butt Dn"); | ||
| 43 | }; | ||
| 44 | void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi) | ||
| 45 | { | ||
| 46 | Serial.println("M Butt Up"); | ||
| 47 | }; | ||
| 48 | void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) | ||
| 49 | { | ||
| 50 | Serial.println("M Butt Dn"); | ||
| 51 | }; | ||
| 52 | |||
| 53 | USB Usb; | ||
| 54 | USBHub Hub(&Usb); | ||
| 55 | HIDBoot<HID_PROTOCOL_MOUSE> HidMouse(&Usb); | ||
| 56 | |||
| 57 | uint32_t next_time; | ||
| 58 | |||
| 59 | MouseRptParser Prs; | ||
| 60 | |||
| 61 | void 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 | |||
| 79 | void 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 | |||
| 13 | USB Usb; | ||
| 14 | USBHub Hub(&Usb); | ||
| 15 | HIDUniversal Hid(&Usb); | ||
| 16 | JoystickEvents JoyEvents; | ||
| 17 | JoystickReportParser Joy(&JoyEvents); | ||
| 18 | |||
| 19 | void 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 | |||
| 35 | void 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 | |||
| 3 | JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : | ||
| 4 | joyEvents(evt), | ||
| 5 | oldHat(0xDE), | ||
| 6 | oldButtons(0) { | ||
| 7 | for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) | ||
| 8 | oldPad[i] = 0xD; | ||
| 9 | } | ||
| 10 | |||
| 11 | void 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 | |||
| 56 | void 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 | |||
| 70 | void JoystickEvents::OnHatSwitch(uint8_t hat) { | ||
| 71 | Serial.print("Hat Switch: "); | ||
| 72 | PrintHex<uint8_t > (hat, 0x80); | ||
| 73 | Serial.println(""); | ||
| 74 | } | ||
| 75 | |||
| 76 | void JoystickEvents::OnButtonUp(uint8_t but_id) { | ||
| 77 | Serial.print("Up: "); | ||
| 78 | Serial.println(but_id, DEC); | ||
| 79 | } | ||
| 80 | |||
| 81 | void 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 | |||
| 6 | struct GamePadEventData { | ||
| 7 | uint8_t X, Y, Z1, Z2, Rz; | ||
| 8 | }; | ||
| 9 | |||
| 10 | class JoystickEvents { | ||
| 11 | public: | ||
| 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 | |||
| 20 | class JoystickReportParser : public HIDReportParser { | ||
| 21 | JoystickEvents *joyEvents; | ||
| 22 | |||
| 23 | uint8_t oldPad[RPT_GEMEPAD_LEN]; | ||
| 24 | uint8_t oldHat; | ||
| 25 | uint16_t oldButtons; | ||
| 26 | |||
| 27 | public: | ||
| 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 | |||
| 13 | class HIDUniversal2 : public HIDUniversal | ||
| 14 | { | ||
| 15 | public: | ||
| 16 | HIDUniversal2(USB *usb) : HIDUniversal(usb) {}; | ||
| 17 | |||
| 18 | protected: | ||
| 19 | uint8_t OnInitSuccessful(); | ||
| 20 | }; | ||
| 21 | |||
| 22 | uint8_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 | |||
| 37 | FailGetReportDescr1: | ||
| 38 | USBTRACE("GetReportDescr1:"); | ||
| 39 | goto Fail; | ||
| 40 | |||
| 41 | FailGetReportDescr2: | ||
| 42 | USBTRACE("GetReportDescr2:"); | ||
| 43 | goto Fail; | ||
| 44 | |||
| 45 | Fail: | ||
| 46 | Serial.println(rcode, HEX); | ||
| 47 | Release(); | ||
| 48 | return rcode; | ||
| 49 | } | ||
| 50 | |||
| 51 | USB Usb; | ||
| 52 | //USBHub Hub(&Usb); | ||
| 53 | HIDUniversal2 Hid(&Usb); | ||
| 54 | UniversalReportParser Uni; | ||
| 55 | |||
| 56 | void 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 | |||
| 73 | void 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 */ | ||
| 10 | const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; | ||
| 11 | const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; | ||
| 12 | const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; | ||
| 13 | const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; | ||
| 14 | const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; | ||
| 15 | const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; | ||
| 16 | const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; | ||
| 17 | const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; | ||
| 18 | const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; | ||
| 19 | const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; | ||
| 20 | const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; | ||
| 21 | const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; | ||
| 22 | const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; | ||
| 23 | const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; | ||
| 24 | const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; | ||
| 25 | const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; | ||
| 26 | const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; | ||
| 27 | const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; | ||
| 28 | const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; | ||
| 29 | const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; | ||
| 30 | const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; | ||
| 31 | const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; | ||
| 32 | const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; | ||
| 33 | const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; | ||
| 34 | const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; | ||
| 35 | const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; | ||
| 36 | const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; | ||
| 37 | const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; | ||
| 38 | const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; | ||
| 39 | const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; | ||
| 40 | const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; | ||
| 41 | const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; | ||
| 42 | const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; | ||
| 43 | const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; | ||
| 44 | const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; | ||
| 45 | const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; | ||
| 46 | const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; | ||
| 47 | const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; | ||
| 48 | const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; | ||
| 49 | const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; | ||
| 50 | const 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 | |||
| 15 | USB Usb; | ||
| 16 | USBHub Hub(&Usb); | ||
| 17 | HIDUniversal Hid(&Usb); | ||
| 18 | JoystickEvents JoyEvents; | ||
| 19 | JoystickReportParser Joy(&JoyEvents); | ||
| 20 | |||
| 21 | void 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 | |||
| 38 | void 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 | |||
| 3 | JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : | ||
| 4 | joyEvents(evt) | ||
| 5 | {} | ||
| 6 | |||
| 7 | void 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 | |||
| 26 | void 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 | |||
| 6 | struct 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 | |||
| 22 | class JoystickEvents | ||
| 23 | { | ||
| 24 | public: | ||
| 25 | virtual void OnGamePadChanged(const GamePadEventData *evt); | ||
| 26 | }; | ||
| 27 | |||
| 28 | #define RPT_GAMEPAD_LEN sizeof(GamePadEventData)/sizeof(uint8_t) | ||
| 29 | |||
| 30 | class JoystickReportParser : public HIDReportParser | ||
| 31 | { | ||
| 32 | JoystickEvents *joyEvents; | ||
| 33 | |||
| 34 | uint8_t oldPad[RPT_GAMEPAD_LEN]; | ||
| 35 | |||
| 36 | public: | ||
| 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 | |||
| 16 | USB Usb; | ||
| 17 | USBHub Hub(&Usb); | ||
| 18 | HIDUniversal Hid(&Usb); | ||
| 19 | Max_LCD LCD(&Usb); | ||
| 20 | ScaleEvents ScaleEvents(&LCD); | ||
| 21 | ScaleReportParser Scale(&ScaleEvents); | ||
| 22 | |||
| 23 | void 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 | |||
| 47 | void 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 | |||
| 4 | const 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 | |||
| 20 | ScaleReportParser::ScaleReportParser(ScaleEvents *evt) : | ||
| 21 | scaleEvents(evt) | ||
| 22 | {} | ||
| 23 | |||
| 24 | void 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 | |||
| 43 | ScaleEvents::ScaleEvents( Max_LCD* pLCD ) : | ||
| 44 | |||
| 45 | pLcd( pLCD ) | ||
| 46 | |||
| 47 | {} | ||
| 48 | |||
| 49 | void ScaleEvents::LcdPrint( const char* str ) | ||
| 50 | { | ||
| 51 | |||
| 52 | while( *str ) { | ||
| 53 | |||
| 54 | pLcd->write( *str++ ); | ||
| 55 | |||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | void 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 */ | ||
| 18 | struct 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 | |||
| 27 | class ScaleEvents | ||
| 28 | { | ||
| 29 | |||
| 30 | Max_LCD* pLcd; | ||
| 31 | |||
| 32 | void LcdPrint( const char* str ); | ||
| 33 | |||
| 34 | public: | ||
| 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 | |||
| 43 | class ScaleReportParser : public HIDReportParser | ||
| 44 | { | ||
| 45 | ScaleEvents *scaleEvents; | ||
| 46 | |||
| 47 | uint8_t oldScale[RPT_SCALE_LEN]; | ||
| 48 | |||
| 49 | public: | ||
| 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__ | ||
