示例#1
0
        public void AfterAnimationHasFinishedBeyondTime()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationBlink * 10));

            Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
            Assert.IsTrue(animation.AnimationFinished);
        }
示例#2
0
        public void BlackColorBeyondTimeAndFinished()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationPulse * 10), RgbColor.Black());

            Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
            Assert.IsTrue(animation.AnimationFinished);
        }
示例#3
0
        public void BlackAtTheEndSecondPulse()
        {
            animation.RepeatPulse = 2;
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationPulse * 2));

            Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
            Assert.IsTrue(animation.AnimationFinished);
        }
示例#4
0
        public void TurnOffHalfWayThrough()
        {
            for (int i = animation.DurationBlink / 2; i < animation.DurationBlink; i++)
            {
                RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(i));

                Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
            }
            Assert.IsTrue(!animation.AnimationFinished);
        }
        void ClientDataReceived(object sender, BayeuxClient.DataReceivedEventArgs e)
        {
            Regex r = new Regex("/([^/]+)$");
            Match m = r.Match(e.Channel);

            if (m.Success)
            {
                if (m.Groups[1].Value == this.AccessCode)
                {
                    int channel  = this.LedChannel;
                    int ledStart = this.LedFirstIndex;
                    int ledEnd   = this.LedLastIndex;
                    int repeat   = 1;
                    int duration = 0;

                    if (e.Data.ContainsKey("channel"))
                    {
                        try
                        {
                            channel = Convert.ToInt32((string)e.Data["channel"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert channel parameter {0}", ex);
                        }

                        if (channel < 0 || channel > 2)
                        {
                            log.Warn("Invalid channel parameter, defaulting to 0");
                            channel = 0;
                        }
                    }

                    if (e.Data.ContainsKey("firstLed"))
                    {
                        try
                        {
                            ledStart = Convert.ToInt32((string)e.Data["firstLed"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert firstLed parameter {0}", ex);
                        }

                        if (ledStart < 0 || ledStart > 63)
                        {
                            log.Warn("Invalid firstLed parameter, defaulting to 0");
                            ledStart = 0;
                        }
                    }

                    if (e.Data.ContainsKey("lastLed"))
                    {
                        try
                        {
                            ledEnd = Convert.ToInt32((string)e.Data["lastLed"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert lastLed parameter {0}", ex);
                        }

                        if (ledEnd < 0 || ledEnd > 63)
                        {
                            log.Warn("Invalid lastLed parameter, defaulting to 0");
                            ledEnd = 0;
                        }
                    }

                    if (e.Data.ContainsKey("repeat"))
                    {
                        if (e.Data["repeat"] == "loop")
                        {
                            repeat = -1;
                        }
                        else
                        {
                            try
                            {
                                repeat = Convert.ToInt32((string)e.Data["repeat"]);
                            }
                            catch (Exception ex)
                            {
                                log.WarnFormat("Failed to convert repeat parameter {0}", ex);
                            }
                        }
                    }

                    if (e.Data.ContainsKey("duration"))
                    {
                        try
                        {
                            duration = Convert.ToInt32((string)e.Data["duration"]);
                        }
                        catch (Exception ex)
                        {
                            log.WarnFormat("Failed to convert duration parameter {0}", ex);
                        }
                    }


                    if (e.Data.ContainsKey("status") && (String)e.Data ["status"] == "off")
                    {
                        log.InfoFormat("Blinkstick device {0} turned off", this.Name);

                        Pattern   pattern   = new Pattern();
                        Animation animation = new Animation();
                        pattern.Animations.Add(animation);
                        animation.Color         = RgbColor.Black();
                        animation.DelaySetColor = 1;

                        OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, 1, 0);

                        //OnColorSend(channel, ledStart, ledEnd, 0, 0, 0, this.Device);
                    }
                    else if (e.Data.ContainsKey("color"))
                    {
                        String color = (String)e.Data ["color"];

                        log.InfoFormat("New color received for Blinkstick device {0} - {1}", color, this.Name);

                        Pattern   pattern   = new Pattern();
                        Animation animation = new Animation();
                        pattern.Animations.Add(animation);
                        animation.Color         = RgbColor.FromString(color);
                        animation.DelaySetColor = 1;

                        OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, 1, 0);

                        //RgbColor c = RgbColor.FromString (color);
                        //OnColorSend(channel, ledStart, ledEnd, c.R, c.G, c.B, this.Device);
                    }
                    else if (e.Data.ContainsKey("pattern"))
                    {
                        String patternName = (String)e.Data ["pattern"];

                        Pattern pattern = this.DataModel.FindPatternByName(patternName);

                        if (pattern != null)
                        {
                            OnPatternSend(channel, ledStart, ledEnd, this.Device, pattern, repeat, duration);
                        }
                        else
                        {
                            log.ErrorFormat("Pattern request received for Blinkstick device {0} - {1}, but pattern not found", patternName, this.Name);
                        }
                    }
                }
            }
        }
示例#6
0
        public void BlackAtTheEnd()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationPulse));

            Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
        }
示例#7
0
        public void StartsWithBlack()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime);

            Assert.AreEqual(RgbColor.Black().ToString(), newColor.ToString());
        }
示例#8
0
        public RgbColor GetColor(DateTime start, DateTime current, RgbColor refColor = null)
        {
            double fraction = 0;
            double frame    = (current - start).TotalMilliseconds;

            switch (this.AnimationType)
            {
            case AnimationTypeEnum.SetColor:
                if (frame >= this.DelaySetColor)
                {
                    this.AnimationFinished = true;
                    frame = this.DelaySetColor;
                }

                break;

            case AnimationTypeEnum.Blink:
                if (frame >= this.DurationBlink * this.RepeatBlink)
                {
                    this.AnimationFinished = true;
                    frame = this.DurationBlink * this.RepeatBlink - 1;
                }

                if (frame % this.DurationBlink < this.DurationBlink / 2)
                {
                    return(this.Color);
                }
                else
                {
                    return(RgbColor.Black());
                }

            case AnimationTypeEnum.Pulse:
                if (frame >= this.DurationPulse * this.RepeatPulse)
                {
                    this.AnimationFinished = true;
                    frame = this.DurationPulse * this.RepeatPulse;
                }

                fraction = frame % this.DurationPulse / this.DurationPulse;

                byte r;
                byte g;
                byte b;

                if (fraction <= 0.5)                         //Fade in
                {
                    fraction = fraction * 2;

                    r = (byte)Math.Ceiling(Color.R * fraction);
                    g = (byte)Math.Ceiling(Color.G * fraction);
                    b = (byte)Math.Ceiling(Color.B * fraction);
                }
                else                         //Fade out
                {
                    fraction = 1 - (fraction - 0.5) * 2;

                    r = (byte)Math.Floor(Color.R * fraction);
                    g = (byte)Math.Floor(Color.G * fraction);
                    b = (byte)Math.Floor(Color.B * fraction);
                }

                return(RgbColor.FromRgb(r, g, b));

            case AnimationTypeEnum.Morph:
                if (refColor == null)
                {
                    throw new ArgumentNullException("refColor", "Reference color is required for Morph animation");
                }

                if (frame >= this.DurationMorph)
                {
                    this.AnimationFinished = true;
                    frame = this.DurationMorph;
                }

                fraction = frame / this.DurationMorph;

                if (Color.R > refColor.R)
                {
                    r = (byte)Math.Ceiling(refColor.R + (Color.R - refColor.R) * fraction);
                }
                else
                {
                    r = (byte)Math.Floor(refColor.R + (Color.R - refColor.R) * fraction);
                }

                if (Color.G > refColor.G)
                {
                    g = (byte)Math.Ceiling(refColor.G + (Color.G - refColor.G) * fraction);
                }
                else
                {
                    g = (byte)Math.Floor(refColor.G + (Color.G - refColor.G) * fraction);
                }

                if (Color.B > refColor.B)
                {
                    b = (byte)Math.Ceiling(refColor.B + (Color.B - refColor.B) * fraction);
                }
                else
                {
                    b = (byte)Math.Floor(refColor.B + (Color.B - refColor.B) * fraction);
                }

                return(RgbColor.FromRgb(r, g, b));

            default:
                break;
            }


            return(this.Color);
        }
示例#9
0
        public void AfterAnimationHasFinished()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationMorph), RgbColor.Black());

            Assert.IsTrue(animation.AnimationFinished);
        }
示例#10
0
        public void TargetColorAtTheEnd()
        {
            RgbColor newColor = animation.GetColor(StartTime, StartTime.AddMilliseconds(animation.DurationMorph), RgbColor.Black());

            Assert.AreEqual(targetColor.ToString(), newColor.ToString());
        }
        private void ProcessSetColorRequest(RequestReceivedEventArgs e, BlinkStickDeviceSettings ledSettings)
        {
            RgbColor color    = RgbColor.Black();
            int      channel  = 0;
            int      firstLed = 0;
            int      lastLed  = 0;

            for (int i = 0; i < e.Context.Request.QueryString.AllKeys.Length; i++)
            {
                string key   = e.Context.Request.QueryString.AllKeys[i].ToLower();
                string value = e.Context.Request.QueryString.GetValues(i)[0];

                if (key == "channel")
                {
                    try
                    {
                        channel = Convert.ToInt32(value);
                        if (channel < 0 || channel > 2)
                        {
                            throw new Exception("not within range of 0..2");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid channel parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "firstled")
                {
                    try
                    {
                        firstLed = Convert.ToInt32(value);
                        if (firstLed < 0 || firstLed > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledStart parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "lastled")
                {
                    try
                    {
                        lastLed = Convert.ToInt32(value);
                        if (lastLed < 0 || lastLed > 63)
                        {
                            throw new Exception("not within range of 0..63");
                        }
                    }
                    catch (Exception ex)
                    {
                        server.SendResponseJson(422, new ErrorResponse()
                        {
                            error = String.Format("Invalid ledEnd parameter: {0}", ex.Message)
                        }, e.Context.Response);
                        e.Handled = true;
                        return;
                    }
                }
                else if (key == "color")
                {
                    try
                    {
                        color = RgbColor.FromString(value);
                    }
                    catch
                    {
                        try
                        {
                            color = RgbColor.FromString("#" + value);
                        }
                        catch
                        {
                            server.SendResponseJson(422, new ErrorResponse()
                            {
                                error = "Invalid color parameter"
                            }, e.Context.Response);
                            e.Handled = true;
                            return;
                        }
                    }
                }
            }

            try
            {
                Pattern pattern = new Pattern();
                pattern.Animations.Add(new Animation());
                pattern.Animations[0].AnimationType = AnimationTypeEnum.SetColor;
                pattern.Animations[0].DelaySetColor = 0;
                pattern.Animations[0].Color         = color;
                OnPatternSend(channel, (byte)firstLed, (byte)lastLed, ledSettings, pattern, 1, 0);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Failed to send color {0}", ex);
            }
            server.SendResponseJson(200, null, e.Context.Response);

            e.Handled = true;
        }