Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <HID.h>
  2.  
  3. #if !defined(_USING_HID)
  4.  
  5. #error "Using legacy HID core (non pluggable)"
  6.  
  7. #endif
  8.  
  9. static const u8 _hidReportDescriptor[] PROGMEM = {
  10. /* Gamepad with buttons and X/Y only */ \
  11. 0x05, 0x01,             /* USAGE_PAGE (Generic Desktop) */ \
  12. 0x09, 0x04,             /* USAGE (Joystick) */ \
  13. 0xa1, 0x01,             /* COLLECTION
  14. (Application) */ \
  15. 0x85, 0x01,          /*   REPORT_ID */ \
  16. /* 32 Buttons */ \
  17. 0x05, 0x09,             /*   USAGE_PAGE (Button) */ \
  18. 0x19, 0x01,             /*   USAGE_MINIMUM (Button 1) */ \
  19. 0x29, 0x20,             /*   USAGE_MAXIMUM (Button 32) */ \
  20. 0x15, 0x00,             /*   LOGICAL_MINIMUM (0) */ \
  21. 0x25, 0x01,             /*   LOGICAL_MAXIMUM (1) */ \
  22. 0x75, 0x01,             /*   REPORT_SIZE (1) */ \
  23. 0x95, 0x20,             /*   REPORT_COUNT (32) */ \
  24. 0x81, 0x02,             /*   INPUT (Data,Var,Abs) */ \
  25. 0xc0                /* END_COLLECTION */
  26. };
  27.  
  28. struct ControlPadReport
  29. {
  30.   uint32_t buttons;
  31. };
  32.  
  33. class ControlPad
  34. {
  35. private:
  36.   ControlPadReport _report;
  37.   HID_ *_hid;
  38. public:
  39.   ControlPad(HID_ *hid)
  40.   {
  41.     _hid = hid;
  42.     static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
  43.     _hid->AppendDescriptor(&node);
  44.   }
  45.   void begin(void)
  46.   {
  47.     releaseAll();
  48.     write();
  49.   }
  50.   void end(void)
  51.   {
  52.     releaseAll();
  53.     write();
  54.   }
  55.   inline void press(uint8_t b)
  56.   {
  57.     bitSet(_report.buttons, b);
  58.   }
  59.   inline void release(uint8_t b)
  60.   {
  61.     bitClear(_report.buttons, b);
  62.   }
  63.   inline void releaseAll()
  64.   {
  65.     memset(&_report, 0, sizeof(ControlPadReport));
  66.   }
  67.   void write()
  68.   {
  69.     _hid->SendReport(1,&_report,sizeof(ControlPadReport));
  70.   }
  71. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement