diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs index 5d1d46b..ee7f94d 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleTwoHubsMotorControl.cs @@ -32,64 +32,63 @@ public override async Task DiscoverAsync(bool enableTrace) public override async Task ExecuteAsync() { - using (TechnicMediumHub technicMediumHub1 = DirectlyConnectedHub1) - using (TwoPortHub technicMediumHub2 = DirectlyConnectedHub2) - { - await technicMediumHub1.VerifyDeploymentModelAsync(modelBuilder => modelBuilder - .AddHub(hubBuilder => hubBuilder - .AddDevice(technicMediumHub1.A) - ) - ); - await technicMediumHub2.VerifyDeploymentModelAsync(modelBuilder => modelBuilder - .AddHub(hubBuilder => hubBuilder - .AddDevice(technicMediumHub2.A) - ) - ); - - var motor1 = technicMediumHub1.A.GetDevice(); - var motor2 = technicMediumHub2.A.GetDevice(); - - await motor1.SetAccelerationTimeAsync(3000); - await motor1.SetDecelerationTimeAsync(1000); - await motor2.SetAccelerationTimeAsync(3000); - await motor2.SetDecelerationTimeAsync(1000); - - await motor1.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile); - await motor2.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile); - - await Task.Delay(5_000); - - await motor1.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - await motor2.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - - await Task.Delay(5_000); - - await motor1.StartSpeedAsync(100, 90, SpeedProfiles.None); - await motor2.StartSpeedAsync(100, 90, SpeedProfiles.None); - - await Task.Delay(5_000); - - await motor1.StartSpeedAsync(-100, 90, SpeedProfiles.None); - await motor2.StartSpeedAsync(-100, 90, SpeedProfiles.None); - - await Task.Delay(5_000); - - await motor1.StartSpeedAsync(0, 90, SpeedProfiles.None); - await motor2.StartSpeedAsync(0, 90, SpeedProfiles.None); - - await Task.Delay(5_000); - - await motor1.GotoPositionAsync(45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - await motor2.GotoPositionAsync(45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - - await Task.Delay(5_000); - - await motor1.GotoPositionAsync(-45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - await motor2.GotoPositionAsync(-45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); - - await technicMediumHub1.SwitchOffAsync(); - await technicMediumHub2.SwitchOffAsync(); - } + using TechnicMediumHub technicMediumHub1 = DirectlyConnectedHub1; + using TwoPortHub technicMediumHub2 = DirectlyConnectedHub2; + + await technicMediumHub1.VerifyDeploymentModelAsync(modelBuilder => modelBuilder + .AddHub(hubBuilder => hubBuilder + .AddDevice(technicMediumHub1.A) + ) + ); + await technicMediumHub2.VerifyDeploymentModelAsync(modelBuilder => modelBuilder + .AddHub(hubBuilder => hubBuilder + .AddDevice(technicMediumHub2.A) + ) + ); + + var motor1 = technicMediumHub1.A.GetDevice(); + var motor2 = technicMediumHub2.A.GetDevice(); + + await motor1.SetAccelerationTimeAsync(3000); + await motor1.SetDecelerationTimeAsync(1000); + await motor2.SetAccelerationTimeAsync(3000); + await motor2.SetDecelerationTimeAsync(1000); + + await motor1.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile); + await motor2.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DecelerationProfile); + + await Task.Delay(5_000); + + await motor1.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + await motor2.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + + await Task.Delay(5_000); + + await motor1.StartSpeedAsync(100, 90, SpeedProfiles.None); + await motor2.StartSpeedAsync(100, 90, SpeedProfiles.None); + + await Task.Delay(5_000); + + await motor1.StartSpeedAsync(-100, 90, SpeedProfiles.None); + await motor2.StartSpeedAsync(-100, 90, SpeedProfiles.None); + + await Task.Delay(5_000); + + await motor1.StartSpeedAsync(0, 90, SpeedProfiles.None); + await motor2.StartSpeedAsync(0, 90, SpeedProfiles.None); + + await Task.Delay(5_000); + + await motor1.GotoPositionAsync(45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + await motor2.GotoPositionAsync(45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + + await Task.Delay(5_000); + + await motor1.GotoPositionAsync(-45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + await motor2.GotoPositionAsync(-45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None); + + await technicMediumHub1.SwitchOffAsync(); + await technicMediumHub2.SwitchOffAsync(); } } } \ No newline at end of file diff --git a/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs b/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs index 0206471..f1270b4 100644 --- a/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs +++ b/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs @@ -205,7 +205,7 @@ public async Task GetLogInfosAsync(int indent) _ = stringToLog.Append($"{indentStr}*** Bluegiga-Adapter-Info ***:" + Environment.NewLine + $"{indentStr}Serial Port Name: {SerialAPI.PortName}" + Environment.NewLine + $"{indentStr}Serial Port Speed: {SerialAPI.BaudRate} baud" + Environment.NewLine); - if (Devices.Count > 0) + if (!Devices.IsEmpty) { _ = stringToLog.Append($"{indentStr}I know about the following {Devices.Count} devices connected to me:{Environment.NewLine}"); foreach (var device in Devices) @@ -220,7 +220,7 @@ public async Task GetLogInfosAsync(int indent) _ = stringToLog.Append($"{indentStr}I DON'T know about any devices connected to me!{Environment.NewLine}"); } - if (DevicesInfo.Count > 0) + if (!DevicesInfo.IsEmpty) { _ = stringToLog.Append($"{indentStr}I know about the following {Devices.Count} devices which have been found by Discovery (not neccessarily connected):{Environment.NewLine}"); var innerindentStr = indentStr + "\t"; diff --git a/src/SharpBrick.PoweredUp.BlueGigaBLE/ServiceCollectionExtensionsForBlueGigaBLE.cs b/src/SharpBrick.PoweredUp.BlueGigaBLE/ServiceCollectionExtensionsForBlueGigaBLE.cs index e44d96d..b2d4181 100644 --- a/src/SharpBrick.PoweredUp.BlueGigaBLE/ServiceCollectionExtensionsForBlueGigaBLE.cs +++ b/src/SharpBrick.PoweredUp.BlueGigaBLE/ServiceCollectionExtensionsForBlueGigaBLE.cs @@ -33,7 +33,7 @@ public static IBlueGigaBluetoothBuilder SetBlueGigaBLEOptions(this IBlueGigaBlue { if (builder == null) { - throw new ArgumentException(nameof(builder)); + throw new ArgumentNullException(nameof(builder)); } _ = builder.Services.Configure(o => { diff --git a/src/SharpBrick.PoweredUp.Mobile/XamarinPoweredUpBluetoothCharacteristic.cs b/src/SharpBrick.PoweredUp.Mobile/XamarinPoweredUpBluetoothCharacteristic.cs index d41078c..7d8811b 100644 --- a/src/SharpBrick.PoweredUp.Mobile/XamarinPoweredUpBluetoothCharacteristic.cs +++ b/src/SharpBrick.PoweredUp.Mobile/XamarinPoweredUpBluetoothCharacteristic.cs @@ -10,13 +10,13 @@ namespace SharpBrick.PoweredUp.Mobile { public class XamarinPoweredUpBluetoothCharacteristic : IPoweredUpBluetoothCharacteristic { - private ICharacteristic _characteristic; + private readonly ICharacteristic _characteristic; public Guid Uuid => _characteristic.Id; public XamarinPoweredUpBluetoothCharacteristic(ICharacteristic characteristic) { - this._characteristic = characteristic; + _characteristic = characteristic; } public async Task NotifyValueChangeAsync(Func notificationHandler) diff --git a/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothDevice.cs b/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothDevice.cs index f70b245..844325a 100644 --- a/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothDevice.cs +++ b/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothDevice.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading.Tasks; using SharpBrick.PoweredUp.Bluetooth; using Windows.Devices.Bluetooth; @@ -30,9 +29,9 @@ public async Task GetServiceAsync(Guid serviceId) { var gatt = await _device.GetGattServicesForUuidAsync(serviceId); - if (gatt.Status == GattCommunicationStatus.Success) + if (gatt.Status == GattCommunicationStatus.Success && gatt.Services.Count > 0) { - var service = gatt.Services.FirstOrDefault(); + var service = gatt.Services[0]; return new WinRTPoweredUpBluetoothService(service); } diff --git a/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothService.cs b/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothService.cs index b890b78..0e8b194 100644 --- a/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothService.cs +++ b/src/SharpBrick.PoweredUp.WinRT/WinRTPoweredUpBluetoothService.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading.Tasks; using SharpBrick.PoweredUp.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; @@ -30,7 +29,7 @@ public async Task GetCharacteristicAsync(Guid if (characteristics.Status == GattCommunicationStatus.Success && characteristics.Characteristics.Count > 0) { - var characteristic = characteristics.Characteristics.FirstOrDefault(); + var characteristic = characteristics.Characteristics[0]; return new WinRTPoweredUpBluetoothCharacteristic(characteristic); }