Skip to content

Commit

Permalink
Fixed Sample and code after testing
Browse files Browse the repository at this point in the history
- Adjusted Sample
- Add DuploColorTag Enum
- Disable Scaling for Speedometer Count

#124 non-breaking
  • Loading branch information
tthiery committed Nov 18, 2020
1 parent 9d299ad commit 5b324bd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
23 changes: 13 additions & 10 deletions examples/SharpBrick.PoweredUp.Examples/ExampleDuploTrainBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder

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}%"));
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}"));
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.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true);

// motor can either be queried for seconds active OR be instructed to run
// 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);
// 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);

// ColorSensor: or standalone
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColorTag, true, deltaInterval: 1);

await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);
Expand Down
6 changes: 2 additions & 4 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseColorSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
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;
Expand All @@ -22,13 +20,13 @@ public class DuploTrainBaseColorSensor : Device, IPoweredUpDevice
public byte ModeIndexRgb { get; protected set; } = 3;

public sbyte Color => _colorMode.SI;
public sbyte ColorTag => _colorTagMode.SI;
public DuploColorTag ColorTag => (DuploColorTag)_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<DuploColorTag> ColorTagObservable => _colorTagMode.Observable.Select(v => (DuploColorTag)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]));
Expand Down
12 changes: 10 additions & 2 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Protocol.Knowledge;
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp
{

public class DuploTrainBaseSpeedometer : Device, IPoweredUpDevice
{
protected SingleValueMode<short> _speedMode;
Expand Down Expand Up @@ -37,6 +36,15 @@ public DuploTrainBaseSpeedometer(ILegoWirelessProtocol protocol, byte hubId, byt
ObserveForPropertyChanged(_countMode.Observable, nameof(Count));
}

public void ExtendPortMode(PortModeInfo portModeInfo)
{
if (portModeInfo.ModeIndex == ModeIndexCount)
{
portModeInfo.DisableScaling = true;
portModeInfo.DisablePercentage = true;
}
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> @"
0B-00-43-13-01-06-03-07-00-00-00
Expand Down
12 changes: 12 additions & 0 deletions src/SharpBrick.PoweredUp/Enums/DuploColorTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SharpBrick.PoweredUp
{
public enum DuploColorTag : sbyte
{
None = -1,
Water = 3, // blue
Redirect = 5, // green
Sound = 7, // yellow
Stop = 9, // red
Light = 10, // white
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ internal static PortValueData<int> CreatePortValueDataInt32(PortModeInfo modeInf
{
var rawValues = MemoryMarshal.Cast<byte, int>(dataSlice).ToArray();

var siValues = rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.SIMin, modeInfo.SIMax)).Select(f => Convert.ToInt32(f)).ToArray();
var pctValues = modeInfo.DisablePercentage switch
var siValues = modeInfo switch
{
{ DisableScaling: true } => rawValues,
_ => rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.SIMin, modeInfo.SIMax)).Select(f => Convert.ToInt32(f)).ToArray(),
};

var pctValues = modeInfo switch
{
false => rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.PctMin, modeInfo.PctMax)).Select(f => Convert.ToInt32(f)).ToArray(),
true => new int[rawValues.Length],
{ DisablePercentage: true } => new int[rawValues.Length],
{ DisableScaling: true } => rawValues,
_ => rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.PctMin, modeInfo.PctMax)).Select(f => Convert.ToInt32(f)).ToArray(),
};

return new PortValueData<int>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ public class PortModeInfo

// Additional Settings
public bool DisablePercentage { get; set; } = false;
public bool DisableScaling { get; set; } = false;
}
}

0 comments on commit 5b324bd

Please sign in to comment.