Example #1
0
 static void SetColorForIndices(HeroPixel pixelStrip, uint aColor, uint[] indices)
 {
     foreach (uint index in indices)
     {
         pixelStrip.setColor(aColor, index, 1);
     }
 }
Example #2
0
        public void RunForever()
        {
            /* get an X,Y, and btn value.  These could come from potentiometers for example, or USB gamepad */
            Microsoft.SPOT.Hardware.AnalogInput aiBtn = new Microsoft.SPOT.Hardware.AnalogInput(CTRE.HERO.IO.Port1.Analog_Pin3);
            Microsoft.SPOT.Hardware.AnalogInput aiX   = new Microsoft.SPOT.Hardware.AnalogInput(CTRE.HERO.IO.Port1.Analog_Pin4);
            Microsoft.SPOT.Hardware.AnalogInput aiY   = new Microsoft.SPOT.Hardware.AnalogInput(CTRE.HERO.IO.Port1.Analog_Pin5);

            /* loop forever */
            while (true)
            {
                /* loop sleep */
                Thread.Sleep(5);

                /* get x,y, and button */
                bool  btn = aiBtn.Read() < 0.5f;
                float x   = (float)aiX.Read() * 2f - 1f; // [-1,1]
                float y   = (float)aiY.Read() * 2f - 1f; // [-1,1]
                x = Deadband(x);
                y = Deadband(y);

                /* figure out next color based on current mode */
                if (_colorWheelMode)
                {
                    /* spin around the top circular slice of an HSV surface,
                     * https://en.wikipedia.org/wiki/HSL_and_HSV */

                    /* calc sat and hue, use 100% for value */
                    float hueDeg = 0;
                    if (y != 0 || x != 0)
                    {
                        hueDeg = (float)System.Math.Atan2(y, x) * 180f / (float)System.Math.PI;
                        /* keep the angle positive */
                        if (hueDeg < 0)
                        {
                            hueDeg += 360f;
                        }
                    }

                    float sat   = (float)System.Math.Sqrt(x * x + y * y);
                    float value = 1f;

                    /* convert to rgb */
                    uint color;
                    uint r, g, b;
                    HsvToRgb.Convert(hueDeg, sat, value, out r, out g, out b);
                    color   = r;
                    color <<= 8;
                    color  |= g;
                    color <<= 8;
                    color  |= b;

                    /* set all LEDs to one color, controlled by analog signals */
                    _heroPixels.setColor(color, 0, _heroPixels.NumberPixels);
                }
                else
                {
                    /* just ramp through the predetermined color sequence */
                    uint color = _colorSequence[_colIdx];
                    _heroPixels.setColor(color, _pixelIdx, 1);
                }

                /* update LEDs using SPI*/
                _heroPixels.writeOutput();

                /* iterate to next pixel */
                if (++_pixelIdx >= _heroPixels.NumberPixels)
                {
                    /* back to first pixel */
                    _pixelIdx = 0;

                    /* step to next color when using the predetermined sequence */
                    if (++_colIdx >= _colorSequence.Length)
                    {
                        _colIdx = 0;
                    }
                }

                /* detect on-press event on button to change mode. */
                if (btn && !_lastBtn)
                {
                    _colorWheelMode = !_colorWheelMode;
                }
                _lastBtn = btn;
            }
        }