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

Fix segmentation fault by passing IntPtr to pinned memory #4

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
30 changes: 17 additions & 13 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ws281xPowerShell.csproj"
],
"problemMatcher": "$msCompile"
}
]
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/ws281xPowerShell.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
10 changes: 5 additions & 5 deletions src/CmdLets/Set/BreathingLedCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace WS281x.CmdLets
{

[Cmdlet(VerbsCommon.Set, "BreathingLed")]
public class BreathingLed : Cmdlet
{
Expand All @@ -28,7 +28,7 @@ public BreathingLed()
{
Invert = false;
GpioPin = 18;

}

protected override void BeginProcessing()
Expand All @@ -46,7 +46,7 @@ protected override void ProcessRecord()
int i = 0;
while(true)
{
settings.Channel = new Channel(30, GpioPin, (byte)_brightness, Invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(30, GpioPin, (byte)_brightness, Invert, StripType.WS2811_STRIP_GRB);
using(WS281x controller = new WS281x(settings))
{
//controller.SetLEDColor(this.LedId, this.Color);
Expand Down Expand Up @@ -74,7 +74,7 @@ protected override void ProcessRecord()
}
else
{
_brightness -=1;
_brightness -=1;
}
i+=1;
Console.WriteLine($"index = {i} / Brightness = {_brightness} / BreathingAsc = {breathingAsc}");
Expand All @@ -87,4 +87,4 @@ protected override void ProcessRecord()
// _Controller.Dispose();
// }
}
}
}
16 changes: 8 additions & 8 deletions src/CmdLets/Set/ExplosionCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace WS281x.CmdLets
{

[Cmdlet(VerbsCommon.Set, "Explosion")]
public class Explosion : Cmdlet
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public Explosion()
{
Invert = false;
GpioPin = 18;

}

protected override void BeginProcessing()
Expand All @@ -65,26 +65,26 @@ protected override void ProcessRecord()
Speed = "Medium";

Settings settings = Settings.CreateDefaultSettings();
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2811_STRIP_GRB);
WS281x controller = new WS281x(settings);

int leftSideIterations = NumberOfLeds / 2;

// If it's even, both sides will have the same amount of iterations, otherwise make right side have one more
int rightSideIterations = (NumberOfLeds % 2 == 0) ? leftSideIterations : leftSideIterations+1;
int rightSideIterations = (NumberOfLeds % 2 == 0) ? leftSideIterations : leftSideIterations+1;
int totalIterations = 0, leftSide = 0, rightSide = NumberOfLeds-1;
for(; totalIterations < rightSideIterations ; ++totalIterations)
{
controller.SetLEDColor(leftSide++,LeftSideColor);

controller.SetLEDColor(leftSide++,LeftSideColor);
controller.SetLEDColor(rightSide--,RightSideColor);
controller.Render();
Thread.Sleep(_speedTranslation[Speed]);
}

for(; totalIterations >= 0 ; --totalIterations)
{
controller.SetLEDColor(leftSide--,ExplosionColor);
controller.SetLEDColor(leftSide--,ExplosionColor);
controller.SetLEDColor(rightSide++,ExplosionColor);
controller.Render();
Thread.Sleep(10);
Expand All @@ -98,4 +98,4 @@ protected override void ProcessRecord()
controller.Dispose();
}
}
}
}
14 changes: 7 additions & 7 deletions src/CmdLets/Set/LedAccrossStripCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace WS281x.CmdLets
{

[Cmdlet(VerbsCommon.Set, "LedAccrossStrip")]
public class LedAccrossStrip : Cmdlet
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public LedAccrossStrip()
{
Invert = false;
GpioPin = 18;

}

protected override void BeginProcessing()
Expand All @@ -51,25 +51,25 @@ protected override void BeginProcessing()
protected override void ProcessRecord()
{
Settings settings = Settings.CreateDefaultSettings();
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2811_STRIP_GRB);
WS281x controller = new WS281x(settings);


for(int i = 0 ; i < NumberOfLeds ; ++i)
{
for(int j = 0 ; j < i ; ++j)
{
controller.SetLEDColor(j, Color.Empty);
}
controller.SetLEDColor(i,Color);
controller.SetLEDColor(i,Color);
controller.Render();
Thread.Sleep(_speedTranslation[Speed]);
}
//"hack". turn the last led off
//"hack". turn the last led off
controller.SetLEDColor(NumberOfLeds-1,Color.Empty);
controller.Render();
//for some reason it has to be explicitly disposed
controller.Dispose();
}
}
}
}
14 changes: 7 additions & 7 deletions src/CmdLets/Set/RainbowCycleCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace WS281x.CmdLets
{

[Cmdlet(VerbsCommon.Set, "RainbowCycle")]
public class RainbowCycle : Cmdlet
{
Expand All @@ -20,21 +20,21 @@ public class RainbowCycle : Cmdlet

[Parameter(Mandatory = false)]
public SwitchParameter Invert { get; set; }

[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 2)]
public int NumberOfCycles { get; set ;}

public int GpioPin {get; set;}

private bool shouldAbort;

//private WS281x _controller;

public RainbowCycle()
{
Invert = false;
GpioPin = 18;

}

protected override void BeginProcessing()
Expand All @@ -50,7 +50,7 @@ protected override void ProcessRecord()
}

Settings settings = Settings.CreateDefaultSettings();
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2811_STRIP_GRB);
WS281x controller = new WS281x(settings);
List<Color> colors = GetColors();

Expand All @@ -64,7 +64,7 @@ protected override void ProcessRecord()
//Iterate over all LEDs and display the current color
controller.SetLEDColor(i,currentColor);
controller.Render();
Thread.Sleep(25);
Thread.Sleep(25);
}
}
}
Expand All @@ -82,4 +82,4 @@ protected override void ProcessRecord()
Color.Blue
};
}
}
}
8 changes: 4 additions & 4 deletions src/CmdLets/Set/SetLedCmdLet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Management.Automation; // PowerShell namespace.
namespace WS281x.CmdLets
{

[Cmdlet(VerbsCommon.Set, "SingleLedColor")]
public class SetSingleLedColor : Cmdlet , IDisposable
{
Expand All @@ -27,13 +27,13 @@ public SetSingleLedColor()
{
Invert = false;
GpioPin = 18;

}

protected override void BeginProcessing()
{
Settings settings = Settings.CreateDefaultSettings();
settings.Channel = new Channel(30, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(30, GpioPin, Brightness, Invert, StripType.WS2811_STRIP_GRB);
_Controller = new WS281x(settings);
}

Expand All @@ -48,4 +48,4 @@ public void Dispose()
_Controller.Dispose();
}
}
}
}
20 changes: 12 additions & 8 deletions src/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 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
2 changes: 2 additions & 0 deletions src/Native/ws2811_channel_t.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -8,6 +9,7 @@
namespace 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
5 changes: 4 additions & 1 deletion src/Native/ws2811_return_t.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace 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
}
}
}
8 changes: 4 additions & 4 deletions src/Native/ws2811_t.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace WS281x.Native
{
[SuppressMessage("IDE1006", "IDE1006", Justification = "Native methods have different naming conventions.")]
[StructLayout(LayoutKind.Sequential)]
internal struct ws2811_t
{
Expand All @@ -11,9 +13,7 @@ internal struct ws2811_t
public IntPtr rpi_hw;
public uint freq;
public int dmanum;
// [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.CustomMarshaler, SizeConst = PInvoke.RPI_PWM_CHANNELS)]
// public ws2811_channel_t[] channel;
public ws2811_channel_t channel1;
public ws2811_channel_t channel2;
public ws2811_channel_t channel_1;
public ws2811_channel_t channel_2;
}
}
2 changes: 1 addition & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void Main(string[] args)
byte brightness = 255;
bool invert = false;
Settings settings = Settings.CreateDefaultSettings();
settings.Channel = new Channel(ledCount, gpioPin, brightness, invert, StripType.WS2812_STRIP);
settings.Channel = new Channel(ledCount, gpioPin, brightness, invert, StripType.WS2811_STRIP_GRB);
using (WS281x controller = new WS281x(settings))
{
controller.SetLEDColor(0, Color.Brown);
Expand Down
Loading