Skip to content

Commit b06cfb6

Browse files
committed
Refactor code to local function in rgb timer to cleanup main loop
1 parent 46f62ac commit b06cfb6

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

Source/TTController.Service/TTService.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -301,40 +301,14 @@ private bool DeviceSpeedTimerCallback()
301301

302302
public bool DeviceRgbTimerCallback()
303303
{
304-
foreach (var profile in _configManager.CurrentConfig.Profiles)
304+
void ApplyConfig(IDictionary<PortIdentifier, List<LedColor>> colorMap)
305305
{
306-
var effects = _effectManager.GetEffects(profile.Guid);
307-
var effect = effects?.FirstOrDefault(e => e.IsEnabled(_cache.AsReadOnly()));
308-
if (effect == null)
309-
continue;
310-
311-
IDictionary<PortIdentifier, List<LedColor>> colorMap;
312-
string effectType;
313-
314-
try
315-
{
316-
colorMap = effect.GenerateColors(profile.Ports, _cache.AsReadOnly());
317-
effectType = effect.EffectType;
318-
}
319-
catch (Exception e)
320-
{
321-
Logger.Fatal("{0} failed with {1}", effect.GetType().Name, e);
322-
colorMap = profile.Ports.ToDictionary(p => p, _ => new List<LedColor>() { new LedColor(255, 0, 0) } );
323-
effectType = "Full";
324-
}
325-
326-
if (colorMap == null)
327-
continue;
328-
329-
foreach (var port in profile.Ports)
306+
foreach (var port in colorMap.Keys.ToList())
330307
{
331308
var config = _cache.GetPortConfig(port);
332309
if (config == null)
333310
continue;
334311

335-
if (!colorMap.ContainsKey(port))
336-
continue;
337-
338312
var colors = colorMap[port];
339313

340314
if (config.LedRotation > 0)
@@ -345,19 +319,19 @@ public bool DeviceRgbTimerCallback()
345319
switch (config.LedCountHandling)
346320
{
347321
case LedCountHandling.Lerp:
348-
{
349-
if (config.LedCount == colors.Count)
350-
break;
322+
{
323+
if (config.LedCount == colors.Count)
324+
break;
351325

352-
var newColors = new List<LedColor>();
353-
var gradient = new LedColorGradient(colors, config.LedCount - 1);
326+
var newColors = new List<LedColor>();
327+
var gradient = new LedColorGradient(colors, config.LedCount - 1);
354328

355-
for (var i = 0; i < config.LedCount; i++)
356-
newColors.Add(gradient.GetColor(i));
329+
for (var i = 0; i < config.LedCount; i++)
330+
newColors.Add(gradient.GetColor(i));
357331

358-
colors = newColors;
359-
break;
360-
}
332+
colors = newColors;
333+
break;
334+
}
361335
case LedCountHandling.Trim:
362336
if (config.LedCount < colors.Count)
363337
colors.RemoveRange(config.LedCount, colors.Count - config.LedCount);
@@ -373,6 +347,34 @@ public bool DeviceRgbTimerCallback()
373347

374348
colorMap[port] = colors;
375349
}
350+
}
351+
352+
foreach (var profile in _configManager.CurrentConfig.Profiles)
353+
{
354+
var effects = _effectManager.GetEffects(profile.Guid);
355+
var effect = effects?.FirstOrDefault(e => e.IsEnabled(_cache.AsReadOnly()));
356+
if (effect == null)
357+
continue;
358+
359+
IDictionary<PortIdentifier, List<LedColor>> colorMap;
360+
string effectType;
361+
362+
try
363+
{
364+
colorMap = effect.GenerateColors(profile.Ports, _cache.AsReadOnly());
365+
effectType = effect.EffectType;
366+
}
367+
catch (Exception e)
368+
{
369+
Logger.Fatal("{0} failed with {1}", effect.GetType().Name, e);
370+
colorMap = profile.Ports.ToDictionary(p => p, _ => new List<LedColor>() { new LedColor(255, 0, 0) });
371+
effectType = "Full";
372+
}
373+
374+
if (colorMap == null)
375+
continue;
376+
377+
ApplyConfig(colorMap);
376378

377379
lock (_deviceManager)
378380
{

0 commit comments

Comments
 (0)