Corsair Lighting Protocol  0.15.0
Control LEDs connected to an Arduino with iCUE
CLPUtils.h
1 /*
2  Copyright 2019 Leon Kiefer
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 #pragma once
17 
18 #include "Arduino.h"
19 #include "CorsairLightingFirmware.h"
20 
21 #define toBigEndian(a) highByte(a), lowByte(a)
22 
23 #if CLP_DEBUG
24 
25 #define CLP_STRCAT(a, b) a##b
26 #define CLP_STRCAT3(a, b, c) a##b##c
27 
28 #define CLP_XSTRCAT(a, b) CLP_STRCAT(a, b)
29 #define CLP_XSTRCAT3(a, b, c) CLP_STRCAT3(a, b, c)
30 
31 #if defined(CLP_DEBUG_PORT)
32 namespace CLP {
33 extern int printf(const char* format, ...);
34 extern int printf(const __FlashStringHelper* format, ...);
35 } // namespace CLP
36 #define clpPrintf CLP::printf
37 #else
38 #define clpPrintf printf
39 #endif
40 
41 namespace CLP {
42 static inline void printVar(uint8_t const* buf, uint32_t bufsize) {
43  for (uint32_t i = 0; i < bufsize; i++) clpPrintf("%02X ", buf[i]);
44 }
45 extern void printData(uint8_t const* buf, uint32_t bufsize, bool address_table);
46 } // namespace CLP
47 
48 /* clang-format off */
49 #define CLP_LOG(n, ...) CLP_XSTRCAT(CLP_LOG, n)(__VA_ARGS__)
50 #define CLP_LOG_VAR(n, ...) CLP_XSTRCAT3(CLP_LOG, n, _VAR)(__VA_ARGS__)
51 #define CLP_LOG_HEX(n, ...) CLP_XSTRCAT3(CLP_LOG, n, _HEX)(__VA_ARGS__)
52 #define CLP_LOG_DAT(n, buf, size, at) CLP_XSTRCAT3(CLP_LOG, n, _DAT)(buf, size, at)
53 #define CLP_LOG_LOCATION() clpPrintf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
54 #define CLP_LOG_FAILED() clpPrintf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
55 #define CLP_LOG_FUNC(func) func
56 
57 // Log Level 1: Error
58 #define CLP_LOG1 clpPrintf
59 #define CLP_LOG1_VAR(_x) CLP::printVar((uint8_t const*)(_x), sizeof(*(_x)))
60 #define CLP_LOG1_HEX(_x) clpPrintf(#_x " = %lX\r\n", (unsigned long) (_x) )
61 #define CLP_LOG1_DAT(_x, _y, _z) CLP::printData((uint8_t const*)(_x), _y, _z)
62 
63 // Log Level 2: Warn
64 #if CLP_DEBUG >= 2
65 #define CLP_LOG2 CLP_LOG1
66 #define CLP_LOG2_VAR CLP_LOG1_VAR
67 #define CLP_LOG2_HEX CLP_LOG1_HEX
68 #define CLP_LOG2_DAT CLP_LOG1_DAT
69 #endif
70 
71 // Log Level 3: Info
72 #if CLP_DEBUG >= 3
73 #define CLP_LOG3 CLP_LOG1
74 #define CLP_LOG3_VAR CLP_LOG1_VAR
75 #define CLP_LOG3_HEX CLP_LOG1_HEX
76 #define CLP_LOG3_DAT CLP_LOG1_DAT
77 #endif
78 
79 // Log Level 4: Data
80 #if CLP_DEBUG >= 4
81 #define CLP_LOG4 CLP_LOG1
82 #define CLP_LOG4_VAR CLP_LOG1_VAR
83 #define CLP_LOG4_HEX CLP_LOG1_HEX
84 #define CLP_LOG4_DAT CLP_LOG1_DAT
85 #endif
86 /* clang-format on */
87 
88 #endif // CLP_DEBUG
89 
90 #ifndef CLP_LOG
91 #define CLP_LOG(n, ...)
92 #define CLP_LOG_VAR(n, ...)
93 #define CLP_LOG_HEX(n, ...)
94 #define CLP_LOG_DAT(n, buf, size, at)
95 #define CLP_LOG_FUNC(n)
96 #endif
97 
98 #define CLP_LOG0(...)
99 #define CLP_LOG0_VAR(...)
100 #define CLP_LOG0_HEX(...)
101 #define CLP_LOG0_DAT(...)
102 
103 #ifndef CLP_LOG1
104 #define CLP_LOG1(...)
105 #define CLP_LOG1_VAR(...)
106 #define CLP_LOG1_HEX(...)
107 #define CLP_LOG1_DAT(...)
108 #endif
109 
110 #ifndef CLP_LOG2
111 #define CLP_LOG2(...)
112 #define CLP_LOG2_VAR(...)
113 #define CLP_LOG2_HEX(...)
114 #define CLP_LOG2_DAT(...)
115 #endif
116 
117 #ifndef CLP_LOG3
118 #define CLP_LOG3(...)
119 #define CLP_LOG3_VAR(...)
120 #define CLP_LOG3_HEX(...)
121 #define CLP_LOG3_DAT(...)
122 #endif
123 
124 #ifndef CLP_LOG4
125 #define CLP_LOG4(...)
126 #define CLP_LOG4_VAR(...)
127 #define CLP_LOG4_HEX(...)
128 #define CLP_LOG4_DAT(...)
129 #endif
130 
131 namespace CLP {
132 
133 template <typename T>
134 void swap(T a, T b) {
135  auto temp = *a;
136  *a = *b;
137  *b = temp;
138 }
139 
140 template <typename T>
141 void reverse(T first, T last) {
142  while ((first != last) && (first != --last)) {
143  CLP::swap(first++, last);
144  }
145 }
146 
147 template <typename T>
148 void rotate(T first, T n_first, T last) {
149  if (first == n_first) return;
150  if (n_first == last) return;
151 
152  T read = n_first;
153  T write = first;
154  T next_read = first;
155 
156  while (read != last) {
157  if (write == next_read) next_read = read;
158  CLP::swap(read++, write++);
159  }
160 
161  (rotate)(write, next_read, last);
162  return;
163 }
164 
165 uint16_t fromBigEndian(const byte& byte1, const byte& byte2);
166 
172 inline uint8_t convert100To255(uint8_t value) { return value * 2.5546875f; }
173 
179 inline uint8_t convert255To100(uint8_t value) { return value / 2.5546875f; }
180 
186 bool isNullID(const DeviceID& deviceId);
187 
193 bool isResetID(const DeviceID& deviceId);
194 
198 void disableBuildInLEDs();
199 
205 void printDeviceID(const DeviceID& deviceId);
206 
207 /*
208  * Measure and print the framerate at the given interval in milliseconds. The higher this value the more precise the
209  * result will be. This function should be called after FastLED.show() to count the FPS.
210  *
211  * @param interval the measurement interval in milliseconds
212  */
213 void printFps(const int interval);
214 } // namespace CLP
Definition: CorsairLightingFirmware.h:40