|
| 1 | +/********************************************************************* |
| 2 | + Adafruit invests time and resources providing this open source code, |
| 3 | + please support Adafruit and open-source hardware by purchasing |
| 4 | + products from Adafruit! |
| 5 | +
|
| 6 | + MIT license, check LICENSE for more information |
| 7 | + Copyright (c) 2021 NeKuNeKo for Adafruit Industries |
| 8 | + All text above, and the splash screen below must be included in |
| 9 | + any redistribution |
| 10 | +*********************************************************************/ |
| 11 | + |
| 12 | +#include "Adafruit_TinyUSB.h" |
| 13 | + |
| 14 | +/* This sketch demonstrates USB HID gamepad use. |
| 15 | + * This sketch is only valid on boards which have native USB support |
| 16 | + * and compatibility with Adafruit TinyUSB library. |
| 17 | + * For example SAMD21, SAMD51, nRF52840. |
| 18 | + * |
| 19 | + * Make sure you select the TinyUSB USB stack if you have a SAMD board. |
| 20 | + * You can test the gamepad on a Windows system by pressing WIN+R, writing Joy.cpl and pressing Enter. |
| 21 | + */ |
| 22 | + |
| 23 | +// HID report descriptor using TinyUSB's template |
| 24 | +// Single Report (no ID) descriptor |
| 25 | +uint8_t const desc_hid_report[] = |
| 26 | +{ |
| 27 | + TUD_HID_REPORT_DESC_GAMEPAD() |
| 28 | +}; |
| 29 | + |
| 30 | +// USB HID object |
| 31 | +Adafruit_USBD_HID usb_hid; |
| 32 | + |
| 33 | +hid_gamepad_report_t gp; // defined in hid.h from Adafruit_TinyUSB_ArduinoCore |
| 34 | +// For Gamepad Button Bit Mask see hid_gamepad_button_bm_t typedef defined in hid.h from Adafruit_TinyUSB_ArduinoCore |
| 35 | +// For Gamepad Hat Bit Mask see hid_gamepad_hat_bm_t typedef defined in hid.h from Adafruit_TinyUSB_ArduinoCore |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.begin(115200); |
| 40 | + |
| 41 | + usb_hid.setPollInterval(2); |
| 42 | + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); |
| 43 | + |
| 44 | + usb_hid.begin(); |
| 45 | + |
| 46 | + // wait until device mounted |
| 47 | + while( !USBDevice.mounted() ) delay(1); |
| 48 | + |
| 49 | + Serial.println("Adafruit TinyUSB HID Gamepad example"); |
| 50 | +} |
| 51 | + |
| 52 | +void loop() |
| 53 | +{ |
| 54 | +// // Remote wakeup |
| 55 | +// if ( USBDevice.suspended() && btn ) |
| 56 | +// { |
| 57 | +// // Wake up host if we are in suspend mode |
| 58 | +// // and REMOTE_WAKEUP feature is enabled by host |
| 59 | +// USBDevice.remoteWakeup(); |
| 60 | +// } |
| 61 | + |
| 62 | + if ( !usb_hid.ready() ) return; |
| 63 | + |
| 64 | + |
| 65 | + // Reset buttons |
| 66 | + Serial.println("No pressing buttons"); |
| 67 | + gp.x = 0; |
| 68 | + gp.y = 0; |
| 69 | + gp.z = 0; |
| 70 | + gp.rz = 0; |
| 71 | + gp.rx = 0; |
| 72 | + gp.ry = 0; |
| 73 | + gp.hat = 0; |
| 74 | + gp.buttons = 0; |
| 75 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 76 | + delay(2000); |
| 77 | + |
| 78 | + |
| 79 | + // Hat/DPAD UP |
| 80 | + Serial.println("Hat/DPAD UP"); |
| 81 | + gp.hat = 1; // GAMEPAD_HAT_UP; |
| 82 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 83 | + delay(2000); |
| 84 | + |
| 85 | + // Hat/DPAD UP RIGHT |
| 86 | + Serial.println("Hat/DPAD UP RIGHT"); |
| 87 | + gp.hat = 2; // GAMEPAD_HAT_UP_RIGHT; |
| 88 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 89 | + delay(2000); |
| 90 | + |
| 91 | + // Hat/DPAD RIGHT |
| 92 | + Serial.println("Hat/DPAD RIGHT"); |
| 93 | + gp.hat = 3; // GAMEPAD_HAT_RIGHT; |
| 94 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 95 | + delay(2000); |
| 96 | + |
| 97 | + // Hat/DPAD DOWN RIGHT |
| 98 | + Serial.println("Hat/DPAD DOWN RIGHT"); |
| 99 | + gp.hat = 4; // GAMEPAD_HAT_DOWN_RIGHT; |
| 100 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 101 | + delay(2000); |
| 102 | + |
| 103 | + // Hat/DPAD DOWN |
| 104 | + Serial.println("Hat/DPAD DOWN"); |
| 105 | + gp.hat = 5; // GAMEPAD_HAT_DOWN; |
| 106 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 107 | + delay(2000); |
| 108 | + |
| 109 | + // Hat/DPAD DOWN LEFT |
| 110 | + Serial.println("Hat/DPAD DOWN LEFT"); |
| 111 | + gp.hat = 6; // GAMEPAD_HAT_DOWN_LEFT; |
| 112 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 113 | + delay(2000); |
| 114 | + |
| 115 | + // Hat/DPAD LEFT |
| 116 | + Serial.println("Hat/DPAD LEFT"); |
| 117 | + gp.hat = 7; // GAMEPAD_HAT_LEFT; |
| 118 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 119 | + delay(2000); |
| 120 | + |
| 121 | + // Hat/DPAD UP LEFT |
| 122 | + Serial.println("Hat/DPAD UP LEFT"); |
| 123 | + gp.hat = 8; // GAMEPAD_HAT_UP_LEFT; |
| 124 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 125 | + delay(2000); |
| 126 | + |
| 127 | + // Hat/DPAD CENTER |
| 128 | + Serial.println("Hat/DPAD CENTER"); |
| 129 | + gp.hat = 0; // GAMEPAD_HAT_CENTERED; |
| 130 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 131 | + delay(2000); |
| 132 | + |
| 133 | + |
| 134 | + // Joystick 1 UP |
| 135 | + Serial.println("Joystick 1 UP"); |
| 136 | + gp.x = 0; |
| 137 | + gp.y = -127; |
| 138 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 139 | + delay(2000); |
| 140 | + |
| 141 | + // Joystick 1 DOWN |
| 142 | + Serial.println("Joystick 1 DOWN"); |
| 143 | + gp.x = 0; |
| 144 | + gp.y = 127; |
| 145 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 146 | + delay(2000); |
| 147 | + |
| 148 | + // Joystick 1 RIGHT |
| 149 | + Serial.println("Joystick 1 RIGHT"); |
| 150 | + gp.x = 127; |
| 151 | + gp.y = 0; |
| 152 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 153 | + delay(2000); |
| 154 | + |
| 155 | + // Joystick 1 LEFT |
| 156 | + Serial.println("Joystick 1 LEFT"); |
| 157 | + gp.x = -127; |
| 158 | + gp.y = 0; |
| 159 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 160 | + delay(2000); |
| 161 | + |
| 162 | + // Joystick 1 CENTER |
| 163 | + Serial.println("Joystick 1 CENTER"); |
| 164 | + gp.x = 0; |
| 165 | + gp.y = 0; |
| 166 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 167 | + delay(2000); |
| 168 | + |
| 169 | + |
| 170 | + // Joystick 2 UP |
| 171 | + Serial.println("Joystick 2 UP"); |
| 172 | + gp.z = 0; |
| 173 | + gp.rz = 127; |
| 174 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 175 | + delay(2000); |
| 176 | + |
| 177 | + // Joystick 2 DOWN |
| 178 | + Serial.println("Joystick 2 DOWN"); |
| 179 | + gp.z = 0; |
| 180 | + gp.rz = -127; |
| 181 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 182 | + delay(2000); |
| 183 | + |
| 184 | + // Joystick 2 RIGHT |
| 185 | + Serial.println("Joystick 2 RIGHT"); |
| 186 | + gp.z = 127; |
| 187 | + gp.rz = 0; |
| 188 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 189 | + delay(2000); |
| 190 | + |
| 191 | + // Joystick 2 LEFT |
| 192 | + Serial.println("Joystick 2 LEFT"); |
| 193 | + gp.z = -127; |
| 194 | + gp.rz = 0; |
| 195 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 196 | + delay(2000); |
| 197 | + |
| 198 | + // Joystick 2 CENTER |
| 199 | + Serial.println("Joystick 2 CENTER"); |
| 200 | + gp.z = 0; |
| 201 | + gp.rz = 0; |
| 202 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 203 | + delay(2000); |
| 204 | + |
| 205 | + |
| 206 | + // Analog Trigger 1 UP |
| 207 | + Serial.println("Analog Trigger 1 UP"); |
| 208 | + gp.rx = 127; |
| 209 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 210 | + delay(2000); |
| 211 | + |
| 212 | + // Analog Trigger 1 DOWN |
| 213 | + Serial.println("Analog Trigger 1 DOWN"); |
| 214 | + gp.rx = -127; |
| 215 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 216 | + delay(2000); |
| 217 | + |
| 218 | + // Analog Trigger 1 CENTER |
| 219 | + Serial.println("Analog Trigger 1 CENTER"); |
| 220 | + gp.rx = 0; |
| 221 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 222 | + delay(2000); |
| 223 | + |
| 224 | + |
| 225 | + // Analog Trigger 2 UP |
| 226 | + Serial.println("Analog Trigger 2 UP"); |
| 227 | + gp.ry = 127; |
| 228 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 229 | + delay(2000); |
| 230 | + |
| 231 | + // Analog Trigger 2 DOWN |
| 232 | + Serial.println("Analog Trigger 2 DOWN"); |
| 233 | + gp.ry = -127; |
| 234 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 235 | + delay(2000); |
| 236 | + |
| 237 | + // Analog Trigger 2 CENTER |
| 238 | + Serial.println("Analog Trigger 2 CENTER"); |
| 239 | + gp.ry = 0; |
| 240 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 241 | + delay(2000); |
| 242 | + |
| 243 | + |
| 244 | + // Test buttons |
| 245 | + for (int i=0; i<=15; ++i) |
| 246 | + { |
| 247 | + Serial.print("Pressing button "); Serial.println(i+1); |
| 248 | + gp.buttons = (0x00 | TU_BIT(i)); |
| 249 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 250 | + delay(1000); |
| 251 | + } |
| 252 | + |
| 253 | + |
| 254 | + // Random touch |
| 255 | + Serial.println("Random touch"); |
| 256 | + gp.x = random(-127, 128); |
| 257 | + gp.y = random(-127, 128); |
| 258 | + gp.z = random(-127, 128); |
| 259 | + gp.rz = random(-127, 128); |
| 260 | + gp.rx = random(-127, 128); |
| 261 | + gp.ry = random(-127, 128); |
| 262 | + gp.hat = random(0, 9); |
| 263 | + gp.buttons = random(0, 0xffff); |
| 264 | + usb_hid.sendReport(0, &gp, sizeof(gp)); |
| 265 | + delay(2000); |
| 266 | + |
| 267 | + // */ |
| 268 | +} |
0 commit comments