RSeries astromech firmware
CH559USBPort.h
Go to the documentation of this file.
1 #ifndef CH559USBPort_h
2 #define CH559USBPort_h
3 
4 #include "ReelTwo.h"
5 
7 {
8 public:
9  CH559USBPort(HardwareSerial& serial) :
10  fSerial(&serial)
11  {
12  }
13 
14  void process()
15  {
16  while (fSerial != NULL && fSerial->available())
17  {
18  delay(2);
19  fUARTRxBuff[fRXPos] = fSerial->read();
20  // DEBUG_PRINT_HEX(fUARTRxBuff[fRXPos]);
21  if (fRXPos == 0 && fUARTRxBuff[fRXPos] == 0xFE)
22  {
23  fCmdType = 1;
24  }
25  else if (fRXPos == 1 && fCmdType == 1)
26  {
27  fCmdLength = fUARTRxBuff[fRXPos];
28  }
29  else if (fRXPos == 2 && fCmdType == 1)
30  {
31  fCmdLength += (fUARTRxBuff[fRXPos] << 8);
32  //printf( "Length: %i\n", fCmdLength);//Only for Debug
33  }
34  else if (fCmdType == 0 && fUARTRxBuff[fRXPos] == '\n')
35  {
36  DEBUG_PRINTLN("No COMMAND Received");
37  for (uint8_t i = 0; i < fRXPos; i ++ )
38  {
39  DEBUG_PRINT_HEX(fUARTRxBuff[i]);
40  DEBUG_PRINT(" ");
41  }
42  DEBUG_PRINTLN();
43  fRXPos = 0;
44  fCmdType = 0;
45  }
46  if ((fRXPos > 0 && fRXPos == fCmdLength + 11 && fCmdType) || (fRXPos > 1024))
47  {
48  filterCommand(fCmdLength, fUARTRxBuff);
49  fRXPos = 0;
50  fCmdType = 0;
51  }
52  else
53  {
54  fRXPos++;
55  }
56  }
57  fRXPos = 0;
58  }
59 
60  const uint8_t* getBTAddress()
61  {
62  return (fBTReceived) ? fBTAddress : NULL;
63  }
64 
65 private:
66  enum
67  {
68  MSG_TYPE_CONNECTED = 0x01,
69  MSG_TYPE_DISCONNECTED = 0x02,
70  MSG_TYPE_ERROR = 0x03,
71  MSG_TYPE_DEVICE_POLL = 0x04,
72  MSG_TYPE_DEVICE_STRING = 0x05,
73  MSG_TYPE_DEVICE_INFO = 0x06,
74  MSG_TYPE_HID_INFO = 0x07,
75  MSG_TYPE_STARTUP = 0x08,
76  MSG_TYPE_BTADDRESS = 0x09
77  };
78 
79  void filterCommand(int buffLength, unsigned char *msgbuffer)
80  {
81  static const char* deviceType[] = {"UNKNOWN", "POINTER", "MOUSE", "RESERVED", "JOYSTICK", "GAMEPAD", "KEYBOARD", "KEYPAD", "MULTI_AXIS", "SYSTEM"};
82 
83  int cmdLength = buffLength;
84  unsigned char msgType = msgbuffer[3];
85  unsigned char devType = msgbuffer[4];
86  unsigned char device = msgbuffer[5];
87  unsigned char endpoint = msgbuffer[6];
88  unsigned char idVendorL = msgbuffer[7];
89  unsigned char idVendorH = msgbuffer[8];
90  unsigned char idProductL = msgbuffer[9];
91  unsigned char idProductH = msgbuffer[10];
92  switch (msgType)
93  {
94  case MSG_TYPE_CONNECTED:
95  DEBUG_PRINT("Device Connected on port");
96  DEBUG_PRINTLN(device);
97  break;
98  case MSG_TYPE_DISCONNECTED:
99  DEBUG_PRINT("Device Disconnected on port");
100  DEBUG_PRINTLN(device);
101  break;
102  case MSG_TYPE_ERROR:
103  DEBUG_PRINT("Device Error ");
104  DEBUG_PRINT(device);
105  DEBUG_PRINT(" on port ");
106  DEBUG_PRINTLN(devType);
107  break;
108  case MSG_TYPE_DEVICE_POLL:
109  DEBUG_PRINT("Device HID Data from port: ");
110  DEBUG_PRINT(device);
111  DEBUG_PRINT(" , Length: ");
112  DEBUG_PRINT(cmdLength);
113  DEBUG_PRINT(" , Type: ");
114  DEBUG_PRINT (deviceType[devType]);
115  DEBUG_PRINT(" , ID: ");
116  for (int j = 0; j < 4; j++)
117  {
118  DEBUG_PRINT("0x");
119  DEBUG_PRINT_HEX(msgbuffer[j + 7]);
120  DEBUG_PRINT(" ");
121  }
122  DEBUG_PRINT(" , ");
123  for (int j = 0; j < cmdLength; j++)
124  {
125  DEBUG_PRINT("0x");
126  DEBUG_PRINT_HEX(msgbuffer[j + 11]);
127  DEBUG_PRINT(" ");
128  }
129  DEBUG_PRINTLN();
130  break;
131  case MSG_TYPE_DEVICE_STRING:
132  DEBUG_PRINT("Device String port ");
133  DEBUG_PRINT(devType);
134  DEBUG_PRINT(" Name: ");
135  for (int j = 0; j < cmdLength; j++)
136  DEBUG_PRINT((char)msgbuffer[j + 11]);
137  DEBUG_PRINTLN();
138  break;
139  case MSG_TYPE_DEVICE_INFO:
140  DEBUG_PRINT("Device info from port");
141  DEBUG_PRINT(device);
142  DEBUG_PRINT(", Descriptor: ");
143  for (int j = 0; j < cmdLength; j++)
144  {
145  DEBUG_PRINT("0x");
146  DEBUG_PRINT_HEX(msgbuffer[j + 11]);
147  DEBUG_PRINT(" ");
148  }
149  DEBUG_PRINTLN();
150  break;
151  case MSG_TYPE_HID_INFO:
152  DEBUG_PRINT("HID info from port");
153  DEBUG_PRINT(device);
154  DEBUG_PRINT(", Descriptor: ");
155  for (int j = 0; j < cmdLength; j++)
156  {
157  DEBUG_PRINT("0x");
158  DEBUG_PRINT_HEX(msgbuffer[j + 11]);
159  DEBUG_PRINT(" ");
160  }
161  DEBUG_PRINTLN();
162  break;
163  case MSG_TYPE_STARTUP:
164  DEBUG_PRINTLN("USB host ready");
165  break;
166  case MSG_TYPE_BTADDRESS:
167  DEBUG_PRINTLN("BT ADDRESS: ");
168  for (int j = 0; j < cmdLength; j++)
169  {
170  DEBUG_PRINT("0x");
171  if (j < sizeof(fBTAddress))
172  fBTAddress[j] = msgbuffer[j + 11];
173  DEBUG_PRINT_HEX(fBTAddress[j]);
174  DEBUG_PRINT(" ");
175  }
176  fBTReceived = true;
177  break;
178  }
179  }
180 
181 private:
182  Stream* fSerial;
183  uint8_t fUARTRxBuff[1024];
184  unsigned fRXPos = 0;
185  unsigned fCmdLength = 0;
186  uint8_t fCmdType = 0;
187  uint8_t fBTAddress[6];
188  bool fBTReceived = false;
189 };
190 #endif
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
ReelTwo.h
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
CH559USBPort::getBTAddress
const uint8_t * getBTAddress()
Definition: CH559USBPort.h:60
CH559USBPort
Definition: CH559USBPort.h:6
DEBUG_PRINT_HEX
#define DEBUG_PRINT_HEX(s)
Definition: ReelTwo.h:192
CH559USBPort::CH559USBPort
CH559USBPort(HardwareSerial &serial)
Definition: CH559USBPort.h:9
CH559USBPort::process
void process()
Definition: CH559USBPort.h:14