Ejemplo n.º 1
0
        /// <summary>
        /// Execute the Action
        /// </summary>
        public void ExecAction(Classes.RGBAnimatorAction action)
        {
            if (OnStartAction != null)
            {
                OnStartAction(action);
            }

            startWorker(action);
        }
Ejemplo n.º 2
0
        public string AddAction(RGBAnimatorAction action)
        {
            List <RGBAnimatorAction> actions = new List <RGBAnimatorAction>(Actions);

            actions.Add(action);

            Actions = actions.ToArray();

            return(action.GUID);
        }
Ejemplo n.º 3
0
        private async Task _tf_DoWork(Object sender, DoWorkEventArgs e)
        {
            Classes.RGBAnimatorAction action = (Classes.RGBAnimatorAction)e.Argument;

            do
            {
                foreach (Classes.RGBAnimatorActionFrame frame in action.Frames)
                {
                    if (frame.UseTransition)
                    {
                        List <decimal> stepIncrementR    = new List <decimal>();
                        List <decimal> stepIncrementG    = new List <decimal>();
                        List <decimal> stepIncrementB    = new List <decimal>();
                        List <decimal> startingPositionR = new List <decimal>();
                        List <decimal> startingPositionG = new List <decimal>();
                        List <decimal> startingPositionB = new List <decimal>();

                        // Get greatest position movement count
                        // ---------------------------------------------------------------------------------------------------------------

                        int currentPositionMin = 0;
                        int toPositionMax      = 0;
                        for (byte x = 0; x < RGBEyes.INDEX_MAX; x++)
                        {
                            currentPositionMin = Functions.Min(currentPositionMin, _eyes.GetRed(x), _eyes.GetGreen(x), _eyes.GetBlue(x));
                            toPositionMax      = Functions.Max(toPositionMax, frame.Red[x], frame.Green[x], frame.Blue[x]);
                        }

                        int stepCount = Math.Abs(currentPositionMin - toPositionMax);

                        if (stepCount == 0)
                        {
                            continue;
                        }

                        // Determine Step
                        // Initialize startingPosition for a decimal point value of the current position
                        // Decimal is used for current position for accuracy
                        // ---------------------------------------------------------------------------------------------------------------
                        for (byte x = 0; x < RGBEyes.INDEX_MAX; x++)
                        {
                            stepIncrementR.Add(Math.Abs(frame.Red[x] - _eyes.GetRed(x)) / (decimal)stepCount);
                            stepIncrementG.Add(Math.Abs(frame.Green[x] - _eyes.GetGreen(x)) / (decimal)stepCount);
                            stepIncrementB.Add(Math.Abs(frame.Blue[x] - _eyes.GetBlue(x)) / (decimal)stepCount);

                            startingPositionR.Add(_eyes.GetRed(x));
                            startingPositionG.Add(_eyes.GetGreen(x));
                            startingPositionB.Add(_eyes.GetBlue(x));
                        }

                        // Execute Steps
                        // ---------------------------------------------------------------------------------------------------------------
                        int cnt = 0;

                        while (!_tf.CancellationPending)
                        {
                            List <RGBEyes.RGBDef> defs = new List <RGBEyes.RGBDef>();

                            for (byte x = 0; x < RGBEyes.INDEX_MAX; x++)
                            {
                                decimal newPositionR = 0;
                                decimal newPositionG = 0;
                                decimal newPositionB = 0;

                                if (startingPositionR[x] < frame.Red[x])
                                {
                                    newPositionR = _eyes.GetRed(x) + stepIncrementR[x];
                                }
                                else
                                {
                                    newPositionR = _eyes.GetRed(x) - stepIncrementR[x];
                                }

                                if (startingPositionG[x] < frame.Green[x])
                                {
                                    newPositionG = _eyes.GetGreen(x) + stepIncrementG[x];
                                }
                                else
                                {
                                    newPositionG = _eyes.GetGreen(x) - stepIncrementG[x];
                                }

                                if (startingPositionB[x] < frame.Blue[x])
                                {
                                    newPositionB = _eyes.GetBlue(x) + stepIncrementB[x];
                                }
                                else
                                {
                                    newPositionB = _eyes.GetBlue(x) - stepIncrementB[x];
                                }

                                byte r = (byte)Math.Max(0, newPositionR);
                                byte g = (byte)Math.Max(0, newPositionG);
                                byte b = (byte)Math.Max(0, newPositionB);

                                defs.Add(new RGBEyes.RGBDef()
                                {
                                    Index = x,
                                    Red   = r,
                                    Green = g,
                                    Blue  = b
                                });

                                if (_tf.CancellationPending)
                                {
                                    return;
                                }
                            }

                            if (!_ezb.IsConnected)
                            {
                                return;
                            }

                            _eyes.SetColor(defs.ToArray());

                            await Task.Delay(frame.TransitionTimeMS / stepCount);

                            if (cnt == stepCount)
                            {
                                break;
                            }

                            cnt++;
                        }
                    }
                    else
                    {
                        List <RGBEyes.RGBDef> defs = new List <RGBEyes.RGBDef>();

                        for (byte x = 0; x < RGBEyes.INDEX_MAX; x++)
                        {
                            defs.Add(new RGBEyes.RGBDef()
                            {
                                Index = x,
                                Red   = frame.Red[x],
                                Green = frame.Green[x],
                                Blue  = frame.Blue[x]
                            });
                        }

                        if (!_ezb.IsConnected)
                        {
                            return;
                        }

                        _eyes.SetColor(defs.ToArray());
                    }

                    await Task.Delay(frame.PauseTimeMS);

                    if (_tf.CancellationPending)
                    {
                        return;
                    }
                }
            } while (action.Repeats && !_tf.CancellationPending);
        }
Ejemplo n.º 4
0
 private void startWorker(Classes.RGBAnimatorAction action)
 {
     _tf.RunWorkerAsync(action);
 }