USB Host Shield 2.0
hiduniversal.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 
18 #include "hiduniversal.h"
19 
20 uint8_t HIDUniversal::Poll() {
21  uint8_t rcode = 0;
22 
23  if(!bPollEnable)
24  return 0;
25 
26  if((int32_t)((uint32_t)millis() - qNextPollTime) >= 0L) {
27  qNextPollTime = (uint32_t)millis() + pollInterval;
28 
29  uint8_t buf[constBuffLen];
30 
31  for(uint8_t i = 0; i < bNumIface; i++) {
32  uint8_t index = hidInterfaces[i].epIndex[epInterruptInIndex];
33  uint16_t read = (uint16_t)epInfo[index].maxPktSize;
34 
36 
37  uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[index].epAddr, &read, buf);
38 
39  if(rcode) {
40  if(rcode != hrNAK)
41  USBTRACE3("(hiduniversal.h) Poll:", rcode, 0x81);
42  return rcode;
43  }
44 
45  if(read > constBuffLen)
46  read = constBuffLen;
47 
48  // TODO: handle read == 0 ? continue like HIDComposite,
49  // return early like in the error case above?
50  // Either way passing an empty buffer to the functions below
51  // probably makes no sense?
52 
53 #if 0
54  Notify(PSTR("\r\nBuf: "), 0x80);
55 
56  for(uint8_t i = 0; i < read; i++) {
57  D_PrintHex<uint8_t > (buf[i], 0x80);
58  Notify(PSTR(" "), 0x80);
59  }
60 
61  Notify(PSTR("\r\n"), 0x80);
62 #endif
63  ParseHIDData(this, bHasReportId, (uint8_t)read, buf);
64 
65  HIDReportParser *prs = GetReportParser(((bHasReportId) ? *buf : 0));
66 
67  if(prs)
68  prs->Parse(this, bHasReportId, (uint8_t)read, buf);
69  }
70  }
71  return rcode;
72 }
uint32_t qNextPollTime
Definition: hidcomposite.h:51
HIDReportParser * GetReportParser(uint8_t id)
static const uint16_t constBuffLen
Definition: hidcomposite.h:55
HIDInterface hidInterfaces[maxHidInterfaces]
Definition: hidcomposite.h:64
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:63
bool bPollEnable
Definition: hidcomposite.h:53
void ZeroMemory(uint8_t len, uint8_t *buf)
bool bHasReportId
Definition: hidcomposite.h:69
uint8_t bNumIface
Definition: hidcomposite.h:49
uint8_t pollInterval
Definition: hidcomposite.h:52
virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)=0
uint8_t Poll() override
uint8_t bAddress
Definition: usbhid.h:146
static const uint8_t epInterruptInIndex
Definition: usbhid.h:149
USB * pUsb
Definition: usbhid.h:145
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition: Usb.cpp:209
@ L
#define USBTRACE3(s, r, l)
Definition: macros.h:85
#define hrNAK
Definition: max3421e.h:218
#define Notify(...)
Definition: message.h:51
uint8_t epIndex[maxEpPerInterface+1]
Definition: hidcomposite.h:45
#define PSTR(str)