diff --git a/README.md b/README.md index ea76f9a..d508e57 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,8 @@ Console.WriteLine($"Or directly read the latest value: {aposMode.SI} / {aposMode ## Connect to Hub and Send a Message and retrieving answers (directly on protocol layer) +***Note**: The `ILegoWirelessProtocol` class was renamed in 3.0. Previously it is known as `IPoweredUpProtocol`.* + ````csharp var serviceProvider = new ServiceCollection() @@ -245,7 +247,7 @@ DI Container Elements - [X] .NET Core 3.1 (on Windows 10 using WinRT) - Library uses `Span` / C# 8.0 and is therefore not supported in .NET Framework 1.0 - 4.8 and UWP Apps until arrival of .NET 5 (WinForms and WPF work in .NET Core 3.1) - Library uses WinRT for communication therefore only Windows 10 - - [ ] Xamarin (on iOS / Android using Xamarin.Essentials) + - [ ] Xamarin (on iOS / Android using ?) - [ ] Blazor (on Browser using WebBluetooth) - Hub Model - Hubs @@ -265,7 +267,7 @@ DI Container Elements - [X] Technic Medium Hub - Accelerometer - [X] Technic Medium Hub - Gyro Sensor - [X] Technic Medium Hub - Tilt Sensor - - [ ] Technic Medium Hub - Gesture Sensor + - [X] Technic Medium Hub - Gesture Sensor (⚠ Usable but Gesture mapping is pending) - [X] Technic XLarge Motor - [X] Technic Large Motor - [ ] Technic Angular Motor (depend on availability of hardware / contributions) diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicMediumHubGestSensor.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicMediumHubGestSensor.cs new file mode 100644 index 0000000..9654aa4 --- /dev/null +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleTechnicMediumHubGestSensor.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading.Tasks; +using System.Reactive.Linq; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using SharpBrick.PoweredUp; + +namespace Example +{ + public class ExampleTechnicMediumHubGestSensor : BaseExample + { + public override async Task ExecuteAsync() + { + using (var technicMediumHub = Host.FindByType()) + { + var device = technicMediumHub.GestureSensor; + + await device.SetupNotificationAsync(0, true, deltaInterval: 0); + + using var _ = device.GestureObservable.Subscribe(x => Log.LogInformation($"Gesture {x}")); + + await Task.Delay(30_000); + + await technicMediumHub.SwitchOffAsync(); + } + } + } +} \ No newline at end of file diff --git a/examples/SharpBrick.PoweredUp.Examples/Program.cs b/examples/SharpBrick.PoweredUp.Examples/Program.cs index 5b7fd17..4bc6a21 100644 --- a/examples/SharpBrick.PoweredUp.Examples/Program.cs +++ b/examples/SharpBrick.PoweredUp.Examples/Program.cs @@ -32,7 +32,8 @@ static async Task Main(string[] args) //example = new Example.ExampleSetHubProperty(); //example = new Example.ExampleHubPropertyObserving(); //example = new Example.ExampleDiscoverByType(); - example = new Example.ExampleCalibrationSteering(); + //example = new Example.ExampleCalibrationSteering(); + example = new Example.ExampleTechnicMediumHubGestSensor(); // NOTE: Examples are programmed object oriented style. Base class implements methods Configure, DiscoverAsync and ExecuteAsync to be overwriten on demand. await example.InitHostAndDiscoverAsync(enableTrace); diff --git a/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs b/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs index fac8606..ccf4754 100644 --- a/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs +++ b/src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs @@ -35,7 +35,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType) DeviceType.RgbLight => typeof(RgbLight), DeviceType.TechnicLargeLinearMotor => typeof(TechnicLargeLinearMotor), DeviceType.TechnicXLargeLinearMotor => typeof(TechnicXLargeLinearMotor), - DeviceType.TechnicMediumHubGestSensor => typeof(TechnicMediumHubGestSensor), + DeviceType.TechnicMediumHubGestureSensor => typeof(TechnicMediumHubGestureSensor), DeviceType.TechnicMediumHubAccelerometer => typeof(TechnicMediumHubAccelerometer), DeviceType.TechnicMediumHubGyroSensor => typeof(TechnicMediumHubGyroSensor), DeviceType.TechnicMediumHubTiltSensor => typeof(TechnicMediumHubTiltSensor), @@ -51,7 +51,7 @@ public static DeviceType GetDeviceTypeFromType(Type type) nameof(RgbLight) => DeviceType.RgbLight, nameof(TechnicLargeLinearMotor) => DeviceType.TechnicLargeLinearMotor, nameof(TechnicXLargeLinearMotor) => DeviceType.TechnicXLargeLinearMotor, - nameof(TechnicMediumHubGestSensor) => DeviceType.TechnicMediumHubGestSensor, + nameof(TechnicMediumHubGestureSensor) => DeviceType.TechnicMediumHubGestureSensor, nameof(TechnicMediumHubAccelerometer) => DeviceType.TechnicMediumHubAccelerometer, nameof(TechnicMediumHubGyroSensor) => DeviceType.TechnicMediumHubGyroSensor, nameof(TechnicMediumHubTiltSensor) => DeviceType.TechnicMediumHubTiltSensor, diff --git a/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs b/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs index 9e036cf..fadb84b 100644 --- a/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs +++ b/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs @@ -3,19 +3,30 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Linq; using SharpBrick.PoweredUp.Protocol; using SharpBrick.PoweredUp.Utils; namespace SharpBrick.PoweredUp { - public class TechnicMediumHubGestSensor : Device, IPoweredUpDevice + public class TechnicMediumHubGestureSensor : Device, IPoweredUpDevice { - public TechnicMediumHubGestSensor() + protected SingleValueMode _gestureMode; + public byte ModeIndexGesture { get; protected set; } = 0; + + public Gesture Gesture => (Gesture)_gestureMode.SI; + public IObservable GestureObservable => _gestureMode.Observable.Select(x => (Gesture)x.SI); + + public TechnicMediumHubGestureSensor() { } - public TechnicMediumHubGestSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId) + public TechnicMediumHubGestureSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId) : base(protocol, hubId, portId) - { } + { + _gestureMode = SingleValueMode(ModeIndexGesture); + + ObserveForPropertyChanged(_gestureMode.Observable, nameof(Gesture)); + } public IEnumerable GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion) => @" diff --git a/src/SharpBrick.PoweredUp/Enums/DeviceType.cs b/src/SharpBrick.PoweredUp/Enums/DeviceType.cs index b126dd5..04c58c1 100644 --- a/src/SharpBrick.PoweredUp/Enums/DeviceType.cs +++ b/src/SharpBrick.PoweredUp/Enums/DeviceType.cs @@ -27,7 +27,7 @@ public enum DeviceType : ushort TechnicXLargeLinearMotor = 0x002F, // UNSPECED, TECHNIC_XLARGE_LINEAR_MOTOR TechnicMediumAngularMotor = 0x0030, // UNSPECED, TECHNIC_MEDIUM_ANGULAR_MOTOR (Spike Prime) TechnicLargeAngularMotor = 0x0031, // UNSPECED, TECHNIC_LARGE_ANGULAR_MOTOR (Spike Prime) - TechnicMediumHubGestSensor = 0x0036, // UNSPECED, TECHNIC_MEDIUM_HUB_GEST_SENSOR + TechnicMediumHubGestureSensor = 0x0036, // UNSPECED, TECHNIC_MEDIUM_HUB_GEST_SENSOR RemoteControlButton = 0x0037, // UNSPECED, REMOTE_CONTROL_BUTTON RemoteControlRssi = 0x0038, // UNSPECED, REMOTE_CONTROL_RSSI TechnicMediumHubAccelerometer = 0x0039, // UNSPECED, TECHNIC_MEDIUM_HUB_ACCELEROMETER diff --git a/src/SharpBrick.PoweredUp/Enums/Gesture.cs b/src/SharpBrick.PoweredUp/Enums/Gesture.cs new file mode 100644 index 0000000..f2c8424 --- /dev/null +++ b/src/SharpBrick.PoweredUp/Enums/Gesture.cs @@ -0,0 +1,34 @@ +namespace SharpBrick.PoweredUp +{ + public enum Gesture : sbyte + { + None = 0, // UNSPECED + /// + /// May be "Tap" gesture. + /// + /// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation. + /// + Gesture1 = 1, // UNSPECED + + /// + /// Some undocumented gesture (maybe bounce). + /// + /// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation. + /// + Gesture2 = 2, + + /// + /// May be "Shake" gesture. + /// + /// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation. + /// + Gesture3 = 3, // UNSPECED + + /// + /// May be "free fall" gesture. + /// + /// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation. + /// + Gesture4 = 4, // UNSPECED + } +} \ No newline at end of file diff --git a/src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs b/src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs index 471145f..f017823 100644 --- a/src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs +++ b/src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs @@ -21,7 +21,7 @@ public TechnicMediumHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFac new Port(97, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubAccelerometer), new Port(98, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGyroSensor), new Port(99, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubTiltSensor), - new Port(100, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGestSensor), + new Port(100, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGestureSensor), }) { } @@ -38,6 +38,6 @@ public TechnicMediumHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFac public TechnicMediumHubAccelerometer Accelerometer => Port(97).GetDevice(); public TechnicMediumHubGyroSensor GyroSensor => Port(98).GetDevice(); public TechnicMediumHubTiltSensor TiltSensor => Port(99).GetDevice(); - public TechnicMediumHubGestSensor GestureSensor => Port(100).GetDevice(); + public TechnicMediumHubGestureSensor GestureSensor => Port(100).GetDevice(); } } \ No newline at end of file diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs index 90bfa72..9b7feb5 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs @@ -18,7 +18,7 @@ public class HubAttachedIOEncoderTest [InlineData("0F-00-04-61-01-39-00-01-00-00-00-01-00-00-00", DeviceType.TechnicMediumHubAccelerometer, 97, "0.0.0.1", "0.0.0.1")] [InlineData("0F-00-04-62-01-3A-00-01-00-00-00-01-00-00-00", DeviceType.TechnicMediumHubGyroSensor, 98, "0.0.0.1", "0.0.0.1")] [InlineData("0F-00-04-63-01-3B-00-01-00-00-00-01-00-00-00", DeviceType.TechnicMediumHubTiltSensor, 99, "0.0.0.1", "0.0.0.1")] - [InlineData("0F-00-04-64-01-36-00-01-00-00-00-01-00-00-00", DeviceType.TechnicMediumHubGestSensor, 100, "0.0.0.1", "0.0.0.1")] + [InlineData("0F-00-04-64-01-36-00-01-00-00-00-01-00-00-00", DeviceType.TechnicMediumHubGestureSensor, 100, "0.0.0.1", "0.0.0.1")] public void HubAttachedIOEncoder_Decode_Attached(string messageAsString, DeviceType expectedType, byte expectedPortId, string expectedHwVersion, string expectedSwVersion) { // arrange