Exemple #1
0
        public override bool Execute()
        {
            const int red   = 0;
            const int green = 16;
            const int blue  = 32;

            try
            {
                const int totalColors = ColorForm.TrackedColors - 1;
                //prepare the color map
                var colorMap = new byte[3 * 16];
                for (int objectNum = 0; objectNum < ColorForm.TrackedColors; objectNum++)
                {
                    Color minColor = Settings.Default.MinColors[objectNum];
                    Color maxColor = Settings.Default.MaxColors[objectNum];
                    if (ColorUtils.IsNotSet(minColor, maxColor))
                    {
                        continue;                                           //skip as black == not set
                    }
                    for (int offset = 0; offset < 16; offset++)
                    {
                        //this is weird, but the gets us to the layout we need
                        //div by 17 to get it back to 0-15
                        var redMask =
                            (byte)((offset >= minColor.R / 17 && offset <= maxColor.R / 17) ? (1 << (totalColors - objectNum)) : 0);
                        colorMap[red + offset] |= redMask;

                        var greenMask =
                            (byte)((offset >= minColor.G / 17 && offset <= maxColor.G / 17) ? (1 << (totalColors - objectNum)) : 0);
                        colorMap[green + offset] |= greenMask;

                        var blueMask =
                            (byte)((offset >= minColor.B / 17 && offset <= maxColor.B / 17) ? (1 << (totalColors - objectNum)) : 0);
                        colorMap[blue + offset] |= blueMask;
                    }
                }

                DumpColorMap(colorMap);

                var cmd = new SetColorMapCommand(_appState, _commsPort)
                {
                    ColorMap = new List <byte>(colorMap)
                };
                cmd.Execute();
                if (cmd.IsSuccessful)
                {
                    Settings.Default.UploadedMinColors = (Color[])Settings.Default.MinColors.Clone();
                    Settings.Default.UploadedMaxColors = (Color[])Settings.Default.MaxColors.Clone();
                    Settings.Default.LastColorUpload   = DateTime.Now;
                    _colorTarget.SetupColors();

                    MessageBox.Show(_colorForm, "Color upload was successful!", Application.ProductName,
                                    MessageBoxButtons.OK, MessageBoxIcon.None);
                }
                else
                {
                    MessageBox.Show(_colorForm, cmd.ErrorDescription, Application.ProductName, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Error uploading colors {0}", ex));
                return(false);
            }
            return(true);
        }