Corsair Lighting Protocol  0.15.0
Control LEDs connected to an Arduino with iCUE
RawHID.h
1 /*
2 Copyright (c) 2014-2015 NicoHood
3 modified by Leon Kiefer
4 See the readme for credit to other people.
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 THE SOFTWARE.
20 */
21 #pragma once
22 
23 #include "Arduino.h"
24 #include "CorsairLightingProtocolConstants.h"
25 
26 #if defined(ARDUINO_ARCH_AVR)
27 #include "HID.h"
28 #endif
29 
30 #if defined(ARDUINO_ARCH_AVR) && defined(USBCON)
31 #define SUPPORT_RAW_HID
32 #endif
33 
34 #if defined(SUPPORT_RAW_HID)
35 // RawHID might never work with multireports, because of OS problems
36 // therefore we have to make it a single report with no idea. No other HID device will be supported then.
37 #undef RAWHID_USAGE_PAGE
38 #define RAWHID_USAGE_PAGE 0xFFC0 // recommended: 0xFF00 to 0xFFFF
39 
40 #undef RAWHID_USAGE
41 #define RAWHID_USAGE 0x0C00 // recommended: 0x0100 to 0xFFFF
42 
43 #if defined(__AVR_ATmega16U2__)
44 #define RAWHID_TX_SIZE 64
45 #else
46 #define RAWHID_TX_SIZE 16
47 #endif
48 #define RAWHID_RX_SIZE 64
49 
50 #endif
51 
52 #if defined(ARDUINO_ARCH_AVR) && defined(USBCON) // Arduino Core
53 
54 #define EPTYPE_DESCRIPTOR_SIZE uint8_t
55 // HID Functional Characteristics HID1.11 Page 10 4.4 Interfaces
56 // Interrupt Out Endpoint is optional, control endpoint is used by default
57 #define ENDPOINT_COUNT 1
58 namespace CLP {
59 class RawHID_ : public PluggableUSBModule, public Stream {
60 public:
61  RawHID_(void);
67  void setSerialNumber(const char* serialNumber);
68 
69  void setFeatureReport(void* report, int length) {
70  if (length > 0) {
71  featureReport = (uint8_t*)report;
72  featureLength = length;
73 
74  // Disable feature report by default
75  disableFeatureReport();
76  }
77  }
78 
79  int availableFeatureReport(void) {
80  if (featureLength < 0) {
81  return featureLength & ~0x8000;
82  }
83  return 0;
84  }
85 
86  void enableFeatureReport(void) { featureLength &= ~0x8000; }
87 
88  void disableFeatureReport(void) { featureLength |= 0x8000; }
89 
90  void begin(void* report, int length) {
91  if (length > 0) {
92  data = (uint8_t*)report;
93  dataLength = length;
94  dataAvailable = 0;
95  }
96  }
97 
98  void end(void) {
99  disable();
100  dataLength = 0;
101  }
102 
103  void enable(void) { dataAvailable = 0; }
104 
105  void disable(void) { dataAvailable = -1; }
106 
107  virtual int available(void) {
108  if (dataAvailable < 0) {
109  return 0;
110  }
111  return dataAvailable;
112  }
113 
114  virtual int read() {
115  // Check if we have data available
116  if (dataAvailable > 0) {
117  // Get next data byte (from the start to the end)
118  return data[dataLength - dataAvailable--];
119  }
120  return -1;
121  }
122 
123  virtual int peek() {
124  // Check if we have data available
125  if (dataAvailable > 0) {
126  return data[dataLength - dataAvailable];
127  }
128  return -1;
129  }
130 
131  virtual void flush(void) {
132  // Writing will always flush by the USB driver
133  }
134 
138  virtual size_t write(uint8_t b) { return write(&b, 1); }
139 
140  virtual size_t write(const uint8_t* buffer, size_t size) {
141  return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, buffer, size);
142  }
143 
144 protected:
145  // Implementation of the PUSBListNode
146  int getInterface(uint8_t* interfaceCount) override;
147  int getDescriptor(USBSetup& setup) override;
148  bool setup(USBSetup& setup) override;
149  uint8_t getShortName(char* name) override;
150 
151  EPTYPE_DESCRIPTOR_SIZE epType[ENDPOINT_COUNT];
152  uint8_t protocol;
153  uint8_t idle;
154 
155  // Buffer pointers to hold the received data
156  int dataLength;
157  int dataAvailable;
158  uint8_t* data;
159  const char* serialNumber;
160 
161  uint8_t* featureReport;
162  int featureLength;
163 };
164 extern RawHID_ RawHID;
165 } // namespace CLP
166 #endif
Definition: RawHID.h:59
virtual size_t write(uint8_t b)
Definition: RawHID.h:138
void setSerialNumber(const char *serialNumber)
Definition: RawHID.cpp:67