-
Notifications
You must be signed in to change notification settings - Fork 4
/
LogiLED.cpp
204 lines (168 loc) · 5.29 KB
/
LogiLED.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
File: LogiLED.cpp
Created on: 21/11/2018
Author: Felix de las Pozas Alvarez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Project
#include <LogiLED.h>
// Logitech gaming SDK
extern "C"
{
#include <LogitechLEDLib.h>
}
// Qt
#include <QTimer>
#include <QReadLocker>
#include <QWriteLocker>
#include <QListIterator>
// C++
#include <chrono>
#include <thread>
const QList<LogiLed::KeyName> KEYS { LogiLed::F1, LogiLed::F2, LogiLed::F3, LogiLed::F4, LogiLed::F5, LogiLed::F6, LogiLed::F7,
LogiLed::F8, LogiLed::F9, LogiLed::F10, LogiLed::F11, LogiLed::F12, LogiLed::PRINT_SCREEN,
LogiLed::SCROLL_LOCK, LogiLed::PAUSE_BREAK };
using namespace LogiLed;
//--------------------------------------------------------------------
LogiLED::LogiLED()
: m_available{LogiLedInitWithName("MultiAlarm")}
, m_current {0}
{
if(m_available)
{
LogiLedSetTargetDevice(LOGI_DEVICETYPE_PERKEY_RGB);
}
}
//--------------------------------------------------------------------
LogiLED::~LogiLED()
{
LogiLedShutdown();
}
//--------------------------------------------------------------------
LogiLED& LogiLED::getInstance()
{
static LogiLED instance;
return instance;
}
//--------------------------------------------------------------------
bool LogiLED::isAvailable()
{
return getInstance().isInitialized();
}
//--------------------------------------------------------------------
bool LogiLED::registerItem(const QString& id, const int progress, const QColor& foreground, const QColor& background)
{
if(m_available)
{
const struct Item item{id, progress, foreground, background};
int currentItemsSize = 0;
{
QWriteLocker lock(&m_lock);
currentItemsSize = m_items.size();
if(!m_items.contains(item))
{
m_items << item;
}
}
// only start updating lights when inserting the first
if (currentItemsSize == 0)
{
updateLights();
}
return true;
}
return false;
}
//--------------------------------------------------------------------
bool LogiLED::unregisterItem(const QString& id)
{
if(m_available)
{
QWriteLocker lock(&m_lock);
for(auto &item: m_items)
{
if(item.id == id)
{
m_items.removeAll(item);
if(m_items.isEmpty())
{
restart();
}
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------
bool LogiLED::updateItem(const QString& id, const int progress, const QColor& foreground, const QColor& background)
{
if(m_available)
{
QWriteLocker lock(&m_lock);
for(auto &item: m_items)
{
if(item.id == id)
{
item.progress = progress;
if(foreground != QColor()) item.foreground = foreground;
if(background != QColor()) item.background = background;
if(m_current == m_items.indexOf(item) || item.id == "NewAlarm")
{
changeKeysToColor(item.progress, item.foreground, item.background);
}
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------
void LogiLED::updateLights()
{
QReadLocker lock(&m_lock);
if(m_available && !m_items.isEmpty())
{
++m_current;
m_current = m_current % m_items.size();
auto &item = m_items.at(m_current);
changeKeysToColor(item.progress, item.foreground, item.background);
QTimer::singleShot(5000, this, SLOT(updateLights()));
}
}
//--------------------------------------------------------------------
void LogiLED::restart() const
{
if(m_available)
{
// Apparently this is the only way to restore the default profile of the user, as there is no way
// in the Logitech SDK to get the keys color (for us to store state) and the methods in the SDK
// to store that info just don't work.
LogiLedShutdown();
LogiLedInitWithName("MultiAlarm");
}
}
//--------------------------------------------------------------------
void LogiLED::changeKeysToColor(const int progress, const QColor foreground, const QColor background)
{
const double KEY_PERCENT = 100./KEYS.size();
double dProgress = progress;
for(auto it = KEYS.begin(); it != KEYS.end(); ++it, dProgress -= KEY_PERCENT)
{
auto key = *it;
if(dProgress < 0) dProgress = 0;
auto percent = dProgress > KEY_PERCENT ? 1 : dProgress/KEY_PERCENT;
LogiLedSetLightingForKeyWithKeyName(key, 100 * (foreground.redF() * percent + background.redF() * (1-percent)),
100 * (foreground.greenF() * percent + background.greenF() * (1-percent)),
100 * (foreground.blueF() * percent + background.blueF() * (1-percent)));
}
}