Skip to content

Commit

Permalink
Add DuploTrainBaseSpeedometer
Browse files Browse the repository at this point in the history
- Add Device
- Add Example

#124 non-breaking
  • Loading branch information
tthiery committed Nov 18, 2020
1 parent 08e574f commit d979864
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
15 changes: 9 additions & 6 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<DuploTrainBaseHub>(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);
Expand All @@ -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();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
};
}
Expand Down
67 changes: 67 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs
Original file line number Diff line number Diff line change
@@ -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<short> _speedMode;
protected SingleValueMode<int> _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<Value<short>> SpeedObservable => _speedMode.Observable;

public int Count => _countMode.SI;
public IObservable<int> CountObservable => _countMode.Observable.Select(v => v.SI);

public DuploTrainBaseSpeedometer()
{ }

public DuploTrainBaseSpeedometer(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_speedMode = SingleValueMode<short>(ModeIndexSpeed);
_countMode = SingleValueMode<int>(ModeIndexCount);

ObserveForPropertyChanged(_speedMode.Observable, nameof(Speed), nameof(SpeedPct));
ObserveForPropertyChanged(_countMode.Observable, nameof(Count));
}

public IEnumerable<byte[]> 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));
}
}
1 change: 1 addition & 0 deletions src/SharpBrick.PoweredUp/Hubs/DuploTrainBaseHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public DuploTrainBaseHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFa
public DuploTrainBaseMotor Motor => Port(0).GetDevice<DuploTrainBaseMotor>();
public DuploTrainBaseSpeaker Speaker => Port(1).GetDevice<DuploTrainBaseSpeaker>();
public RgbLight RgbLight => Port(17).GetDevice<RgbLight>();
public DuploTrainBaseSpeedometer Speedometer => Port(19).GetDevice<DuploTrainBaseSpeedometer>();
public Voltage Voltage => Port(20).GetDevice<Voltage>();
}
}

0 comments on commit d979864

Please sign in to comment.