Skip to content

Commit

Permalink
Add option to decode to double (#160)
Browse files Browse the repository at this point in the history
- Reorganized decoder (PortValueSingleEncoder)
- Reorganized Single/MultiValueMode infrastructure of devices
- Re-aligned all devices to new TOutputType
- No Testing yet
- No override to double for any port mode

#126 breaking
  • Loading branch information
tthiery authored Apr 22, 2021
1 parent 4ea75b8 commit bc66846
Show file tree
Hide file tree
Showing 35 changed files with 258 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ await technicMediumHub.VerifyDeploymentModelAsync(mb => mb
await dynamicDeviceWhichIsAMotor.UnlockFromCombinedModeNotificationSetupAsync(true);

// get the individual modes for input and output
var powerMode = dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0);
var posMode = dynamicDeviceWhichIsAMotor.SingleValueMode<int>(2);
var aposMode = dynamicDeviceWhichIsAMotor.SingleValueMode<short>(3);
var powerMode = dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte, sbyte>(0);
var posMode = dynamicDeviceWhichIsAMotor.SingleValueMode<int, int>(2);
var aposMode = dynamicDeviceWhichIsAMotor.SingleValueMode<short, short>(3);

// use their observables to report values
using var disposable = posMode.Observable.Subscribe(x => Log.LogWarning($"Position: {x.SI} / {x.Pct}"));
Expand Down
6 changes: 3 additions & 3 deletions src/SharpBrick.PoweredUp/Devices/AbsoluteMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ namespace SharpBrick.PoweredUp
{
public abstract class AbsoluteMotor : TachoMotor
{
protected SingleValueMode<short> _absoluteMode;
protected SingleValueMode<short, short> _absoluteMode;
public byte ModeIndexAbsolutePosition { get; protected set; } = 3;

public short AbsolutePosition => _absoluteMode.SI;
public short AbsolutePositionPct => _absoluteMode.Pct;
public IObservable<Value<short>> AbsolutePositionObservable => _absoluteMode.Observable;
public IObservable<Value<short, short>> AbsolutePositionObservable => _absoluteMode.Observable;

public AbsoluteMotor()
{ }

protected AbsoluteMotor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_absoluteMode = SingleValueMode<short>(ModeIndexAbsolutePosition);
_absoluteMode = SingleValueMode<short, short>(ModeIndexAbsolutePosition);

ObserveForPropertyChanged(_absoluteMode.Observable, nameof(AbsolutePosition), nameof(AbsolutePositionPct));
}
Expand Down
6 changes: 3 additions & 3 deletions src/SharpBrick.PoweredUp/Devices/BasicMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace SharpBrick.PoweredUp
{
public abstract class BasicMotor : Device
{
protected SingleValueMode<sbyte> _powerMode;
protected SingleValueMode<sbyte, sbyte> _powerMode;
public byte ModeIndexPower { get; protected set; } = 0;

public sbyte Power => _powerMode.SI;
public sbyte PowerPct => _powerMode.Pct;
public IObservable<Value<sbyte>> PowerObservable => _powerMode.Observable;
public IObservable<Value<sbyte, sbyte>> PowerObservable => _powerMode.Observable;

public BasicMotor()
{ }

public BasicMotor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_powerMode = SingleValueMode<sbyte>(ModeIndexPower);
_powerMode = SingleValueMode<sbyte, sbyte>(ModeIndexPower);

ObserveForPropertyChanged(_powerMode.Observable, nameof(Power), nameof(PowerPct));
}
Expand Down
106 changes: 53 additions & 53 deletions src/SharpBrick.PoweredUp/Devices/ColorDistanceSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,62 @@

namespace SharpBrick.PoweredUp
{
public class ColorDistanceSensor : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte> _colorMode;
protected SingleValueMode<sbyte> _proximityMode;
protected SingleValueMode<int> _countMode;
protected SingleValueMode<sbyte> _reflectionMode;
protected SingleValueMode<sbyte> _ambientLightMode;
protected SingleValueMode<sbyte> _lightMode;
protected MultiValueMode<short> _rgbMode;
public class ColorDistanceSensor : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte, sbyte> _colorMode;
protected SingleValueMode<sbyte, sbyte> _proximityMode;
protected SingleValueMode<int, int> _countMode;
protected SingleValueMode<sbyte, sbyte> _reflectionMode;
protected SingleValueMode<sbyte, sbyte> _ambientLightMode;
protected SingleValueMode<sbyte, sbyte> _lightMode;
protected MultiValueMode<short, short> _rgbMode;

public byte ModeIndexColor { get; protected set; } = 0;
public byte ModeIndexProximity { get; protected set; } = 1;
public byte ModeIndexCount { get; protected set; } = 2;
public byte ModeIndexReflection { get; protected set; } = 3;
public byte ModeIndexAmbientLight { get; protected set; } = 4;
public byte ModeIndexLight { get; protected set; } = 5;
public byte ModeIndexRgb { get; protected set; } = 6;
public byte ModeIndexIRTx { get; protected set; } = 7;
public byte ModeIndexSPEC1 { get; protected set; } = 8;
public byte ModeIndexDebug { get; protected set; } = 9;
public byte ModeIndexCalibration { get; protected set; } = 10;
public byte ModeIndexColor { get; protected set; } = 0;
public byte ModeIndexProximity { get; protected set; } = 1;
public byte ModeIndexCount { get; protected set; } = 2;
public byte ModeIndexReflection { get; protected set; } = 3;
public byte ModeIndexAmbientLight { get; protected set; } = 4;
public byte ModeIndexLight { get; protected set; } = 5;
public byte ModeIndexRgb { get; protected set; } = 6;
public byte ModeIndexIRTx { get; protected set; } = 7;
public byte ModeIndexSPEC1 { get; protected set; } = 8;
public byte ModeIndexDebug { get; protected set; } = 9;
public byte ModeIndexCalibration { get; protected set; } = 10;

public TechnicColor Color => (TechnicColor)_colorMode.SI;
public IObservable<TechnicColor> ColorObservable => _colorMode.Observable.Select(v => (TechnicColor)v.SI);
public IObservable<sbyte> ProximityObservable => _proximityMode.Observable.Select(v => v.SI);
public IObservable<int> CountObservable => _countMode.Observable.Select(v => v.SI);
public IObservable<sbyte> ReflectionObservable => _reflectionMode.Observable.Select(v => v.SI);
public IObservable<sbyte> AmbientLightObservable => _ambientLightMode.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 TechnicColor Color => (TechnicColor)_colorMode.SI;
public IObservable<TechnicColor> ColorObservable => _colorMode.Observable.Select(v => (TechnicColor)v.SI);
public IObservable<sbyte> ProximityObservable => _proximityMode.Observable.Select(v => v.SI);
public IObservable<int> CountObservable => _countMode.Observable.Select(v => v.SI);
public IObservable<sbyte> ReflectionObservable => _reflectionMode.Observable.Select(v => v.SI);
public IObservable<sbyte> AmbientLightObservable => _ambientLightMode.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 ColorDistanceSensor()
{ }
public ColorDistanceSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_colorMode = SingleValueMode<sbyte>(ModeIndexColor);
_proximityMode = SingleValueMode<sbyte>(ModeIndexProximity);
_countMode = SingleValueMode<int>(ModeIndexCount);
_reflectionMode = SingleValueMode<sbyte>(ModeIndexReflection);
_ambientLightMode = SingleValueMode<sbyte>(ModeIndexAmbientLight);
_lightMode = SingleValueMode<sbyte>(ModeIndexLight);
_rgbMode = MultiValueMode<short>(ModeIndexRgb);
}
public ColorDistanceSensor()
{ }
public ColorDistanceSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_colorMode = SingleValueMode<sbyte, sbyte>(ModeIndexColor);
_proximityMode = SingleValueMode<sbyte, sbyte>(ModeIndexProximity);
_countMode = SingleValueMode<int, int>(ModeIndexCount);
_reflectionMode = SingleValueMode<sbyte, sbyte>(ModeIndexReflection);
_ambientLightMode = SingleValueMode<sbyte, sbyte>(ModeIndexAmbientLight);
_lightMode = SingleValueMode<sbyte, sbyte>(ModeIndexLight);
_rgbMode = MultiValueMode<short, short>(ModeIndexRgb);
}

protected override uint GetDefaultDeltaInterval(byte modeIndex)
=> modeIndex switch
{
0 => 1,
2 => 1,
_ => base.GetDefaultDeltaInterval(modeIndex),
};
protected override uint GetDefaultDeltaInterval(byte modeIndex)
=> modeIndex switch
{
0 => 1,
2 => 1,
_ => base.GetDefaultDeltaInterval(modeIndex),
};

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> ((softwareVersion, hardwareVersion, systemType) switch
{
(_, _, _) => @"
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> ((softwareVersion, hardwareVersion, systemType) switch
{
(_, _, _) => @"
0B-00-43-01-01-07-0B-5F-06-A0-00
07-00-43-01-02-4F-00
12-00-44-01-00-00-43-4F-4C-4F-52-00-00-00-00-00-00-00
Expand Down Expand Up @@ -145,6 +145,6 @@ public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Ve
08-00-44-01-0A-05-10-00
0A-00-44-01-0A-80-08-01-05-00
"
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
}
12 changes: 6 additions & 6 deletions src/SharpBrick.PoweredUp/Devices/Current.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ namespace SharpBrick.PoweredUp
{
public class Current : Device, IPoweredUpDevice
{
protected SingleValueMode<short> _currentLMode;
protected SingleValueMode<short> _currentSMode;
protected SingleValueMode<short, short> _currentLMode;
protected SingleValueMode<short, short> _currentSMode;
public byte ModeIndexCurrentL { get; protected set; } = 0x00;
public byte ModeIndexCurrentS { get; protected set; } = 0x01;

public short CurrentL => _currentLMode.SI;
public short CurrentLPct => _currentLMode.Pct;
public IObservable<Value<short>> CurrentLObservable => _currentLMode.Observable;
public IObservable<Value<short, short>> CurrentLObservable => _currentLMode.Observable;

public short CurrentS => _currentSMode.SI;
public short CurrentSPct => _currentSMode.Pct;
public IObservable<Value<short>> CurrentSObservable => _currentSMode.Observable;
public IObservable<Value<short, short>> CurrentSObservable => _currentSMode.Observable;

public Current()
{ }

public Current(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_currentLMode = SingleValueMode<short>(ModeIndexCurrentL);
_currentSMode = SingleValueMode<short>(ModeIndexCurrentS);
_currentLMode = SingleValueMode<short, short>(ModeIndexCurrentL);
_currentSMode = SingleValueMode<short, short>(ModeIndexCurrentS);

ObserveForPropertyChanged(_currentLMode.Observable, nameof(CurrentL), nameof(CurrentLPct));
ObserveForPropertyChanged(_currentSMode.Observable, nameof(CurrentS), nameof(CurrentSPct));
Expand Down
8 changes: 4 additions & 4 deletions src/SharpBrick.PoweredUp/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public Device(ILegoWirelessProtocol protocol, byte hubId, byte portId)
BuildModes();
}

public SingleValueMode<TPayload> SingleValueMode<TPayload>(byte modeIndex)
=> _modes.TryGetValue(modeIndex, out var mode) ? mode as SingleValueMode<TPayload> : default;
public MultiValueMode<TPayload> MultiValueMode<TPayload>(byte modeIndex)
=> _modes.TryGetValue(modeIndex, out var mode) ? mode as MultiValueMode<TPayload> : default;
public SingleValueMode<TDatasetType, TOutputType> SingleValueMode<TDatasetType, TOutputType>(byte modeIndex)
=> _modes.TryGetValue(modeIndex, out var mode) ? mode as SingleValueMode<TDatasetType, TOutputType> : default;
public MultiValueMode<TDatasetType, TOutputType> MultiValueMode<TDatasetType, TOutputType>(byte modeIndex)
=> _modes.TryGetValue(modeIndex, out var mode) ? mode as MultiValueMode<TDatasetType, TOutputType> : default;

protected void BuildModes()
{
Expand Down
16 changes: 8 additions & 8 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseColorSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace SharpBrick.PoweredUp
{
public class DuploTrainBaseColorSensor : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte> _colorMode;
protected SingleValueMode<sbyte> _colorTagMode;
protected SingleValueMode<sbyte> _reflectionMode;
protected MultiValueMode<short> _rgbMode;
protected SingleValueMode<sbyte, sbyte> _colorMode;
protected SingleValueMode<sbyte, sbyte> _colorTagMode;
protected SingleValueMode<sbyte, sbyte> _reflectionMode;
protected MultiValueMode<short, short> _rgbMode;

public byte ModeIndexColor { get; protected set; } = 0;
public byte ModeIndexColorTag { get; protected set; } = 1;
Expand All @@ -37,10 +37,10 @@ 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);
_colorMode = SingleValueMode<sbyte, sbyte>(ModeIndexColor);
_colorTagMode = SingleValueMode<sbyte, sbyte>(ModeIndexColorTag);
_reflectionMode = SingleValueMode<sbyte, sbyte>(ModeIndexReflection);
_rgbMode = MultiValueMode<short, short>(ModeIndexRgb);

ObserveForPropertyChanged(_colorMode.Observable, nameof(Color));
ObserveForPropertyChanged(_colorTagMode.Observable, nameof(ColorTag));
Expand Down
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SharpBrick.PoweredUp
{
public class DuploTrainBaseMotor : Device, IPoweredUpDevice
{
public SingleValueMode<int> _onSecMode;
public SingleValueMode<int, int> _onSecMode;

public byte ModeIndexMotor { get; protected set; } = 0;
public byte ModeIndexOnSec { get; protected set; } = 1;
Expand All @@ -26,7 +26,7 @@ public DuploTrainBaseMotor()
public DuploTrainBaseMotor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_onSecMode = SingleValueMode<int>(ModeIndexOnSec);
_onSecMode = SingleValueMode<int, int>(ModeIndexOnSec);

ObserveForPropertyChanged(_onSecMode.Observable, nameof(OnSeconds));
}
Expand Down
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SharpBrick.PoweredUp

public class DuploTrainBaseSpeaker : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte> _soundMode;
protected SingleValueMode<sbyte, sbyte> _soundMode;

public byte ModeIndexTone { get; protected set; } = 0;
public byte ModeIndexSound { get; protected set; } = 1;
Expand All @@ -23,7 +23,7 @@ public DuploTrainBaseSpeaker()
public DuploTrainBaseSpeaker(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_soundMode = SingleValueMode<sbyte>(ModeIndexSound);
_soundMode = SingleValueMode<sbyte, sbyte>(ModeIndexSound);
}

public async Task<PortFeedback> PlaySoundAsync(DuploTrainBaseSound sound)
Expand Down
10 changes: 5 additions & 5 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseSpeedometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace SharpBrick.PoweredUp
{
public class DuploTrainBaseSpeedometer : Device, IPoweredUpDevice
{
protected SingleValueMode<short> _speedMode;
protected SingleValueMode<int> _countMode;
protected SingleValueMode<short, short> _speedMode;
protected SingleValueMode<int, 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 IObservable<Value<short, short>> SpeedObservable => _speedMode.Observable;

public int Count => _countMode.SI;
public IObservable<int> CountObservable => _countMode.Observable.Select(v => v.SI);
Expand All @@ -29,8 +29,8 @@ public DuploTrainBaseSpeedometer()
public DuploTrainBaseSpeedometer(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_speedMode = SingleValueMode<short>(ModeIndexSpeed);
_countMode = SingleValueMode<int>(ModeIndexCount);
_speedMode = SingleValueMode<short, short>(ModeIndexSpeed);
_countMode = SingleValueMode<int, int>(ModeIndexCount);

ObserveForPropertyChanged(_speedMode.Observable, nameof(Speed), nameof(SpeedPct));
ObserveForPropertyChanged(_countMode.Observable, nameof(Count));
Expand Down
8 changes: 4 additions & 4 deletions src/SharpBrick.PoweredUp/Devices/MarioHubAccelerometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace SharpBrick.PoweredUp
{
public class MarioHubAccelerometer : Device, IPoweredUpDevice
{
protected MultiValueMode<sbyte> _rawMode;
protected MultiValueMode<short> _gestMode;
protected MultiValueMode<sbyte, sbyte> _rawMode;
protected MultiValueMode<short, short> _gestMode;

public byte ModeIndexRaw { get; protected set; } = 0;
public byte ModeIndexGesture { get; protected set; } = 1;
Expand All @@ -26,8 +26,8 @@ public MarioHubAccelerometer()
public MarioHubAccelerometer(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_rawMode = MultiValueMode<sbyte>(ModeIndexRaw);
_gestMode = MultiValueMode<short>(ModeIndexGesture);
_rawMode = MultiValueMode<sbyte, sbyte>(ModeIndexRaw);
_gestMode = MultiValueMode<short, short>(ModeIndexGesture);

//ObserveForPropertyChanged(_rawMode.Observable, nameof(Coins));
}
Expand Down
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Devices/MarioHubPants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SharpBrick.PoweredUp
// https://github.com/bricklife/LEGO-Mario-Reveng/blob/master/IOType-0x4a.md
public class MarioHubPants : Device, IPoweredUpDevice
{
protected SingleValueMode<sbyte> _pantsMode;
protected SingleValueMode<sbyte, sbyte> _pantsMode;

public byte ModeIndexPants { get; protected set; } = 0;

Expand All @@ -23,7 +23,7 @@ public MarioHubPants()
public MarioHubPants(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_pantsMode = SingleValueMode<sbyte>(ModeIndexPants);
_pantsMode = SingleValueMode<sbyte, sbyte>(ModeIndexPants);

ObserveForPropertyChanged(_pantsMode.Observable, nameof(Pants));
}
Expand Down
8 changes: 4 additions & 4 deletions src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace SharpBrick.PoweredUp
{
public class MarioHubTagSensor : Device, IPoweredUpDevice
{
protected MultiValueMode<short> _tagMode;
protected MultiValueMode<sbyte> _rgbMode;
protected MultiValueMode<short, short> _tagMode;
protected MultiValueMode<sbyte, sbyte> _rgbMode;

public byte ModeIndexTag { get; protected set; } = 0;
public byte ModeIndexRgb { get; protected set; } = 1;
Expand All @@ -31,8 +31,8 @@ public MarioHubTagSensor()
public MarioHubTagSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_tagMode = MultiValueMode<short>(ModeIndexTag);
_rgbMode = MultiValueMode<sbyte>(ModeIndexRgb);
_tagMode = MultiValueMode<short, short>(ModeIndexTag);
_rgbMode = MultiValueMode<sbyte, sbyte>(ModeIndexRgb);

ObserveForPropertyChanged(_tagMode.Observable, nameof(Barcode));
ObserveForPropertyChanged(_tagMode.Observable, nameof(ColorNo));
Expand Down
Loading

0 comments on commit bc66846

Please sign in to comment.