Étape 6: Ajouter le code HID à vos fichiers d’environnement Arduino
Vous avez maintenant votre descripteur, que vous devez l’ajouter à votre environnement Arduino donc il peut la programmer sur votre appareil. Pour ce faire, vous devrez modifier les deux fichiers. Naviguez jusqu’au répertoire de C:\Program Files (x86) \Arduino\hardware\arduino\cores\arduino (Win) ou Documents/Arduino/matériel/arduino/carottes/arduino (Mac). En prendre des copies de sauvegarde des fichiers HID.cpp et USBAPI.h, juste au cas où.
Ouvrez HID.cpp dans votre éditeur de texte préféré (EditPlus 2 ou Notepad ++ sont bonnes pour Windows, Text Wrangler est utile pour Mac) et regardez vers le haut pour la section suivante :
#if defined(USBCON)#ifdef HID_ENABLED // #define RAWHID_ENABLED // Singletons for mouse and keyboard Mouse_ Mouse; Keyboard_ Keyboard;
Vous devez maintenant ajouter une définition et une variable de singleton comme suit :
#if defined(USBCON)#ifdef HID_ENABLED// #define RAWHID_ENABLED #define JOYHID_ENABLED// Singletons for mouse and keyboard Mouse_ Mouse; Keyboard_ Keyboard;// And now a joystick object too Joystick_ Joystick;
Alors ne cherchez plus loin vers le bas pour la section RAWHID :
#if RAWHID_ENABLED // RAW HID 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), 0xA1, 0x01, // Collection 0x01 0x85, 0x03, // REPORT_ID (3) 0x75, 0x08, // report size = 8 bits 0x15, 0x00, // logical minimum = 0 0x26, 0xFF, 0x00, // logical maximum = 255 0x95, 64, // report count TX 0x09, 0x01, // usage 0x81, 0x02, // Input (array) 0x95, 64, // report count RX 0x09, 0x02, // usage 0x91, 0x02, // Output (array) 0xC0 // end collection #endif };
Maintenant modifier cette option pour changer le #if #ifdef et insérez votre nouveau descripteur de caché à l’intérieur de sa section #ifdef
#ifdef RAWHID_ENABLED// RAW HID 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), 0xA1, 0x01, // Collection 0x01 0x85, 0x03, // REPORT_ID (3) 0x75, 0x08, // report size = 8 bits 0x15, 0x00, // logical minimum = 0 0x26, 0xFF, 0x00, // logical maximum = 255 0x95, 64, // report count TX 0x09, 0x01, // usage 0x81, 0x02, // Input (array) 0x95, 64, // report count RX 0x09, 0x02, // usage 0x91, 0x02, // Output (array) 0xC0 // end collection #endif// *** Here is where the RAW_HID has been converted to a Game Pad device // *** Inspired by helmpcb.com/electronics/usb-joystick // *** Check out www.usb.org/developers/hidpage/ for more than you'll ever need to know about USB HID // *** HID descriptor created using the HID descriptor tool from www.usb.org/developers/hidpage/dt2_4.zip#ifdef JOYHID_ENABLED 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x05, // USAGE (Game Pad) 0xa1, 0x01, // COLLECTION (Application) 0x85, 0x03, // REPORT_ID (3) (This is important when HID_SendReport() is called) 0xA1, 0x00, // COLLECTION (Physical) // 8 buttons 0x05, 0x09, // USAGE_PAGE (Button) 0x19, 0x01, // USAGE_MINIMUM (Button 1) 0x29, 0x08, // USAGE_MAXIMUM (Button 8) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x95, 0x08, // REPORT_COUNT (8) 0x75, 0x01, // REPORT_SIZE (1) 0x81, 0x02, // INPUT (Data,Var,Abs) // 1 Hat Switch 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x39, // USAGE (Hat switch) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x07, // LOGICAL_MAXIMUM (7) 0x35, 0x00, // PHYSICAL_MINIMUM (0) 0x46, 0x3B, 0x01, // PHYSICAL_MAXIMUM (315) 0x65, 0x14, // UNIT (Eng Rot:Angular Pos) 0x75, 0x04, // REPORT_SIZE (4) 0x95, 0x01, // REPORT_COUNT (1) 0x81, 0x02, // INPUT (Data,Var,Abs) // Padding (4 bytes) 0x75, 0x04, // REPORT_SIZE (4) 0x95, 0x01, // REPORT_COUNT (1) 0x81, 0x03, // INPUT (Cnst,Var,Abs) // 1 D-pads - Dummy so Elite:Dangerous recognises it 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) 0x15, 0x81, // LOGICAL_MINIMUM (-127) 0x25, 0x7f, // LOGICAL_MAXIMUM (127) 0x75, 0x08, // REPORT_SIZE (8) 0x95, 0x02, // REPORT_COUNT (2) 0x81, 0x02, // INPUT (Data,Var,Abs) 0xC0, //END COLLECTION 0xC0, //END COLLECTION#endif };
Maintenant que de fait, il faut ajouter du code pour recevoir les données de notre croquis, que nous avons créé plus tôt et envoyer le rapport. Ne cherchez plus loin vers le bas pour le code suivant :
//=============================================================================//============================================================================= // Mouse Mouse_::Mouse_(void) : _buttons(0) { }
Directement au-dessus de cela, ajoutez un nouveau morceau de code pour gérer les données comme suit
//=============================================================================//============================================================================= // Joystick // Usage: Joystick.move(inputs go here) // // The report data format must match the one defined in the descriptor exactly // or it either won't work, or the pc will make a mess of unpacking the data //Joystick_::Joystick_() { }#define joyBytes 4 // should be equivalent to sizeof(JoyState_t)void Joystick_::setState(JoyState_t *joySt) { uint8_t data[joyBytes]; data[0] = joySt->buttons & 0xFF; data[1] = joySt->hatSw1; data[2] = joySt->left_x; data[3] = joySt->left_y; //HID_SendReport(Report number, array of values in same order as HID descriptor, length) // The joystick is specified as using report 3 in the descriptor. That's where the "3" comes from HID_SendReport(3, data, joyBytes); }
Ici, nous définissons le nombre d’octets qui sont envoyés. Dans ce cas 2-1 octet pour les boutons et 1 octet pour le chapeau, y compris de remplissage. Ensuite, nous avons la fonction simple pour accepter les données envoyées à partir de l’esquisse, il hacher et puis cracher à l’aide de la fonction HID_SendReport. Celui-ci est très simple, mais vous pouvez trouver que plus compliqué ceux ailleurs. Ce site a un bon exemple des 8 axes, 32-bouton, configuration 1 chapeau. Cela m’a beaucoup aidé, mais était beaucoup trop compliqué pour ce que je voulais, donc j’ai simplifié il autant que je le pouvais.
Une fois que vous avez fait tout cela, enregistrez le fichier, ouvrez USBAPI.h. Dans ce fichier, nous allons créer une classe de l’objet de la manette et struct. Trouver le code suivant :
//==========================================================================//========================================================================== // Mouse #define MOUSE_LEFT 1 #define MOUSE_RIGHT 2
Comme avant, insérez ce code directement au-dessus de cette
//=============================================================================//============================================================================= // Joystick // Implemented in HID.cpp // The list of parameters here needs to match the implementation in HID.cpptypedef struct JoyState // Pretty self explanitory. Simple state to store all the joystick parameters { uint8_t buttons; uint8_t hatSw1; int8_t left_x; int8_t left_y; } JoyState_t;class Joystick_ { public: Joystick_(); void setState(JoyState_t *joySt);}; extern Joystick_ Joystick;
Enregistrez le fichier et vous avez terminé. Encore une fois, si vous avez plus une application exigeante simplement ajouter du code pour gérer le matériel supplémentaire. Le site mentionné ci-dessus sera encore utile.