public static byte[] GenWS2812(Color[] cc) { UInt32 count = 0; if (cc.Length > 0) { byte[] data = new byte[cc.Length * 4 * 2]; foreach (Color c in cc) { byte r = (byte)(c.R * c.ScA); byte g = (byte)(c.G * c.ScA); byte b = (byte)(c.B * c.ScA); LedData led = new LedData() { addr = count, color = (UInt32)(r + (g << 8) + (b << 16)) }; byte[] data_b = getBytes(led); data_b.CopyTo(data, count * 4 * 2); count++; } return(data); } return(null); }
static byte[] getBytes(LedData str) { int size = Marshal.SizeOf(str); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(str, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }