Esempio n. 1
0
        private static Task TestUltrasoundSensor()
        {
            var module = new PiTopModule();
            var plate  = module.GetOrCreatePlate <FoundationPlate>();

            var cancellationSource = new CancellationTokenSource();

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

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

            return(Task.Run(() =>
            {
                Console.ReadLine();
                module.Dispose();
                cancellationSource.Cancel(false);
            }, cancellationSource.Token));
        }
Esempio n. 2
0
        private static Task TestPotentiometer(AnaloguePort port)
        {
            var cancellationSource = new CancellationTokenSource();

            var module = new PiTopModule();
            var plate  = module.GetOrCreatePlate <FoundationPlate>();

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

                Observable
                .Interval(TimeSpan.FromSeconds(0.5))
                .Select(_ => potentiometer.Position)
                .Subscribe(Console.WriteLine);

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

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

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

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

                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();
                });

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

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

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

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

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

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


                    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;
                    }
                });

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


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

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