Skip to content

Commit

Permalink
Added UI option to switch between using LED strip or Razer Chroma
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdeory committed Mar 28, 2020
1 parent ab7fa19 commit cddb25c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 12 deletions.
2 changes: 2 additions & 0 deletions LedDashboard/LedDashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<Compile Include="Led.cs" />
<Compile Include="LedManager.cs" />
<Compile Include="LEDModule.cs" />
<Compile Include="LightController.cs" />
<Compile Include="LightControllerType.cs" />
<Compile Include="Main.cs" />
<Compile Include="MainUI.cs">
<SubType>Form</SubType>
Expand Down
23 changes: 22 additions & 1 deletion LedDashboard/LedManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using LedDashboard.Modules.BlinkWhite;
using LedDashboard.Modules.LeagueOfLegends;
using System;

namespace LedDashboard
{
Expand All @@ -21,10 +22,15 @@ class LedManager

bool reverseOrder;

LightController lightController;


/// <param name="ledCount">Number of lights in the LED strip</param>
/// <param name="reverseOrder">Set to true if you want the lights to be reverse in order (i.e. Color for LED 0 will be applied to the last LED in the strip)</param>
public LedManager(int ledCount, bool reverseOrder)
{
lightController = RazerChromaController.Create();

this.leds = new Led[ledCount];
for (int i = 0; i < this.leds.Length; i++)
{
Expand Down Expand Up @@ -84,14 +90,29 @@ public byte[] ToByteArray(bool reverseOrder = false)

return data;
}

/// <summary>
/// Sets the light controller to be used
/// </summary>
public void SetController(LightControllerType type)
{
((IDisposable)lightController).Dispose();
if (type == LightControllerType.LED_Strip)
{
lightController = SACNController.Create();
} else if (type == LightControllerType.RazerChroma)
{
lightController = RazerChromaController.Create();
}
}

/// <summary>
/// Sends LED data to a wireless LED strip using the E1.31 sACN protocol.
/// </summary>
public async void SendData()
{
//await SACNController.Send(this.ToByteArray(reverseOrder));
RazerChromaController.SendData(this.leds.Length, this.ToByteArray());
lightController.SendData(this.leds.Length, this.ToByteArray());
}

}
Expand Down
13 changes: 13 additions & 0 deletions LedDashboard/LightController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LedDashboard
{
public interface LightController
{
public void SendData(int ledCount, byte[] data);
}
}
13 changes: 13 additions & 0 deletions LedDashboard/LightControllerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LedDashboard
{
public enum LightControllerType
{
RazerChroma, LED_Strip
}
}
31 changes: 30 additions & 1 deletion LedDashboard/MainUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class MainUI : Form
Graphics canvas;
LedManager ledManager;
FormsPlot plt;
Button chromabtn;
Button ledstripbtn;

public MainUI()
{
Expand All @@ -30,11 +32,24 @@ public MainUI()
box.BackColor = Color.White;
canvas = box.CreateGraphics();

chromabtn = new Button();
chromabtn.Text = "Use Razer Chroma";
chromabtn.Size = new Size(200, 50);
chromabtn.Location = new Point(0, 300);
chromabtn.Click += UseRazerChromaClicked;
chromabtn.Enabled = false;

ledstripbtn = new Button();
ledstripbtn.Text = "Use LED Strip";
ledstripbtn.Size = new Size(200, 50);
ledstripbtn.Location = new Point(0, 350);
ledstripbtn.Click += UseLEDStripClicked;

plt = new FormsPlot();
plt.Location = new Point(0, 100);
plt.Size = new Size(800, 200);

this.Controls.AddRange(new Control[] { box, plt });
this.Controls.AddRange(new Control[] { box, plt, chromabtn, ledstripbtn });

Gradient.GeneratePalettes();

Expand Down Expand Up @@ -65,5 +80,19 @@ public void UpdateUI(Led[] leds)
}
}

public void UseRazerChromaClicked(object s, EventArgs e)
{
ledManager.SetController(LightControllerType.RazerChroma);
chromabtn.Enabled = false;
ledstripbtn.Enabled = true;
}

public void UseLEDStripClicked(object s, EventArgs e)
{
ledManager.SetController(LightControllerType.LED_Strip);
chromabtn.Enabled = true;
ledstripbtn.Enabled = false;
}

}
}
24 changes: 17 additions & 7 deletions LedDashboard/RazerChromaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@

namespace LedDashboard
{
class RazerChromaController
class RazerChromaController : LightController, IDisposable
{
public static void Init()
public static RazerChromaController Create()
{
NativeRazerApi api = new NativeRazerApi();
keyboardFrame = new KeyboradFrame(api);
return new RazerChromaController();
}

private static KeyboradFrame keyboardFrame;
private static NativeRazerApi api;


public static void SendData(int ledCount, byte[] colorArray)
private RazerChromaController()
{
if (keyboardFrame == null) Init();
if (api == null) api = new NativeRazerApi();
if (keyboardFrame == null) keyboardFrame = new KeyboradFrame(api);
}

public void SendData(int ledCount, byte[] colorArray)
{
List<Point> points = new List<Point>();
for (int i = 0; i < ledCount; i++)
{
Expand All @@ -41,5 +45,11 @@ public static void SendData(int ledCount, byte[] colorArray)

}

public void Dispose()
{
api.Dispose();
api = null;
keyboardFrame = null;
}
}
}
13 changes: 10 additions & 3 deletions LedDashboard/SACNController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@

namespace LedDashboard
{
class SACNController
class SACNController : LightController, IDisposable
{
public static SACNController Create()
{
return new SACNController();
}

static SACNSender sender;
public static async Task Send(byte[] data)
public void SendData(int ledCount, byte[] data)
{
if (sender == null) sender = new SACNSender(Guid.NewGuid(), "wled-nico");
await sender.Send(1, data);
Task.Run(() => sender.Send(1, data));
}

public void Dispose() { }
}
}

0 comments on commit cddb25c

Please sign in to comment.