Skip to content

Commit

Permalink
C# 9.0 Updates - Records, Code Analyzers, some pattern matching (#139)
Browse files Browse the repository at this point in the history
- Converted Messages to Records
- Switched Protocol Core from netstandard2.1 to net5.0.
- Merged Records in Model files
- Pattern Match null
- Fix Code Analyzers Issues
- Ignore make function static Code Analysis
- Fixed unused usings

#42 non-breaking
  • Loading branch information
tthiery committed Mar 23, 2021
1 parent d0e92ad commit 3a7bd9f
Show file tree
Hide file tree
Showing 149 changed files with 1,759 additions and 2,178 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*.cs]
dotnet_diagnostic.CA1822.severity = none
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"omnisharp.enableRoslynAnalyzers": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ The `poweredup` command line utility intends to allow the inspection of LEGO Wir

## Installation Instruction

1. Install the [latest .NET (Core)](https://dotnet.microsoft.com/download) on your machine (e.g. .NET Core 3.1).
1. Install the [latest .NET](https://dotnet.microsoft.com/download) on your machine (e.g. .NET 5).
2. Install the `poweredup` dotnet utility using the following instruction
````
dotnet tool install -g SharpBrick.PoweredUp.Cli
Expand Down Expand Up @@ -260,7 +260,7 @@ DI Container Elements
## Implementation Status

- Bluetooth Adapter
- [X] .NET Core 3.1 (on Windows 10 using WinRT Bluetooth)
- [X] .NET Core 3.1 (on Windows 10 using WinRT Bluetooth). Please use version v3.4.0 and consider upgrading to .NET 5
- [X] .NET 5 (on Windows 10 using WinRT Bluetooth)
- [ ] UWP (most likely December 2021; UWP currently does not support .NET Standard 2.1 and C# 8.0+)
- [ ] .NET Framework 4.8 (will never be supported; .NET Framework does not and will never support .NET Standard 2.1 and C# 8.0+)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

Expand All @@ -21,22 +20,21 @@ public override async Task DiscoverAsync(bool enableTrace)

public override async Task ExecuteAsync()
{
using (var technicMediumHub = DirectlyConnectedHub)
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);
using var technicMediumHub = DirectlyConnectedHub;

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);

await technicMediumHub.SwitchOffAsync();
}
await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
20 changes: 9 additions & 11 deletions examples/SharpBrick.PoweredUp.Examples/ExampleBluetoothByName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

Expand All @@ -10,22 +9,21 @@ public class ExampleBluetoothByName : BaseExample

public override async Task ExecuteAsync()
{
using (var technicMediumHub = Host.FindByName<TechnicMediumHub>("Technic Hub"))
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);
using var technicMediumHub = Host.FindByName<TechnicMediumHub>("Technic Hub");

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);

await technicMediumHub.SwitchOffAsync();
}
await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ public class ExampleCalibrationSteering : BaseExample
{
public override async Task ExecuteAsync()
{
using (var technicMediumHub = Host.FindByType<TechnicMediumHub>())
{
var motor = technicMediumHub.A.GetDevice<TechnicLargeLinearMotor>();
using var technicMediumHub = Host.FindByType<TechnicMediumHub>();

var calibration = ServiceProvider.GetService<LinearMidCalibration>();
await calibration.ExecuteAsync(motor);
await technicMediumHub.WaitButtonClickAsync();
var motor = technicMediumHub.A.GetDevice<TechnicLargeLinearMotor>();

await motor.GotoPositionAsync(CW * 50, 20, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile);
await technicMediumHub.WaitButtonClickAsync();
var calibration = ServiceProvider.GetService<LinearMidCalibration>();
await calibration.ExecuteAsync(motor);
await technicMediumHub.WaitButtonClickAsync();

await motor.GotoPositionAsync(CCW * 50, 20, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile);
await Task.Delay(5000);
await motor.GotoPositionAsync(CW * 50, 20, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile);
await technicMediumHub.WaitButtonClickAsync();

await technicMediumHub.SwitchOffAsync();
}
await motor.GotoPositionAsync(CCW * 50, 20, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile);
await Task.Delay(5000);

await technicMediumHub.SwitchOffAsync();
}


Expand Down
12 changes: 5 additions & 7 deletions examples/SharpBrick.PoweredUp.Examples/ExampleColors.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

Expand All @@ -8,14 +7,13 @@ public class ExampleColors : BaseExample
{
public override async Task ExecuteAsync()
{
using (var technicMediumHub = Host.FindByType<TechnicMediumHub>())
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
using var technicMediumHub = Host.FindByType<TechnicMediumHub>();

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await technicMediumHub.SwitchOffAsync();
}
await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
20 changes: 9 additions & 11 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDiscoverByType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using SharpBrick.PoweredUp;

Expand All @@ -20,22 +19,21 @@ public override async Task DiscoverAsync(bool enableTrace)

public override async Task ExecuteAsync()
{
using (var technicMediumHub = DirectlyConnectedHub)
{
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);
using var technicMediumHub = DirectlyConnectedHub;

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0x00, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);

await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);
await Task.Delay(2000);

await Task.Delay(2000);
await technicMediumHub.RgbLight.SetRgbColorsAsync(0xff, 0xff, 0x00);

await technicMediumHub.SwitchOffAsync();
}
await Task.Delay(2000);

await technicMediumHub.SwitchOffAsync();
}
}
}
93 changes: 46 additions & 47 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,62 @@ public class ExampleDuploTrainBase : BaseExample
{
public override async Task ExecuteAsync()
{
using (var train = Host.FindByType<DuploTrainBaseHub>())
{
await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<DuploTrainBaseHub>(hubBuilder => { })
);
using var train = Host.FindByType<DuploTrainBaseHub>();

using var d1 = train.Voltage.VoltageSObservable.Subscribe(x => Log.LogWarning($"Voltage: {x.Pct}% / {x.SI}"));
using var d2 = train.Motor.OnSecondsObservable.Subscribe(x => Log.LogWarning($"Seconds: {x}"));
using var d3 = train.Speedometer.SpeedObservable.Subscribe(x => Log.LogWarning($"Speed: {x.Pct}% / {x.SI}"));
using var d4 = train.Speedometer.CountObservable.Subscribe(x => Log.LogWarning($"Count: {x}"));
using var d5 = train.ColorSensor.ColorObservable.Subscribe(x => Log.LogWarning($"Color: {x}"));
using var d6 = train.ColorSensor.ColorTagObservable.Subscribe(x => Log.LogWarning($"Color Tag: {x}")); // does not work
using var d7 = train.ColorSensor.ReflectionObservable.Subscribe(x => Log.LogWarning($"Reflection: {x}"));
using var d8 = train.ColorSensor.RgbObservable.Subscribe(x => Log.LogWarning($"RGB {x.red}/{x.green}/{x.blue}"));
await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<DuploTrainBaseHub>(hubBuilder => { })
);

await train.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true);
using var d1 = train.Voltage.VoltageSObservable.Subscribe(x => Log.LogWarning($"Voltage: {x.Pct}% / {x.SI}"));
using var d2 = train.Motor.OnSecondsObservable.Subscribe(x => Log.LogWarning($"Seconds: {x}"));
using var d3 = train.Speedometer.SpeedObservable.Subscribe(x => Log.LogWarning($"Speed: {x.Pct}% / {x.SI}"));
using var d4 = train.Speedometer.CountObservable.Subscribe(x => Log.LogWarning($"Count: {x}"));
using var d5 = train.ColorSensor.ColorObservable.Subscribe(x => Log.LogWarning($"Color: {x}"));
using var d6 = train.ColorSensor.ColorTagObservable.Subscribe(x => Log.LogWarning($"Color Tag: {x}")); // does not work
using var d7 = train.ColorSensor.ReflectionObservable.Subscribe(x => Log.LogWarning($"Reflection: {x}"));
using var d8 = train.ColorSensor.RgbObservable.Subscribe(x => Log.LogWarning($"RGB {x.red}/{x.green}/{x.blue}"));

// Motor: motor can either be queried for seconds active OR be instructed to run
//await train.Motor.SetupNotificationAsync(train.Motor.ModeIndexOnSec, false);
await train.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true);

await train.Speedometer.TryLockDeviceForCombinedModeNotificationSetupAsync(train.Speedometer.ModeIndexSpeed, train.Speedometer.ModeIndexCount);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexSpeed, true, deltaInterval: 1);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexCount, true, deltaInterval: 1);
await train.Speedometer.UnlockFromCombinedModeNotificationSetupAsync(true);
// Motor: motor can either be queried for seconds active OR be instructed to run
//await train.Motor.SetupNotificationAsync(train.Motor.ModeIndexOnSec, false);

// ColorSensor: either this combination
// await train.ColorSensor.TryLockDeviceForCombinedModeNotificationSetupAsync(train.ColorSensor.ModeIndexColor, train.ColorSensor.ModeIndexReflection, train.ColorSensor.ModeIndexRgb);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColor, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexReflection, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexRgb, true, deltaInterval: 1);
// await train.ColorSensor.UnlockFromCombinedModeNotificationSetupAsync(true);
await train.Speedometer.TryLockDeviceForCombinedModeNotificationSetupAsync(train.Speedometer.ModeIndexSpeed, train.Speedometer.ModeIndexCount);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexSpeed, true, deltaInterval: 1);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexCount, true, deltaInterval: 1);
await train.Speedometer.UnlockFromCombinedModeNotificationSetupAsync(true);

// ColorSensor: or standalone
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColorTag, true, deltaInterval: 1);
// ColorSensor: either this combination
// await train.ColorSensor.TryLockDeviceForCombinedModeNotificationSetupAsync(train.ColorSensor.ModeIndexColor, train.ColorSensor.ModeIndexReflection, train.ColorSensor.ModeIndexRgb);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColor, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexReflection, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexRgb, true, deltaInterval: 1);
// await train.ColorSensor.UnlockFromCombinedModeNotificationSetupAsync(true);

await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Yellow);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.StationDeparture);
// ColorSensor: or standalone
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColorTag, true, deltaInterval: 1);

await train.Motor.StartPowerAsync(40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Steam);
await train.Motor.StartPowerAsync(-40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Brake);
await train.Motor.StopByFloatAsync();
await Task.Delay(1_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.WaterRefill);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Yellow);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.StationDeparture);

await Task.Delay(1_000);
await train.Motor.StartPowerAsync(40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Steam);
await train.Motor.StartPowerAsync(-40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Brake);
await train.Motor.StopByFloatAsync();
await Task.Delay(1_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.WaterRefill);

await train.SwitchOffAsync();
}
await Task.Delay(1_000);

await train.SwitchOffAsync();
}
}
}
Loading

0 comments on commit 3a7bd9f

Please sign in to comment.