-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIlluminate.cpp
131 lines (118 loc) · 3.2 KB
/
Illuminate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Illuminate.h"
#include "XPLMDataAccess.h"
bool Illuminate::Start() {
// Request control of Corsair devices
CorsairPerformProtocolHandshake();
if (CorsairGetLastError())
{
return false;
}
CorsairRequestControl(CAM_ExclusiveLightingControl);
ReloadConfig();
return true;
}
bool Illuminate::Enable() {
return Start();
}
void Illuminate::Stop() {
CorsairReleaseControl(CAM_ExclusiveLightingControl);
}
void Illuminate::Disable() {
Stop();
}
float Illuminate::FLCB(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void* inRefcon) {
// Evaluate all datarefs.
std::map<std::string, bool> results;
for each (Condition c in config.conditions)
{
if (c.dataRef == NULL) continue;
switch (c.dataType) {
case xplmType_Int:
results[c.dataRefName] = c.Evaluate(XPLMGetDatai(c.dataRef));
break;
case xplmType_IntArray:
int intVal;
XPLMGetDatavi(c.dataRef, &intVal, c.index, 1);
results[c.dataRefName] = c.Evaluate(intVal);
break;
case xplmType_Float:
results[c.dataRefName] = c.Evaluate(XPLMGetDataf(c.dataRef));
break;
case xplmType_FloatArray:
float fVal;
XPLMGetDatavf(c.dataRef, &fVal, c.index, 1);
results[c.dataRefName] = c.Evaluate(fVal);
break;
case xplmType_Double:
results[c.dataRefName] = c.Evaluate(XPLMGetDatad(c.dataRef));
break;
}
}
// Evaluate all keys.
vector<CorsairLedColor> corsairLEDColors;
for each (auto key in config.keys)
{
bool result = true;
for (int i = 0; i < key.conditions.size(); i++)
{
string condition = key.conditions[i];
if (results.find(condition) != results.end())
{
if (!results[condition]) {
result = false;
break;
}
}
}
// We only want to set the key color if all conditions are true.
if (result) {
corsairLEDColors.push_back(
CorsairLedColor{
key.LedId,
key.Color[0],
key.Color[1],
key.Color[2]
});
}
}
// We only want to update colors that have actually changed.
vector<CorsairLedColor> changedColors;
for each (CorsairLedColor color in corsairLEDColors)
{
if (previousColors.find(color.ledId) != previousColors.end()) {
CorsairLedColor previousColor = previousColors[color.ledId];
int r = previousColor.r, g = previousColor.g, b = previousColor.b;
r -= color.r;
g -= color.g;
b -= color.b;
if ((r + g + b) != 0) {
changedColors.push_back(color);
}
}
else {
changedColors.push_back(color);
}
}
// Update LEDs with new colors.
CorsairSetLedsColors(corsairLEDColors.size(), &corsairLEDColors[0]);
// Set previous colors to new colors.
previousColors.clear();
for each(CorsairLedColor key in corsairLEDColors) {
previousColors[key.ledId] = key;
}
return 0.1f;
}
void Illuminate::ReloadConfig() {
config = Config();
config.Load();
setBackgroundColor();
}
void Illuminate::setBackgroundColor() {
CorsairLedPositions* ledPositions = CorsairGetLedPositions();
vector<CorsairLedColor> bgKeys;
for (int ledIndex = 0; ledIndex < ledPositions->numberOfLed; ledIndex++)
{
bgKeys.push_back(CorsairLedColor{ ledPositions->pLedPosition[ledIndex].ledId , config.BackgroundColor[0], config.BackgroundColor[1], config.BackgroundColor[2] });
}
CorsairSetLedsColors(bgKeys.size(), &bgKeys[0]);
}