Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/no_cache_on_discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Menno Lodder committed Jan 19, 2024
2 parents 338d743 + 996beac commit 5064fb5
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,5 @@ $RECYCLE.BIN/
!.vscode/extensions.json
/src/SharpBrick.PoweredUp.BlueGigaBLE/.editorconfig
/examples/SharpBrick.PoweredUp.Examples/Properties/launchSettings.json
src/SharpBrick.PoweredUp.Cli/Properties/launchSettings.json
/src/SharpBrick.PoweredUp.Cli/Properties/launchSettings.json
/src/SharpBrick.PoweredUp.Cli/poweredup.json
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ DI Container Elements
- [X] Technic Large Angular Motor (Grey)
- [X] Technic Color Sensor
- [X] Technic Distance Sensor
- [X] Motor WeDo 2.0 Medium (21980)
- .. other devices depend on availability of hardware / contributions
- Protocol
- [X] Message Encoding (98% [spec coverage](docs/specification/coverage.md))
Expand Down
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Devices/Current.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Ve
08-00-44-3B-01-05-10-00
0A-00-44-3B-01-80-01-01-04-00
",
_ => throw new NotSupportedException(),
_ => throw BuildNotSupportedException(softwareVersion, hardwareVersion, systemType),
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
5 changes: 5 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ protected void AssertIsVirtualPort()
}
}

protected NotSupportedException BuildNotSupportedException(Version softwareVersion, Version hardwareVersion, SystemType systemType)
{
return new NotSupportedException($"Device '{this.GetType().Name}' does not support Systemtype '{systemType}' on Hardware '{hardwareVersion}' and Software '{ softwareVersion }'");
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
Expand Down
2 changes: 2 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public IPoweredUpDevice CreateConnected(DeviceType deviceType, ILegoWirelessProt
public Type GetTypeFromDeviceType(DeviceType deviceType)
=> deviceType switch
{
DeviceType.Motor => typeof(SimpleMediumLinearMotor),
DeviceType.Voltage => typeof(Voltage),
DeviceType.Current => typeof(Current),
DeviceType.RgbLight => typeof(RgbLight),
Expand Down Expand Up @@ -65,6 +66,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType)
public static DeviceType GetDeviceTypeFromType(Type type)
=> type.Name switch // fuzzy but will work
{
nameof(SimpleMediumLinearMotor) => DeviceType.Motor,
nameof(Voltage) => DeviceType.Voltage,
nameof(Current) => DeviceType.Current,
nameof(RgbLight) => DeviceType.RgbLight,
Expand Down
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Devices/RgbLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Ve
08-00-44-32-01-05-00-10
0A-00-44-32-01-80-03-00-03-00
",
_ => throw new NotSupportedException(),
_ => throw BuildNotSupportedException(softwareVersion, hardwareVersion, systemType)
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
34 changes: 34 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/SimpleMediumLinearMotor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp;

/// <summary>
/// SimpleMediumLinearMotor, advertised as Motor, only supports power mode, this works for the WoDo 2.0 Medium (LPF2-MMOTOR)
/// </summary>
public class SimpleMediumLinearMotor : BasicMotor, IPoweredUpDevice
{
public SimpleMediumLinearMotor()
: base()
{ }
public SimpleMediumLinearMotor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{ }

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
// Dump taken from LEGO 6290182 - 21980 - Electric, Motor WeDo 2.0 Medium which reports as 'LPF2-MMOTOR'.
=> @"
0B-00-43-02-01-01-01-00-00-01-00
05-00-43-02-02
11-00-44-02-00-00-4C-50-46-32-2D-4D-4D-4F-54-4F-52
0E-00-44-02-00-01-00-00-C8-C2-00-00-C8-42
0E-00-44-02-00-02-00-00-C8-C2-00-00-C8-42
0E-00-44-02-00-03-00-00-C8-C2-00-00-C8-42
0A-00-44-02-00-04-00-00-00-00
08-00-44-02-00-05-00-10
0A-00-44-02-00-80-01-00-04-00
".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Ve
08-00-44-63-02-05-00-10
0A-00-44-63-02-80-02-00-03-00
".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s)),
_ => throw new NotSupportedException("SharpBrick.PoweredUp currently does not support this version of the sensor."),
_ => throw BuildNotSupportedException(softwareVersion, hardwareVersion, systemType),
};
}
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Devices/Voltage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Ve
08-00-44-3C-01-05-10-00
0A-00-44-3C-01-80-01-01-04-00
",
_ => throw new NotImplementedException(),
_ => throw BuildNotSupportedException(softwareVersion, hardwareVersion, systemType),
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public LegoWirelessMessage Decode(byte hubId, in Span<byte> data)

var message = informationType switch
{
PortModeInformationType.Name => new PortModeInformationForNameMessage(portId, mode, Encoding.ASCII.GetString(payload.Slice(0, payload.IndexOf<byte>(0x00)))),
// Here the specs say to use the length field and just pick the remainder of the bytes, no termination.
PortModeInformationType.Name => new PortModeInformationForNameMessage(portId, mode, ParseStringIgnoringTrainingNullCharacters(payload)),
PortModeInformationType.Raw => new PortModeInformationForRawMessage(portId, mode, RawMin: BitConverter.ToSingle(payload.Slice(0, 4)), RawMax: BitConverter.ToSingle(payload.Slice(4, 4))),
PortModeInformationType.Pct => new PortModeInformationForPctMessage(portId, mode, PctMin: BitConverter.ToSingle(payload.Slice(0, 4)), PctMax: BitConverter.ToSingle(payload.Slice(4, 4))),
PortModeInformationType.SI => new PortModeInformationForSIMessage(portId, mode, SIMin: BitConverter.ToSingle(payload.Slice(0, 4)), SIMax: BitConverter.ToSingle(payload.Slice(4, 4))),
Expand All @@ -34,6 +35,18 @@ public LegoWirelessMessage Decode(byte hubId, in Span<byte> data)
return message;
}

private static string ParseStringIgnoringTrainingNullCharacters(Span<byte> data)
{
// Most devices pad the name with 0x00
var firstNullCharIndex = data.IndexOf<byte>(0x00);
var dataToDecode = data;
if (firstNullCharIndex != -1) {
// Trim everything after the last 0x00.
dataToDecode = data.Slice(0, firstNullCharIndex);
}

return Encoding.ASCII.GetString(dataToDecode);
}
private PortModeInformationMessage DecodeMapping(byte portId, byte mode, in Span<byte> data)
=> new PortModeInformationForMappingMessage(
portId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public PortModeInfo PortMode(byte hubId, byte portId, byte modeIndex)

if (!(port.Modes.TryGetValue(modeIndex, out var result)))
{
throw new ArgumentException("modeIndex is null", nameof(modeIndex));
throw new ArgumentException($"Unknown modeIndex {modeIndex}, on portId {portId} for hubId {hubId}", nameof(modeIndex));
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Protocol/LegoWirelessProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ await KnowledgeManager.ApplyDynamicProtocolKnowledge(new HubPropertyMessage<Syst
}, Knowledge, _deviceFactory, UseCachedInformation());

await _kernel.ConnectAsync();

_logger.LogInformation("Connected to device, getting protocol information");
await _kernel.ReceiveBytesAsync(async data =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class PortModeInformationEncoderTest
[InlineData("11-00-44-00-03-00-41-50-4F-53-00-00-00-00-00-00-00", 0x00, 0x03, PortModeInformationType.Name, "APOS")]
[InlineData("11-00-44-00-04-00-4C-4F-41-44-00-00-00-00-00-00-00", 0x00, 0x04, PortModeInformationType.Name, "LOAD")]
[InlineData("11-00-44-00-05-00-43-41-4C-49-42-00-00-00-00-00-00", 0x00, 0x05, PortModeInformationType.Name, "CALIB")]
// WeDo 2.0 actually uses full name length
[InlineData("11-00-44-00-00-00-4C-50-46-32-2D-4D-4D-4F-54-4F-52", 0x00, 0x00, PortModeInformationType.Name, "LPF2-MMOTOR")]
public void PortModeInformationEncoder_Decode_Name(string data, byte expectedPort, byte expectedMode, PortModeInformationType expectedType, string expectedText)
{
var message = Decode<PortModeInformationForNameMessage>(data);
Expand Down

0 comments on commit 5064fb5

Please sign in to comment.