Skip to content

Instantly share code, notes, and snippets.

@nzbr
Last active May 26, 2021 21:21
Show Gist options
  • Save nzbr/a88943aa1904decd298e5765c7f5aaa5 to your computer and use it in GitHub Desktop.
Save nzbr/a88943aa1904decd298e5765c7f5aaa5 to your computer and use it in GitHub Desktop.
UDP Device Script for Aurora
using Aurora;
using Aurora.Devices;
using Aurora.Settings;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO.Ports;
public class UDP {
public string devicename = "UDP";
public bool enabled = true;
private VariableRegistry default_registry;
private Stopwatch redrawWatch = new Stopwatch();
private static UdpClient udp;
private WiFiConnection[] connections;
private static DeviceKeys devicekey;
private int ledcount;
private static int udpPort;
private Color lastColor;
private uint lastUpdateTime = uint.MaxValue;
public string GetDeviceName() {
return devicename;
}
public bool Initialize() {
devicekey = Global.Configuration.VarRegistry.GetVariable<DeviceKeys>("udp_devicekey");
ledcount = Global.Configuration.VarRegistry.GetVariable<int>("udp_led_count");
connections = new WiFiConnection[0];
udp = new UdpClient();
udpPort = Global.Configuration.VarRegistry.GetVariable<int>("udp_wifi_port_udp");
var ips = Global.Configuration.VarRegistry.GetVariable<string>("udp_wifi").Split(',');
connections = new WiFiConnection[ips.Length];
for (var i = 0; i < ips.Length; i++)
{
connections[i] = new WiFiConnection(ips[i]);
}
lastColor = Color.Black;
redrawWatch.Start();
return true;
}
public void Shutdown()
{
udp = null;
connections = null;
}
public void Reset()
{
Shutdown();
Initialize();
}
public bool Reconnect() { return true; } //That's done automatically
public bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, bool forced = false)
{
if (keyColors.ContainsKey(devicekey))
{
var c = keyColors[devicekey];
if (redrawWatch.ElapsedMilliseconds < 1000)
{
if (c == lastColor && !forced) return true;
} else
{
redrawWatch.Restart();
}
lastColor = c;
foreach (var connection in connections)
{
var payload = new byte[3*ledcount];
for (int i = 0; i < ledcount*3; i += 3) {
payload[i+0] = c.R;
payload[i+1] = c.G;
payload[i+2] = c.B;
}
connection.sendColors(payload);
}
}
return true;
}
public VariableRegistry GetRegisteredVariables() {
if (default_registry == null)
{
default_registry = new VariableRegistry();
default_registry.Register("udp_devicekey", DeviceKeys.Peripheral, "Key to Use", DeviceKeys.MOUSEPADLIGHT15, DeviceKeys.Peripheral);
default_registry.Register("udp_wifi", "", "IP Adresses (Comma separated)");
default_registry.Register("udp_wifi_port_udp", 19446, "UDP Port");
default_registry.Register("udp_led_count", 300, "LED Count");
}
return default_registry;
}
private class WiFiConnection
{
private string ip;
public WiFiConnection(string _ip)
{
ip = _ip;
}
public void sendColors(byte[] payload)
{
udp.SendAsync(payload, payload.Length, ip, udpPort);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment