Skip to content

Commit

Permalink
Add DuploTrainBaseColorSensor
Browse files Browse the repository at this point in the history
- Add Sensor
- Extend Example

#124 non-breaking
  • Loading branch information
tthiery committed Nov 18, 2020
1 parent d979864 commit 9d299ad
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ DI Container Elements
- [X] Duplo Train Base (set 10874) - Motor
- [X] Duplo Train Base (set 10874) - Speaker
- [X] Duplo Train Base (set 10874) - Rgb Light
- [ ] Duplo Train Base (set 10874) - ColorSensor
- [ ] Duplo Train Base (set 10874) - Speedometer
- [X] Duplo Train Base (set 10874) - ColorSensor
- [X] Duplo Train Base (set 10874) - Speedometer
- [X] Medium Linear Motor (88008)
- [X] Remote Control Button (88010)
- [X] Remote Control RSSI (88010)
Expand Down
13 changes: 12 additions & 1 deletion examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@ await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
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}%"));
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}"));
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.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true);

// works exclusive vs. motor
// motor can either be queried for seconds active OR be instructed to run
//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.ColorSensor.TryLockDeviceForCombinedModeNotificationSetupAsync(train.ColorSensor.ModeIndexColor, train.ColorSensor.ModeIndexColorTag, train.ColorSensor.ModeIndexReflection, train.ColorSensor.ModeIndexRgb);
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColor, true, deltaInterval: 5);
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColorTag, true, deltaInterval: 5);
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexReflection, true, deltaInterval: 5);
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexRgb, true, deltaInterval: 5);
await train.ColorSensor.UnlockFromCombinedModeNotificationSetupAsync(true);

await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);
await Task.Delay(1_000);
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.DuploTrainBaseColorSensor => typeof(DuploTrainBaseColorSensor),
DeviceType.DuploTrainBaseSpeedometer => typeof(DuploTrainBaseSpeedometer),
_ => null,
};
Expand Down Expand Up @@ -81,6 +82,7 @@ public static DeviceType GetDeviceTypeFromType(Type type)
nameof(MediumLinearMotor) => DeviceType.MediumLinearMotor,
nameof(DuploTrainBaseMotor) => DeviceType.DuploTrainBaseMotor,
nameof(DuploTrainBaseSpeaker) => DeviceType.DuploTrainBaseSpeaker,
nameof(DuploTrainBaseColorSensor) => DeviceType.DuploTrainBaseColorSensor,
nameof(DuploTrainBaseSpeedometer) => DeviceType.DuploTrainBaseSpeedometer,
_ => DeviceType.Unknown,
};
Expand Down
94 changes: 94 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseColorSensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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 DuploTrainBaseColorSensor : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte> _colorMode;
protected SingleValueMode<sbyte> _colorTagMode;
protected SingleValueMode<sbyte> _reflectionMode;
protected MultiValueMode<short> _rgbMode;

public byte ModeIndexColor { get; protected set; } = 0;
public byte ModeIndexColorTag { get; protected set; } = 1;
public byte ModeIndexReflection { get; protected set; } = 2;
public byte ModeIndexRgb { get; protected set; } = 3;

public sbyte Color => _colorMode.SI;
public sbyte ColorTag => _colorTagMode.SI;
public sbyte Reflection => _reflectionMode.SI;
public (short red, short green, short blue) Rgb => (_rgbMode.SI[0], _rgbMode.SI[1], _rgbMode.SI[2]);
public (short red, short green, short blue) RgbPct => (_rgbMode.Pct[0], _rgbMode.Pct[1], _rgbMode.Pct[2]);

public IObservable<sbyte> ColorObservable => _colorMode.Observable.Select(v => v.SI);
public IObservable<sbyte> ColorTagObservable => _colorTagMode.Observable.Select(v => v.SI);
public IObservable<sbyte> ReflectionObservable => _reflectionMode.Observable.Select(v => v.SI);
public IObservable<(short red, short green, short blue)> RgbObservable => _rgbMode.Observable.Select(v => (v.SI[0], v.SI[1], v.SI[2]));
public IObservable<(short red, short green, short blue)> RgbPctObservable => _rgbMode.Observable.Select(v => (v.Pct[0], v.Pct[1], v.Pct[2]));

public DuploTrainBaseColorSensor()
{ }

public DuploTrainBaseColorSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_colorMode = SingleValueMode<sbyte>(ModeIndexColor);
_colorTagMode = SingleValueMode<sbyte>(ModeIndexColorTag);
_reflectionMode = SingleValueMode<sbyte>(ModeIndexReflection);
_rgbMode = MultiValueMode<short>(ModeIndexRgb);

ObserveForPropertyChanged(_colorMode.Observable, nameof(Color));
ObserveForPropertyChanged(_colorTagMode.Observable, nameof(ColorTag));
ObserveForPropertyChanged(_reflectionMode.Observable, nameof(Reflection));
ObserveForPropertyChanged(_rgbMode.Observable, nameof(Rgb), nameof(RgbPct));
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> @"
0B-00-43-12-01-06-05-1F-00-00-00
07-00-43-12-02-0F-00
11-00-44-12-00-00-43-4F-4C-4F-52-00-00-00-00-00-00
0E-00-44-12-00-01-00-00-00-00-00-00-20-41
0E-00-44-12-00-02-00-00-00-00-00-00-C8-42
0E-00-44-12-00-03-00-00-00-00-00-00-20-41
0A-00-44-12-00-04-69-64-78-00
08-00-44-12-00-05-84-00
0A-00-44-12-00-80-01-00-03-00
11-00-44-12-01-00-43-20-54-41-47-00-00-00-00-00-00
0E-00-44-12-01-01-00-00-00-00-00-00-20-41
0E-00-44-12-01-02-00-00-00-00-00-00-C8-42
0E-00-44-12-01-03-00-00-00-00-00-00-20-41
0A-00-44-12-01-04-69-64-78-00
08-00-44-12-01-05-C4-00
0A-00-44-12-01-80-01-00-03-00
11-00-44-12-02-00-52-45-46-4C-54-00-00-00-00-00-00
0E-00-44-12-02-01-00-00-00-00-00-00-C8-42
0E-00-44-12-02-02-00-00-00-00-00-00-C8-42
0E-00-44-12-02-03-00-00-00-00-00-00-C8-42
0A-00-44-12-02-04-72-61-77-00
08-00-44-12-02-05-10-00
0A-00-44-12-02-80-01-00-03-00
11-00-44-12-03-00-52-47-42-20-49-00-00-00-00-00-00
0E-00-44-12-03-01-00-00-00-00-00-C0-7F-44
0E-00-44-12-03-02-00-00-00-00-00-00-C8-42
0E-00-44-12-03-03-00-00-00-00-00-C0-7F-44
0A-00-44-12-03-04-72-61-77-00
08-00-44-12-03-05-10-00
0A-00-44-12-03-80-03-01-05-00
11-00-44-12-04-00-43-41-4C-49-42-00-00-00-00-00-00
0E-00-44-12-04-01-00-00-00-00-00-00-FA-45
0E-00-44-12-04-02-00-00-00-00-00-00-C8-42
0E-00-44-12-04-03-00-00-00-00-00-00-FA-45
0A-00-44-12-04-04-00-00-00-00
08-00-44-12-04-05-10-00
0A-00-44-12-04-80-03-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 DuploTrainBaseColorSensor ColorSensor => Port(18).GetDevice<DuploTrainBaseColorSensor>();
public DuploTrainBaseSpeedometer Speedometer => Port(19).GetDevice<DuploTrainBaseSpeedometer>();
public Voltage Voltage => Port(20).GetDevice<Voltage>();
}
Expand Down

0 comments on commit 9d299ad

Please sign in to comment.