Esempio n. 1
0
        private static Task TestUltrasoundSensor()
        {
            var plate = new Plate();

            var cancellationSource = new CancellationTokenSource();

            Task.Run(() =>
            {
                var sensor =
                    plate.GetOrCreateDigitalDevice <UltrasonicSensor>(DigitalPort.D3, (dp) => new UltrasonicSensor(dp));
                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Subscribe(_ => { Console.WriteLine(sensor.Distance); });

                PrintPlate(plate);
                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);

            return(Task.Run(() =>
            {
                Console.ReadLine();
                plate.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Esempio n. 2
0
        private static Task TestButton(DigitalPort buttonPort, DigitalPort[] ledPorts)
        {
            var cancellationSource = new CancellationTokenSource();
            var plate = new Plate();

            Task.Run(() =>
            {
                var button = plate.GetOrCreateDigitalDevice <Button>(buttonPort);

                foreach (var digitalPort in ledPorts)
                {
                    plate.GetOrCreateDigitalDevice <Led>(digitalPort);
                }

                AllLedOff(plate);
                var leds = plate.DigitalDevices.OfType <Led>().ToArray();

                var buttonStream = Observable
                                   .FromEventPattern <bool>(h => button.PressedChanged += h, h => button.PressedChanged -= h);
                var pos = -1;
                buttonStream
                .Where(e => e.EventArgs)
                .Select(_ =>
                {
                    var next = ((pos + 1) % leds.Length);
                    var pair = new { Prev = pos, Next = ((pos + 1) % leds.Length) };
                    pos      = next;
                    return(pair);
                })
                .Subscribe(p =>
                {
                    if (p.Prev >= 0)
                    {
                        leds[p.Prev].Off();
                    }
                    leds[p.Next].On();
                    PrintPlate(plate);
                });

                PrintPlate(plate);
                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);

            return(Task.Run(() =>
            {
                Console.ReadLine();
                plate.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Esempio n. 3
0
        /// <summary>
        /// Cleans up all resources that the current game has been holding before switching to another gamestate
        /// </summary>
        private void EndGame()
        {
            if (_endGameCalled)
            {
                return;
            }

            for (int i = 0; i < bricks.Length; i++)
            {
                bricks[i]?.Dispose();
            }

            bricks = null;

            ball.Dispose();
            ball = null;

            plate.Dispose();
            plate = null;

            _state         = GameState.Over;
            _endGameCalled = true;
        }
Esempio n. 4
0
        private static Task TestPotentiometer(AnaloguePort port)
        {
            var cancellationSource = new CancellationTokenSource();
            var plate = new Plate();

            Task.Run(() =>
            {
                var potentiometer = plate.GetOrCreateAnalogueDevice <Potentiometer>(port);

                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Select(_ => potentiometer.Position)
                .Subscribe(Console.WriteLine);
                PrintPlate(plate);
                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);

            return(Task.Run(() =>
            {
                Console.ReadLine();
                plate.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Esempio n. 5
0
        private Task TestSemaphore(DigitalPort ultrasonicSensorPort, DigitalPort greenLedPort, DigitalPort yellowLedPort, DigitalPort redLedPort, int greenThreshold, int yellowThreshold, int redThreshold)
        {
            var plate = new Plate();

            var cancellationSource = new CancellationTokenSource();
            var greenLed           = plate.GetOrCreateDigitalDevice <Led>(greenLedPort);

            greenLed.DisplayProperties.Add(new NamedCssColor("green"));
            var yellowLed = plate.GetOrCreateDigitalDevice <Led>(yellowLedPort);

            yellowLed.DisplayProperties.Add(new NamedCssColor("yellow"));
            var redLed = plate.GetOrCreateDigitalDevice <Led>(redLedPort);

            redLed.DisplayProperties.Add(new NamedCssColor("red"));
            ClearLeds();
            Task.Run(() =>
            {
                var sensor = plate.GetOrCreateDigitalDevice <UltrasonicSensor>(ultrasonicSensorPort);
                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Subscribe(_ =>
                {
                    var distance = sensor.Distance;


                    switch (distance)
                    {
                    case { } x when x > greenThreshold:
                        greenLed.On();
                        yellowLed.Off();
                        redLed.Off();
                        break;

                    case { } x when x <greenThreshold && x> yellowThreshold:
                        greenLed.Off();
                        yellowLed.On();
                        redLed.Off();
                        break;

                    case { } x when x < redThreshold:
                        greenLed.Off();
                        yellowLed.Off();
                        redLed.On();
                        break;
                    }

                    PrintPlate(plate);
                });

                PrintPlate(plate);
                Console.WriteLine("press enter key to exit");
            }, cancellationSource.Token);


            return(Task.Run(() =>
            {
                Console.ReadLine();
                plate.Dispose();
                ClearLeds();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));

            void ClearLeds()
            {
                greenLed.Off();
                yellowLed.Off();
                redLed.Off();
            }
        }