Skip to content

Commit

Permalink
Convert existing devices to ValueModes
Browse files Browse the repository at this point in the history
#37 non-breaking
  • Loading branch information
tthiery committed Jul 10, 2020
1 parent c588e93 commit 720fedd
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 101 deletions.
2 changes: 0 additions & 2 deletions examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ static async Task Main(string[] args)
{
var enableTrace = (args.Length > 0 && args[0] == "--trace");

//await ; return;

Example.BaseExample example;

//example = new Example.ExampleColors();
Expand Down
11 changes: 5 additions & 6 deletions src/SharpBrick.PoweredUp/Devices/AbsoluteMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ namespace SharpBrick.PoweredUp
{
public abstract class AbsoluteMotor : TachoMotor
{
protected SingleValueMode<short> _absoluteMode;
public byte ModeIndexAbsolutePosition { get; protected set; } = 3;

public short AbsolutePosition { get; private set; } = 0;
public short AbsolutePositionPct { get; private set; } = 0;
public IObservable<Value<short>> AbsolutePositionObservable { get; }
public short AbsolutePosition => _absoluteMode.SI;
public short AbsolutePositionPct => _absoluteMode.Pct;
public IObservable<Value<short>> AbsolutePositionObservable => _absoluteMode.Observable;

public AbsoluteMotor()
{ }

protected AbsoluteMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
AbsolutePositionObservable = CreateSinglePortModeValueObservable<short>(ModeIndexAbsolutePosition);

ObserveOnLocalProperty(AbsolutePositionObservable, v => AbsolutePosition = v.SI, v => AbsolutePositionPct = v.Pct);
_absoluteMode = SingleValueMode<short>(ModeIndexAbsolutePosition);
}

/// <summary>
Expand Down
11 changes: 5 additions & 6 deletions src/SharpBrick.PoweredUp/Devices/BasicMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ namespace SharpBrick.PoweredUp
{
public abstract class BasicMotor : Device
{
protected SingleValueMode<sbyte> _powerMode;
public byte ModeIndexPower { get; protected set; } = 0;

public sbyte Power { get; private set; } = 0;
public sbyte PowerPct { get; private set; } = 0;
public IObservable<Value<sbyte>> PowerObservable { get; }
public sbyte Power => _powerMode.SI;
public sbyte PowerPct => _powerMode.Pct;
public IObservable<Value<sbyte>> PowerObservable => _powerMode.Observable;

public BasicMotor()
{ }

public BasicMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
PowerObservable = CreateSinglePortModeValueObservable<sbyte>(ModeIndexPower);

ObserveOnLocalProperty(PowerObservable, v => Power = v.SI, v => PowerPct = v.Pct);
_powerMode = SingleValueMode<sbyte>(ModeIndexPower);
}

/// <summary>
Expand Down
25 changes: 12 additions & 13 deletions src/SharpBrick.PoweredUp/Devices/Current.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@ namespace SharpBrick.PoweredUp
{
public class Current : Device, IPoweredUpDevice
{
public byte ModeIndexCurrentL { get; } = 0x00;
public byte ModeIndexCurrentS { get; } = 0x01;
protected SingleValueMode<short> _currentLMode;
protected SingleValueMode<short> _currentSMode;
public byte ModeIndexCurrentL { get; protected set; } = 0x00;
public byte ModeIndexCurrentS { get; protected set; } = 0x01;

public short CurrentL { get; private set; } = 0;
public short CurrentLPct { get; private set; } = 0;
public IObservable<Value<short>> CurrentLObservable { get; }
public short CurrentL => _currentLMode.SI;
public short CurrentLPct => _currentLMode.Pct;
public IObservable<Value<short>> CurrentLObservable => _currentLMode.Observable;

public short CurrentS { get; private set; } = 0;
public short CurrentSPct { get; private set; } = 0;
public IObservable<Value<short>> CurrentSObservable { get; }
public short CurrentS => _currentSMode.SI;
public short CurrentSPct => _currentSMode.Pct;
public IObservable<Value<short>> CurrentSObservable => _currentSMode.Observable;

public Current()
{ }

public Current(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
CurrentLObservable = CreateSinglePortModeValueObservable<short>(ModeIndexCurrentL);
CurrentSObservable = CreateSinglePortModeValueObservable<short>(ModeIndexCurrentS);

ObserveOnLocalProperty(CurrentLObservable, v => CurrentL = v.SI, v => CurrentLPct = v.Pct);
ObserveOnLocalProperty(CurrentSObservable, v => CurrentS = v.SI, v => CurrentSPct = v.Pct);
_currentLMode = SingleValueMode<short>(ModeIndexCurrentL);
_currentSMode = SingleValueMode<short>(ModeIndexCurrentS);
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
Expand Down
27 changes: 0 additions & 27 deletions src/SharpBrick.PoweredUp/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,6 @@ protected void BuildModes()
}
}

protected void ObserveOnLocalProperty<T>(IObservable<T> modeObservable, params Action<T>[] updaters)
{
var disposable = modeObservable.Subscribe(v =>
{
foreach (var u in updaters)
{
u(v);
}
});

_compositeDisposable.Add(disposable);
}

protected IObservable<Value<TPayload>> CreateSinglePortModeValueObservable<TPayload>(byte modeIndex)
=> CreatePortModeValueObservable<TPayload, Value<TPayload>>(modeIndex, pvd => new Value<TPayload>()
{
Raw = pvd.InputValues[0],
SI = pvd.SIInputValues[0],
Pct = pvd.PctInputValues[0],
});

protected IObservable<TTarget> CreatePortModeValueObservable<TSource, TTarget>(byte modeIndex, Func<PortValueData<TSource>, TTarget> converter)
=> _portValueObservable
.Where(pvd => pvd.ModeIndex == modeIndex)
.Cast<PortValueData<TSource>>()
.Select(converter);

public async Task SetupNotificationAsync(byte modeIndex, bool enabled, uint deltaInterval = 5)
{
AssertIsConnected();
Expand Down
21 changes: 10 additions & 11 deletions src/SharpBrick.PoweredUp/Devices/TachoMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@ namespace SharpBrick.PoweredUp
{
public abstract class TachoMotor : BasicMotor
{
protected SingleValueMode<sbyte> _speedMode;
protected SingleValueMode<int> _positionMode;
public byte ModeIndexSpeed { get; protected set; } = 1;
public byte ModeIndexPosition { get; protected set; } = 2;

public sbyte Speed { get; private set; } = 0;
public sbyte SpeedPct { get; private set; } = 0;
public IObservable<Value<sbyte>> SpeedObservable { get; }
public sbyte Speed => _speedMode.SI;
public sbyte SpeedPct => _speedMode.Pct;
public IObservable<Value<sbyte>> SpeedObservable => _speedMode.Observable;

public int Position { get; private set; } = 0;
public int PositionPct { get; private set; } = 0;
public IObservable<Value<int>> PositionObservable { get; }
public int Position => _positionMode.SI;
public int PositionPct => _positionMode.Pct;
public IObservable<Value<int>> PositionObservable => _positionMode.Observable;

public TachoMotor()
{ }

protected TachoMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
SpeedObservable = CreateSinglePortModeValueObservable<sbyte>(ModeIndexSpeed);
PositionObservable = CreateSinglePortModeValueObservable<int>(ModeIndexPosition);

ObserveOnLocalProperty(SpeedObservable, v => Speed = v.SI, v => SpeedPct = v.Pct);
ObserveOnLocalProperty(PositionObservable, v => Position = v.SI, v => PositionPct = v.Pct);
_speedMode = SingleValueMode<sbyte>(ModeIndexSpeed);
_positionMode = SingleValueMode<int>(ModeIndexPosition);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Utils;

Expand All @@ -9,21 +10,21 @@ namespace SharpBrick.PoweredUp
// accelerometers measure translation
public class TechnicMediumHubAccelerometer : Device, IPoweredUpDevice
{
protected MultiValueMode<short> _gravityMode;

public byte ModeIndexGravity { get; protected set; } = 0;
public byte ModeIndexCalibration { get; protected set; } = 1;

public (short x, short y, short z) Gravity { get; private set; }
public IObservable<(short x, short y, short z)> GravityObservable { get; }
public (short x, short y, short z) Gravity => (_gravityMode.SI[0], _gravityMode.SI[1], _gravityMode.SI[2]);
public IObservable<(short x, short y, short z)> GravityObservable => _gravityMode.Observable.Select(v => (v.SI[0], v.SI[1], v.SI[2]));

public TechnicMediumHubAccelerometer()
{ }

public TechnicMediumHubAccelerometer(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
GravityObservable = CreatePortModeValueObservable<short, (short x, short y, short z)>(ModeIndexGravity, pvd => (pvd.SIInputValues[0], pvd.SIInputValues[1], pvd.SIInputValues[2]));

ObserveOnLocalProperty(GravityObservable, g => Gravity = g);
_gravityMode = MultiValueMode<short>(ModeIndexGravity);
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
Expand Down
10 changes: 5 additions & 5 deletions src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGyroSensor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Utils;

Expand All @@ -9,20 +10,19 @@ namespace SharpBrick.PoweredUp
// gyroscopes measure rotation
public class TechnicMediumHubGyroSensor : Device, IPoweredUpDevice
{
protected MultiValueMode<short> _rotationMode;
public byte ModeIndexRotation { get; protected set; } = 0;

public (short x, short y, short z) Rotation { get; private set; }
public IObservable<(short x, short y, short z)> RotationObservable { get; }
public (short x, short y, short z) Rotation => (_rotationMode.SI[0], _rotationMode.SI[1], _rotationMode.SI[2]);
public IObservable<(short x, short y, short z)> RotationObservable => _rotationMode.Observable.Select(v => (v.SI[0], v.SI[1], v.SI[2]));

public TechnicMediumHubGyroSensor()
{ }

public TechnicMediumHubGyroSensor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
RotationObservable = CreatePortModeValueObservable<short, (short x, short y, short z)>(ModeIndexRotation, pvd => (pvd.SIInputValues[0], pvd.SIInputValues[1], pvd.SIInputValues[2]));

ObserveOnLocalProperty(RotationObservable, r => Rotation = r);
_rotationMode = MultiValueMode<short>(ModeIndexRotation);
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ namespace SharpBrick.PoweredUp
{
public class TechnicMediumHubTemperatureSensor : Device, IPoweredUpDevice
{
protected SingleValueMode<short> _temperatureMode;
public byte ModeIndexTemperature { get; protected set; } = 0;

public short Temperature { get; private set; }
public short TemperaturePct { get; private set; }
public IObservable<Value<short>> TemperatureObservable { get; }
public short Temperature => _temperatureMode.SI;
public short TemperaturePct => _temperatureMode.Pct;
public IObservable<Value<short>> TemperatureObservable => _temperatureMode.Observable;

public TechnicMediumHubTemperatureSensor()
{ }

public TechnicMediumHubTemperatureSensor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
TemperatureObservable = CreateSinglePortModeValueObservable<short>(ModeIndexTemperature);

ObserveOnLocalProperty(TemperatureObservable, v => Temperature = v.SI, v => TemperaturePct = v.Pct);
_temperatureMode = SingleValueMode<short>(ModeIndexTemperature);
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
Expand Down
18 changes: 9 additions & 9 deletions src/SharpBrick.PoweredUp/Devices/TechnicMediumHubTiltSensor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Protocol.Messages;
Expand All @@ -10,26 +11,25 @@ namespace SharpBrick.PoweredUp
{
public class TechnicMediumHubTiltSensor : Device, IPoweredUpDevice
{
protected MultiValueMode<short> _positionMode;
protected SingleValueMode<int> _impactsMode;
public byte ModeIndexPosition { get; protected set; } = 0;
public byte ModeIndexImpacts { get; protected set; } = 1;
public byte ModeIndexConfig { get; protected set; } = 2;

public (short x, short y, short z) Position { get; private set; }
public int Impacts { get; private set; }
public IObservable<(short x, short y, short z)> PositionObservable { get; }
public IObservable<Value<int>> ImpactsObservable { get; }
public (short x, short y, short z) Position => (_positionMode.SI[0], _positionMode.SI[1], _positionMode.SI[2]);
public int Impacts => _impactsMode.SI;
public IObservable<(short x, short y, short z)> PositionObservable => _positionMode.Observable.Select(v => (v.SI[0], v.SI[1], v.SI[2]));
public IObservable<Value<int>> ImpactsObservable => _impactsMode.Observable;

public TechnicMediumHubTiltSensor()
{ }

public TechnicMediumHubTiltSensor(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
PositionObservable = CreatePortModeValueObservable<short, (short, short, short)>(ModeIndexPosition, pvd => (pvd.SIInputValues[0], pvd.SIInputValues[1], pvd.SIInputValues[2]));
ImpactsObservable = CreateSinglePortModeValueObservable<int>(ModeIndexImpacts);

ObserveOnLocalProperty(PositionObservable, v => Position = v);
ObserveOnLocalProperty(ImpactsObservable, v => Impacts = v.SI);
_positionMode = MultiValueMode<short>(ModeIndexPosition);
_impactsMode = SingleValueMode<int>(ModeIndexImpacts);
}

/// <summary>
Expand Down
21 changes: 10 additions & 11 deletions src/SharpBrick.PoweredUp/Devices/Voltage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@ namespace SharpBrick.PoweredUp
{
public class Voltage : Device, IPoweredUpDevice
{
protected SingleValueMode<short> _voltageLMode;
protected SingleValueMode<short> _voltageSMode;
public byte ModeIndexVoltageL { get; protected set; } = 0;
public byte ModeIndexVoltageS { get; protected set; } = 1;

public short VoltageL { get; private set; }
public short VoltageLPct { get; private set; }
public IObservable<Value<short>> VoltageLObservable { get; }
public short VoltageL => _voltageLMode.SI;
public short VoltageLPct => _voltageLMode.Pct;
public IObservable<Value<short>> VoltageLObservable => _voltageLMode.Observable;

public short VoltageS { get; private set; }
public short VoltageSPct { get; private set; }
public IObservable<Value<short>> VoltageSObservable { get; }
public short VoltageS => _voltageSMode.SI;
public short VoltageSPct => _voltageSMode.Pct;
public IObservable<Value<short>> VoltageSObservable => _voltageSMode.Observable;

public Voltage()
{ }

public Voltage(IPoweredUpProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
VoltageLObservable = CreateSinglePortModeValueObservable<short>(ModeIndexVoltageL);
VoltageSObservable = CreateSinglePortModeValueObservable<short>(ModeIndexVoltageS);

ObserveOnLocalProperty(VoltageLObservable, v => VoltageL = v.SI, v => VoltageLPct = v.Pct);
ObserveOnLocalProperty(VoltageSObservable, v => VoltageS = v.SI, v => VoltageSPct = v.Pct);
_voltageLMode = SingleValueMode<short>(ModeIndexVoltageL);
_voltageSMode = SingleValueMode<short>(ModeIndexVoltageS);
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
Expand Down

0 comments on commit 720fedd

Please sign in to comment.