Skip to content

Instantly share code, notes, and snippets.

@crbednarz
Last active June 26, 2020 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crbednarz/df5567e0757af914f7e78dc75bcb5b07 to your computer and use it in GitHub Desktop.
Save crbednarz/df5567e0757af914f7e78dc75bcb5b07 to your computer and use it in GitHub Desktop.
Custom RGB Device
using Microsoft.Xna.Framework;
using ReLogic.Peripherals.RGB;
using System.Net;
using System.Net.Sockets;
namespace TerrariaLeds
{
class MyRgbDevice : RgbDevice
{
private UdpClient _udpClient;
private static Fragment CreateFragment()
{
var gridPositions = new Point[150];
var canvasPositions = new Vector2[150];
for (var i = 0; i < 150; i++)
{
gridPositions[i].X = i;
gridPositions[i].Y = 0;
// Dividing by 50 will make our LED strip considered
// roughly as long as a keyboard.
canvasPositions[i].X = i / 50.0f;
canvasPositions[i].Y = 0;
}
return Fragment.FromCustom(gridPositions, canvasPositions);
}
public MyRgbDevice() :
base(RgbDeviceVendor.Unknown,
RgbDeviceType.Generic,
CreateFragment(),
new DeviceColorProfile())
{
PreferredLevelOfDetail = EffectDetailLevel.Low;
_udpClient = new UdpClient();
// Update this to the local IP of the ESP32.
_udpClient.Connect(IPAddress.Parse("10.0.0.173"), 8585);
}
public override void Present()
{
byte[] output = new byte[LedCount * 3];
for (int i = 0; i < LedCount; i++)
{
var color = GetProcessedLedColor(i);
output[i * 3 + 0] = (byte)(color.X * 255.0f);
output[i * 3 + 1] = (byte)(color.Y * 255.0f);
output[i * 3 + 2] = (byte)(color.Z * 255.0f);
}
_udpClient.Send(output, output.Length);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment