Example #1
0
        public static async Task Main(string[] args)
        {
            using (Aero aero = new Aero())
            {
                IRgbController rgb = aero.Keyboard.Rgb;

                byte r = 40;
                byte g = 255;
                byte b = 90;
                bool showOriginalWhite = false;

                byte[] image = new byte[512];

                for (;;)
                {
                    for (int i = 0; i < 128; ++i)
                    {
                        image[4 * i + 0] = (byte)i;
                        image[4 * i + 1] = r;
                        image[4 * i + 2] = g;
                        image[4 * i + 3] = b;
                    }


                    if (showOriginalWhite)
                    {
                        Console.WriteLine("white");
                        await rgb.SetEffectAsync(new RgbEffect { Type = RgbEffectType.Static, Color = RgbEffectColor.White, Brightness = 51 });
                    }
                    else
                    {
                        Console.WriteLine($"{r}, {g}, {b}");
                        await rgb.SetEffectAsync(new RgbEffect { Type = RgbEffectType.Custom0, Brightness = 51 });

                        await rgb.SetImageAsync(0, image);
                    }

                    await Task.Delay(50);

                    switch (Console.ReadKey(true).KeyChar)
                    {
                    case ' ':
                        showOriginalWhite = !showOriginalWhite;
                        break;

                    case 'r':
                        --r;
                        break;

                    case 'R':
                        ++r;
                        break;

                    case 'g':
                        --g;
                        break;

                    case 'G':
                        ++g;
                        break;


                    case 'b':
                        --b;
                        break;

                    case 'B':
                        ++b;
                        break;
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Apply the effect.
        /// </summary>
        private static async Task <bool> applyAsync(IRgbController rgb, EffectState state, bool suppressException)
        {
            // Check if the RGB controller even exists.
            if (rgb == null)
            {
                return(false);
            }

            if ((state & EffectState.LidClosed) != 0)
            {
                // Turn off when lid is closed.
                try
                {
                    int brightness = (await rgb.GetEffectAsync()).Brightness;
                    await rgb.SetEffectAsync(new RgbEffect
                    {
                        Type       = RgbEffectType.Static,
                        Color      = RgbEffectColor.Black,
                        Brightness = brightness,
                    });

                    return(true);
                }
                catch (Win32Exception) when(suppressException)
                {
                    return(false);
                }
                catch (IOException) when(suppressException)
                {
                    return(false);
                }
            }

            byte[] image = new byte[512];
            // void setColor(int key, Color color)
            // {
            //  image[4 * key + 0] = (byte)key;
            //  image[4 * key + 1] = color.R;
            //  image[4 * key + 2] = color.G;
            //  image[4 * key + 3] = color.B;
            // }

            // Fill with base color.
            for (int i = 0; i < 128; ++i)
            {
                setColor(i, baseColor);
            }

            // Apply changes to certain keys / areas.
            setColor(capsLockKey, (state & EffectState.Caps) != 0 ? highlightColor : baseColor);
            setColor(numLockKey, (state & EffectState.Scroll) != 0 ? highlightColor : baseColor);
            foreach (int k in numPadKeys)
            {
                setColor(k, (state & EffectState.Num) == 0 ? highlightColor : baseColor);
            }

            // Try to apply the effect.
            // Under certain conditions this seems to fail because the USB device gets disconnected (e.g. waking from sleep).
            // In that case it will exit the inner loop in order to try again.
            try
            {
                // Read current brightness from controller. This can be changed independently from this app by the user through
                // the keyboard brightness shortcut (Fn + Space).
                int brightness = (await rgb.GetEffectAsync()).Brightness;

                // Set new image.
                await rgb.SetImageAsync(0, image);

                await rgb.SetEffectAsync(new RgbEffect
                {
                    Type       = RgbEffectType.Custom0,
                    Brightness = brightness,
                });

                return(true);
            }
            catch (Win32Exception) when(suppressException)
            {
                return(false);
            }
            catch (IOException) when(suppressException)
            {
                return(false);
            }
        }