コード例 #1
0
        protected override void ProcessRecord()
        {
            if (NumberOfCycles == 0)
            {
                NumberOfCycles = 1;
            }

            Settings settings = Settings.CreateDefaultSettings();

            settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
            WS281x       controller = new WS281x(settings);
            List <Color> colors     = GetColors();

            for (int iterations = 0; iterations < NumberOfCycles; ++iterations)
            {
                for (int colorCycle = 0; colorCycle < colors.Count; ++colorCycle)
                {
                    Color currentColor = colors[colorCycle];
                    for (int i = 0; i < NumberOfLeds; ++i)
                    {
                        //Iterate over all LEDs and display the current color
                        controller.SetLEDColor(i, currentColor);
                        controller.Render();
                        Thread.Sleep(25);
                    }
                }
            }
        }
コード例 #2
0
        public void LightningPattern(CancellationToken token)
        {
            var random = new Random();

            using (var rpi = new WS281x(settings))
            {
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        this.logger.Information("Cancelling Lightning Pattern...");
                        break;
                    }
                    var blue = random.NextDouble();
                    if (blue < 100)
                    {
                        blue = 100;
                    }
                    rpi.SetBrightness((int)(255 * random.NextDouble()));
                    rpi.SetAll(Color.FromArgb(255, 100, 100, (int)blue));
                    Thread.Sleep((int)(2000 * random.NextDouble()));
                    rpi.SetBrightness(0);
                    Thread.Sleep((int)(5000 * random.NextDouble()));
                }
            }
        }
コード例 #3
0
ファイル: ClientTests.cs プロジェクト: JorisAlbers/Stella
        private static void CreateStellaClientInstance(int id)
        {
            // Light
            int      ledCount = 300;
            Settings settings = Settings.CreateDefaultSettings();

            settings.Channels[0] = new Channel(ledCount, 18, 255, false, StripType.WS2812_STRIP);
            WS281x        ledstrip      = new WS281x(settings);
            LedController ledController = new LedController(ledstrip);

            // Server
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(Program.SERVER_IP), 20055);

            StellaClientLib.Network.StellaServer stellaServer = new StellaClientLib.Network.StellaServer();
            stellaServer.RenderFrameReceived += (sender, frame) => ledController.RenderFrame(frame);
            stellaServer.Start(localEndPoint, 20056, id);

            string input;

            Console.Out.WriteLine($"Running StellaClient instance with id {id}");
            while ((input = Console.ReadLine()) != "q")
            {
                Console.Out.WriteLine("q - quit");

                switch (input)
                {
                default:
                    Console.Out.WriteLine("Unknown command.");
                    break;
                }
            }
            stellaServer.Dispose();
        }
コード例 #4
0
ファイル: Leds.cs プロジェクト: Ellerbach/Nabaztag.Net
        public Leds()
        {
            var settings = new Settings(800_000, 12);

            settings.Channels[Channel] = new Channel(LedCount, PinPwm, 200, false, StripType.WS2812_STRIP);
            _ledStrip = new WS281x(settings);
        }
コード例 #5
0
        public void Execute(AbortRequest request)
        {
            Console.Clear();
            Console.Write("How many LEDs to you want to use: ");

            var ledCount = Int32.Parse(Console.ReadLine());

            //The default settings uses a frequency of 800000 Hz and the DMA channel 10.
            var settings = Settings.CreateDefaultSettings();

            //Use Unknown as strip type. Then the type will be set in the native assembly.
            var controller = settings.AddController(ControllerType.PWM0, ledCount, StripType.WS2812_STRIP);

            using (var device = new WS281x(settings))
            {
                var colors = GetAnimationColors();
                while (!request.IsAbortRequested)
                {
                    for (int i = 0; i < controller.LEDCount; i++)
                    {
                        var colorIndex = (i + colorOffset) % colors.Count;
                        controller.SetLED(i, colors[colorIndex]);
                    }

                    device.Render();

                    if (colorOffset == int.MaxValue)
                    {
                        colorOffset = 0;
                    }
                    colorOffset++;
                    System.Threading.Thread.Sleep(50);
                }
            }
        }
コード例 #6
0
        public void Execute(AbortRequest request)
        {
            Console.Clear();
            Console.Write("How many LEDs do you want to use: ");

            var ledCount = Int32.Parse(Console.ReadLine());
            var settings = Settings.CreateDefaultSettings();

            var controller = settings.AddController(ledCount, Pin.Gpio18, StripType.WS2811_STRIP_RGB);

            using (var device = new WS281x(settings))
            {
                var colors = GetAnimationColors();
                while (!request.IsAbortRequested)
                {
                    for (int i = 0; i < controller.LEDCount; i++)
                    {
                        var colorIndex = (i + colorOffset) % colors.Count;
                        controller.SetLED(i, colors[colorIndex]);
                    }
                    device.Render();
                    colorOffset = (colorOffset + 1) % colors.Count;

                    Thread.Sleep(500);
                }
                device.Reset();
            }
        }
コード例 #7
0
        public LedStrip(LedData ld)
        {
            _ld = ld ?? throw new ArgumentException("Invalid LED Data.");
            LogUtil.Write("Initializing LED Strip, type is " + ld.StripType);
            _ledCount = ld.VCount * 2 + ld.HCount * 2;
            var stripType = ld.StripType switch {
                1 => StripType.SK6812W_STRIP,
                2 => StripType.WS2811_STRIP_RBG,
                0 => StripType.WS2812_STRIP,
                _ => StripType.SK6812W_STRIP
            };
            var pin = Pin.Gpio18;

            if (ld.PinNumber == 13)
            {
                pin = Pin.Gpio13;
            }
            LogUtil.Write($@"Count, pin, type: {_ledCount}, {ld.PinNumber}, {(int)stripType}");
            var settings = Settings.CreateDefaultSettings();

            _controller = settings.AddController(_ledCount, pin, stripType, ControllerType.PWM0, (byte)ld.Brightness);
            _strip      = new WS281x(settings);
            LogUtil.Write($@"Strip created using {_ledCount} LEDs.");
            Demo();
        }
コード例 #8
0
        protected override void BeginProcessing()
        {
            Settings settings = Settings.CreateDefaultSettings();

            settings.Channel = new Channel(30, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
            _Controller      = new WS281x(settings);
        }
コード例 #9
0
ファイル: ColorWipe.cs プロジェクト: AganyaD/0.10s
        public void Execute(AbortRequest request)
        {
            Console.Clear();
            Console.Write("How many LEDs to you want to use: ");

            var ledCount = Int32.Parse(Console.ReadLine());

            //The default settings uses a frequency of 800000 Hz and the DMA channel 10.
            var settings = Settings.CreateDefaultSettings();

            //Set brightness to maximum (255)
            //Use Unknown as strip type. Then the type will be set in the native assembly.
            settings.Channels[0] = new Channel(ledCount, 18, 10, false, StripType.WS2812_STRIP);


            using (var controller = new WS281x(settings))
            {
                while (!request.IsAbortRequested)
                {
                    Wipe(controller, Color.Red);
                    Wipe(controller, Color.Green);
                    Wipe(controller, Color.Blue);
                    Wipe(controller, Color.Yellow);
                    Wipe(controller, Color.FromArgb(255, 255, 255));
                }
            }
        }
コード例 #10
0
        public void Startup()
        {
            var settings = Settings.CreateDefaultSettings();

            settings.Channels[0] = new Channel(140, 18, 255, false, StripType.WS2812_STRIP);

            _ws281x = new WS281x(settings);
        }
コード例 #11
0
 public Task <bool> TurnOff()
 {
     using (var rpi = new WS281x(settings))
     {
         rpi.Reset();
         rpi.Dispose();
         return(Task.FromResult <bool>(true));
     }
 }
コード例 #12
0
 private static void Wipe(WS281x controller, Color color)
 {
     for (int i = 0; i <= controller.GetController().LEDCount - 1; i++)
     {
         controller.SetLEDColor(0, i, color);
         controller.Render();
         System.Threading.Thread.Sleep(1000 / 15);
     }
 }
コード例 #13
0
        private void RenderColor(WS281x device, Controller controller, Color color)
        {
            for (int i = 0; controller.LEDCount > i; i++)
            {
                controller.SetLED(i, color);
            }

            device.Render();
        }
コード例 #14
0
 protected static void Wipe(WS281x controller, Color color)
 {
     for (int i = 0; i <= controller.Settings.Channels[0].LEDs.Count - 1; i++)
     {
         controller.SetLEDColor(0, i, color);
         controller.Render();
         System.Threading.Thread.Sleep(1000 / 15);
     }
 }
コード例 #15
0
        public SetSingleLedColor()
        {
            this.Brightness = 255;
            this.Invert     = false;
            this.GpioPin    = 18;
            Settings settings = Settings.CreateDefaultSettings();

            settings.Channels[0] = new Channel(1, this.GpioPin, this.Brightness, this.Invert, StripType.WS2812_STRIP);
            controller           = new WS281x(settings);
        }
コード例 #16
0
        private static void Wipe(WS281x device, Color color)
        {
            var controller = device.GetController(ControllerType.PWM0);

            for (int i = 0; i < controller.LEDCount; i++)
            {
                controller.SetLED(i, color);
                device.Render();
                System.Threading.Thread.Sleep(1000 / 15);
            }
        }
コード例 #17
0
        public void FlamePattern(CancellationToken token, Pixels pixels)
        {
            var random = new Random();

            using (var rpi = new WS281x(settings))
            {
                int r, g, b;
                if (pixels.r > pixels.g && pixels.r > pixels.b)
                {
                    r = 226; g = 121; b = 35;
                }
                else if (pixels.g > pixels.r && pixels.g > pixels.b)
                {
                    r = 74; g = 150; b = 12;
                }
                else if (pixels.b > pixels.r && pixels.b > pixels.r)
                {
                    r = 158; g = 8; b = 148;
                }
                else
                {
                    r = 226; g = 121; b = 35;
                }
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        this.logger.Information("Cancelling Lightning Pattern...");
                        break;
                    }
                    for (int i = 0; i < 46; i++)
                    {
                        int flicker = (int)(55 * random.NextDouble());
                        int r1 = r - flicker, g1 = g - flicker, b1 = b - flicker;
                        if (r1 < 0)
                        {
                            r1 = 0;
                        }
                        if (g1 < 0)
                        {
                            g1 = 0;
                        }
                        if (b1 < 0)
                        {
                            b1 = 0;
                        }
                        rpi.SetLed(i, Color.FromArgb(255, r1, g1, b1));
                    }
                    rpi.Render();
                    Thread.Sleep(random.Next(10, 133));
                }
            }
        }
コード例 #18
0
        public void Init(WS281x rpi, int ledCount)
        {
            this.rpi      = rpi;
            this.LedCount = ledCount;

            this.Controller = this.rpi.GetController();

            this.LedGroup = new List <LedGroup>();
            for (int i = 0; i < this.LedCount; i++)
            {
                this.LedGroup.Add(new LedGroup());
            }
        }
コード例 #19
0
        public LightingController(int lightCount, int controlPin, ushort defaultBrightness = 150)
        {
            _lightCount        = lightCount;
            _defaultBrightness = defaultBrightness < 0 || defaultBrightness > 255 ? (byte)150 : (byte)defaultBrightness;

            //The default settings uses a frequency of 800000 Hz and the DMA channel 10.
            var settings = Settings.CreateDefaultSettings();

            //Use Unknown as strip type. Then the type will be set in the native assembly.
            _controller = settings.AddController(_lightCount, GetPin(controlPin), StripType.WS2812_STRIP, ControllerType.SPI, _defaultBrightness, false);

            _ws281x = new WS281x(settings);
        }
コード例 #20
0
        public void Init(EntitySet entities)
        {
            var settings = Settings.CreateDefaultSettings();
            var ledCount = _screen.Width * _screen.Height;

            try {
                _controller = settings.AddController(
                    ledCount, Pin.Gpio18, StripType.WS2812_STRIP, brightness: _config.Brightness);
                _device = new WS281x(settings);
            } catch (DllNotFoundException e) {
                var debug = entities.GetFirstComponent <DebugState>();
                debug?.Log($"Failed to load device DLL (it's fine for debug): {e.Message}");
            }
        }
コード例 #21
0
        private static void Wipe(WS281x device, Color color)
        {
            var controller = device.GetController();

            foreach (var led in controller.LEDs)
            {
                led.Color = color;
                device.Render();

                // wait for a minimum of 5 milliseconds
                var waitPeriod = (int)Math.Max(500.0 / controller.LEDCount, 5.0);

                Thread.Sleep(waitPeriod);
            }
        }
コード例 #22
0
        private static void Wipe(WS281x device, Color color)
        {
            var controller = device.GetController();

            foreach (var led in controller.LEDs)
            {
                led.Color = color;
                device.Render(true); // by manipulating the LED directly instead of using the controller, the IsDirty state is lost.
                                     // nothing will get rendered

                // wait for a minimum of 5 milliseconds
                var waitPeriod = (int)Math.Max(500.0 / controller.LEDCount, 5.0);

                Thread.Sleep(waitPeriod);
            }
        }
コード例 #23
0
        protected override void ProcessRecord()
        {
            Settings settings = Settings.CreateDefaultSettings();
            //settings.Channels[0] = new Channel(30, GpioPin, _brightness, Invert, StripType.WS2812_STRIP);
            bool breathingAsc = true;
            int  i            = 0;

            while (true)
            {
                settings.Channel = new Channel(30, GpioPin, (byte)_brightness, Invert, StripType.WS2812_STRIP);
                using (WS281x controller = new WS281x(settings))
                {
                    //controller.SetLEDColor(this.LedId, this.Color);
                    controller.SetLEDColor(0, Color.Green);
                    controller.SetLEDColor(1, Color.Yellow);
                    controller.SetLEDColor(2, Color.Red);
                    controller.Render();
                }
                //Console.WriteLine($"Hello - {i}");
                //Console.WriteLine($"Current brightness - {_brightness}");

                if (_brightness >= 255)
                {
                    _brightness  = 255;
                    breathingAsc = false;
                }
                else if (_brightness <= 0)
                {
                    breathingAsc = true;
                    _brightness  = 0;
                }

                if (breathingAsc)
                {
                    _brightness += 1;
                }
                else
                {
                    _brightness -= 1;
                }
                i += 1;
                Console.WriteLine($"index = {i} / Brightness = {_brightness} / BreathingAsc = {breathingAsc}");
                //Thread.Sleep(50);
            }
        }
コード例 #24
0
        protected override void ProcessRecord()
        {
            if (ExplosionColor == Color.Empty)
            {
                ExplosionColor = Color.OrangeRed;
            }
            if (string.IsNullOrEmpty(Speed))
            {
                Speed = "Medium";
            }

            Settings settings = Settings.CreateDefaultSettings();

            settings.Channel = new Channel(NumberOfLeds, GpioPin, Brightness, Invert, StripType.WS2812_STRIP);
            WS281x controller = new WS281x(settings);

            int leftSideIterations = NumberOfLeds / 2;

            // If it's even, both sides will have the same amount of iterations, otherwise make right side have one more
            int rightSideIterations = (NumberOfLeds % 2 == 0) ? leftSideIterations : leftSideIterations + 1;
            int totalIterations = 0, leftSide = 0, rightSide = NumberOfLeds - 1;

            for (; totalIterations < rightSideIterations; ++totalIterations)
            {
                controller.SetLEDColor(leftSide++, LeftSideColor);
                controller.SetLEDColor(rightSide--, RightSideColor);
                controller.Render();
                Thread.Sleep(_speedTranslation[Speed]);
            }

            for (; totalIterations >= 0; --totalIterations)
            {
                controller.SetLEDColor(leftSide--, ExplosionColor);
                controller.SetLEDColor(rightSide++, ExplosionColor);
                controller.Render();
                Thread.Sleep(10);
            }

            //Thread.Sleep(_speedTranslation[Speed]);

            //controller.SetColorOnAllLEDs(ExplosionColor);
            //controller.Render();
            //for some reason it has to be explicitly disposed
            controller.Dispose();
        }
コード例 #25
0
 public Task <bool> SolidPixels(Pixels pixels)
 {
     using (var rpi = new WS281x(settings))
     {
         rpi.SetAll(Color.FromArgb(255, pixels.r, pixels.g, pixels.b));
         return(Task.FromResult <bool>(true));
         //rpi.SetLed(0, Color.Blue);
         //rpi.SetLed(1, Color.Red);
         //rpi.Render();
         //var brightness = rpi.GetBrightness();
         //rpi.SetBrightness(128);
         //var ledCount = rpi.GetLedCount();
         //rpi.SetLedCount(32);
         //rpi.SetAll(Color.Green);
         //rpi.Reset();
         //rpi.Dispose();
     }
 }
コード例 #26
0
 public void FadePixels(CancellationToken token, Pixels pixels)
 {
     using (var rpi = new WS281x(settings))
     {
         rpi.SetAll(Color.FromArgb(255, pixels.r, pixels.g, pixels.b));
         while (true)
         {
             for (double x = 0.0; x <= 2.0 * Math.PI; x += (5.0 / 255.0))
             {
                 if (token.IsCancellationRequested)
                 {
                     this.logger.Information("Cancelling Fade Pattern...");
                     break;
                 }
                 rpi.SetBrightness((int)(255.0 * Math.Abs(Trig.Sin(x))));
                 Thread.Sleep(10);
             }
         }
     }
 }
コード例 #27
0
        protected WS281x GetController(int?inputLedCount = null, byte?inputBrightness = null)
        {
            Console.Clear();
            Console.Write("How many LEDs to you want to use: ");

            var ledCount = inputLedCount ?? Int32.Parse(Console.ReadLine());

            Console.Write("What brightness do you want to use (0-255)?");
            var brightness = inputBrightness ?? byte.Parse(Console.ReadLine());

            //The default settings uses a frequency of 800000 Hz and the DMA channel 10.
            var settings = Settings.CreateDefaultSettings();

            //Use Unknown as strip type. Then the type will be set in the native assembly.
            settings.Channels[0] = new Channel(ledCount, 18, brightness, false, StripType.WS2812_STRIP);

            var controller = new WS281x(settings);

            return(controller);
        }
コード例 #28
0
        public void Execute(AbortRequest request)
        {
            Console.Clear();
            Console.Write("How many LEDs do you want to use: ");

            var ledCount = Int32.Parse(Console.ReadLine());
            var settings = Settings.CreateDefaultSettings();

            var channel = settings.AddController(ledCount, Pin.Gpio18, StripType.WS2811_STRIP_RGB);

            using (var device = new WS281x(settings))
            {
                while (!request.IsAbortRequested)
                {
                    Wipe(device, Color.Red);
                    Wipe(device, Color.Green);
                    Wipe(device, Color.Blue);
                }
                device.Reset();
            }
        }
コード例 #29
0
        private bool InitLed(string[] args)
        {
            var       path = args.GetString("--led");
            LedConfig led;

            try
            {
                using var sr = new StreamReader(path);
                led          = LedConfig.Deserialize(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("Failed to open the audio configuration file");
                return(false);
            }

            try
            {
                var settings   = Settings.CreateDefaultSettings();
                var controller = settings.AddController(led.LedCount, led.Pin, led.StripType, led.ControllerType);
                var device     = new WS281x(settings);

                LedDevice     = device;
                LedController = controller;
            }
            catch (WS281xException ex)
            {
                Console.WriteLine(ex);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Failed to open the LED device");
                Console.WriteLine("Try executing this program with root privilege");
                Console.ResetColor();
                return(false);
            }

            SayInitialized($"LED {led.StripType}x{led.LedCount}, {led.Pin}, {led.ControllerType}");
            return(true);
        }
コード例 #30
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancel;

            settings = Settings.CreateDefaultSettings();
            settings?.AddController(
                LedCount,
                Pin.Gpio18,
                StripType.WS2812_STRIP,
                brightness: 255,
                invert: false);

            rpi = new WS281x(settings);

            app = new App(rpi, LedCount, args);
            app.Start();

            while (app.Update())
            {
            }

            Close();
        }