Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from akmadian/develop
Browse files Browse the repository at this point in the history
v2.0.0-preview3
  • Loading branch information
Ari Madian authored Feb 28, 2019
2 parents da9b995 + c4d7396 commit 1d0adbd
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 114 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ ASALocalRun/
.localhistory/

TestApp/
NZXTSharpTesterApp/
NZXTSharpTesterApp/
Scripts/
.vscode/
4 changes: 1 addition & 3 deletions NZXTSharp/COM/USB/USBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class USBController
private NZXTDeviceType _Type;
private HIDDeviceID _ID;
private int CurrProductID;
private HIDDeviceID _VendorID = HIDDeviceID.VendorID;
private readonly HIDDeviceID _VendorID = HIDDeviceID.VendorID;
private HidReport _LastReport;
private bool _IsAttached = false;
private HidDevice _Device;
Expand Down Expand Up @@ -58,9 +58,7 @@ internal class USBController
public USBController(NZXTDeviceType Type) {
this._Type = Type;
ResolveDeviceID();
Console.WriteLine("DeviceID Resolved, Initializing...");
Initialize();
Console.WriteLine("Initialization Complete");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion NZXTSharp/Core/Common/Effects/Alternating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public List<byte[]> BuildBytes(NZXTDeviceType Type, IChannel Channel) {
List<byte[]> KrakenXOutList = new List<byte[]>();
for (int colorIndex = 0; colorIndex < Colors.Length; colorIndex++)
{
byte[] KrakenXSettingsBytes = new byte[] { 0x42, 0x4c, direction, 0x05, new CISS(colorIndex) };
byte[] KrakenXSettingsBytes = new byte[] { 0x2, 0x4c, direction, 0x05, new CISS(colorIndex, this.speed) };
byte[] final = KrakenXSettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(Colors[colorIndex]));
KrakenXOutList.Add(final);
}
Expand Down
11 changes: 10 additions & 1 deletion NZXTSharp/Core/Common/Effects/CoveringMarquee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using NZXTSharp;
using NZXTSharp.HuePlus;
using NZXTSharp.KrakenX;
using NZXTSharp.Exceptions;

namespace NZXTSharp
Expand Down Expand Up @@ -96,7 +97,15 @@ public List<byte[]> BuildBytes(NZXTDeviceType Type, IChannel Channel) {

return outList;
case NZXTDeviceType.KrakenX:
// TODO
List<byte[]> KrakenOutList = new List<byte[]>();
DCB param = new DCB(Channel.ChannelByte, Param1.IsForward);
for (int colorIndex = 0; colorIndex < _Colors.Length; colorIndex++)
{
byte[] KrakenSettingsBytes = new byte[] {0x2, 0x4c, (byte)param.GetValue(), 0x04, new CISS(colorIndex, _Speed)};
byte[] KrakenFinal = KrakenSettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(_Colors[colorIndex]));
KrakenOutList.Add(KrakenFinal);
}
return KrakenOutList;
default:
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion NZXTSharp/Core/Common/Effects/SpectrumWave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List<byte[]> BuildBytes(NZXTDeviceType Type, IChannel Channel) {
return new List<byte[]>() { final };
case NZXTDeviceType.KrakenX:
DCB param = new DCB(Channel.ChannelByte, Param1.IsForward);
byte[] KrakenSettingsBytes = new byte[] { 0x42, 0x4c, (byte)param.GetValue(), 0x02, (byte)speed };
byte[] KrakenSettingsBytes = new byte[] { 0x2, 0x4c, (byte)param.GetValue(), 0x02, (byte)speed };
byte[] KrakenFinal = KrakenSettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(new Color(0, 0, 255)));
return new List<byte[]>() { KrakenFinal };
default:
Expand Down
40 changes: 22 additions & 18 deletions NZXTSharp/Core/Common/Effects/Wings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Wings : IEffect {
/// <summary>
/// The array of colors used by the effect.
/// </summary>
public Color[] Colors;
public Color Color;
private IChannel _Channel;
private CISS _Param2;
private int speed = 2;
Expand All @@ -41,42 +41,46 @@ public class Wings : IEffect {
/// <summary>
/// Constructs a <see cref="Wings"/> effect with the given <see cref="Color"/> array and speed.
/// </summary>
/// <param name="Colors"></param>
/// <param name="Color"></param>
/// <param name="Speed">Speed values must be 0-4 (inclusive). 0 being slowest, 2 being normal, and 4 being fastest. Defaults to 2.</param>
public Wings(Color[] Colors, int Speed = 2) {
this.Colors = Colors;
public Wings(Color Color, int Speed = 2) {
this.Color = Color;
this.speed = Speed;
ValidateParams();
}

private void ValidateParams() {
if (this.Colors.Length > 15) {
throw new TooManyColorsProvidedException();
}

if (speed > 4 || speed < 0) {
private void ValidateParams()
{
if (speed > 4 || speed < 0)
{
throw new InvalidEffectSpeedException();
}
}

/// <inheritdoc/>
public bool IsCompatibleWith(NZXTDeviceType Type) {
public bool IsCompatibleWith(NZXTDeviceType Type)
{
return CompatibleWith.Contains(Type) ? true : false;
}

/// <inheritdoc/>
public List<byte[]> BuildBytes(NZXTDeviceType Type, IChannel Channel) {
public List<byte[]> BuildBytes(NZXTDeviceType Type, IChannel Channel)
{
switch (Type) {
case NZXTDeviceType.HuePlus:
List<byte[]> outList = new List<byte[]>();
for (int colorIndex = 0; colorIndex < Colors.Length; colorIndex++) {
byte[] SettingsBytes = new byte[] { 0x4b, (byte)Channel.ChannelByte, 0x0c, 0x03, new CISS(colorIndex, this.speed) };
byte[] final = SettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(Colors[colorIndex]));
outList.Add(final);
}
byte[] SettingsBytes = new byte[] { 0x4b, (byte)Channel.ChannelByte, 0x0c, 0x03, new CISS(0, this.speed) };
byte[] final = SettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(Color));
outList.Add(final);

return outList;
case NZXTDeviceType.KrakenX:
// TODO
List<byte[]> KrakenXOutBytes = new List<byte[]>();
byte[] KrakenXSettingsBytes = new byte[] { 0x02, 0x4c, (byte)Channel.ChannelByte, 0x0c, (byte)speed };
byte[] KrakenXFinal = KrakenXSettingsBytes.ConcatenateByteArr(Channel.BuildColorBytes(Color));
KrakenXOutBytes.Add(KrakenXFinal);

return KrakenXOutBytes;
default:
return null;
}
Expand Down
120 changes: 105 additions & 15 deletions NZXTSharp/Core/DeviceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class DeviceLoader

private DeviceLoadFilter _Filter;

private bool _Initialized = false;

private bool _ThrowExceptions = true;

/// <summary>
/// Returns the first <see cref="KrakenX.KrakenX"/> instance owned
/// by the <see cref="DeviceLoader"/> if one exists.
Expand All @@ -45,6 +49,11 @@ public class DeviceLoader
/// </summary>
public IEnumerable<INZXTDevice> Devices { get => new ReadOnlyCollection<INZXTDevice>(_Devices); }

/// <summary>
/// Whether or not the <see cref="DeviceLoader"/> is fully initialized.
/// </summary>
public bool IsInitialized { get => _Initialized; }

/// <summary>
/// Gets the number of <see cref="INZXTDevice"/>s owned by the <see cref="DeviceLoader"/>.
/// </summary>
Expand All @@ -56,7 +65,16 @@ public class DeviceLoader
public DeviceLoadFilter Filter
{
get => _Filter;
set => _Filter = Filter;
set => _Filter = value;
}

/// <summary>
/// Whether or not the <see cref="DeviceLoader"/> will throw exceptions.
/// </summary>
public bool ThrowExceptions
{
get => _ThrowExceptions;
set => _ThrowExceptions = value;
}
#endregion

Expand Down Expand Up @@ -88,6 +106,33 @@ public DeviceLoader(bool InitializeDevices, DeviceLoadFilter Filter = DeviceLoad
}
}

/// <summary>
/// Creates a <see cref="DeviceLoader"/> instance from a given array of <see cref="INZXTDevice"/>s.
/// </summary>
/// <param name="Devices">An array of <see cref="INZXTDevice"/>s.</param>
public DeviceLoader(INZXTDevice[] Devices)
{
this._Devices = new List<INZXTDevice>(Devices);
}

/// <summary>
/// Creates a <see cref="DeviceLoader"/> instance from a given list of <see cref="INZXTDevice"/>s.
/// </summary>
/// <param name="Devices">A <see cref="List{T}"/> of <see cref="INZXTDevice"/>s.</param>
public DeviceLoader(List<INZXTDevice> Devices)
{
this._Devices = Devices;
}

/// <summary>
/// Creates a <see cref="DeviceLoader"/> instance from a given list of <see cref="INZXTDevice"/>s.
/// </summary>
/// <param name="Devices">A <see cref="ReadOnlyCollection{T}"/> of <see cref="INZXTDevice"/>s.</param>
public DeviceLoader(ReadOnlyCollection<INZXTDevice> Devices)
{
this._Devices = new List<INZXTDevice>(Devices);
}

#endregion
#region Methods

Expand All @@ -96,16 +141,25 @@ public DeviceLoader(bool InitializeDevices, DeviceLoadFilter Filter = DeviceLoad
/// </summary>
public void Initialize()
{
if (_Initialized)
{
if (_ThrowExceptions)
throw new InvalidOperationException("DeviceLoader already initialized.");
else
return;
}

this._Initialized = false;
_Devices = GetDevices(_Filter);
this._Initialized = true;
}

/// <summary>
/// Applies a given <see cref="IEffect"/> to all devices owned by the
/// <see cref="DeviceLoader"/> instance which have RGB capabilites.
/// </summary>
/// <param name="Effect">An <see cref="IEffect"/> to apply.</param>
/// <param name="ThrowExceptions">Whether or not to throw exceptions, defaults to true.</param>
public void ApplyEffectToDevices(IEffect Effect, bool ThrowExceptions = true)
public void ApplyEffectToDevices(IEffect Effect)
{
foreach (INZXTDevice Device in this._Devices)
{
Expand All @@ -116,11 +170,13 @@ public void ApplyEffectToDevices(IEffect Effect, bool ThrowExceptions = true)
catch (InvalidOperationException) {}
catch (IncompatibleEffectException e)
{
if (ThrowExceptions)
if (_ThrowExceptions)
{
throw new IncompatibleEffectException(
"DeviceLoader.ApplyEffectToDevices; Given effect incompatible with an owned device",
e
);
}
}
}
}
Expand Down Expand Up @@ -227,6 +283,27 @@ public void ModifyFilter(DeviceLoadFilter Filter)
this._Filter = Filter;
}

/// <summary>
/// Filters the existing devices in <see cref="DeviceLoader.Devices"/>
/// based on the given <see cref="DeviceLoadFilter"/>.
/// </summary>
/// <param name="Filter">What kinds of devices to keep.</param>
public void FilterDevices(DeviceLoadFilter Filter)
{
int[] allowedIDs = MapFilterToSupportedIDs.Map(Filter);
List<INZXTDevice> nDevices = new List<INZXTDevice>();

foreach (INZXTDevice Device in _Devices)
{
if (allowedIDs.Contains(Device.ID))
{
nDevices.Add(Device);
}
}

_Devices = nDevices;
}

/// <summary>
/// Sets all fans owned by all <see cref="INZXTDevice"/>s owned by the
/// <see cref="DeviceLoader"/> to a given <paramref name="Speed"/>.
Expand Down Expand Up @@ -273,10 +350,31 @@ public void LightingOff()
KrakenX.Logo.Off();
}

/// <summary>
/// Toggles whether or not the <see cref="DeviceLoader"/> throws exceptions.
/// </summary>
public void ToggleThrowExceptions()
{
this._ThrowExceptions = this._ThrowExceptions ? false : true;
}
#endregion
#endregion

#region Static

/// <summary>
/// Implicitly converts the <see cref="DeviceLoader"/> to an array of <see cref="INZXTDevice"/>s.
/// </summary>
/// <param name="Loader"></param>
public static implicit operator INZXTDevice[] (DeviceLoader Loader) => Loader.Devices.ToArray();

/// <summary>
/// Implicitly converts the <see cref="DeviceLoader"/> to a <see cref="List{T}"/>
/// of <see cref="INZXTDevice"/>s.
/// </summary>
/// <param name="Loader"></param>
public static implicit operator List<INZXTDevice>(DeviceLoader Loader) => Loader.Devices.ToList();

/// <summary>
/// Gets and returns all connected devices.
/// </summary>
Expand Down Expand Up @@ -432,24 +530,16 @@ internal static int[] Map(DeviceLoadFilter Filter)
case DeviceLoadFilter.FanControllers:
return new int[]
{
0x1711, 0x1714, 0x1713, 0x2005
0x1711, 0x1714, 0x1713, 0x2005, 0x170e
};
case DeviceLoadFilter.LightingControllers:
return new int[]
{
0x1715, 0x170e, 0x1712, 0x2002, 0x2001,
0x2005, 0x1714, 0x1713, 0x11111111
};
case DeviceLoadFilter.Grid:
return new int[]
{
0x1711
};
case DeviceLoadFilter.Gridv3:
return new int[]
{
0x1711
};
case DeviceLoadFilter.Grid: return new int[] { 0x1711 };
case DeviceLoadFilter.Gridv3: return new int[] { 0x1711 };
case DeviceLoadFilter.Hue:
return new int[]
{
Expand Down
19 changes: 17 additions & 2 deletions NZXTSharp/Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,20 @@ public static byte[] PadColorArr(this byte[] thisone)
}

// TOFIX
public static string ColorArrToString(this byte[] thisone)
public static string ColorArrToString(this byte[] thisone, bool asHex = true)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < thisone.Length; i++)
{
if (!(i % 12 == 0) || i == 0)
{
sb.Append(thisone[i] + " ");
if (asHex)
{
sb.Append(thisone[i].ToString("X2") + " ");
} else
{
sb.Append(thisone[i].ToString("X2") + " ");
}
} else
{
sb.Append("\n");
Expand All @@ -114,4 +120,13 @@ public static string ColorArrToString(this byte[] thisone)
return sb.ToString();
}
}

internal static class ByteExtensions
{
public static int ConcatenateInt(this byte thisone, int other)
{
int thisByte = (int)thisone;
return Convert.ToInt32(thisByte.ToString() + other.ToString());
}
}
}
5 changes: 5 additions & 0 deletions NZXTSharp/Core/INZXTDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public interface INZXTDevice
/// </summary>
NZXTDeviceType Type { get; }

/// <summary>
/// A unique device ID.
/// </summary>
int ID { get; }

/// <summary>
/// Applies an <see cref="IEffect"/> to the <see cref="INZXTDevice"/>.
/// </summary>
Expand Down
Loading

0 comments on commit 1d0adbd

Please sign in to comment.