public void Push(ControlDevice controlDevice) { int deviceIndex = ((CorsairDevice)controlDevice).CorsairDeviceIndex; int numberOfLedsToUpdate = controlDevice.LEDs.Length; int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); int ptrSize = structSize * numberOfLedsToUpdate; IntPtr ptr = Marshal.AllocHGlobal(ptrSize); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (var led in controlDevice.LEDs) { _CorsairLedColor color = new _CorsairLedColor { ledId = ((CorsairLedData)led.Data).CorsairLedId, r = (byte)led.Color.Red, g = (byte)led.Color.Green, b = (byte)led.Color.Blue }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColorsBufferByDeviceIndex(deviceIndex, numberOfLedsToUpdate, ptr); _CUESDK.CorsairSetLedsColorsFlushBuffer(); Marshal.FreeHGlobal(ptr); }
public void Pull(ControlDevice controlDevice) { /* * int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); * IntPtr ptr = Marshal.AllocHGlobal(structSize * controlDevice.LEDs.Count()); * IntPtr addPtr = new IntPtr(ptr.ToInt64()); * foreach (var led in controlDevice.LEDs) * { * _CorsairLedColor color = new _CorsairLedColor { ledId = (int)(led.Data.LEDNumber) }; * Marshal.StructureToPtr(color, addPtr, false); * addPtr = new IntPtr(addPtr.ToInt64() + structSize); * } * * _CUESDK.CorsairGetLedsColorsByDeviceIndex(((CorsairDevice)controlDevice).CorsairDeviceIndex, controlDevice.LEDs.Count(), ptr); * * _CorsairLedColorStruct[] derp = MakeArray<_CorsairLedColorStruct>(ptr.ToInt32(), controlDevice.LEDs.Length); * * IntPtr readPtr = ptr; * for (int i = 0; i < controlDevice.LEDs.Count(); i++) * { * _CorsairLedColor ledColor = (_CorsairLedColor)Marshal.PtrToStructure(readPtr, typeof(_CorsairLedColor)); * * var led= controlDevice.LEDs.FirstOrDefault(x => x.Data is CorsairLedData cled && cled.CorsairLedId == ledColor.ledId); * * if (led != null) * { * led.Color = new LEDColor(ledColor.r, ledColor.g, ledColor.b); * } * * readPtr = new IntPtr(readPtr.ToInt64() + structSize); * } * * Marshal.FreeHGlobal(ptr);*/ int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * controlDevice.LEDs.Count()); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (var led in controlDevice.LEDs) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)((CorsairLedData)led.Data).CorsairLedId }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairGetLedsColorsByDeviceIndex(((CorsairDevice)controlDevice).CorsairDeviceIndex, controlDevice.LEDs.Count(), ptr); IntPtr readPtr = ptr; for (int i = 0; i < controlDevice.LEDs.Count(); i++) { _CorsairLedColor ledColor = (_CorsairLedColor)Marshal.PtrToStructure(readPtr, typeof(_CorsairLedColor)); var setme = controlDevice.LEDs.FirstOrDefault(x => ((CorsairLedData)x.Data).CorsairLedId == ledColor.ledId); if (setme != null) { setme.Color = new LEDColor(ledColor.r, ledColor.g, ledColor.b); } readPtr = new IntPtr(readPtr.ToInt64() + structSize); } Marshal.FreeHGlobal(ptr); }