Skip to content

Commit d2cb5cc

Browse files
committed
Reduce unnecessary indentation to improve readability.
1 parent 6a43678 commit d2cb5cc

File tree

1 file changed

+54
-65
lines changed

1 file changed

+54
-65
lines changed

src/HID/HID.cpp

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,44 @@ int HID_::getInterface(uint8_t* interfaceCount)
3434
D_INTERFACE(pluggedInterface, 2, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
3535
D_HIDREPORT(descriptorSize),
3636
D_ENDPOINT(USB_ENDPOINT_IN(HID_TX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x14),
37-
D_ENDPOINT(USB_ENDPOINT_OUT(HID_RX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x0A)
37+
D_ENDPOINT(USB_ENDPOINT_OUT(HID_RX), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x0A)
3838
};
3939
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
4040
}
4141

4242
// Since this function is not exposed in USBCore API, had to replicate here.
4343
static bool USB_SendStringDescriptor(const char* string_P, u8 string_len, uint8_t flags) {
44+
u8 c[2] = {(u8)(2 + string_len * 2), 3};
4445

45-
u8 c[2] = {(u8)(2 + string_len * 2), 3};
46+
USB_SendControl(0,&c,2);
4647

47-
USB_SendControl(0,&c,2);
48-
49-
bool pgm = flags & TRANSFER_PGM;
50-
for(u8 i = 0; i < string_len; i++) {
51-
c[0] = pgm ? pgm_read_byte(&string_P[i]) : string_P[i];
52-
c[1] = 0;
53-
int r = USB_SendControl(0,&c,2);
54-
if(!r) {
55-
return false;
56-
}
57-
}
58-
return true;
48+
bool pgm = flags & TRANSFER_PGM;
49+
for(u8 i = 0; i < string_len; i++) {
50+
c[0] = pgm ? pgm_read_byte(&string_P[i]) : string_P[i];
51+
c[1] = 0;
52+
int r = USB_SendControl(0,&c,2);
53+
if(!r) {
54+
return false;
55+
}
56+
}
57+
return true;
5958
}
6059

6160
int HID_::getDescriptor(USBSetup& setup)
6261
{
63-
64-
u8 t = setup.wValueH;
65-
66-
// HID-specific strings
67-
if(USB_STRING_DESCRIPTOR_TYPE == t) {
68-
69-
// we place all strings in the 0xFF00-0xFFFE range
70-
HIDReport* rep = GetFeature(0xFF00 | setup.wValueL );
71-
if(rep) {
72-
return USB_SendStringDescriptor((char*)rep->data, strlen_P((char*)rep->data), TRANSFER_PGM);
73-
}
74-
else {
75-
return 0;
76-
}
62+
u8 t = setup.wValueH;
63+
64+
// HID-specific strings
65+
if(USB_STRING_DESCRIPTOR_TYPE == t) {
66+
// we place all strings in the 0xFF00-0xFFFE range
67+
HIDReport* rep = GetFeature(0xFF00 | setup.wValueL );
68+
if(rep) {
69+
return USB_SendStringDescriptor((char*)rep->data, strlen_P((char*)rep->data), TRANSFER_PGM);
7770
}
71+
else {
72+
return 0;
73+
}
74+
}
7875

7976
// Check if this is a HID Class Descriptor request
8077
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
@@ -108,15 +105,13 @@ uint8_t HID_::getShortName(char *name)
108105
return strlen_P(serial);
109106
}
110107
else {
111-
112108
// default serial number
113-
114-
name[0] = 'H';
115-
name[1] = 'I';
116-
name[2] = 'D';
117-
name[3] = 'A' + (descriptorSize & 0x0F);
118-
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
119-
return 5;
109+
name[0] = 'H';
110+
name[1] = 'I';
111+
name[2] = 'D';
112+
name[3] = 'A' + (descriptorSize & 0x0F);
113+
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
114+
return 5;
120115
}
121116
}
122117

@@ -151,7 +146,6 @@ int HID_::SetFeature(uint16_t id, const void* data, int len)
151146
break;
152147
}
153148
}
154-
155149
}
156150

157151
reportCount++;
@@ -205,20 +199,17 @@ bool HID_::setup(USBSetup& setup)
205199
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
206200
{
207201
if (request == HID_GET_REPORT) {
202+
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
203+
{
204+
HIDReport* current = GetFeature(setup.wValueL);
205+
if(current){
206+
if(USB_SendControl(0, &(current->id), 1)>0 &&
207+
USB_SendControl(0, current->data, current->length)>0)
208+
return true;
209+
}
208210

209-
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
210-
{
211-
212-
HIDReport* current = GetFeature(setup.wValueL);
213-
if(current){
214-
if(USB_SendControl(0, &(current->id), 1)>0 &&
215-
USB_SendControl(0, current->data, current->length)>0)
216-
return true;
217-
}
218-
219-
return false;
220-
221-
}
211+
return false;
212+
}
222213
return true;
223214
}
224215
if (request == HID_GET_PROTOCOL) {
@@ -244,21 +235,19 @@ bool HID_::setup(USBSetup& setup)
244235
}
245236
if (request == HID_SET_REPORT)
246237
{
247-
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
248-
{
249-
250-
HIDReport* current = GetFeature(setup.wValueL);
251-
if(!current) return false;
252-
if(setup.wLength != current->length + 1) return false;
253-
uint8_t* data = new uint8_t[setup.wLength];
254-
USB_RecvControl(data, setup.wLength);
255-
if(*data != current->id) return false;
256-
memcpy((uint8_t*)current->data, data+1, current->length);
257-
delete[] data;
258-
return true;
259-
260-
}
261-
238+
if(setup.wValueH == HID_REPORT_TYPE_FEATURE)
239+
{
240+
241+
HIDReport* current = GetFeature(setup.wValueL);
242+
if(!current) return false;
243+
if(setup.wLength != current->length + 1) return false;
244+
uint8_t* data = new uint8_t[setup.wLength];
245+
USB_RecvControl(data, setup.wLength);
246+
if(*data != current->id) return false;
247+
memcpy((uint8_t*)current->data, data+1, current->length);
248+
delete[] data;
249+
return true;
250+
}
262251
}
263252
}
264253

0 commit comments

Comments
 (0)