private static void HackStrutCData(ShipConstruct ship, Part p,
                                    int part_base)
 {
     //Debug.Log (String.Format ("[EL] before {0}", p.customPartData));
     string[] Params = p.customPartData.Split(';');
     for (int i = 0; i < Params.Length; i++)
     {
         string[] keyval = Params[i].Split(':');
         string   Key    = keyval[0].Trim();
         string   Value  = keyval[1].Trim();
         if (Key == "tgt")
         {
             string[] pnameval = Value.Split('_');
             string   pname    = pnameval[0];
             int      val      = int.Parse(pnameval[1]);
             if (val == -1)
             {
                 Strut strut = new Strut(p, Params);
                 if (strut.target != null)
                 {
                     val = ship.parts.IndexOf(strut.target);
                 }
             }
             if (val != -1)
             {
                 val += part_base;
             }
             Params[i] = "tgt: " + pname + "_" + val.ToString();
             break;
         }
     }
     p.customPartData = String.Join("; ", Params);
     //Debug.Log (String.Format ("[EL] after {0}", p.customPartData));
 }
Exemplo n.º 2
0
        /// <summary>
        /// User has changed a strut or one of its settings.
        /// </summary>
        /// <param name="sender">The StrucPicker which generated this event</param>
        /// <param name="e">(unused)</param>
        void handle_StrutChangedEvent(object sender, EventArgs e)
        {
            Strut strut = ((StrutPicker)sender).Strut;

            webServiceAdapter.SetStrut(strut.Material.Name, strut.Length, strut.CrossSectionalArea);
            simulate();
        }
        private static void HackStrutCData(ShipConstruct ship, Part p,
											int part_base)
        {
            //Debug.Log (String.Format ("[EL] before {0}", p.customPartData));
            string[] Params = p.customPartData.Split (';');
            for (int i = 0; i < Params.Length; i++) {
                string[] keyval = Params[i].Split (':');
                string Key = keyval[0].Trim ();
                string Value = keyval[1].Trim ();
                if (Key == "tgt") {
                    string[] pnameval = Value.Split ('_');
                    string pname = pnameval[0];
                    int val = int.Parse (pnameval[1]);
                    if (val == -1) {
                        Strut strut = new Strut (p, Params);
                        if (strut.target != null) {
                            val = ship.parts.IndexOf (strut.target);
                        }
                    }
                    if (val != -1) {
                        val += part_base;
                    }
                    Params[i] = "tgt: " + pname + "_" + val.ToString ();
                    break;
                }
            }
            p.customPartData = String.Join ("; ", Params);
            //Debug.Log (String.Format ("[EL] after {0}", p.customPartData));
        }
Exemplo n.º 4
0
 // Use this for initialization
 void Start()
 {
     if (tree)
     {
         treeScript = GetComponent <TreeResource>();
     }
     else if (strut)
     {
         strutScript = GetComponent <Strut>();
     }
     else if (channelBuilder)
     {
         channelBuilderScript = GetComponent <ChannelBuilder>();
     }
     else if (channel)
     {
         channelScript = GetComponent <Channel>();
     }
     else if (windmill)
     {
         Debug.Log("Get the windmill");
     }
     else if (waterWheel)
     {
         Debug.Log("Get the waterWheel");
     }
 }
Exemplo n.º 5
0
            public void BreakOff()
            {
                DestroyJoint();

                if (!target)
                {
                    return;
                }

                if (mReceiving)
                {
                    target.BreakOff();
                }
                else
                {
                    Debug.Log("Tosh_AutoStrut breaking " + this + " from " + target);
                    mTarget.mTarget = null;
                }

                if (mStrut && strutRenderer)
                {
                    strutRenderer.enabled = false;
                }
                if (mAnchor && anchorRenderer)
                {
                    anchorRenderer.enabled = false;
                }

                mTarget = null;
            }
Exemplo n.º 6
0
            public void ConnectTo(Strut strut)
            {
                if (!strut)
                {
                    return;
                }

                mTarget            = strut;
                mTarget.mTarget    = this;
                mReceiving         = false;
                mTarget.mReceiving = true;

                if (mAnchor && anchorRenderer)
                {
                    anchorRenderer.enabled = true;
                }
                if (target.mAnchor && target.anchorRenderer)
                {
                    target.anchorRenderer.enabled = true;
                }

                if (mStrut && strutRenderer)
                {
                    strutRenderer.enabled = true;
                }

                Debug.Log("Tosh_AutoStrut connecting " + this + " to " + target);
            }
        /**
         * percentageLit: what percentage of this strut should be lit?
         * startLitRange,endLitRange refer to the portion of the lit range this
         *   strut represents. if it's the first strut startLitRange is 0.0; f it's
         *   the last lit strut, then endLitRange is 1.0. keep in mind that the lit
         *   range is not the same as the whole range.
         */
        private void UpdateStrut(
            Strut strut,
            double percentageLit,
            double startLitRange,
            double endLitRange
            )
        {
            double step = (endLitRange - startLitRange) / (strut.Length * percentageLit);

            for (int i = 0; i < strut.Length; i++)
            {
                double gradientPos =
                    strut.GetGradientPos(percentageLit, startLitRange, endLitRange, i);
                int color;
                if (gradientPos != -1.0)
                {
                    color = this.ColorFromPart(strut.Index, gradientPos);
                    //color = this.ColorFromIndex(strut.Index, gradientPos);
                    //color = this.ColorFromRandom(strut.Index);
                    //color = this.ColorFromPartAndSpoke(strut.Index, gradientPos);
                }
                else
                {
                    color = 0x000000;
                }
                this.dome.SetPixel(strut.Index, i, color);
            }
        }
Exemplo n.º 8
0
        public void Visualize()
        {
            //-----------------------------------------------------------------------
            // SETTING THE RACERS
            //-----------------------------------------------------------------------

            this.racer_spacing = this.config.domeRadialSize > 1 ? 1.0 : this.config.domeRadialSize;
            var curTicks = DateTime.Now.Ticks;

            if (this.lastTicks == -1)
            {
                this.lastTicks = curTicks;
            }
            else
            {
                double numSeconds = ((double)(curTicks - this.lastTicks)) / (1000 * 1000 * 10);
                foreach (Racer r in Racers)
                {
                    r.Move(numSeconds, audio, config);
                }
                this.lastTicks = curTicks;
            }

            //-----------------------------------------------------------------------
            // SETTING LEDS
            //-----------------------------------------------------------------------

            for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
            {
                Strut strut = Strut.FromIndex(this.config, i);
                var   leds  = LEDDomeOutput.GetNumLEDs(i);
                for (int j = 0; j < leds; j++)
                {
                    var pixel_p = StrutLayoutFactory.GetProjectedLEDPointParametric(i, j);
                    // Vertical height
                    var y   = 1.0 - pixel_p.Item4;
                    var rad = pixel_p.Item3;
                    Tuple <Racer, double, double, Racer> loc = this.GetRacer(y, rad);
                    if (loc == null)
                    {
                        // color = 0 is off.
                        this.dome.SetPixel(i, j, 0);
                    }
                    else
                    {
                        // Let the racer choose its color
                        var color = loc.Item4.Color(dome, config, loc.Item2, loc.Item3);
                        this.dome.SetPixel(i, j, color);
                    }
                }
            }

            // No messages will be sent to the Beaglebone until Flush is called. This
            // makes it imperative that you call Flush at the end of Visualize, or
            // whenever you want to update the LEDs. The LEDs themselves are stateful,
            // and will maintain whatever color you set them to until they lose power.
            this.dome.Flush();
        }
        public void Visualize()
        {
            if (this.stopwatch.ElapsedMilliseconds <= 250)
            {
                return;
            }
            this.stopwatch.Restart();
            this.state = (this.state + 1) % 2;

            if (this.state == 0)
            {
                for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
                {
                    Strut strut = Strut.FromIndex(this.config, i);
                    for (int j = 0; j < strut.Length; j++)
                    {
                        this.dome.SetPixel(i, j, 0x000000);
                    }
                }
                this.dome.Flush();
                return;
            }

            byte brightnessByte = (byte)(
                0xFF * this.config.domeMaxBrightness *
                this.config.domeBrightness
                );
            int color = brightnessByte << 16
                        | brightnessByte << 8
                        | brightnessByte;

            for (int teensy = 0; teensy < 5; teensy++)
            {
                for (int localIndex = 0; localIndex < 38; localIndex++)
                {
                    var   strutIndex = LEDDomeOutput.FindStrutIndex(teensy, localIndex);
                    Strut strut      = Strut.FromIndex(this.config, strutIndex);
                    if (this.state == 2)
                    {
                        for (int j = 1; j < strut.Length - 1; j++)
                        {
                            this.dome.SetPixel(strutIndex, j, 0x000000);
                        }
                        this.dome.SetPixel(strutIndex, 0, color);
                        this.dome.SetPixel(strutIndex, strut.Length - 1, color);
                    }
                    else
                    {
                        for (int j = 0; j < strut.Length; j++)
                        {
                            this.dome.SetPixel(strutIndex, j, color);
                        }
                    }
                }
            }
            this.dome.Flush();
        }
Exemplo n.º 10
0
        public void ParametricTest()
        {
            double progress = this.config.beatBroadcaster.ProgressThroughBeat(
                this.config.domeVolumeRotationSpeed
                );

            double level = this.audio.LevelForChannel(0);

            for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
            {
                Strut strut = Strut.FromIndex(this.config, i);
                var   leds  = LEDDomeOutput.GetNumLEDs(i);
                for (int j = 0; j < leds; j++)
                {
                    var    p = StrutLayoutFactory.GetProjectedLEDPointParametric(i, j);
                    double r = 0;
                    double g = 0;
                    double b = 0;

                    //radar effect
                    double a = (p.Item3 + Math.PI) / (Math.PI * 2);
                    r = progress - a;
                    if (r < 0)
                    {
                        r += 1;
                    }
                    if (r > 1)
                    {
                        r = 1;
                    }

                    //pulse effect
                    double dist = Math.Abs(progress - p.Item4);
                    r = 1 - dist;
                    if (r < 0.9)
                    {
                        r = 0;
                    }

                    //spiral effect
                    double m = p.Item4 - a;
                    if (m < 0)
                    {
                        m += 1;
                    }
                    double n = progress - m;
                    if (n < 0)
                    {
                        n += 1;
                    }
                    r = 1 - n;

                    this.dome.SetPixel(i, j, LEDColor.FromDoubles(r, g, b));
                }
            }
        }
Exemplo n.º 11
0
 public void Static()
 {
     for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
     {
         Strut strut = Strut.FromIndex(this.config, i);
         for (int j = 0; j < strut.Length; j++)
         {
             this.dome.SetPixel(i, j, this.RandomColor());
         }
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Set up to handle change events from the Strut and Cooler pickers.
        ///
        /// Would like to do this in the constructor, but the pickers need a reference to this object so they
        /// can get their Materials and Coolers lists.  Since they need that reference, they cannot be constructed
        /// until after this object is constructed.  Therefore this routine starts everything going.
        /// </summary>
        /// <param name="strutPicker"></param>
        /// <param name="coolerPicker"></param>
        public void SetControllers(StrutPicker strutPicker, CoolerPicker coolerPicker)
        {
            // Set initial cooler and strut choices into the simulator
            Strut  strut  = strutPicker.Strut;
            Cooler cooler = coolerPicker.Cooler;

            webServiceAdapter.SetStrut(strut.Material.Name, strut.Length, strut.CrossSectionalArea);
            webServiceAdapter.SetCooler(cooler.Name, coolerPicker.PowerFactor);

            // Run the simulator for the first time
            simulate();

            // Set up to handle change events from the controllers
            strutPicker.RaiseStrutChangedEvent   += new EventHandler(handle_StrutChangedEvent);
            coolerPicker.RaiseCoolerChangedEvent += new EventHandler(handle_CoolerChangedEvent);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Simulate the steady-state problem with the given strut
        /// and cooler.  Output the results.
        /// </summary>
        /// <param name="strut"></param>
        /// <param name="cooler"></param>
        private void simulate(Strut strut, Cooler cooler)
        {
            Cursor savedCursor = this.Cursor;

            try {
                this.Cursor = Cursors.WaitCursor;

                this.Refresh();


                double temp = steadyStateSim.simulate(strut.Length, strut.CrossSectionalArea, strut.Material, cooler);

                answerBox.Text = temp.ToString();
            } finally {
                this.Cursor = savedCursor;
            }
        }
Exemplo n.º 14
0
        private StrutLayout BuildSpokeLayout()
        {
            int[][] strutsBySpoke = new int[][] {
                new int[] {
                    73, 81, 89, 97, 105, 74, 82, 90, 98, 106, 0, 2, 4, 6, 8, 10, 12, 14,
                    16, 18,
                },
                new int[] {
                    21, 26, 29, 34, 37, 71, 20, 111, 122, 27, 84, 125, 28, 87, 100, 35,
                    136, 103, 36, 139,
                },
                new int[] {
                    22, 25, 30, 33, 38, 76, 23, 115, 118, 24, 79, 92, 31, 129, 132, 32,
                    95, 108, 39, 143,
                },
                new int[] {
                    113, 120, 127, 134, 141, 185, 186, 187, 188, 189, 147, 171, 152, 174,
                    157, 177, 162, 180, 183, 167, 146, 148, 55, 56, 151, 153, 57, 58, 156,
                    158, 59, 60, 161, 163, 61, 62, 166, 168, 63, 64, 40, 41, 43, 44, 46,
                    47, 49, 50, 52, 53, 170, 172, 173, 175, 176, 178, 179, 181, 182, 184,
                },
            };
            HashSet <int> reversedStruts = new HashSet <int>()
            {
                71, 73, 74, 22, 81, 82, 26, 90, 30, 89, 97, 98, 34, 38, 106, 105, 185,
                189, 188, 187, 186, 0, 20, 41, 115, 23, 2, 4, 79, 24, 44, 122, 27, 6, 8,
                87, 28, 47, 129, 31, 10, 12, 95, 32, 50, 136, 35, 14, 16, 103, 36, 53,
                143, 39, 18, 183, 184, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
                180, 181, 182,
            };

            StrutLayoutSegment[] segments =
                new StrutLayoutSegment[strutsBySpoke.Length];
            for (int i = 0; i < strutsBySpoke.Length; i++)
            {
                segments[i] = new StrutLayoutSegment(new HashSet <Strut>(
                                                         strutsBySpoke[i].Select(
                                                             strut => reversedStruts.Contains(strut)
              ? Strut.ReversedFromIndex(this.config, strut)
              : Strut.FromIndex(this.config, strut)
                                                             )
                                                         ));
            }
            return(new StrutLayout(segments));
        }
Exemplo n.º 15
0
            public void Add(Strut from, Strut to, float length)
            {
                StrutConnection c = new StrutConnection(from, to, length);

                if (!Find(c))
                {
                    // no duplicate link already listed
                    int i = IndexOf(from.owner, to.owner);
                    if (i < 0)
                    {
                        // no linking same part twice
                        base.Add(c);
                    }
                    else if (c.length < this[i].length)
                    {
                        // new link is shorter. Replace.
                        this[i] = c;
                    }
                }
            }
Exemplo n.º 16
0
        public void Visualize()
        {
            if (this.wipeStrutsNextCycle)
            {
                for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
                {
                    Strut strut = Strut.FromIndex(this.config, i);
                    for (int j = 0; j < strut.Length; j++)
                    {
                        this.dome.SetPixel(i, j, 0x000000);
                    }
                }
                this.wipeStrutsNextCycle = false;
            }

            //this.StaticRingsAnimated();
            this.ParametricTest();

            //this.wipeStrutsNextCycle = true;
            this.dome.Flush();
        }
Exemplo n.º 17
0
        private StrutLayout BuildRingsLayout()
        {
            int[][] strutsByRing = new int[][] {
                new int [] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
                new int [] { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
                new int [] { 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54 },
                new int [] { 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 },
                new int [] { 65, 66, 67, 68, 69 }
            };

            StrutLayoutSegment[] segments =
                new StrutLayoutSegment[strutsByRing.Length];
            for (int i = 0; i < strutsByRing.Length; i++)
            {
                segments[i] = new StrutLayoutSegment(new HashSet <Strut>(
                                                         strutsByRing[i].Select(
                                                             strut => Strut.FromIndex(this.config, strut)
                                                             )
                                                         ));
            }
            return(new StrutLayout(segments));
        }
Exemplo n.º 18
0
        // Determine a "desired" strut configuration. If it differs from
        // current one, then the struts need to be rebuilt.
        private StrutConfig DesiredConfig()
        {
            StrutConfig  cfg          = new StrutConfig();
            Counterparts counterparts = GetCounterparts();

            // FIXME: 'naive' algorythm glitches a lot.
            // A better solution would be to minimize a sum of connection lengths
            // amongst all the configurations possible.

            foreach (Strut s in mStruts)
            {
                float min  = Mathf.Infinity;
                Strut cMin = null;

                foreach (Tosh_AutoStrut c in counterparts)
                {
                    foreach (Strut cs in c.mStruts)
                    {
                        float v = (s.position - cs.position).sqrMagnitude;
                        if (v < min)
                        {
                            min  = v;
                            cMin = cs;
                        }
                    }
                }

                if (cMin)
                {
                    cfg.Add(s, cMin, min); // 'add' method eliminates double links
                }
                // and replaces longer ones automatically.
            }

            return(cfg);
        }
Exemplo n.º 19
0
 public StrutConnection(Strut AFrom, Strut ATo, float ALength)
 {
     from   = AFrom;
     to     = ATo;
     length = ALength;
 }
        public void Visualize()
        {
            var  commands = this.midi.GetCommandsSinceLastTick();
            bool valueSet = false;

            foreach (MidiCommand command in commands)
            {
                if (command.type != MidiCommandType.Note)
                {
                    continue;
                }
                if (command.index < 48 || command.index > 51)
                {
                    continue;
                }

                valueSet = true;
                if (this.strutStates.ContainsKey(command.index))
                {
                    var   existingStrutIndex = this.strutStates[command.index];
                    Strut existingStrut      = Strut.FromIndex(this.config, existingStrutIndex);
                    for (int i = 0; i < existingStrut.Length; i++)
                    {
                        this.dome.SetPixel(existingStrutIndex, i, 0x000000);
                    }
                    this.strutStates.Remove(command.index);
                    if (command.value == 0.0)
                    {
                        continue;
                    }
                }

                int color          = 0;
                int brightnessByte = (int)(
                    0xFF * this.config.domeMaxBrightness *
                    this.config.domeBrightness * command.value
                    );
                if (command.index == 48)
                {
                    color = brightnessByte << 16;
                }
                else if (command.index == 49)
                {
                    color = brightnessByte << 8;
                }
                else if (command.index == 50)
                {
                    color = brightnessByte;
                }
                else if (command.index == 51)
                {
                    color = brightnessByte | brightnessByte << 8 | brightnessByte << 16;
                }

                int strutIndex = -1;
                while (strutIndex == -1)
                {
                    int candidateStrutIndex = (int)(this.rand.NextDouble() * LEDDomeOutput.GetNumStruts());
                    if (this.strutStates.ContainsValue(candidateStrutIndex))
                    {
                        continue;
                    }
                    strutIndex = candidateStrutIndex;
                }

                Strut strut = Strut.FromIndex(this.config, strutIndex);
                for (int i = 0; i < strut.Length; i++)
                {
                    this.dome.SetPixel(strutIndex, i, color);
                }
                this.strutStates[command.index] = strutIndex;
            }

            if (valueSet)
            {
                this.dome.Flush();
            }
        }
Exemplo n.º 21
0
        public void Visualize()
        {
            if (this.stopwatch.ElapsedMilliseconds <= 1000)
            {
                return;
            }
            this.stopwatch.Restart();
            this.lastIndex++;
            byte brightnessByte = (byte)(
                0xFF * this.config.domeMaxBrightness *
                this.config.domeBrightness
                );
            int whiteColor = brightnessByte << 16
                             | brightnessByte << 8
                             | brightnessByte;

            if (this.lastIndex == 38)
            {
                this.lastIndex  = 0;
                this.lastTeensy = (this.lastTeensy + 1) % 5;

                // Reset every LED to blue
                for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
                {
                    Strut blueStrut = Strut.FromIndex(this.config, i);
                    for (int j = 0; j < blueStrut.Length; j++)
                    {
                        this.dome.SetPixel(i, j, 0x0000FF);
                    }
                }

                if (this.lastTeensy == 0)
                {
                    if (this.color == 0xFF0000)
                    {
                        this.color = 0x00FF00;
                    }
                    else if (this.color == 0x00FF00)
                    {
                        this.color = 0x0000FF;
                    }
                    else if (this.color == 0x0000FF)
                    {
                        this.color = 0xFFFFFF;
                    }
                    else if (this.color == 0xFFFFFF)
                    {
                        this.color = 0xFF0000;
                    }
                }
            }
            var strutIndex = LEDDomeOutput.FindStrutIndex(
                this.lastTeensy,
                this.lastIndex
                );
            Strut strut = Strut.FromIndex(this.config, strutIndex);

            for (int i = 0; i < strut.Length; i++)
            {
                this.dome.SetPixel(strutIndex, i, this.color & whiteColor);
            }
            this.dome.Flush();
        }
Exemplo n.º 22
0
        void Render()
        {
            double progress = this.config.beatBroadcaster.ProgressThroughMeasure;

            currentAngle     += this.config.domeVolumeRotationSpeed * Wrap(progress - this.lastProgress, 0, 1);
            currentAngle      = Wrap(currentAngle, 0, 1);
            currentGradient  += this.config.domeGradientSpeed * Wrap(progress - this.lastProgress, 0, 1);
            currentGradient   = Wrap(currentGradient, 0, 1);
            this.lastProgress = progress;

            double level         = this.audio.LevelForChannel(0);
            double adjustedLevel = Map(level, 0, 1, 0.02, 1);

            for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
            {
                Strut strut = Strut.FromIndex(this.config, i);
                var   leds  = LEDDomeOutput.GetNumLEDs(i);
                for (int j = 0; j < leds; j++)
                {
                    var p = StrutLayoutFactory.GetProjectedLEDPointParametric(i, j);

                    // map angle to 0-1
                    var angle = MapWrap(p.Item3, -Math.PI, Math.PI, 0.0, 1.0);
                    var dist  = p.Item4;

                    double val         = 0;
                    double gradientVal = 0;

                    switch (this.config.domeRadialEffect)
                    {
                    case 0:
                        // radar mapping
                        val         = MapWrap(angle, currentAngle, 1 + currentAngle, 0, 1);
                        gradientVal = dist;
                        break;

                    case 1:
                        // pulse mapping
                        val         = MapWrap(dist, currentAngle, 1 + currentAngle, 0, 1);
                        gradientVal = Math.Abs(Map(angle, 0, 1, -1, 1));
                        break;

                    case 2:
                        // spiral mapping
                        val         = MapWrap(angle + dist / this.config.domeRadialFrequency, currentAngle, 1 + currentAngle, 0, 1);
                        gradientVal = dist;
                        break;
                    }

                    // scale val according to radial frequency
                    val = Wrap(val * this.config.domeRadialFrequency, 0, 1);
                    // center around val = 1/0 (0.5 maps to 0, 0 and 1 map to 1)
                    var centeredVal = Math.Abs(Map(val, 0, 1, -1, 1));

                    // size limit is scaled according the size slider and the current level
                    var  sizeLimit = this.config.domeRadialSize * adjustedLevel;
                    bool on        = centeredVal <= sizeLimit;

                    var color = on ? this.dome.GetGradientColor(0, gradientVal, currentGradient, true) : 0;

                    this.dome.SetPixel(i, j, color);
                }
            }
        }
Exemplo n.º 23
0
        public void Visualize()
        {
            if (this.wipeStrutsNextCycle)
            {
                for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
                {
                    Strut strut = Strut.FromIndex(this.config, i);
                    for (int j = 0; j < strut.Length; j++)
                    {
                        this.dome.SetPixel(i, j, 0x000000);
                    }
                }
                this.wipeStrutsNextCycle = false;
            }

            this.UpdateCenter();
            this.UpdateAnimationSize(this.config.domeVolumeAnimationSize);

            int subdivisions    = this.partLayout.NumSegments / 2;
            int totalParts      = this.config.domeVolumeAnimationSize;
            int volumeSplitInto = 2 * ((totalParts - 1) / 2 + 1);

            for (int part = 0; part < totalParts; part += 2)
            {
                var    outwardSegment = this.partLayout.GetSegment(part);
                double startRange     = (double)part / volumeSplitInto;
                double endRange       = (double)(part + 2) / volumeSplitInto;
                double level          = this.audio.LevelForChannel(0);
                double scaled         = (level - startRange) /
                                        (endRange - startRange);
                scaled     = Math.Max(Math.Min(scaled, 1.0), 0.0);
                startRange = Math.Min(startRange / level, 1.0);
                endRange   = Math.Min(endRange / level, 1.0);

                foreach (Strut strut in outwardSegment.GetStruts())
                {
                    this.UpdateStrut(strut, scaled, startRange, endRange);
                }

                if (part + 1 == totalParts)
                {
                    break;
                }

                for (int i = 0; i < 6; i++)
                {
                    StrutLayoutSegment segment =
                        this.sectionLayout.GetSegment(i + part * 3);
                    double gradientStartPos = 0.0;
                    double gradientStep     = 1.0 / segment.GetStruts().Count;
                    foreach (Strut strut in segment.GetStruts())
                    {
                        double gradientEndPos = gradientStartPos + gradientStep;
                        this.UpdateStrut(
                            strut,
                            scaled == 1.0 ? 1.0 : 0.0,
                            gradientStartPos,
                            gradientEndPos
                            );
                        gradientStartPos = gradientEndPos;
                    }
                }
            }
            this.dome.Flush();
        }
Exemplo n.º 24
0
        public void Visualize()
        {
            // Check if any active animations need to be removed because they use
            // struts that are now reserved, or because they are finished
            var toRemove = new HashSet <Animation>(
                this.activeAnimations.Where(a => !a.Active)
                );

            foreach (var animation in toRemove)
            {
                animation.shape.activeAnimation = null;
                if (this.padsToLastAnimation[animation.pad] == animation)
                {
                    this.padsToLastAnimation.Remove(animation.pad);
                }
                // We only need to clear the LEDs on unreserved struts
                var unreservedStruts =
                    animation.shape.struts.Except(this.dome.ReservedStruts());
                foreach (int strutIndex in unreservedStruts)
                {
                    Strut strut = Strut.FromIndex(this.config, strutIndex);
                    for (int i = 0; i < strut.Length; i++)
                    {
                        this.dome.SetPixel(strutIndex, i, 0x000000);
                    }
                }
            }
            this.activeAnimations.RemoveAll(a => toRemove.Contains(a));

            // See if we need to create any new animations
            var commands = this.midi.GetCommandsSinceLastTick();

            foreach (MidiCommand command in commands)
            {
                if (command.type != MidiCommandType.Note)
                {
                    continue;
                }
                if (command.index > 15)
                {
                    continue;
                }
                if (this.padsToLastAnimation.ContainsKey(command.index))
                {
                    var padLastAnimation = this.padsToLastAnimation[command.index];
                    padLastAnimation.Release();
                    if (command.value == 0.0)
                    {
                        continue;
                    }
                }
                Animation animation = this.NewRandomAnimation(
                    ShapeType.Polygon,
                    command.index,
                    command.value
                    );
                if (animation == null)
                {
                    // If we aren't able to get any shape at all,
                    // then give up on new animations
                    break;
                }
                this.lastUserAnimationCreated = animation.startingTime;
            }

            // If we haven't seen any animations in a bit and computer animation is
            // enabled, then let's try and see if we should create one
            long timestamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

            if (timestamp > this.lastUserAnimationCreated + 2000)
            {
                foreach (AudioEvent audioEvent in this.audio.GetEventsSinceLastTick())
                {
                    int colorIndex;
                    if (audioEvent.type == AudioDetectorType.Kick)
                    {
                        colorIndex = 4;
                    }
                    else if (audioEvent.type == AudioDetectorType.Snare)
                    {
                        colorIndex = 5;
                    }
                    else
                    {
                        throw new Exception("invalid AudioDetectorType");
                    }
                    Animation animation = this.NewRandomAnimation(
                        ShapeType.Polygon,
                        colorIndex,
                        audioEvent.significance
                        );
                    if (animation == null)
                    {
                        // If we aren't able to get any shape at all,
                        // then give up on new animations
                        break;
                    }
                    animation.Release();
                }
            }

            // Okay, now we have to actually animate each of the active animations
            foreach (var animation in this.activeAnimations)
            {
                animation.Animate(this.dome);
            }
        }
        public void Visualize()
        {
            if (this.stopwatch.ElapsedMilliseconds <= 1000)
            {
                return;
            }
            this.stopwatch.Restart();
            this.state = (this.state + 1) % 4;

            if (this.state == 0)
            {
                for (int i = 0; i < LEDDomeOutput.GetNumStruts(); i++)
                {
                    Strut strut = Strut.FromIndex(this.config, i);
                    for (int j = 0; j < strut.Length; j++)
                    {
                        this.dome.SetPixel(i, j, 0x000000);
                    }
                }
                this.dome.Flush();
                return;
            }

            byte brightnessByte = (byte)(
                0xFF * this.config.domeMaxBrightness *
                this.config.domeBrightness
                );
            int whiteColor = brightnessByte << 16
                             | brightnessByte << 8
                             | brightnessByte;

            int[] colors =
            {
                whiteColor& 0xFF0000,
                  whiteColor& 0x00FF00,
                  whiteColor& 0x0000FF,
                  whiteColor& 0xFFFF00,
                  whiteColor& 0xFF00FF,
                  whiteColor& 0x00FFFF,
            };

            for (int controlBox = 0; controlBox < 5; controlBox++)
            {
                int colorIndex = 0;
                for (int localIndex = 0; localIndex < 38; localIndex++)
                {
                    var   strutIndex = LEDDomeOutput.FindStrutIndex(controlBox, localIndex);
                    Strut strut      = Strut.FromIndex(this.config, strutIndex);
                    if (this.state == 2)
                    {
                        for (int j = 1; j < strut.Length - 1; j++)
                        {
                            this.dome.SetPixel(strutIndex, j, 0x000000);
                        }
                        this.dome.SetPixel(strutIndex, 0, colors[colorIndex]);
                        this.dome.SetPixel(strutIndex, strut.Length - 1, colors[colorIndex]);
                    }
                    else
                    {
                        for (int j = 0; j < strut.Length; j++)
                        {
                            this.dome.SetPixel(strutIndex, j, colors[colorIndex]);
                        }
                    }
                    colorIndex = (colorIndex + 1) % colors.Length;
                }
            }
            this.dome.Flush();
        }