Example #1
0
        /// <summary>
        ///     Initializes a new <see cref="Neopixel" /> instance.
        /// </summary>
        /// <param name="ledCount">The count of leds.</param>
        /// <param name="pin">The GPIO pin index connected to the display signal line.</param>
        /// <param name="stripType">The strip type to use.</param>
        /// <param name="frequency">The frequency of the display signal.</param>
        /// <param name="dma">The DMA channel to use.</param>
        /// <param name="invert">Whether the signal line should be inverted</param>
        /// <param name="brightness">The overall brightness.</param>
        /// <param name="channel">The PWM channel to use</param>
        public Neopixel(int ledCount, int pin, int stripType, uint frequency = 800000, int dma = 10,
                        bool invert = false, byte brightness = 255, int channel = 0)
        {
            _leds = new ws2811_t();

            // Initialize the channels to zero
            for (var i = 0; i < 2; i++)
            {
                var chan    = rpi_ws281x.ws2811_channel_get(_leds, i);
                var chanRef = ws2811_channel_t.getCPtr(chan);
                rpi_ws281xPINVOKE.ws2811_channel_t_count_set(chanRef, 0);
                rpi_ws281xPINVOKE.ws2811_channel_t_gpionum_set(chanRef, 0);
                rpi_ws281xPINVOKE.ws2811_channel_t_invert_set(chanRef, 0);
                rpi_ws281xPINVOKE.ws2811_channel_t_brightness_set(chanRef, 0);
            }

            // Initialize the channel in use
            _channel = rpi_ws281x.ws2811_channel_get(_leds, channel);
            var channelRef = ws2811_channel_t.getCPtr(_channel);

            rpi_ws281xPINVOKE.ws2811_channel_t_count_set(channelRef, ledCount);
            rpi_ws281xPINVOKE.ws2811_channel_t_gpionum_set(channelRef, pin);
            rpi_ws281xPINVOKE.ws2811_channel_t_invert_set(channelRef, Convert.ToInt32(invert));
            rpi_ws281xPINVOKE.ws2811_channel_t_brightness_set(channelRef, brightness);
            rpi_ws281xPINVOKE.ws2811_channel_t_strip_type_set(channelRef, stripType);

            // Initialize the controller
            var ledRef = ws2811_t.getCPtr(_leds);

            rpi_ws281xPINVOKE.ws2811_t_freq_set(ledRef, frequency);
            rpi_ws281xPINVOKE.ws2811_t_dmanum_set(ledRef, dma);

            //Grab the led data array.
            LedList = new LedList(_channel);
        }
Example #2
0
        public LedStripControl()
        {
            //init the LED Colors
            for (byte x = 0; x < 13; x++)
            {
                LED led = new LED(x);
                led.Color = DisplayedColor;
                LedList.Add(led);
            }

            InitializeComponent();

            //thread that updates the LEDs
            new Thread(() =>
            {
                int currentLed = 0;

                byte currentColor = 0;
                byte red          = 0;
                byte green        = 0;
                byte blue         = 0;

                while (true)
                {
                    if (((MainWindow.ActiveWindow.IsMinimized || mode != MainWindow.ActiveWindow.SelectedMode) && !isMusicMode) || (isMusicMode && MainWindow.ActiveWindow.SelectedMode != "Music"))
                    {
                        Thread.Sleep(100);
                        continue;
                    }

                    //set the correct mode arguments if it's the music preview
                    if (isMusicMode)
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            switch (mode)
                            {
                            case "Static":
                                DisplayedColor = new SolidColorBrush(Color.FromArgb(brightness, MainWindow.ActiveWindow.modeStatic.SelectedColor.R,
                                                                                    MainWindow.ActiveWindow.modeStatic.SelectedColor.G, MainWindow.ActiveWindow.modeStatic.SelectedColor.B));
                                speed = 10;
                                break;

                            case "Cycle":
                                speed = MainWindow.ActiveWindow.modeCycle.Speed;
                                break;

                            case "Rainbow":
                                Speed = MainWindow.ActiveWindow.modeRainbow.Speed;
                                break;

                            case "Color Overlay":
                                speed = MainWindow.ActiveWindow.modeOverlay.Speed;
                                break;
                            }
                        });
                    }

                    if (mode == "Rainbow")
                    {
                        foreach (LED led in LedList)
                        {
                            if (!led.RainbowIsInit)
                            {
                                led.InitRainbow();
                            }

                            led.CycleColor(brightness);
                        }
                    }
                    else if (mode == "Cycle")
                    {
                        //get the color from the first led and apply it to all others
                        if (!LedList[0].RainbowIsInit)
                        {
                            LedList[0].InitRainbow();
                        }

                        LedList[0].CycleColor(brightness);

                        for (int x = 1; x < LedList.Count; x++)
                        {
                            LedList[x].Color = LedList[0].Color;
                        }
                    }
                    else if (mode == "Color Overlay")
                    {
                        if (currentLed == LedList.Count || currentLed == -1)
                        {
                            if (direction == 0)
                            {
                                currentLed = LedList.Count - 1;
                            }
                            else
                            {
                                currentLed = 0;
                            }

                            currentColor++;

                            switch (currentColor)
                            {
                            case 0:
                                red   = 255;
                                green = 0;
                                blue  = 0;
                                break;

                            case 1:
                                red   = 255;
                                green = 127;
                                blue  = 0;
                                break;

                            case 2:
                                red   = 255;
                                green = 255;
                                blue  = 0;
                                break;

                            case 3:
                                red   = 127;
                                green = 255;
                                blue  = 0;
                                break;

                            case 4:
                                red   = 0;
                                green = 255;
                                blue  = 0;
                                break;

                            case 5:
                                red   = 0;
                                green = 255;
                                blue  = 127;
                                break;

                            case 6:
                                red   = 0;
                                green = 255;
                                blue  = 255;
                                break;

                            case 7:
                                red   = 0;
                                green = 127;
                                blue  = 255;
                                break;

                            case 8:
                                red   = 0;
                                green = 0;
                                blue  = 255;
                                break;

                            case 9:
                                red   = 127;
                                green = 0;
                                blue  = 255;
                                break;

                            case 10:
                                red   = 255;
                                green = 0;
                                blue  = 255;
                                break;

                            case 11:
                                red   = 255;
                                green = 0;
                                blue  = 127;
                                break;

                            case 12:
                                currentColor = 0;
                                continue;
                            }
                        }

                        LedList[currentLed].Color = new SolidColorBrush(Color.FromArgb(brightness, red, green, blue));

                        if (direction == 0)
                        {
                            currentLed--;
                        }
                        else
                        {
                            currentLed++;
                        }
                    }
                    else if (mode == "Lightning")
                    {
                        if (currentLed == LedList.Count + 20 || currentLed == -20)
                        {
                            if (direction == 0)
                            {
                                currentLed = LedList.Count - 1;
                            }
                            else
                            {
                                currentLed = 0;
                            }
                        }

                        for (int x = 0; x < LedList.Count; x++)
                        {
                            if (x == currentLed)
                            {
                                LedList[x].Color = new SolidColorBrush(Color.FromRgb(displayedRed, displayedGreen, displayedBlue));
                            }
                            else
                            {
                                LedList[x].Color = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                            }
                        }

                        if (direction == 0)
                        {
                            currentLed--;
                        }
                        else
                        {
                            currentLed++;
                        }
                    }

                    OnPropertyChanged("LedList");

                    Thread.Sleep(speed + 1);
                }
            })
            {
                IsBackground = true
            }.Start();
        }