1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Fixed some code-issues in novation-library

This commit is contained in:
Darth Affe 2017-08-16 19:56:15 +02:00
parent b642cd31b0
commit 5440f9844f
11 changed files with 106 additions and 42 deletions

View File

@ -1,21 +0,0 @@
using System;
using System.Linq;
using System.Reflection;
namespace RGB.NET.Core.Extensions
{
public static class EnumExtensions
{
/// <summary>
/// A generic extension method that aids in reflecting
/// and retrieving any attribute that is applied to an `Enum`.
/// </summary>
public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)
where TAttribute : Attribute
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<TAttribute>();
}
}
}

View File

@ -54,7 +54,6 @@
<Compile Include="Devices\Layout\LedImage.cs" />
<Compile Include="Devices\Layout\LedImageLayout.cs" />
<Compile Include="Devices\RGBDeviceLighting.cs" />
<Compile Include="Extensions\EnumExtensions.cs" />
<Compile Include="Helper\CultureHelper.cs" />
<Compile Include="Helper\PathHelper.cs" />
<Compile Include="Positioning\Shape.cs" />

View File

@ -0,0 +1,33 @@
using System;
namespace RGB.NET.Devices.Novation.Attributes
{
/// <summary>
/// Specifies the device-id of a field.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class DeviceIdAttribute : Attribute
{
#region Properties & Fields
/// <summary>
/// Gets the Id.
/// </summary>
public string Id { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="DeviceIdAttribute"/> class.
/// </summary>
/// <param name="id">The id.</param>
public DeviceIdAttribute(string id)
{
this.Id = id;
}
#endregion
}
}

View File

@ -1,10 +1,17 @@
using System.ComponentModel.DataAnnotations;
#pragma warning disable 1591
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
using RGB.NET.Devices.Novation.Attributes;
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Represents a specific novation device.
/// </summary>
public enum NovationDevices
{
[Display(Name = "Launchpad S")]
[DeviceId("Launchpad S")]
LaunchpadS
}
}

View File

@ -1,4 +1,5 @@
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
#pragma warning disable 1591 // Missing XML comment for publicly visible type or member

View File

@ -9,7 +9,7 @@ using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Represents a generic Novation-device. (keyboard, mouse, headset, mousepad).
/// Represents a generic Novation-device. (launchpad).
/// </summary>
public abstract class NovationRGBDevice : AbstractRGBDevice
{
@ -30,10 +30,11 @@ namespace RGB.NET.Devices.Novation
/// Initializes a new instance of the <see cref="NovationRGBDevice"/> class.
/// </summary>
/// <param name="info">The generic information provided by Novation for the device.</param>
protected NovationRGBDevice(IRGBDeviceInfo info)
protected NovationRGBDevice(NovationRGBDeviceInfo info)
{
this.DeviceInfo = info;
_outputDevice = new OutputDevice(((NovationRGBDeviceInfo)DeviceInfo).DeviceId);
_outputDevice = new OutputDevice(info.DeviceId);
}
#endregion
@ -139,9 +140,9 @@ namespace RGB.NET.Devices.Novation
{
color = 17;
if(((led.Color.R > 127) && (led.Color.G < 127)) || ((led.Color.R < 127) && (led.Color.G > 127)))
if (((led.Color.R > 127) && (led.Color.G < 127)) || ((led.Color.R < 127) && (led.Color.G > 127)))
color = 34;
if((led.Color.R > 127) && (led.Color.G > 127))
if ((led.Color.R > 127) && (led.Color.G > 127))
color = 51;
}

View File

@ -25,7 +25,9 @@ namespace RGB.NET.Devices.Novation
/// <inheritdoc />
public RGBDeviceLighting Lighting => RGBDeviceLighting.Key;
/// <inheritdoc />
/// <summary>
/// Gets the (midi)-id of the <see cref="IRGBDevice"/>..
/// </summary>
public int DeviceId { get; }
#endregion
@ -37,6 +39,7 @@ namespace RGB.NET.Devices.Novation
/// </summary>
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="model">The represented device model.</param>
/// <param name="deviceId">The (midi)-id of the <see cref="IRGBDevice"/>.</param>
internal NovationRGBDeviceInfo(RGBDeviceType deviceType, string model, int deviceId)
{
this.DeviceType = deviceType;

View File

@ -0,0 +1,33 @@
using System;
using System.Reflection;
using RGB.NET.Devices.Novation.Attributes;
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Offers some extensions and helper-methods for enum related things.
/// </summary>
internal static class EnumExtension
{
/// <summary>
/// Gets the value of the <see cref="DeviceIdAttribute"/>.
/// </summary>
/// <param name="source">The enum value to get the description from.</param>
/// <returns>The value of the <see cref="DeviceIdAttribute"/> of the source.</returns>
internal static string GetDeviceId(this Enum source) => source.GetAttribute<DeviceIdAttribute>()?.Id;
/// <summary>
/// Gets the attribute of type T.
/// </summary>
/// <param name="source">The enum value to get the attribute from</param>
/// <typeparam name="T">The generic attribute type</typeparam>
/// <returns>The <see cref="Attribute"/>.</returns>
private static T GetAttribute<T>(this Enum source)
where T : Attribute
{
FieldInfo fi = source.GetType().GetField(source.ToString());
T[] attributes = (T[])fi.GetCustomAttributes(typeof(T), false);
return attributes.Length > 0 ? attributes[0] : null;
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
@ -38,8 +37,7 @@ namespace RGB.NET.Devices.Novation
protected override void InitializeLayout()
{
//TODO DarthAffe 15.08.2017: Check if all launchpads are using the same basic layout
//TODO DarthAffe 15.08.2017: Measure button size
const int BUTTON_SIZE = 16;
const int BUTTON_SIZE = 20;
foreach (NovationLedIds ledId in Enum.GetValues(typeof(NovationLedIds)))
{
Rectangle rectangle;

View File

@ -1,13 +1,18 @@
using System;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Core.Extensions;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Represents a device provider responsible for Novation devices.
/// </summary>
public class NovationDeviceProvider : IRGBDeviceProvider
{
#region Properties & Fields
@ -63,15 +68,18 @@ namespace RGB.NET.Devices.Novation
for (int index = 0; index < OutputDeviceBase.DeviceCount; index++)
{
MidiOutCaps outCaps = OutputDeviceBase.GetDeviceCapabilities(index);
if (outCaps.name == null) continue;
NovationDevices? deviceId = (NovationDevices?)Enum.GetValues(typeof(NovationDevices)).Cast<Enum>()
.FirstOrDefault(x => string.Equals(x.GetDeviceId(), outCaps.name, StringComparison.OrdinalIgnoreCase));
if (deviceId == null) continue;
if (outCaps.name.Equals(NovationDevices.LaunchpadS.GetAttribute<DisplayAttribute>().Name))
{
NovationRGBDevice device = new NovationLaunchpadRGBDevice(new NovationLaunchpadRGBDeviceInfo(outCaps.name, index));
device.Initialize();
devices.Add(device);
}
}
}
catch
{
if (throwExceptions)

View File

@ -49,11 +49,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\DeviceIdAttribute.cs" />
<Compile Include="Enum\NovationDevices.cs" />
<Compile Include="Enum\NovationLedIds.cs" />
<Compile Include="Generic\NovationLedId.cs" />
<Compile Include="Generic\NovationRGBDevice.cs" />
<Compile Include="Generic\NovationRGBDeviceInfo.cs" />
<Compile Include="Helper\EnumExtension.cs" />
<Compile Include="Helper\NovationLedIdsExtension.cs" />
<Compile Include="Launchpad\NovationLaunchpadRGBDevice.cs" />
<Compile Include="Launchpad\NovationLaunchpadRGBDeviceInfo.cs" />