Skip to content

Commit

Permalink
Add capability to override default delta interval
Browse files Browse the repository at this point in the history
- MarioTagSensor reduce default delta interval to 3
  to be able to scan blue color no

#91
  • Loading branch information
tthiery committed Nov 15, 2020
1 parent e6aec69 commit 3b3e09e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/SharpBrick.PoweredUp/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ protected void BuildModes()
}
}

public async Task SetupNotificationAsync(byte modeIndex, bool enabled, uint deltaInterval = 5)
public async Task SetupNotificationAsync(byte modeIndex, bool enabled, uint deltaInterval = uint.MaxValue)
{
AssertIsConnected();

if (deltaInterval == uint.MaxValue)
{
deltaInterval = GetDefaultDeltaInterval(modeIndex);
}

await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
{
HubId = _hubId,
Expand All @@ -83,6 +88,9 @@ await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
});
}

protected virtual uint GetDefaultDeltaInterval(byte modeIndex)
=> 5;

private byte IndexOfSupportedCombinedMode(byte[] modeIndices)
{
var portInfo = _protocol.Knowledge.Port(_hubId, _portId);
Expand Down
8 changes: 8 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public MarioHubTagSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId
ObserveForPropertyChanged(_rgbMode.Observable, nameof(RgbColor));
}

protected override uint GetDefaultDeltaInterval(byte modeIndex)
=> modeIndex switch
{
0x00 => 3,
0x01 => 5,
_ => 5,
};

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> @"
0B-00-43-01-01-06-02-03-00-00-00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ internal static PortValueData<sbyte> CreatePortValueDataSByte(PortModeInfo modeI
var rawValues = MemoryMarshal.Cast<byte, sbyte>(dataSlice).ToArray();

var siValues = rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.SIMin, modeInfo.SIMax)).Select(f => Convert.ToSByte(f)).ToArray();
//TODO: temporary constraint to enable Mario
var pctValues = rawValues.Select(rv => Scale(rv, modeInfo.RawMin, modeInfo.RawMax, modeInfo.PctMin, modeInfo.PctMax)).Select(f => (sbyte)((f > 128 || f < -127) ? 128 : Convert.ToSByte(f))).ToArray();

return new PortValueData<sbyte>()
Expand Down

0 comments on commit 3b3e09e

Please sign in to comment.