/// <summary>
        /// Generates a value corresponding to a LED by analyzing the screen
        /// </summary>
        /// <param name="ledIndex">index of the LED</param>
        /// <param name="data">The entire byte array corresponding to each pixel of the screen</param>
        /// <param name="positions">The position on the screen to analyze</param>
        /// <returns>a LED</returns>
        private Led GenerateLed(Target ledIndex, DataStream data, List<long> positions)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var result = new Led();
            result.LedIndex = ledIndex;

            if (positions.Count == 0)
            {
                result.Color = new Color(0, 0, 0);
                return result;
            }

            var buffer = new byte[4];
            var red = 0;
            var green = 0;
            var blue = 0;
            var total = 0;

            // colors average
            foreach (var position in positions)
            {
                data.Position = position;
                data.Read(buffer, 0, 4);
                red += buffer[2];
                green += buffer[1];
                blue += buffer[0];
                total++;
            }

            red /= total;
            green /= total;
            blue /= total;

            // gamma correction
            red = (byte)(Math.Pow(red / 255.0, 2) * 127.0 + 0.5);
            green = (byte)(Math.Pow(green / 255.0, 2) * 127.0 + 0.5);
            blue = (byte)(Math.Pow(blue / 255.0, 2) * 127.0 + 0.5);

            result.Color = new Color((byte)red, (byte)green, (byte)blue);
            return result;
        }
Пример #2
0
 /// <summary>
 /// Generates a query to ask the Arduino to change the color of an LED
 /// </summary>
 /// <param name="led">LED with desired color</param>
 /// <returns>The generated query</returns>
 public static byte[] GenerateLedQuery(Led led)
 {
     if (led.Color == null)
         led.Color = new Color(0, 0, 0);
     return new[] { (byte)led.LedIndex, led.Color.Red, led.Color.Green, led.Color.Blue };
 }
Пример #3
0
        public void ShowColor()
        {
            this.CurrentArduinoColor = ApplicationSettings.GetSetting<Color>("CurrentColor");
            this.CurrentArduinoBrightness = ApplicationSettings.GetSetting<byte>("CurrentBrightness");

            var led = new Led();
            led.LedIndex = Target.AllLeds;
            led.Color = this.CurrentArduinoColor;

            this.Bluetooth.Send(QueryManager.GenerateBrightnessQuery(this.CurrentArduinoBrightness));
            this.Bluetooth.Send(QueryManager.GenerateLedQuery(led));
        }