From 3b3e09ed5bdbc7b1b659323a456771e10cb457bd Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Sun, 15 Nov 2020 14:27:29 +0100 Subject: [PATCH] Add capability to override default delta interval - MarioTagSensor reduce default delta interval to 3 to be able to scan blue color no #91 --- src/SharpBrick.PoweredUp/Devices/Device.cs | 10 +++++++++- src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs | 8 ++++++++ .../Protocol/Formatter/PortValueSingleEncoder.cs | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/SharpBrick.PoweredUp/Devices/Device.cs b/src/SharpBrick.PoweredUp/Devices/Device.cs index 825e6e1..bc9bd3a 100644 --- a/src/SharpBrick.PoweredUp/Devices/Device.cs +++ b/src/SharpBrick.PoweredUp/Devices/Device.cs @@ -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, @@ -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); diff --git a/src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs b/src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs index 8298f2b..258b3be 100644 --- a/src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs +++ b/src/SharpBrick.PoweredUp/Devices/MarioHubTagSensor.cs @@ -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 GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType) => @" 0B-00-43-01-01-06-02-03-00-00-00 diff --git a/src/SharpBrick.PoweredUp/Protocol/Formatter/PortValueSingleEncoder.cs b/src/SharpBrick.PoweredUp/Protocol/Formatter/PortValueSingleEncoder.cs index 07c248c..71bc9a8 100644 --- a/src/SharpBrick.PoweredUp/Protocol/Formatter/PortValueSingleEncoder.cs +++ b/src/SharpBrick.PoweredUp/Protocol/Formatter/PortValueSingleEncoder.cs @@ -79,6 +79,7 @@ internal static PortValueData CreatePortValueDataSByte(PortModeInfo modeI var rawValues = MemoryMarshal.Cast(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()