For() public method

public For ( int state ) : IRunnableState
state int
return IRunnableState
        public override void Start()
        {
            hours.AddRange("5:00 pm", "11:00 pm");

            bool.TryParse(Exec.GetKey("InflatablesRunning", false.ToString()), out inflatablesRunning);

            buttonOverrideHours.ActiveChanged += (i, e) =>
            {
                if (e.NewState)
                {
                    hours.SetForced(true);
                }
                else
                {
                    hours.SetForced(null);
                }
            };

            candyCane
            .WhenExecuted
            .SetUp(() => allPixels.TurnOff())
            .Execute(instance =>
            {
                const int spacing = 4;

                while (true)
                {
                    for (int i = 0; i < spacing; i++)
                    {
                        allPixels.Inject((i % spacing) == 0 ? Color.Red : Color.White, 0.5);

                        instance.WaitFor(S(0.2), true);
                    }
                }
            })
            .TearDown(() =>
            {
                allPixels.TurnOff();
            });

            twinkleSeq
            .WhenExecuted
            .SetUp(() => allPixels.TurnOff())
            .Execute(instance =>
            {
                var rnd = new Random();

                while (!instance.IsCancellationRequested)
                {
                    allPixels.SetAll(Color.White, 0.5);
                    instance.WaitFor(S(1.0), true);

                    int pixel1 = rnd.Next(allPixels.Pixels);
                    int pixel2 = rnd.Next(allPixels.Pixels);
                    var task1  = allPixels.FadeToAsync(instance, pixel1, Color.Red, 1.0, S(2.0));
                    var task2  = allPixels.FadeToAsync(instance, pixel2, Color.Red, 1.0, S(2.0));
                    Task.WaitAll(task1, task2);

                    instance.WaitFor(S(1.0), true);

                    task1 = allPixels.FadeToAsync(instance, pixel1, Color.White, 0.5, S(1.0));
                    task2 = allPixels.FadeToAsync(instance, pixel2, Color.White, 0.5, S(1.0));
                    Task.WaitAll(task1, task2);
                }
            })
            .TearDown(() => allPixels.TurnOff());

            backgroundLoop
            .WhenExecuted
            .SetUp(() =>
            {
                pulsatingEffect1.Start();
                flickerEffect.Start();
                switchButtonBlue.SetPower(true);
                switchButtonRed.SetPower(true);
                lightTreeUp.SetOnlyColor(Color.Red);

                faderIn.Restart();

                Executor.Current.Execute(twinkleSeq);
            })
            .TearDown(() =>
            {
                Executor.Current.Cancel(twinkleSeq);

                switchButtonBlue.SetPower(false);
                switchButtonRed.SetPower(false);
                EverythingOff();
            });

            offHours1Seq
            .WhenExecuted
            .Execute(instance =>
            {
                audioPlayer.PlayEffect("force1");
                instance.WaitFor(S(4));
            });

            offHours2Seq
            .WhenExecuted
            .Execute(instance =>
            {
                audioPlayer.PlayEffect("darkside");
                instance.WaitFor(S(4));
            });

            music1Seq
            .WhenExecuted
            .SetUp(() =>
            {
                audioPlayer.CueTrack("21. Christmas Canon Rock");
                // Make sure it's ready
                System.Threading.Thread.Sleep(800);

                EverythingOff();
            })
            .Execute(instance =>
            {
                audioPlayer.ResumeTrack();
                var task = timeline1.Start();

                try
                {
                    task.Wait(instance.CancelToken);

                    instance.WaitFor(S(10));
                }
                finally
                {
                    timeline1.Stop();
                    audioPlayer.PauseTrack();
                }

                if (!instance.IsCancellationRequested)
                {
                    instance.WaitFor(S(2));
                }
                EverythingOff();

                instance.WaitFor(S(2));
            })
            .TearDown(() =>
            {
                EverythingOff();
            });

            starwarsCane
            .WhenExecuted
            .SetUp(() =>
            {
                allPixels.TurnOff();
                starwarsPixels.TurnOff();
            })
            .Execute(instance =>
            {
                const int spacing = 4;

                while (!instance.CancelToken.IsCancellationRequested)
                {
                    for (int i = 0; i < spacing; i++)
                    {
                        switch (i % spacing)
                        {
                        case 0:
                        case 1:
                            starwarsPixels.InjectRev(Color.Yellow, 1.0);
                            break;

                        case 2:
                        case 3:
                            starwarsPixels.InjectRev(Color.Orange, 0.2);
                            break;
                        }

                        instance.WaitFor(S(0.1));

                        if (instance.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                }
            })
            .TearDown(() => starwarsPixels.TurnOff());

            fatherSeq
            .WhenExecuted
            .Execute(instance =>
            {
                EverythingOff();

                Executor.Current.Execute(starwarsCane);

                audioPlayer.PlayTrack("01. Star Wars - Main Title");

                //lightCeiling1.SetOnlyColor(Color.Yellow);
                //lightCeiling2.SetOnlyColor(Color.Yellow);
                //lightCeiling3.SetOnlyColor(Color.Yellow);
                //pulsatingEffect2.Start();
                instance.WaitFor(S(16));
                //pulsatingEffect2.Stop();
                audioPlayer.PauseTrack();
                Executor.Current.Cancel(starwarsCane);
                allPixels.TurnOff();
                instance.WaitFor(S(0.5));

                elJesus.SetPower(true);
                pulsatingStar.Start();
                lightJesus.SetColor(Color.White, 0.3);
                light3wise.SetOnlyColor(Color.LightYellow);
                light3wise.RunEffect(new Effect2.Fader(0.0, 1.0), S(1.0));
                lightVader.SetOnlyColor(Color.LightYellow);
                lightVader.RunEffect(new Effect2.Fader(0.0, 1.0), S(1.0));

                instance.WaitFor(S(2.5));

                //elLightsaber.SetPower(true);
                audioPlayer.PlayEffect("saberon");
                for (int sab = 0; sab < 60; sab++)
                {
                    saberPixels.Inject(Color.Red, 0.5);
                    instance.WaitFor(S(0.01));
                }

//                    lightVader.SetColor(Color.Red, 1.0);
                audioPlayer.PlayEffect("father");
                instance.WaitFor(S(4));

                lightVader.TurnOff();
                light3wise.TurnOff();
                lightJesus.TurnOff();
                pulsatingStar.Stop();
                elJesus.TurnOff();

                audioPlayer.PlayEffect("force1");
                instance.WaitFor(S(4));

                lightVader.TurnOff();
                audioPlayer.PlayEffect("saberoff");
                instance.WaitFor(S(0.7));
                for (int sab = 0; sab < 30; sab++)
                {
                    saberPixels.InjectRev(Color.Black, 0);
                    saberPixels.InjectRev(Color.Black, 0);
                    instance.WaitFor(S(0.01));
                }
                //elLightsaber.SetPower(false);
                instance.WaitFor(S(2));

                //lightJesus.TurnOff();
                //light3wise.TurnOff();
                //elLightsaber.TurnOff();
                //pulsatingStar.Stop();
                //elJesus.TurnOff();
                //instance.WaitFor(S(2));
            })
            .TearDown(() => {
                EverythingOff();
            });


            waveformSeq
            .WhenExecuted
            .SetUp(() =>
            {
                audioPlayer.CueTrack("05. Frozen - Let It Go");
                // Make sure it's ready
                System.Threading.Thread.Sleep(800);

                EverythingOff();
            })
            .Execute(i =>
            {
                var timer = new Controller.HighPrecisionTimer(50, false);
                byte[] buffer;
                using (var fs = System.IO.File.OpenRead("Let It Go - Waveform 50ms.dat"))
                {
                    buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                }

                var cb = new ColorBrightness[allPixels.Pixels];
                for (int cb_pos = 0; cb_pos < cb.Length; cb_pos++)
                {
                    cb[cb_pos] = new ColorBrightness(Color.Turquoise, 0.0);
                }

                double lastValue = 0;

                timer.Tick += (o, e) =>
                {
                    int pos = (int)e.TotalTicks;
                    if (pos >= buffer.Length)
                    {
                        e.Cancel = true;
                        return;
                    }

                    double curValue = buffer[pos].GetDouble();
                    if (curValue > lastValue)
                    {
                        lastValue = curValue;
                    }
                    double value = lastValue;
                    lastValue    = (lastValue - 0.02).Limit(0.0, 1.0);

                    const double d1             = 0.00;
                    const double d2             = 0.15;
                    const double d3             = 0.20;
                    const double d4             = 0.25;
                    lightHat1.Brightness        = value.LimitAndScale(d1, 0.2);
                    lightHat2.Brightness        = value.LimitAndScale(d1, 0.2);
                    lightHat3.Brightness        = value.LimitAndScale(d2, 0.2);
                    lightHat4.Brightness        = value.LimitAndScale(d2, 0.2);
                    lightTreeUp.Brightness      = value.LimitAndScale(d3, 0.2);
                    lightGarland1.Brightness    = value.LimitAndScale(d3, 0.2);
                    lightGarland2.Brightness    = value.LimitAndScale(d3, 0.2);
                    lightGarland3.Brightness    = value.LimitAndScale(d3, 0.2);
                    lightGarland4.Brightness    = value.LimitAndScale(d3, 0.2);
                    lightNet1.Brightness        = value.LimitAndScale(d2, 0.2);
                    lightNet2.Brightness        = value.LimitAndScale(d2, 0.2);
                    lightSnow1.Brightness       = value.LimitAndScale(d3, 0.2);
                    lightSnow2.Brightness       = value.LimitAndScale(d3, 0.2);
                    lightStairs1.Brightness     = value.LimitAndScale(d1, 0.2);
                    lightStairs2.Brightness     = value.LimitAndScale(d1, 0.2);
                    lightDeerSmall.Brightness   = curValue;
                    lightTopperLarge.Brightness = curValue;
                    lightDeerLarge.Brightness   = curValue;
                    lightTopperSmall.Brightness = curValue;
                    lightString1.Brightness     = value.LimitAndScale(d4, 0.2);
                    lightString2.Brightness     = value.LimitAndScale(d4, 0.2);
                    lightXmasTree.Brightness    = value.LimitAndScale(d4, 0.2);
                    lightStar.Brightness        = value.LimitAndScale(d4, 0.2);

                    int vuPos = (int)(cb.Length * value);
                    for (int cb_pos = 0; cb_pos < cb.Length; cb_pos++)
                    {
                        cb[cb_pos].Brightness = cb_pos < vuPos ? 1.0 : 0.2;
                    }

                    allPixels.SetColors(0, cb);
                };

                timer.Start();
                audioPlayer.ResumeTrack();

                timer.WaitUntilFinished(i);
            })
            .TearDown(() =>
            {
                EverythingOff();
            });

            buttonStartInflatables.ActiveChanged += (o, e) =>
            {
                if (e.NewState && hours.IsOpen)
                {
                    InflatablesRunning = true;

                    switchDeerHuge.SetPower(true);
                    switchSanta.SetPower(true);
                }
            };

            this.oscServer.RegisterAction <int>("/osc/button1", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    stateMachine.SetState(States.Vader);
                }
            });

            this.oscServer.RegisterAction <int>("/osc/button2", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    stateMachine.SetState(States.Music1);
                }
            });

            this.oscServer.RegisterAction <int>("/osc/button3", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    stateMachine.SetState(States.Music2);
                }
            });

            this.oscServer.RegisterAction <int>("/osc/button4", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    stateMachine.SetState(States.Background);
                }
            });

            this.oscServer.RegisterAction <int>("/osc/button5", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    audioPlayer.PlayEffect("darkside");
                }
            });

            this.oscServer.RegisterAction <int>("/osc/button6", (msg, data) =>
            {
                if (data.Any() && data.First() == 1)
                {
                    audioPlayer.PlayEffect("Darth Breathing");
                }
            });

            // Test Button
            buttonTest.ActiveChanged += (sender, e) =>
            {
                //                lightGarland4.Brightness = e.NewState ? 1.0 : 0.0;

                if (e.NewState)
                {
                    Exec.Execute(waveformSeq);
                }
                else
                {
                    Exec.Cancel(waveformSeq);
                }

                if (!e.NewState)
                {
                    return;
                }


                //Exec.Cancel(candyCane);

                //allPixels.RunEffect(new Effect2.Fader(1.0, 0.0), S(2.0)).Wait();
                //allPixels.SetAllOnlyColor(Color.Purple);
                //allPixels.RunEffect(new Effect2.Fader(0.0, 1.0), S(2.0)).Wait();
                //allPixels.RunEffect(new Effect2.Fader(1.0, 0.0), S(2.0)).Wait();

                //allPixels.SetAllOnlyColor(Color.Orange);
                //allPixels.RunEffect(new Effect2.Fader(0.0, 1.0), S(2.0)).Wait();

                //allPixels.RunEffect(new Effect2.Fader(1.0, 0.0), S(2.0)).Wait();

                //Exec.Execute(candyCane);
            };

            buttonBlue.ActiveChanged += (o, e) =>
            {
                if (!e.NewState)
                {
                    return;
                }

                if (mute.HasValue && (DateTime.Now - mute.Value).TotalSeconds < 30)
                {
                    return;
                }

                if (hours.IsOpen)
                {
                    if (stateMachine.CurrentState == States.Music1 ||
                        stateMachine.CurrentState == States.Music2)
                    {
                        // Stop
                        //stateMachine.SetState(States.Background);
                        return;
                    }

                    switch (this.whichMusic++ % 2)
                    {
                    case 0:
                        stateMachine.SetState(States.Music1);
                        break;

                    case 1:
                        stateMachine.SetState(States.Music2);
                        break;
                    }
                }
                else
                {
                    Exec.Execute(offHours1Seq);
                }

                mute = DateTime.Now;
            };

            buttonRed.ActiveChanged += (o, e) =>
            {
                if (!e.NewState)
                {
                    return;
                }

                if (mute.HasValue && (DateTime.Now - mute.Value).TotalSeconds < 30)
                {
                    return;
                }

                if (hours.IsOpen)
                {
                    if (stateMachine.CurrentState == States.Vader)
                    {
                        return;
                    }
                    //stateMachine.SetState(States.Background);
                    else
                    {
                        stateMachine.SetState(States.Vader);
                    }
                }
                else
                {
                    Exec.Execute(offHours2Seq);
                }

                mute = DateTime.Now;
            };

            audioPlayer.AudioTrackDone += (o, e) =>
            {
                //                switchButtonBlue.SetPower(false);
            };

            pulsatingEffect1
            .AddDevice(lightStar);

            pulsatingStar
            .AddDevice(lightStar);

            flickerEffect
            .AddDevice(lightHat1)
            .AddDevice(lightHat2)
            .AddDevice(lightHat3)
            .AddDevice(lightHat4);

            hours.OpenHoursChanged += (i, e) =>
            {
                if (e.IsOpenNow)
                {
                    stateMachine.SetBackgroundState(States.Background);
                    stateMachine.SetState(States.Background);
                    //lightTreeUp.SetColor(Color.Red, 1.0);
                    //lightSnow1.SetBrightness(1.0);
                    //lightSnow2.SetBrightness(1.0);

                    if (InflatablesRunning)
                    {
                        switchDeerHuge.SetPower(true);
                        switchSanta.SetPower(true);
                    }
                }
                else
                {
                    if (buttonOverrideHours.Active)
                    {
                        return;
                    }

                    stateMachine.Hold();
                    stateMachine.SetBackgroundState(null);
                    EverythingOff();
                    System.Threading.Thread.Sleep(200);

                    switchDeerHuge.TurnOff();
                    switchSanta.TurnOff();
                    InflatablesRunning = false;
                }
            };

            faderIn
            .AddDevice(lightGarland1)
            .AddDevice(lightGarland2)
            .AddDevice(lightGarland3)
            .AddDevice(lightGarland4)
            .AddDevice(lightStairs1)
            .AddDevice(lightStairs2)
            .AddDevice(lightXmasTree)
            .AddDevice(lightDeerSmall)
            .AddDevice(lightDeerLarge)
            .AddDevice(lightTopperLarge)
            .AddDevice(lightTopperSmall)
            .AddDevice(lightNet1)
            .AddDevice(lightNet2)
            .AddDevice(lightString1)
            .AddDevice(lightString2)
            .AddDevice(lightSnow1)
            .AddDevice(lightSnow2)
            .AddDevice(lightTreeUp);

            stateMachine.ForFromSequence(States.Background, backgroundLoop);
            stateMachine.ForFromSequence(States.Music1, music1Seq);
            stateMachine.ForFromSequence(States.Music2, waveformSeq);
            stateMachine.ForFromSequence(States.Vader, fatherSeq);

            hatLightState.For(0).Execute(i => lightHat1.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));
            hatLightState.For(1).Execute(i => lightHat2.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));
            hatLightState.For(2).Execute(i => lightHat3.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));
            hatLightState.For(3).Execute(i => lightHat4.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));
            hatLightState.For(4).Execute(i => lightHat3.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));
            hatLightState.For(5).Execute(i => lightHat2.RunEffect(new Effect2.Fader(1.0, 0.0), S(0.5)));

            //lightGarland1.Follow(hours);
            //lightGarland2.Follow(hours);
            //lightGarland3.Follow(hours);
            //lightGarland4.Follow(hours);
            //lightGarland5.Follow(hours);
            //lightXmasTree.Follow(hours);
            //lightStairs1.Follow(hours);
            //lightDeerSmall.Follow(hours);
            //lightDeerLarge.Follow(hours);
            //lightTopperLarge.Follow(hours);
            //lightTopperSmall.Follow(hours);
            //lightNet1.Follow(hours);
            //lightNet2.Follow(hours);
            //lightString1.Follow(hours);
            //lightString2.Follow(hours);

            //light3wise.Follow(hours);
            //lightVader.Follow(hours);
            //switchButtonBlue.Follow(hours);
            //switchButtonRed.Follow(hours);

            ConfigureMusic1();
        }