internal static LedState GetColor(this string str)
        {
            // Remove any internal spaces/tabs:
            str = str.RemoveChars(new[] { ' ', '\t' });

            string pattern = @"(\d+,\d+,\d+,\d+)";
            var    match   = Regex.Match(str, pattern);

            if (!match.Success)
            {
                throw new Exception();
            }

            var color = new LedState
            {
                // Convert from 1-based to 0-based led index:
                Red    = int.Parse(str.Split(',')[0]),
                Green  = int.Parse(str.Split(',')[1]),
                Blue   = int.Parse(str.Split(',')[2]),
                Bright = int.Parse(str.Split(',')[3])
            };

            // Validation color component values:
            if ((color.Red < 0 || color.Red > 255) ||
                (color.Green < 0 || color.Green > 255) ||
                (color.Blue < 0 || color.Blue > 255) ||
                (color.Bright < 0 || color.Bright > 31))
            {
                Console.WriteLine("Color value(s) outside allowed range");
                throw new Exception();
            }

            return(color);
        }
Beispiel #2
0
 internal static bool IsColor(this LedState ledState, int red, int green, int blue, int bright)
 {
     return(ledState.Red == red &&
            ledState.Green == green &&
            ledState.Blue == blue &&
            ledState.Bright == bright);
 }
Beispiel #3
0
 internal bool Equals(LedState otherLedState)
 {
     return(
         Red == otherLedState.Red &&
         Green == otherLedState.Green &&
         Blue == otherLedState.Blue &&
         Bright == otherLedState.Bright);
 }
        internal void Parse()
        {
            CodeLine.LineStr = DefineTable.Dealias(CodeLine.LineStr);

            // Perform regex:
            const string instrPattern = "glowimmediate";
            const string gap          = @"[\s|\t]*";
            const string ledsPattern  = @"\[(.*?)]";
            const string colorPattern = @"\((.*?)\)";
            var          pattern      = $"^{instrPattern}{gap}:{gap}{ledsPattern}{gap}{colorPattern}$";
            var          match        = Regex.Match(CodeLine.LineStr, pattern);

            if (!match.Success)
            {
                Console.WriteLine("Invalid 'glowImmediate' instruction");
                throw new Exception();
            }

            // Get zero-based led list:
            try
            {
                LedIdxList = match.Groups[1].Value.GetLeds();
            }
            catch
            {
                Console.WriteLine("Invalid LED(s) in 'glowImmediate' instruction");
                throw new Exception();
            }

            // Get color:
            try
            {
                LedColor = match.Groups[2].Value.GetColor();
            }
            catch
            {
                Console.WriteLine("Invalid color in 'glowImmediate' instruction");
                throw new Exception();
            }
        }
Beispiel #5
0
        internal LedState DeepCopy()
        {
            var ledState = new LedState(Red, Green, Blue, Bright);

            return(ledState);
        }
        internal void Parse()
        {
            CodeLine.LineStr = DefineTable.Dealias(CodeLine.LineStr);

            // Perform regex:
            const string instrPattern    = "glowramp";
            const string gap             = @"[\s|\t]*";
            const string ledsPattern     = @"\[(.*?)]";
            const string colorPattern    = @"\((.*?)\)";
            const string timePattern     = @"(\d+)";
            const string timeUnitPattern = @"(ms|s|m|h|t)";
            var          pattern         = $@"^{instrPattern}{gap}:{gap}{ledsPattern}{gap}{colorPattern}{gap}to{gap}{colorPattern}{gap}in{gap}{timePattern}{timeUnitPattern}$";
            var          match           = Regex.Match(CodeLine.LineStr, pattern);

            if (!match.Success)
            {
                Console.WriteLine("Invalid 'glowRamp' instruction");
                throw new Exception();
            }

            // Get zero-based led list:
            try
            {
                LedIdxList = match.Groups[1].Value.GetLeds();
            }
            catch
            {
                Console.WriteLine("Invalid LED(s) in 'glowRamp' instruction");
                throw new Exception();
            }

            // Get color:
            try
            {
                // Get start color:
                LedColorFrom = match.Groups[2].Value.GetColor();

                // Get end color:
                LedColorTo = match.Groups[3].Value.GetColor();
            }
            catch
            {
                Console.WriteLine("Invalid color in 'glowRamp' instruction");
                throw new Exception();
            }

            // Get ramp duration in millisecs:
            long millisecs = match.Groups[4].Value.GetDuration(match.Groups[5].Value);

            if (millisecs > 3888000000)    // 45-day upper limit.
            {
                Console.WriteLine("Pause duration exceed 45-days (3888000-seconds)");
                throw new Exception();
            }
            RampMs = (uint)millisecs;

            // Calculate from-to delta (negative values indicate decrement):
            LedColorDiff.Red    = LedColorTo.Red - LedColorFrom.Red;
            LedColorDiff.Green  = LedColorTo.Green - LedColorFrom.Green;
            LedColorDiff.Blue   = LedColorTo.Blue - LedColorFrom.Blue;
            LedColorDiff.Bright = LedColorTo.Bright - LedColorFrom.Bright;

            // Calculate red tick/color step to get as close as possible to target color in ramp ticks:
            if (LedColorDiff.Red != 0 && RampTicks >= Math.Abs(LedColorDiff.Red))
            {
                TickStep.Red  = (int)Math.Floor((double)RampTicks / Math.Abs(LedColorDiff.Red));
                ColorStep.Red = 1;
            }
            else if (LedColorDiff.Red != 0 && RampTicks < Math.Abs(LedColorDiff.Red))
            {
                TickStep.Red  = 1;
                ColorStep.Red = (int)Math.Floor((double)Math.Abs(LedColorDiff.Red) / RampTicks);
            }

            // Calculate green tick/color step to get as close as possible to target color in ramp ticks:
            if (LedColorDiff.Green != 0 && RampTicks >= Math.Abs(LedColorDiff.Green))
            {
                TickStep.Green  = (int)Math.Floor((double)RampTicks / Math.Abs(LedColorDiff.Green));
                ColorStep.Green = 1;
            }
            else if (LedColorDiff.Green != 0 && RampTicks < Math.Abs(LedColorDiff.Green))
            {
                TickStep.Green  = 1;
                ColorStep.Green = (int)Math.Floor((double)Math.Abs(LedColorDiff.Green) / RampTicks);
            }

            // Calculate blue tick/color step to get as close as possible to target color in ramp ticks:
            if (LedColorDiff.Blue != 0 && RampTicks >= Math.Abs(LedColorDiff.Blue))
            {
                TickStep.Blue  = (int)Math.Floor((double)RampTicks / Math.Abs(LedColorDiff.Blue));
                ColorStep.Blue = 1;
            }
            else if (LedColorDiff.Blue != 0 && RampTicks < Math.Abs(LedColorDiff.Blue))
            {
                TickStep.Blue  = 1;
                ColorStep.Blue = (int)Math.Floor((double)Math.Abs(LedColorDiff.Blue) / RampTicks);
            }

            // Calculate bright tick/color step to get as close as possible to target color in ramp ticks:
            if (LedColorDiff.Bright != 0 && RampTicks >= Math.Abs(LedColorDiff.Bright))
            {
                TickStep.Bright  = (int)Math.Floor((double)RampTicks / Math.Abs(LedColorDiff.Bright));
                ColorStep.Bright = 1;
            }
            else if (LedColorDiff.Bright != 0 && RampTicks < Math.Abs(LedColorDiff.Bright))
            {
                TickStep.Bright  = 1;
                ColorStep.Bright = (int)Math.Floor((double)Math.Abs(LedColorDiff.Bright) / RampTicks);
            }

            // Generate pre glow immediate instruction to initialize glow ramp:
            PreGlowImmediateInstruction = new GlowImmediateInstruction
            {
                LedIdxList = new List <int>(LedIdxList),
                LedColor   = LedColorFrom.DeepCopy(),
                Path       = Path,
                ZOrder     = ZOrder
            };

            // Generate post glow immediate instruction to finalize glow ramp:
            PostGlowImmediateInstruction = new GlowImmediateInstruction
            {
                LedIdxList = new List <int>(LedIdxList),
                LedColor   = LedColorTo.DeepCopy(),
                Path       = Path,
                ZOrder     = ZOrder
            };

            // Generate post-ramp ledstrip state:
            foreach (var ledIdx in LedIdxList)
            {
                LedstripPostState.LedStateList[ledIdx] = LedColorTo;
            }
        }