1- /*
1+ /*
22 * The MIT License (MIT)
33 *
44 * Copyright (c) 2019 Ha Thach for Adafruit Industries
2424
2525#include " Adafruit_USBD_HID.h"
2626
27- #define EPOUT 0x00
28- #define EPIN 0x80
27+ #define EPOUT 0x00
28+ #define EPIN 0x80
2929
30- uint8_t const _ascii2keycode[128 ][2 ] = { HID_ASCII_TO_KEYCODE };
30+ uint8_t const _ascii2keycode[128 ][2 ] = { HID_ASCII_TO_KEYCODE};
3131
32- static Adafruit_USBD_HID* _hid_dev = NULL ;
32+ static Adafruit_USBD_HID * _hid_dev = NULL ;
3333
3434// ------------- IMPLEMENTATION -------------//
35- Adafruit_USBD_HID::Adafruit_USBD_HID (void )
36- {
35+ Adafruit_USBD_HID::Adafruit_USBD_HID (void ) {
3736 _interval_ms = 10 ;
3837 _protocol = HID_PROTOCOL_NONE;
3938
@@ -47,106 +46,107 @@ Adafruit_USBD_HID::Adafruit_USBD_HID(void)
4746 _set_report_cb = NULL ;
4847}
4948
50- void Adafruit_USBD_HID::setPollInterval (uint8_t interval_ms)
51- {
49+ void Adafruit_USBD_HID::setPollInterval (uint8_t interval_ms) {
5250 _interval_ms = interval_ms;
5351}
5452
55- void Adafruit_USBD_HID::setBootProtocol (uint8_t protocol)
56- {
53+ void Adafruit_USBD_HID::setBootProtocol (uint8_t protocol) {
5754 _protocol = protocol;
5855}
5956
60- void Adafruit_USBD_HID::enableOutEndpoint (bool enable)
61- {
57+ void Adafruit_USBD_HID::enableOutEndpoint (bool enable) {
6258 _out_endpoint = enable;
6359}
6460
65- void Adafruit_USBD_HID::setReportDescriptor (uint8_t const * desc_report, uint16_t len)
66- {
61+ void Adafruit_USBD_HID::setReportDescriptor (uint8_t const * desc_report,
62+ uint16_t len) {
6763 _desc_report = desc_report;
6864 _desc_report_len = len;
6965}
7066
71- void Adafruit_USBD_HID::setReportCallback (get_report_callback_t get_report, set_report_callback_t set_report)
72- {
67+ void Adafruit_USBD_HID::setReportCallback (get_report_callback_t get_report,
68+ set_report_callback_t set_report) {
7369 _get_report_cb = get_report;
7470 _set_report_cb = set_report;
7571}
7672
77- uint16_t Adafruit_USBD_HID::getDescriptor (uint8_t itfnum, uint8_t * buf, uint16_t bufsize)
78- {
79- if ( !_desc_report_len ) return 0 ;
73+ uint16_t Adafruit_USBD_HID::getDescriptor (uint8_t itfnum, uint8_t *buf,
74+ uint16_t bufsize) {
75+ if (!_desc_report_len)
76+ return 0 ;
8077
81- if ( _out_endpoint )
82- {
78+ if (_out_endpoint) {
8379 // usb core will automatically update endpoint number
84- uint8_t desc[] = { TUD_HID_INOUT_DESCRIPTOR (itfnum, 0 , _protocol, _desc_report_len, EPOUT, EPIN, CFG_TUD_HID_BUFSIZE, _interval_ms) };
80+ uint8_t desc[] = {
81+ TUD_HID_INOUT_DESCRIPTOR (itfnum, 0 , _protocol, _desc_report_len, EPOUT,
82+ EPIN, CFG_TUD_HID_BUFSIZE, _interval_ms)};
8583 uint16_t const len = sizeof (desc);
8684
87- if ( bufsize < len ) return 0 ;
85+ if (bufsize < len)
86+ return 0 ;
8887 memcpy (buf, desc, len);
8988
9089 return len;
91- }else
92- {
90+ } else {
9391 // usb core will automatically update endpoint number
94- uint8_t desc[] = { TUD_HID_DESCRIPTOR (itfnum, 0 , _protocol, _desc_report_len, EPIN, CFG_TUD_HID_BUFSIZE, _interval_ms) };
92+ uint8_t desc[] = {TUD_HID_DESCRIPTOR (itfnum, 0 , _protocol, _desc_report_len,
93+ EPIN, CFG_TUD_HID_BUFSIZE,
94+ _interval_ms)};
9595 uint16_t const len = sizeof (desc);
9696
97- if ( bufsize < len ) return 0 ;
97+ if (bufsize < len)
98+ return 0 ;
9899 memcpy (buf, desc, len);
99100
100101 return len;
101102 }
102103}
103104
104- bool Adafruit_USBD_HID::begin (void )
105- {
106- if ( !USBDevice. addInterface (* this ) ) return false ;
105+ bool Adafruit_USBD_HID::begin (void ) {
106+ if (!USBDevice. addInterface (* this ))
107+ return false ;
107108
108109 _hid_dev = this ;
109110 return true ;
110111}
111112
112- bool Adafruit_USBD_HID::ready (void )
113- {
114- return tud_hid_ready ();
115- }
113+ bool Adafruit_USBD_HID::ready (void ) { return tud_hid_ready (); }
116114
117- bool Adafruit_USBD_HID::sendReport (uint8_t report_id, void const * report, uint8_t len)
118- {
115+ bool Adafruit_USBD_HID::sendReport (uint8_t report_id, void const * report,
116+ uint8_t len) {
119117 return tud_hid_report (report_id, report, len);
120118}
121119
122120// ------------- TinyUSB callbacks -------------//
123- extern " C"
124- {
121+ extern " C" {
125122
126123// Invoked when received GET HID REPORT DESCRIPTOR
127- // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
128- uint8_t const * tud_hid_descriptor_report_cb (void )
129- {
130- if (!_hid_dev) return NULL ;
124+ // Application return pointer to descriptor, whose contents must exist long
125+ // enough for transfer to complete
126+ uint8_t const *tud_hid_descriptor_report_cb (void ) {
127+ if (!_hid_dev)
128+ return NULL ;
131129
132130 return _hid_dev->_desc_report ;
133131}
134132
135133// Invoked when received GET_REPORT control request
136134// Application must fill buffer report's content and return its length.
137135// Return zero will cause the stack to STALL request
138- uint16_t tud_hid_get_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t * buffer, uint16_t reqlen)
139- {
140- if ( !(_hid_dev && _hid_dev->_get_report_cb ) ) return 0 ;
136+ uint16_t tud_hid_get_report_cb (uint8_t report_id, hid_report_type_t report_type,
137+ uint8_t *buffer, uint16_t reqlen) {
138+ if (!(_hid_dev && _hid_dev->_get_report_cb ))
139+ return 0 ;
141140
142141 return _hid_dev->_get_report_cb (report_id, report_type, buffer, reqlen);
143142}
144143
145144// Invoked when received SET_REPORT control request or
146145// received data on OUT endpoint ( Report ID = 0, Type = 0 )
147- void tud_hid_set_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t const * buffer, uint16_t bufsize)
148- {
149- if ( !(_hid_dev && _hid_dev->_set_report_cb ) ) return ;
146+ void tud_hid_set_report_cb (uint8_t report_id, hid_report_type_t report_type,
147+ uint8_t const *buffer, uint16_t bufsize) {
148+ if (!(_hid_dev && _hid_dev->_set_report_cb ))
149+ return ;
150150
151151 _hid_dev->_set_report_cb (report_id, report_type, buffer, bufsize);
152152}
@@ -157,56 +157,52 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uin
157157// Keyboard
158158// --------------------------------------------------------------------+
159159
160- bool Adafruit_USBD_HID::keyboardReport (uint8_t report_id, uint8_t modifier, uint8_t keycode[ 6 ])
161- {
160+ bool Adafruit_USBD_HID::keyboardReport (uint8_t report_id, uint8_t modifier,
161+ uint8_t keycode[ 6 ]) {
162162 return tud_hid_keyboard_report (report_id, modifier, keycode);
163163}
164164
165- bool Adafruit_USBD_HID::keyboardPress (uint8_t report_id, char ch)
166- {
167- uint8_t keycode[6 ] = { 0 };
168- uint8_t modifier = 0 ;
165+ bool Adafruit_USBD_HID::keyboardPress (uint8_t report_id, char ch) {
166+ uint8_t keycode[6 ] = {0 };
167+ uint8_t modifier = 0 ;
169168
170- if ( _ascii2keycode[ch][0 ] ) modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
169+ if (_ascii2keycode[ch][0 ])
170+ modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
171171 keycode[0 ] = _ascii2keycode[ch][1 ];
172172
173173 return tud_hid_keyboard_report (report_id, modifier, keycode);
174174}
175175
176- bool Adafruit_USBD_HID::keyboardRelease (uint8_t report_id)
177- {
176+ bool Adafruit_USBD_HID::keyboardRelease (uint8_t report_id) {
178177 return tud_hid_keyboard_report (report_id, 0 , NULL );
179178}
180179
181180// --------------------------------------------------------------------+
182181// Mouse
183182// --------------------------------------------------------------------+
184183
185- bool Adafruit_USBD_HID::mouseReport (uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal)
186- {
184+ bool Adafruit_USBD_HID::mouseReport (uint8_t report_id, uint8_t buttons,
185+ int8_t x, int8_t y, int8_t vertical,
186+ int8_t horizontal) {
187187 // cache mouse button for other API such as move, scroll
188188 _mouse_button = buttons;
189189
190190 return tud_hid_mouse_report (report_id, buttons, x, y, vertical, horizontal);
191191}
192192
193- bool Adafruit_USBD_HID::mouseMove (uint8_t report_id, int8_t x, int8_t y)
194- {
193+ bool Adafruit_USBD_HID::mouseMove (uint8_t report_id, int8_t x, int8_t y) {
195194 return tud_hid_mouse_report (report_id, _mouse_button, x, y, 0 , 0 );
196195}
197196
198- bool Adafruit_USBD_HID::mouseScroll (uint8_t report_id, int8_t scroll, int8_t pan)
199- {
197+ bool Adafruit_USBD_HID::mouseScroll (uint8_t report_id, int8_t scroll,
198+ int8_t pan) {
200199 return tud_hid_mouse_report (report_id, _mouse_button, 0 , 0 , scroll, pan);
201200}
202201
203- bool Adafruit_USBD_HID::mouseButtonPress (uint8_t report_id, uint8_t buttons)
204- {
202+ bool Adafruit_USBD_HID::mouseButtonPress (uint8_t report_id, uint8_t buttons) {
205203 return tud_hid_mouse_report (report_id, buttons, 0 , 0 , 0 , 0 );
206204}
207205
208- bool Adafruit_USBD_HID::mouseButtonRelease (uint8_t report_id)
209- {
206+ bool Adafruit_USBD_HID::mouseButtonRelease (uint8_t report_id) {
210207 return tud_hid_mouse_report (report_id, 0 , 0 , 0 , 0 , 0 );
211208}
212-
0 commit comments