Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .NET Core #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/TestApp/ColorWipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Execute(AbortRequest request)

//Set brightness to maximum (255)
//Use Unknown as strip type. Then the type will be set in the native assembly.
settings.Channels[0] = new Channel(ledCount, 18, 255, false, StripType.WS2812_STRIP);
settings.Channel_1 = new Channel(ledCount, 18, 255, false, StripType.WS2812_STRIP);

using (var controller = new WS281x(settings))
{
Expand All @@ -37,7 +37,7 @@ public void Execute(AbortRequest request)

private static void Wipe(WS281x controller, Color color)
{
for (int i = 0; i <= controller.Settings.Channels[0].LEDs.Count - 1; i++)
for (int i = 0; i <= controller.Settings.Channel_1.LEDs.Count - 1; i++)
{
controller.SetLEDColor(0, i, color);
controller.Render();
Expand Down
4 changes: 2 additions & 2 deletions src/TestApp/RainbowColorAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public void Execute(AbortRequest request)

//Set brightness to maximum (255)
//Use Unknown as strip type. Then the type will be set in the native assembly.
settings.Channels[0] = new Channel(ledCount, 18, 255, false, StripType.WS2812_STRIP);
settings.Channel_1 = new Channel(ledCount, 18, 255, false, StripType.WS2812_STRIP);

using (var controller = new WS281x(settings))
{
var colors = GetAnimationColors();
while (!request.IsAbortRequested)
{

for (int i = 0; i <= controller.Settings.Channels[0].LEDCount - 1; i++)
for (int i = 0; i <= controller.Settings.Channel_1.LEDCount - 1; i++)
{
var colorIndex = (i + colorOffset) % colors.Count;
controller.SetLEDColor(0, i, colors[colorIndex]);
Expand Down
25 changes: 25 additions & 0 deletions src/rpi-ws281x-dotnet.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.757
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "rpi-ws281x-dotnet", "rpi_ws281x\rpi-ws281x-dotnet.csproj", "{5432FD49-28E2-40CC-AAE8-E340503981ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5432FD49-28E2-40CC-AAE8-E340503981ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5432FD49-28E2-40CC-AAE8-E340503981ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5432FD49-28E2-40CC-AAE8-E340503981ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5432FD49-28E2-40CC-AAE8-E340503981ED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05598D32-E9AC-47E9-8CC6-D5EED433D349}
EndGlobalSection
EndGlobal
63 changes: 31 additions & 32 deletions src/rpi_ws281x/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ namespace rpi_ws281x
/// </summary>
public class Channel
{
public Channel() : this(0, 0) { }
public Channel(int ledCount, int gpioPin) : this(ledCount, gpioPin, 255, false, rpi_ws281x.StripType.Unknown) { }

public Channel(int ledCount, int gpioPin, byte brightness, bool invert, StripType stripType)
{
GPIOPin = gpioPin;
Invert = invert;
Brightness = brightness;
StripType = stripType;

var ledList = new List<LED>();
for(int i= 0; i<= ledCount-1; i++)
{
ledList.Add(new LED(i));
}

LEDs = new ReadOnlyCollection<LED>(ledList);
}
/// <summary>
/// Returns the GPIO pin which is connected to the LED strip
/// </summary>
public int GPIOPin { get; private set; }
public Channel() : this(0, 0) { }

public Channel(int ledCount, int gpioPin) : this(ledCount, gpioPin, 255, false, StripType.Unknown) { }

public Channel(int ledCount, int gpioPin, byte brightness, bool invert, StripType stripType)
{
GPIOPin = gpioPin;
Invert = invert;
Brightness = brightness;
StripType = stripType;

var ledList = new List<LED>();
for (int i = 0; i <= ledCount - 1; i++)
{
ledList.Add(new LED(i));
}

LEDs = new ReadOnlyCollection<LED>(ledList);
}

/// <summary>
/// Returns the GPIO pin which is connected to the LED strip
/// </summary>
public int GPIOPin { get; private set; }

/// <summary>
/// Returns a value which indicates if the signal needs to be inverted.
Expand All @@ -41,7 +41,7 @@ public Channel(int ledCount, int gpioPin, byte brightness, bool invert, StripTy

/// <summary>
/// Gets or sets the brightness of the LEDs
/// 0 = darkes, 255 = brightest
/// 0 = darkest, 255 = brightest
/// </summary>
public byte Brightness { get; set; }

Expand All @@ -51,12 +51,11 @@ public Channel(int ledCount, int gpioPin, byte brightness, bool invert, StripTy
/// </summary>
public StripType StripType { get; private set; }

/// <summary>
/// Returns all LEDs on this channel
/// </summary>
public ReadOnlyCollection<LED> LEDs { get; private set; }
/// <summary>
/// Returns all LEDs on this channel
/// </summary>
public ReadOnlyCollection<LED> LEDs { get; private set; }

public int LEDCount { get => LEDs.Count; }

}
public int LEDCount { get => LEDs.Count; }
}
}
22 changes: 13 additions & 9 deletions src/rpi_ws281x/Native/PInvoke.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Native
namespace rpi_ws281x.Native
{
internal class PInvoke
{
public const int RPI_PWM_CHANNELS = 2;

[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[DllImport("ws2811.so")]
public static extern ws2811_return_t ws2811_init(ref ws2811_t ws2811);

public static extern ws2811_return_t ws2811_init(IntPtr ws2811);

[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[DllImport("ws2811.so")]
public static extern ws2811_return_t ws2811_render(ref ws2811_t ws2811);
public static extern ws2811_return_t ws2811_render(IntPtr ws2811);

[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[DllImport("ws2811.so")]
public static extern ws2811_return_t ws2811_wait(ref ws2811_t ws2811);

public static extern ws2811_return_t ws2811_wait(IntPtr ws2811);

[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[DllImport("ws2811.so")]
public static extern void ws2811_fini(ref ws2811_t ws2811);
public static extern void ws2811_fini(IntPtr ws2811);

[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[DllImport("ws2811.so")]
public static extern IntPtr ws2811_get_return_t_str(int state);
}
Expand Down
4 changes: 3 additions & 1 deletion src/rpi_ws281x/Native/ws2811_channel_t.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Native
namespace rpi_ws281x.Native
{
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
internal struct ws2811_channel_t
{
public int gpionum;
Expand Down
7 changes: 5 additions & 2 deletions src/rpi_ws281x/Native/ws2811_return_t.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Native
using System.Diagnostics.CodeAnalysis;

namespace rpi_ws281x.Native
{
[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
internal enum ws2811_return_t
{
WS2811_SUCCESS = 0,
Expand All @@ -18,4 +21,4 @@ internal enum ws2811_return_t
WS2811_ERROR_SPI_SETUP = -13,
WS2811_ERROR_SPI_TRANSFER = -14
}
}
}
10 changes: 6 additions & 4 deletions src/rpi_ws281x/Native/ws2811_t.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Native
namespace rpi_ws281x.Native
{
[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[StructLayout(LayoutKind.Sequential)]
internal struct ws2811_t
internal class ws2811_t
{
public long render_wait_time;
public IntPtr device;
public IntPtr rpi_hw;
public uint freq;
public int dmanum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = PInvoke.RPI_PWM_CHANNELS)]
public ws2811_channel_t[] channel;
public ws2811_channel_t channel_1;
public ws2811_channel_t channel_2;
}
}
73 changes: 48 additions & 25 deletions src/rpi_ws281x/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Native;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using rpi_ws281x.Native;

namespace rpi_ws281x
{
Expand All @@ -9,42 +9,65 @@ namespace rpi_ws281x
/// </summary>
public class Settings
{

/// <summary>
/// Settings to initialize the WS281x controller
/// </summary>
/// <param name="frequency">Set frequency in Hz</param>
/// <param name="dmaChannel">Set DMA channel to use</param>
public Settings(uint frequency, int dmaChannel)
{
Frequency = frequency;
DMAChannel = dmaChannel;
Channels = new Channel[PInvoke.RPI_PWM_CHANNELS];
}

/// <summary>
/// <summary>
/// Settings to initialize the WS281x controller with one channel
/// </summary>
/// <param name="frequency">Set frequency in Hz</param>
/// <param name="dmaChannel">Set DMA channel to use</param>
public Settings(Channel channel, uint frequency = 800000, int dmaChannel = 10) : this (channel, null, frequency, dmaChannel) { }

/// <summary>
/// Settings to initialize the WS281x controller with up to two channels
/// </summary>
/// <param name="frequency">Set frequency in Hz</param>
/// <param name="dmaChannel">Set DMA channel to use</param>
public Settings(Channel channel1, Channel channel2, uint frequency = 800000, int dmaChannel = 10)
{
Channel_1 = channel1;
if (channel2 == null)
ChannelCount = 1;
else {
Channel_2 = channel2;
ChannelCount = 2;
}
Frequency = frequency;
DMAChannel = dmaChannel;
}

/// <summary>
/// Returns default settings.
/// Use a frequency of 800000 Hz and DMA channel 10
/// </summary>
/// <returns></returns>
public static Settings CreateDefaultSettings()
{
return new Settings(800000, 10);
}
{
return new Settings(null, 800000, 10);
}

/// <summary>
/// Returns the used frequency in Hz
/// </summary>
public uint Frequency { get; private set; }
/// <summary>
/// Returns the used frequency in Hz
/// </summary>
public uint Frequency { get; private set; }

/// <summary>
/// Returns the DMA channel
/// </summary>
public int DMAChannel { get; private set; }

/// <summary>
/// Returns the number of channels being used
/// </summary>
public int ChannelCount { get; private set; }

/// <summary>
/// Returns the channels which holds the LEDs
/// Returns Channel 1
/// </summary>
public Channel Channel_1 { get; set; }

/// <summary>
/// Returns Channel 1
/// </summary>
public Channel[] Channels { get; private set; }
}
public Channel Channel_2 { get; set; }
}
}
Loading