From d979864805121b949a33541721a2fa9e9e5ab012 Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Wed, 18 Nov 2020 20:21:51 +0100 Subject: [PATCH] Add DuploTrainBaseSpeedometer - Add Device - Add Example #124 non-breaking --- .../ExampleDuploTrainBase.cs | 15 +++-- .../Devices/DeviceFactory.cs | 2 + .../Devices/DuploTrainBaseSpeedometer.cs | 67 +++++++++++++++++++ .../Hubs/DuploTrainBaseHub.cs | 1 + 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs index 275c99e..4f09e05 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs @@ -15,14 +15,21 @@ await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder .AddHub(hubBuilder => { }) ); - var d1 = train.Voltage.VoltageSObservable.Subscribe(x => Log.LogWarning($"Voltage: {x.Pct}% / {x.SI}")); - var d2 = train.Motor.OnSecondsObservable.Subscribe(x => Log.LogWarning($"Seconds: {x}")); + 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.SI}% / {x.SI}")); + using var d4 = train.Speedometer.CountObservable.Subscribe(x => Log.LogWarning($"Count: {x}%")); await train.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true); // works exclusive vs. motor //await train.Motor.SetupNotificationAsync(train.Motor.ModeIndexOnSec, false); + 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); + await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn); await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red); await Task.Delay(1_000); @@ -41,12 +48,8 @@ await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder await Task.Delay(1_000); await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.WaterRefill); - await Task.Delay(1_000); - d1.Dispose(); - d2.Dispose(); - await train.SwitchOffAsync(); } } diff --git a/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs b/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs index 9e1009e..80866b1 100644 --- a/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs +++ b/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs @@ -52,6 +52,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType) DeviceType.MediumLinearMotor => typeof(MediumLinearMotor), DeviceType.DuploTrainBaseMotor => typeof(DuploTrainBaseMotor), DeviceType.DuploTrainBaseSpeaker => typeof(DuploTrainBaseSpeaker), + DeviceType.DuploTrainBaseSpeedometer => typeof(DuploTrainBaseSpeedometer), _ => null, }; @@ -80,6 +81,7 @@ public static DeviceType GetDeviceTypeFromType(Type type) nameof(MediumLinearMotor) => DeviceType.MediumLinearMotor, nameof(DuploTrainBaseMotor) => DeviceType.DuploTrainBaseMotor, nameof(DuploTrainBaseSpeaker) => DeviceType.DuploTrainBaseSpeaker, + nameof(DuploTrainBaseSpeedometer) => DeviceType.DuploTrainBaseSpeedometer, _ => DeviceType.Unknown, }; } diff --git a/src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs b/src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs new file mode 100644 index 0000000..4c245c0 --- /dev/null +++ b/src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; +using System.Threading.Tasks; +using SharpBrick.PoweredUp.Protocol; +using SharpBrick.PoweredUp.Utils; + +namespace SharpBrick.PoweredUp +{ + + public class DuploTrainBaseSpeedometer : Device, IPoweredUpDevice + { + protected SingleValueMode _speedMode; + protected SingleValueMode _countMode; + + public byte ModeIndexSpeed { get; protected set; } = 0; + public byte ModeIndexCount { get; protected set; } = 1; + + public short Speed => _speedMode.SI; + public short SpeedPct => _speedMode.Pct; + public IObservable> SpeedObservable => _speedMode.Observable; + + public int Count => _countMode.SI; + public IObservable CountObservable => _countMode.Observable.Select(v => v.SI); + + public DuploTrainBaseSpeedometer() + { } + + public DuploTrainBaseSpeedometer(ILegoWirelessProtocol protocol, byte hubId, byte portId) + : base(protocol, hubId, portId) + { + _speedMode = SingleValueMode(ModeIndexSpeed); + _countMode = SingleValueMode(ModeIndexCount); + + ObserveForPropertyChanged(_speedMode.Observable, nameof(Speed), nameof(SpeedPct)); + ObserveForPropertyChanged(_countMode.Observable, nameof(Count)); + } + + public IEnumerable GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType) + => @" +0B-00-43-13-01-06-03-07-00-00-00 +07-00-43-13-02-03-00 +11-00-44-13-00-00-53-50-45-45-44-00-00-00-00-00-00 +0E-00-44-13-00-01-00-00-96-C3-00-00-96-43 +0E-00-44-13-00-02-00-00-C8-C2-00-00-C8-42 +0E-00-44-13-00-03-00-00-20-C1-00-00-20-41 +0A-00-44-13-00-04-73-70-64-00 +08-00-44-13-00-05-50-00 +0A-00-44-13-00-80-01-01-05-00 +11-00-44-13-01-00-43-4F-55-4E-54-00-00-00-00-00-00 +0E-00-44-13-01-01-00-00-00-41-00-00-00-41 +0E-00-44-13-01-02-00-00-C8-C2-00-00-C8-42 +0E-00-44-13-01-03-00-00-B4-43-00-00-B4-43 +0A-00-44-13-01-04-63-6E-74-00 +08-00-44-13-01-05-08-00 +0A-00-44-13-01-80-01-02-04-00 +11-00-44-13-02-00-43-41-4C-49-42-00-00-00-00-00-00 +0E-00-44-13-02-01-00-00-00-00-00-00-80-45 +0E-00-44-13-02-02-00-00-00-00-00-00-C8-42 +0E-00-44-13-02-03-00-00-00-00-00-00-80-45 +0A-00-44-13-02-04-6E-2F-61-00 +08-00-44-13-02-05-08-00 +0A-00-44-13-02-80-04-01-05-00 +".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s)); + } +} \ No newline at end of file diff --git a/src/SharpBrick.PoweredUp/Hubs/DuploTrainBaseHub.cs b/src/SharpBrick.PoweredUp/Hubs/DuploTrainBaseHub.cs index a4f795f..e5e6d22 100644 --- a/src/SharpBrick.PoweredUp/Hubs/DuploTrainBaseHub.cs +++ b/src/SharpBrick.PoweredUp/Hubs/DuploTrainBaseHub.cs @@ -38,6 +38,7 @@ public DuploTrainBaseHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFa public DuploTrainBaseMotor Motor => Port(0).GetDevice(); public DuploTrainBaseSpeaker Speaker => Port(1).GetDevice(); public RgbLight RgbLight => Port(17).GetDevice(); + public DuploTrainBaseSpeedometer Speedometer => Port(19).GetDevice(); public Voltage Voltage => Port(20).GetDevice(); } } \ No newline at end of file