2424
2525#include " Adafruit_USBD_HID.h"
2626
27-
28- // --------------------------------------------------------------------+
29- //
30- // --------------------------------------------------------------------+
3127#define EPOUT 0x00
3228#define EPIN 0x80
3329
3430uint8_t const _ascii2keycode[128 ][2 ] = { HID_ASCII_TO_KEYCODE };
3531
32+ static Adafruit_USBD_HID* _hid_dev = NULL ;
33+
3634// ------------- IMPLEMENTATION -------------//
3735Adafruit_USBD_HID::Adafruit_USBD_HID (void )
3836{
3937 _interval_ms = 10 ;
4038 _protocol = HID_PROTOCOL_NONE;
4139
40+ _out_endpoint = false ;
4241 _mouse_button = 0 ;
4342
4443 _desc_report = NULL ;
@@ -58,6 +57,11 @@ void Adafruit_USBD_HID::setBootProtocol(uint8_t protocol)
5857 _protocol = protocol;
5958}
6059
60+ void Adafruit_USBD_HID::enableOutEndpoint (bool enable)
61+ {
62+ _out_endpoint = enable;
63+ }
64+
6165void Adafruit_USBD_HID::setReportDescriptor (uint8_t const * desc_report, uint16_t len)
6266{
6367 _desc_report = desc_report;
@@ -74,18 +78,33 @@ uint16_t Adafruit_USBD_HID::getDescriptor(uint8_t* buf, uint16_t bufsize)
7478{
7579 if ( !_desc_report_len ) return 0 ;
7680
77- uint8_t desc[] = { TUD_HID_DESCRIPTOR (0 , 0 , _protocol, _desc_report_len, EPIN, 16 , _interval_ms) };
78- uint16_t const len = sizeof (desc);
81+ if ( _out_endpoint )
82+ {
83+ uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR (0 , 0 , _protocol, _desc_report_len, EPIN, EPOUT, CFG_TUD_HID_BUFSIZE, _interval_ms) };
84+ uint16_t const len = sizeof (desc);
85+
86+ if ( bufsize < len ) return 0 ;
87+ memcpy (buf, desc, len);
88+
89+ return len;
90+ }else
91+ {
92+ uint8_t desc[] = { TUD_HID_DESCRIPTOR (0 , 0 , _protocol, _desc_report_len, EPIN, CFG_TUD_HID_BUFSIZE, _interval_ms) };
93+ uint16_t const len = sizeof (desc);
7994
80- if ( bufsize < len ) return 0 ;
81- memcpy (buf, desc, len);
82- return len;
95+ if ( bufsize < len ) return 0 ;
96+ memcpy (buf, desc, len);
97+
98+ return len;
99+ }
83100}
84101
85102bool Adafruit_USBD_HID::begin (void )
86103{
87104 if ( !USBDevice.addInterface (*this ) ) return false ;
105+
88106 tud_desc_set.hid_report = _desc_report;
107+ _hid_dev = this ;
89108
90109 return true ;
91110}
@@ -100,6 +119,31 @@ bool Adafruit_USBD_HID::sendReport(uint8_t report_id, void const* report, uint8_
100119 return tud_hid_report (report_id, report, len);
101120}
102121
122+ // ------------- TinyUSB callbacks -------------//
123+ extern " C"
124+ {
125+
126+ // Invoked when received GET_REPORT control request
127+ // Application must fill buffer report's content and return its length.
128+ // Return zero will cause the stack to STALL request
129+ uint16_t tud_hid_get_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t * buffer, uint16_t reqlen)
130+ {
131+ if ( !(_hid_dev && _hid_dev->_get_report_cb ) ) return 0 ;
132+
133+ return _hid_dev->_get_report_cb (report_id, report_type, buffer, reqlen);
134+ }
135+
136+ // Invoked when received SET_REPORT control request or
137+ // received data on OUT endpoint ( Report ID = 0, Type = 0 )
138+ void tud_hid_set_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t const * buffer, uint16_t bufsize)
139+ {
140+ if ( !(_hid_dev && _hid_dev->_set_report_cb ) ) return ;
141+
142+ _hid_dev->_set_report_cb (report_id, report_type, buffer, bufsize);
143+ }
144+
145+ } // extern "C"
146+
103147// --------------------------------------------------------------------+
104148// Keyboard
105149// --------------------------------------------------------------------+
0 commit comments