diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicDistanceSensor.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicDistanceSensor.cs index 4321867..ff59cbb 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicDistanceSensor.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicDistanceSensor.cs @@ -20,8 +20,8 @@ await hub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder // measure distances (only single subscription) using var m1 = technicDistanceSensor.DistanceObservable.Subscribe(x => Log.LogWarning($"Distl: {x}")); - using var m2 = technicDistanceSensor.DistsObservable.Subscribe(x => Log.LogWarning($"Dists: {x}")); - using var m3 = technicDistanceSensor.SinglObservable.Subscribe(x => Log.LogWarning($"Singl: {x}")); + using var m2 = technicDistanceSensor.ShortOnlyDistanceObservable.Subscribe(x => Log.LogWarning($"Dists: {x}")); + using var m3 = technicDistanceSensor.SingleObservable.Subscribe(x => Log.LogWarning($"Singl: {x}")); await technicDistanceSensor.SetupNotificationAsync(technicDistanceSensor.ModeIndexDistance, true); diff --git a/src/SharpBrick.PoweredUp/Devices/TechnicDistanceSensor.cs b/src/SharpBrick.PoweredUp/Devices/TechnicDistanceSensor.cs index e94c3b5..e2f2258 100644 --- a/src/SharpBrick.PoweredUp/Devices/TechnicDistanceSensor.cs +++ b/src/SharpBrick.PoweredUp/Devices/TechnicDistanceSensor.cs @@ -23,9 +23,9 @@ public class TechnicDistanceSensor : Device, IPoweredUpDevice public short Distance => _distlMode.SI; public IObservable DistanceObservable => _distlMode.Observable.Select(x => x.SI); public short ShortOnlyDistance => _distsMode.SI; - public IObservable DistsObservable => _distsMode.Observable.Select(x => x.SI); - public short SingleDistanceMeasurementResult => _singlMode.SI; - public IObservable SinglObservable => _singlMode.Observable.Select(x => x.SI); + public IObservable ShortOnlyDistanceObservable => _distsMode.Observable.Select(x => x.SI); + public short Single => _singlMode.SI; + public IObservable SingleObservable => _singlMode.Observable.Select(x => x.SI); public TechnicDistanceSensor() { } @@ -39,7 +39,7 @@ public TechnicDistanceSensor(ILegoWirelessProtocol protocol, byte hubId, byte po ObserveForPropertyChanged(_distlMode.Observable, nameof(Distance)); ObserveForPropertyChanged(_distsMode.Observable, nameof(ShortOnlyDistance)); - ObserveForPropertyChanged(_singlMode.Observable, nameof(SingleDistanceMeasurementResult)); + ObserveForPropertyChanged(_singlMode.Observable, nameof(Single)); } public Task SetEyeLightAsync(byte leftTop = 0x00, byte rightTop = 0x00, byte leftBottom = 0x00, byte rightBottom = 0x00) diff --git a/test/SharpBrick.PoweredUp.TestScript/Program.cs b/test/SharpBrick.PoweredUp.TestScript/Program.cs index ae5a062..a6eeb70 100644 --- a/test/SharpBrick.PoweredUp.TestScript/Program.cs +++ b/test/SharpBrick.PoweredUp.TestScript/Program.cs @@ -25,6 +25,7 @@ static async Task Main(string[] args) IEnumerable scripts = new ITestScript[] { new TechnicMotorTestScript(), + new MindstormsSensorsTestScript(), }; var context = new TestScriptExecutionContext(serviceProvider.GetService>()); diff --git a/test/SharpBrick.PoweredUp.TestScript/Scripts/MindstormsSensorsTestScript.cs b/test/SharpBrick.PoweredUp.TestScript/Scripts/MindstormsSensorsTestScript.cs new file mode 100644 index 0000000..6404c3d --- /dev/null +++ b/test/SharpBrick.PoweredUp.TestScript/Scripts/MindstormsSensorsTestScript.cs @@ -0,0 +1,173 @@ +using System; +using System.Reactive.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using SharpBrick.PoweredUp.Deployment; + +namespace SharpBrick.PoweredUp.TestScript +{ + public class MindstormsSensorsTestScript : ITestScript + { + public void DefineDeploymentModel(DeploymentModelBuilder builder) + => builder.AddHub(hubBuilder => + { + hubBuilder + .AddDevice(0) + .AddDevice(1); + }); + + public async Task ExecuteScriptAsync(Hub hub, TestScriptExecutionContext context) + { + var distanceSensor = hub.Port(0).GetDevice(); + var colorSensor = hub.Port(1).GetDevice(); + + context.Log.LogInformation("Start Testing DistanceSensor"); + + await TestCase1_TechnicDistanceSensorLightsAsync(context, distanceSensor); + + await TestCase2_TechnicDistanceSensorLightsBrightnessAsync(context, distanceSensor); + + await TestCase3_TechnicDistanceSensorShortDistanceAsync(context, distanceSensor); + + await TestCase4_TechnicDistanceSensorDistanceAsync(context, distanceSensor); + + context.Log.LogInformation("Start Testing ColorSensor"); + + await TestCase5_TechnicColorIndexColorSensorAsync(context, colorSensor); + + await TestCase6_TechnicColorSectorLightsAsync(context, colorSensor); + + await TestCase7_TechnicColorHsvSensorAsync(context, colorSensor); + } + + private async Task TestCase1_TechnicDistanceSensorLightsAsync(TestScriptExecutionContext context, TechnicDistanceSensor distanceSensor) + { + await distanceSensor.SetEyeLightAsync(leftTop: 100); + await context.ConfirmAsync("TechnicDistanceSensor.SetEyeLightAsync: Light Left Top?"); + + await distanceSensor.SetEyeLightAsync(rightTop: 100); + await context.ConfirmAsync("TechnicDistanceSensor.SetEyeLightAsync: Light Right Top?"); + + await distanceSensor.SetEyeLightAsync(leftBottom: 100); + await context.ConfirmAsync("TechnicDistanceSensor.SetEyeLightAsync: Light Left Bottom?"); + + await distanceSensor.SetEyeLightAsync(rightBottom: 100); + await context.ConfirmAsync("TechnicDistanceSensor.SetEyeLightAsync: Light Right Bottom?"); + } + + private async Task TestCase2_TechnicDistanceSensorLightsBrightnessAsync(TestScriptExecutionContext context, TechnicDistanceSensor distanceSensor) + { + // play with the eye lights (0-100) + for (byte idx = 0; idx < 100; idx += 5) + { + await distanceSensor.SetEyeLightAsync(0b0000_0000, idx, 0b0110_0100, idx); + + await Task.Delay(500); + } + + await distanceSensor.SetEyeLightAsync(0, 0, 0, 0); + + await context.ConfirmAsync("TechnicDistanceSensor.SetEyeLightAsync: Did the right side brighten up?"); + } + + private async Task TestCase3_TechnicDistanceSensorShortDistanceAsync(TestScriptExecutionContext context, TechnicDistanceSensor distanceSensor) + { + context.Log.LogInformation("Test 0 at more than 32cm; Test distances between 4 and 32cm for 10s"); + using var d1 = distanceSensor.ShortOnlyDistanceObservable.Subscribe(x => context.Log.LogInformation($"Distance (short): {x}")); + + await distanceSensor.SetupNotificationAsync(distanceSensor.ModeIndexShortOnlyDistance, true); + + await Task.Delay(10_000); + + await distanceSensor.SetupNotificationAsync(distanceSensor.ModeIndexShortOnlyDistance, false); + + await context.ConfirmAsync("Are the distances measured accurately?"); + } + + private async Task TestCase4_TechnicDistanceSensorDistanceAsync(TestScriptExecutionContext context, TechnicDistanceSensor distanceSensor) + { + context.Log.LogInformation("Test distances between 4 and 250cm for 10s"); + using var d1 = distanceSensor.DistanceObservable.Subscribe(x => context.Log.LogInformation($"Distance: {x}")); + + await distanceSensor.SetupNotificationAsync(distanceSensor.ModeIndexDistance, true); + + await Task.Delay(10_000); + + await distanceSensor.SetupNotificationAsync(distanceSensor.ModeIndexDistance, false); + + await context.ConfirmAsync("Are the distances measured accurately?"); + } + + private async Task TestCase5_TechnicColorIndexColorSensorAsync(TestScriptExecutionContext context, TechnicColorSensor colorSensor) + { + context.Log.LogInformation("Testing known Colors"); + + await colorSensor.SetupNotificationAsync(colorSensor.ModeIndexColor, true, 1); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Black).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Black)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Blue).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Blue)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Teal).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Teal)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Green).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Green)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Yellow).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Yellow)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.Red).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.Red)}"); + + await colorSensor.ColorObservable.Where(c => c == TechnicColor.White).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] {nameof(TechnicColor.White)}"); + + + await colorSensor.SetupNotificationAsync(colorSensor.ModeIndexColor, false); + + context.Log.LogInformation("All known colors recognized."); + } + + private async Task TestCase6_TechnicColorSectorLightsAsync(TestScriptExecutionContext context, TechnicColorSensor colorSensor) + { + await colorSensor.SetSectorLightAsync(100, 0, 0); + + await context.ConfirmAsync("TechnicColorSensor.SetSectorLightAsync: Light on Sector 1?"); + + await colorSensor.SetSectorLightAsync(0, 100, 0); + + await context.ConfirmAsync("TechnicColorSensor.SetSectorLightAsync: Light on Sector 2?"); + + await colorSensor.SetSectorLightAsync(0, 0, 100); + + await context.ConfirmAsync("TechnicColorSensor.SetSectorLightAsync: Light on Sector 3?"); + + await colorSensor.SetSectorLightAsync(0, 0, 0); + } + + private async Task TestCase7_TechnicColorHsvSensorAsync(TestScriptExecutionContext context, TechnicColorSensor colorSensor) + { + context.Log.LogInformation("Testing HSV"); + + await colorSensor.SetupNotificationAsync(colorSensor.ModeIndexHSV, true, 1); + + await colorSensor.HsvObservable.Where(c => (c.hue is > 340 and < 360 || c.hue is > 0 and < 20) && c.saturation > 90 && c.value > 90).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] Reached Significant Red"); + + await colorSensor.HsvObservable.Where(c => (c.hue is > 100 and < 140) && c.saturation > 90 && c.value > 90).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] Reached Significant Green"); + + await colorSensor.HsvObservable.Where(c => (c.hue is > 220 and < 260) && c.saturation > 90 && c.value > 90).FirstAsync().GetAwaiter(); + context.Log.LogInformation($"[OK] Reached Significant Blue"); + + + await colorSensor.SetupNotificationAsync(colorSensor.ModeIndexHSV, false); + + context.Log.LogInformation("All known colors recognized."); + } + + } +} \ No newline at end of file diff --git a/test/SharpBrick.PoweredUp.TestScript/TechnicMotorTestScript.cs b/test/SharpBrick.PoweredUp.TestScript/Scripts/TechnicMotorTestScript.cs similarity index 100% rename from test/SharpBrick.PoweredUp.TestScript/TechnicMotorTestScript.cs rename to test/SharpBrick.PoweredUp.TestScript/Scripts/TechnicMotorTestScript.cs