Skip to content

Instantly share code, notes, and snippets.

@S0und
Last active March 17, 2020 08:33
Show Gist options
  • Save S0und/7f68ca0b156b8dd2f167ba1264e28fb8 to your computer and use it in GitHub Desktop.
Save S0und/7f68ca0b156b8dd2f167ba1264e28fb8 to your computer and use it in GitHub Desktop.
Wifi RGB LED controller Device for Aurora
using Aurora;
using Aurora.Devices;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
// This is for a Wifi LED controller like this: https://i.imgur.com/SwAi2Hx.jpg
// If you are using a device like this: https://i.imgur.com/axSlR6h.png, send an extra 0x00,
// exp: var data = new List<byte> { 0x31, processed_color.R, processed_color.G, processed_color.B,>> 0x00 <<, 0x00, 0xf0, 0x0f };
// This type of device expects a white intensity info on top of RGB.
// Credit: https://github.com/beville/flux_led
public class ExampleDeviceScript
{
public string devicename = "RGB Strip Controller";
public bool enabled = true;
private Color device_color = Color.Black;
private string _ip = "192.168.1.16";
private double _brightness = 0.3;
public bool Initialize()
{
try
{
// turn on the device
var data2 = new List<byte> { 0x71, 0x23, 0x0f };
sendCommand(data2);
//Perform necessary actions to initialize your device
return true;
}
catch(Exception exc)
{
return false;
}
}
public void Reset()
{
// off
var data1 = new List<byte> { 0x71, 0x24, 0x0f };
sendCommand(data1);
// on
var data2 = new List<byte> { 0x71, 0x23, 0x0f };
sendCommand(data2);
}
public void Shutdown()
{
// turn off the device
var data = new List<byte> { 0x71, 0x24, 0x0f };
sendCommand(data);
}
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, bool forced)
{
try
{
foreach (KeyValuePair<DeviceKeys, Color> key in keyColors)
{
//Iterate over each key and color and send them to your device
// the ESC sets the strip's color
if(key.Key == DeviceKeys.Peripheral)
{
SendColorToDevice(key.Value, forced);
}
}
return true;
}
catch(Exception exc)
{
return false;
}
}
//Custom method to send the color to the device
private void SendColorToDevice(Color color, bool forced)
{
Color processed_color = Color.Red;
var black = System.Drawing.Color.FromArgb(255, 0, 0, 0);
//Check if device's current color is the same, no need to update if they are the same
if (!device_color.Equals(color) || forced)
{
// if the input color is not black
if (color != black)
{
ColorConv.RGB rgb = new ColorConv.RGB(color.R, color.G, color.B);
ColorConv.HSV hsv = ColorConv.RGBToHSV(rgb);
hsv.V = _brightness; // <- Brightness settings
ColorConv.RGB lower = ColorConv.HSVToRGB(hsv);
processed_color = Color.FromArgb(lower.R, lower.G, lower.B);
}
else
{
processed_color = color;
}
var data = new List<byte> { 0x31, processed_color.R, processed_color.G, processed_color.B, 0x00, 0xf0, 0x0f };
sendCommand(data);
device_color = color;
}
}
private void sendCommand(List<byte> list)
{
using (var client = new TcpClient(_ip, 5577))
{
var data = list;
int acc = 0;
foreach (var item in data)
{
acc += item;
}
data.Add(Convert.ToByte(acc % 256));
data.Add(0xFF);
var Array = data.ToArray();
using (NetworkStream stream = client.GetStream())
{
stream.WriteAsync(Array, 0, Array.Length);
}
}
}
}
public class ColorConv
{
public struct RGB
{
private byte _r;
private byte _g;
private byte _b;
public RGB(byte r, byte g, byte b)
{
this._r = r;
this._g = g;
this._b = b;
}
public byte R
{
get { return this._r; }
set { this._r = value; }
}
public byte G
{
get { return this._g; }
set { this._g = value; }
}
public byte B
{
get { return this._b; }
set { this._b = value; }
}
public bool Equals(RGB rgb)
{
return (this.R == rgb.R) && (this.G == rgb.G) && (this.B == rgb.B);
}
}
public struct HSV
{
private double _h;
private double _s;
private double _v;
public HSV(double h, double s, double v)
{
this._h = h;
this._s = s;
this._v = v;
}
public double H
{
get { return this._h; }
set { this._h = value; }
}
public double S
{
get { return this._s; }
set { this._s = value; }
}
public double V
{
get { return this._v; }
set { this._v = value; }
}
public bool Equals(HSV hsv)
{
return (this.H == hsv.H) && (this.S == hsv.S) && (this.V == hsv.V);
}
}
public static RGB HSVToRGB(HSV hsv)
{
double r = 0, g = 0, b = 0;
if (hsv.S == 0)
{
r = hsv.V;
g = hsv.V;
b = hsv.V;
}
else
{
int i;
double f, p, q, t;
if (hsv.H == 360)
hsv.H = 0;
else
hsv.H = hsv.H / 60;
i = (int)Math.Truncate(hsv.H);
f = hsv.H - i;
p = hsv.V * (1.0 - hsv.S);
q = hsv.V * (1.0 - (hsv.S * f));
t = hsv.V * (1.0 - (hsv.S * (1.0 - f)));
switch (i)
{
case 0:
r = hsv.V;
g = t;
b = p;
break;
case 1:
r = q;
g = hsv.V;
b = p;
break;
case 2:
r = p;
g = hsv.V;
b = t;
break;
case 3:
r = p;
g = q;
b = hsv.V;
break;
case 4:
r = t;
g = p;
b = hsv.V;
break;
default:
r = hsv.V;
g = p;
b = q;
break;
}
}
return new RGB((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static HSV RGBToHSV(RGB rgb)
{
double delta, min;
double h = 0, s, v;
min = Math.Min(Math.Min(rgb.R, rgb.G), rgb.B);
v = Math.Max(Math.Max(rgb.R, rgb.G), rgb.B);
delta = v - min;
if (v == 0.0)
s = 0;
else
s = delta / v;
if (s == 0)
h = 0.0;
else
{
if (rgb.R == v)
h = (rgb.G - rgb.B) / delta;
else if (rgb.G == v)
h = 2 + (rgb.B - rgb.R) / delta;
else if (rgb.B == v)
h = 4 + (rgb.R - rgb.G) / delta;
h *= 60;
if (h < 0.0)
h = h + 360;
}
return new HSV(h, s, (v / 255));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment