private static async Task TestLed01() { var module = new PiTopModule(); var plate = module.GetOrCreatePlate <FoundationPlate>(); var ports = DigitalPort.D0.GetDigitalPortRange(3); var leds = ports .Select(p => plate.GetOrCreateDevice(p, (dp, c) => new Led(dp, c))) .ToArray(); foreach (var led in leds) { led.Off(); } var pos = 0; for (var i = 0; i < (leds.Length * 10); i++) { leds[pos].Toggle(); pos = (pos + 1) % 3; await Task.Delay(300); } foreach (var led in leds) { led.Off(); } }
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)); }
static void Main(string[] args) { using var pipeline = Pipeline.Create("PiTop", true); using var module = new PiTopModule(); var plate = module.GetOrCreatePlate <FoundationPlate>(); var threshold = plate .GetOrCreateDevice <Potentiometer>(AnaloguePort.A0) .CreateComponent(pipeline, TimeSpan.FromSeconds(0.5)); var distance = plate .GetOrCreateDevice <UltrasonicSensor>(DigitalPort.D3) .CreateComponent(pipeline, TimeSpan.FromSeconds(0.2)); var alert = new ValueAlertComponent(pipeline, new[] { plate.GetOrCreateDevice <Led>(DigitalPort.D0), plate.GetOrCreateDevice <Led>(DigitalPort.D1), plate.GetOrCreateDevice <Led>(DigitalPort.D2) }); threshold .Select(t => t * 50) .PipeTo(alert.Threshold); distance .Select(d => d.Value) .PipeTo(alert.Value); pipeline.Run(); }
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)); }
public void can_obtain_plate_from_module() { using var module = new PiTopModule(new DummyGpioController()); using var plate = module.GetOrCreatePlate <FoundationPlate>(); plate.Should().NotBeNull(); }
public void plate_can_create_led() { using var module = new PiTopModule(new DummyGpioController()); using var plate = module.GetOrCreatePlate <FoundationPlate>(); using var led = plate.GetOrCreateDevice <Led>(DigitalPort.D0); led.Should().NotBeNull(); }
public void plate_returns_previously_created_devices() { using var module = new PiTopModule(new DummyGpioController()); using var plate = module.GetOrCreatePlate <FoundationPlate>(); var led1 = plate.GetOrCreateDevice <Led>(DigitalPort.D0); var led2 = plate.GetOrCreateDevice <Led>(DigitalPort.D0); led2.Should().BeSameAs(led1); }
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)); }
public void cannot_create_a_different_device_on_allocated_pins() { using var module = new PiTopModule(new DummyGpioController()); using var plate = module.GetOrCreatePlate <FoundationPlate>(); plate.GetOrCreateDevice <Led>(DigitalPort.D0); var action = new Action(() => { plate.GetOrCreateDevice <UltrasonicSensor>(DigitalPort.D0); }); action.Should().Throw <InvalidOperationException>() .Which .Message .Should().Match("Connection D0 is already used by PiTopMakerArchitecture.Foundation.Components.Led device"); }
public LEDService() { _module = new PiTopModule(); _plate = _module.GetOrCreatePlate <FoundationPlate>(); _green = _plate.GetOrCreateDevice <Led>(DigitalPort.D0); _green.DisplayProperties.Add(new NamedCssColor("green")); _amber = _plate.GetOrCreateDevice <Led>(DigitalPort.D1); _amber.DisplayProperties.Add(new NamedCssColor("gold")); _red = _plate.GetOrCreateDevice <Led>(DigitalPort.D2); _red.DisplayProperties.Add(new NamedCssColor("red")); _green.Off(); _amber.Off(); _red.Off(); }
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(); } }