Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-wh committed Jul 30, 2018
2 parents 7f3a983 + 86baa61 commit 44f1f22
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 112 deletions.
35 changes: 15 additions & 20 deletions Project-Aurora/Project-Aurora/Devices/AtmoOrb/AtmoOrbDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Aurora.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void Shutdown()
sw.Stop();
}

public void Connect(CancellationToken? token = null)
public void Connect(DoWorkEventArgs token = null)
{
try
{
Expand All @@ -128,20 +129,14 @@ public void Connect(CancellationToken? token = null)
var multiCastIp = IPAddress.Parse("239.15.18.2");
var port = 49692;

token?.ThrowIfCancellationRequested();

socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
ipClientEndpoint = new IPEndPoint(multiCastIp, port);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(multiCastIp));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);

token?.ThrowIfCancellationRequested();

socket.Connect(ipClientEndpoint);

token?.ThrowIfCancellationRequested();

isConnected = true;
isConnecting = false;
}
Expand All @@ -152,23 +147,23 @@ public void Connect(CancellationToken? token = null)
}
}

public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationToken token, bool forced = false)
public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

watch.Restart();

// Connect if needed
if (!isConnected)
Connect(token);
Connect(e);

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

// Reduce sending based on user config
if (!sw.IsRunning)
sw.Start();

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

if (sw.ElapsedMilliseconds >
Global.Configuration.VarRegistry.GetVariable<int>($"{devicename}_send_delay"))
Expand All @@ -188,26 +183,26 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationTo
}
}

SendColorsToOrb(averageColor.R, averageColor.G, averageColor.B, token);
SendColorsToOrb(averageColor.R, averageColor.G, averageColor.B, e);
sw.Restart();
}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

watch.Stop();
lastUpdateTime = watch.ElapsedMilliseconds;

return true;
}

public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationToken token, bool forced = false)
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false)
{
throw new NotImplementedException();
}

public void SendColorsToOrb(byte red, byte green, byte blue, CancellationToken? token = null)
public void SendColorsToOrb(byte red, byte green, byte blue, DoWorkEventArgs e = null)
{
if (token?.IsCancellationRequested ?? false) return;
if (e?.Cancel ?? false) return;
if (!isConnected)
{
Reconnect();
Expand All @@ -225,11 +220,11 @@ public void SendColorsToOrb(byte red, byte green, byte blue, CancellationToken?
orbIDs = new List<string>() { "1" };
}

if (token?.IsCancellationRequested ?? false) return;
if (e?.Cancel ?? false) return;

foreach (var orbID in orbIDs)
{
if (token?.IsCancellationRequested ?? false) return;
if (e?.Cancel ?? false) return;
if (String.IsNullOrWhiteSpace(orbID))
continue;

Expand Down Expand Up @@ -260,7 +255,7 @@ public void SendColorsToOrb(byte red, byte green, byte blue, CancellationToken?
bytes[6] = green;
bytes[7] = blue;

if (token?.IsCancellationRequested ?? false) return;
if (e?.Cancel ?? false) return;
socket.Send(bytes, bytes.Length, SocketFlags.None);
}
catch (Exception)
Expand Down
19 changes: 10 additions & 9 deletions Project-Aurora/Project-Aurora/Devices/Clevo/ClevoDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Aurora.Settings;
using System.ComponentModel;

namespace Aurora.Devices.Clevo
{
Expand Down Expand Up @@ -142,24 +143,24 @@ public bool IsConnected()
throw new NotImplementedException();
}

public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationToken token, bool forced = false) // Is this necessary?
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false) // Is this necessary?
{
throw new NotImplementedException();
}

public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationToken token, bool forced = false)
public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
watch.Restart();
bool update_result = false;

Dictionary<DeviceKeys, Color> keyColors = colorComposition.keyColors;
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
try
{
foreach (KeyValuePair<DeviceKeys, Color> pair in keyColors)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
if (useGlobalPeriphericColors)
{
if (pair.Key == DeviceKeys.Peripheral) // This is not working anymore. Was working in MASTER
Expand All @@ -182,7 +183,7 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationTo
}
}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
if (!useGlobalPeriphericColors)
{
// Clevo 3 region keyboard
Expand All @@ -209,7 +210,7 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationTo
ColorUpdated = true;
}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

// Center (Other Half of Spacebar to F11) - Clevo keyboards are very compact and the right side color bleeds over to the up/left/right/down keys)
BitmapRectangle keymap_f11 = Effects.GetBitmappingFromDeviceKey(DeviceKeys.F11);
Expand All @@ -229,7 +230,7 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationTo
ColorUpdated = true;
}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

// Right Side
BitmapRectangle keymap_numenter = Effects.GetBitmappingFromDeviceKey(DeviceKeys.NUM_ENTER);
Expand All @@ -248,7 +249,7 @@ public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationTo

}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
SendColorsToKeyboard(forced);
update_result = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CoolerMaster;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
Expand Down Expand Up @@ -558,7 +559,7 @@ public bool IsInitialized()
return this.isInitialized;
}

public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationToken token, bool forced = false)
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false)
{
try
{
Expand All @@ -569,7 +570,7 @@ public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationTo

foreach (KeyValuePair<DeviceKeys, Color> key in keyColors)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;

int[] coordinates = new int[2];

Expand Down Expand Up @@ -599,22 +600,22 @@ public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationTo
if (coords.TryGetValue(dev_key, out coordinates))
SetOneKey(coordinates, (Color)key.Value);
}
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
SendColorsToKeyboard(forced || !keyboard_updated);
return true;
}
catch (Exception e)
catch (Exception exc)
{
Global.logger.Error("Failed to Update Device" + e.ToString());
Global.logger.Error("Failed to Update Device" + exc.ToString());
return false;
}
}

public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationToken token, bool forced = false)
public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false)
{
watch.Restart();

bool update_result = UpdateDevice(colorComposition.keyColors, token, forced);
bool update_result = UpdateDevice(colorComposition.keyColors, e, forced);

watch.Stop();
lastUpdateTime = watch.ElapsedMilliseconds;
Expand Down
21 changes: 11 additions & 10 deletions Project-Aurora/Project-Aurora/Devices/Corsair/CorsairDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Threading.Tasks;
using Aurora.Settings;
using Microsoft.Win32.TaskScheduler;
using System.ComponentModel;

namespace Aurora.Devices.Corsair
{
Expand Down Expand Up @@ -167,17 +168,17 @@ public bool IsConnected()
throw new NotImplementedException();
}

public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationToken token, bool forced = false)
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
CorsairLedId keyindex = CorsairLedId.Invalid;

try
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
foreach (KeyValuePair<DeviceKeys, Color> key in keyColors)
{
if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
CorsairLedId localKey = ToCorsair(key.Key);

if (localKey == CorsairLedId.Invalid && key.Key == DeviceKeys.Peripheral_Logo ||
Expand All @@ -202,23 +203,23 @@ public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationTo
keyindex = localKey;
}

if (token.IsCancellationRequested) return false;
if (e.Cancel) return false;
SendColorsToKeyboard(forced);
return true;
}
catch (Exception e)
catch (Exception exc)
{
Global.logger.Error("Corsair device, error when updating device. Error: " + e);
Console.WriteLine(e);
Global.logger.Error("Corsair device, error when updating device. Error: " + exc);
Console.WriteLine(exc);
return false;
}
}

public bool UpdateDevice(DeviceColorComposition colorComposition, CancellationToken token, bool forced = false)
public bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false)
{
watch.Restart();

bool update_result = UpdateDevice(colorComposition.keyColors, token, forced);
bool update_result = UpdateDevice(colorComposition.keyColors, e, forced);

watch.Stop();
lastUpdateTime = watch.ElapsedMilliseconds;
Expand Down
4 changes: 2 additions & 2 deletions Project-Aurora/Project-Aurora/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,14 +1266,14 @@ public interface Device
/// <param name="keyColors">A dictionary of DeviceKeys their corresponding Colors</param>
/// <param name="forced">A boolean value indicating whether or not to forcefully update this device</param>
/// <returns></returns>
bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, CancellationToken token, bool forced = false);
bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false);

/// <summary>
/// Updates the device with a specified color composition.
/// </summary>
/// <param name="colorComposition">A struct containing a dictionary of colors as well as the resulting bitmap</param>
/// <param name="forced">A boolean value indicating whether or not to forcefully update this device</param>
/// <returns></returns>
bool UpdateDevice(DeviceColorComposition colorComposition, CancellationToken token, bool forced = false);
bool UpdateDevice(DeviceColorComposition colorComposition, DoWorkEventArgs e, bool forced = false);
}
}
19 changes: 12 additions & 7 deletions Project-Aurora/Project-Aurora/Devices/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class DeviceContainer
public BackgroundWorker Worker = new BackgroundWorker();
public Thread UpdateThread { get; set; } = null;

public CancellationTokenSource UpdateTaskCancellationTokenSource { get; set; } = null;

private Tuple<DeviceColorComposition, bool> currentComp = null;
private bool newFrame = false;

Expand All @@ -37,22 +35,29 @@ public DeviceContainer(Device device)
private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
newFrame = false;
UpdateTaskCancellationTokenSource = new CancellationTokenSource();
Device.UpdateDevice(currentComp.Item1, UpdateTaskCancellationTokenSource.Token,
Device.UpdateDevice(currentComp.Item1, doWorkEventArgs,
currentComp.Item2);
}

public void UpdateDevice(DeviceColorComposition composition, bool forced = false)
{
UpdateTaskCancellationTokenSource?.Cancel();
if (Worker.IsBusy)
Worker.CancelAsync();

newFrame = true;
currentComp = new Tuple<DeviceColorComposition, bool>(composition, forced);

lock (Worker)
{
if (!Worker.IsBusy)
Worker.RunWorkerAsync();
try
{
if (!Worker.IsBusy)
Worker.RunWorkerAsync();
}
catch(Exception e)
{
Global.logger.LogLine(e.ToString(), Logging_Level.Error);
}
}
}
}
Expand Down
Loading

0 comments on commit 44f1f22

Please sign in to comment.