private void UpdateLeds(ICollection <LedUpateRequest> updateRequests) { updateRequests = updateRequests.Where(x => x.Color != CorsairColor.Transparent).ToList(); OnLedsUpdating(updateRequests); if (updateRequests.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * updateRequests.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (LedUpateRequest ledUpdateRequest in updateRequests) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)ledUpdateRequest.LedId, r = ledUpdateRequest.Color.R, g = ledUpdateRequest.Color.G, b = ledUpdateRequest.Color.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColors(updateRequests.Count, ptr); Marshal.FreeHGlobal(ptr); } OnLedsUpdated(updateRequests); }
private Dictionary <CorsairLedId, CorsairColor> GetColors() { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * LedMapping.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (KeyValuePair <CorsairLedId, CorsairLed> led in LedMapping) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)led.Value.Id }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairGetLedsColors(LedMapping.Count, ptr); IntPtr readPtr = ptr; Dictionary <CorsairLedId, CorsairColor> colorData = new Dictionary <CorsairLedId, CorsairColor>(); for (int i = 0; i < LedMapping.Count; i++) { _CorsairLedColor ledColor = (_CorsairLedColor)Marshal.PtrToStructure(readPtr, typeof(_CorsairLedColor)); colorData.Add((CorsairLedId)ledColor.ledId, new CorsairColor((byte)ledColor.r, (byte)ledColor.g, (byte)ledColor.b)); readPtr = new IntPtr(readPtr.ToInt64() + structSize); } Marshal.FreeHGlobal(ptr); return(colorData); }
/// <inheritdoc /> protected override void UpdateLeds(IEnumerable <Led> ledsToUpdate) { List <Led> leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList(); if (leds.Count > 0) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * leds.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (Led led in leds) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)((CorsairLedId)led.Id).LedId, r = led.Color.R, g = led.Color.G, b = led.Color.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColors(leds.Count, ptr); Marshal.FreeHGlobal(ptr); } }
/// <inheritdoc /> protected override void Update(Dictionary <object, Color> dataSet) { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); foreach (KeyValuePair <object, Color> data in dataSet) { _CorsairLedColor color = new _CorsairLedColor { ledId = (int)data.Key, r = data.Value.R, g = data.Value.G, b = data.Value.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } _CUESDK.CorsairSetLedsColors(dataSet.Count, ptr); Marshal.FreeHGlobal(ptr); }