Exemplo n.º 1
0
        private static void RunLights()
        {
            ThreadRunning = true;
            Type current = null;

            selected = null;
            var stopwatch = new Stopwatch();

            using (var strip = new Strip(NUM_LIGHTS))
            {
                lock (wait)
                    pixels = strip.Lights;
                strip.DefineEdge(EDGE_BOTTOM, EDGE_RIGHT, EDGE_TOP, EDGE_LEFT);
                strip.Controller.Skip    = SKIP;
                strip.Controller.Reverse = REVERSE;
                var running = true;
                while (running)
                {
                    if (selected != null)
                    {
                        lock (wait)
                        {
                            selected.Execute(strip);
                            strip.Render();
                        }
                        frame++;
                        if (frame % 10 == 0)
                        {
                            frame = 0;
                            stopwatch.Stop();
                            var f = stopwatch.ElapsedMilliseconds / 1000f;
                            lock (wait)
                                if (f > 0)
                                {
                                    framerate = (int)Math.Truncate((1 / f) * 10);
                                }
                                else
                                {
                                    framerate = 0;
                                }
                            stopwatch.Reset();
                            stopwatch.Start();
                        }
                    }
                    Thread.Sleep(10);
                    lock (wait)
                    {
                        if (effect != current)
                        {
                            frame     = 0;
                            framerate = 0;
                            stopwatch.Reset();
                            stopwatch.Start();
                            if (effect == null)
                            {
                                current  = effect;
                                selected = null;
                                strip.Clear();
                            }
                            else
                            {
                                current = effect;
                                for (var i = 0; i < effects.Length; i++)
                                {
                                    if (effects[i] == effect)
                                    {
                                        selected = selection[i];
                                        break;
                                    }
                                }
                            }
                        }
                        running = !stopped;
                    }
                }
                lock (wait)
                    strip.Clear();
            }
            ThreadRunning = false;
        }