Skip to content

Commit

Permalink
Add gesture sensor w/ incomplete gesture list (#84)
Browse files Browse the repository at this point in the history
* Add gesture sensor w/ incomplete gesture list

#18 non-breaking (new api)
  • Loading branch information
tthiery authored Sep 26, 2020
1 parent 2ea4db1 commit ba95de8
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -245,7 +247,7 @@ DI Container Elements
- [X] .NET Core 3.1 (on Windows 10 using WinRT)
- Library uses `Span<T>` / 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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TechnicMediumHub>())
{
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();
}
}
}
}
3 changes: 2 additions & 1 deletion examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<sbyte> _gestureMode;
public byte ModeIndexGesture { get; protected set; } = 0;

public Gesture Gesture => (Gesture)_gestureMode.SI;
public IObservable<Gesture> 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<sbyte>(ModeIndexGesture);

ObserveForPropertyChanged(_gestureMode.Observable, nameof(Gesture));
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
=> @"
Expand Down
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp/Enums/DeviceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/SharpBrick.PoweredUp/Enums/Gesture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace SharpBrick.PoweredUp
{
public enum Gesture : sbyte
{
None = 0, // UNSPECED
/// <summary>
/// May be "Tap" gesture.
///
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
/// </summary>
Gesture1 = 1, // UNSPECED

/// <summary>
/// Some undocumented gesture (maybe bounce).
///
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
/// </summary>
Gesture2 = 2,

/// <summary>
/// May be "Shake" gesture.
///
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
/// </summary>
Gesture3 = 3, // UNSPECED

/// <summary>
/// May be "free fall" gesture.
///
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
/// </summary>
Gesture4 = 4, // UNSPECED
}
}
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
{ }

Expand All @@ -38,6 +38,6 @@ public TechnicMediumHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFac
public TechnicMediumHubAccelerometer Accelerometer => Port(97).GetDevice<TechnicMediumHubAccelerometer>();
public TechnicMediumHubGyroSensor GyroSensor => Port(98).GetDevice<TechnicMediumHubGyroSensor>();
public TechnicMediumHubTiltSensor TiltSensor => Port(99).GetDevice<TechnicMediumHubTiltSensor>();
public TechnicMediumHubGestSensor GestureSensor => Port(100).GetDevice<TechnicMediumHubGestSensor>();
public TechnicMediumHubGestureSensor GestureSensor => Port(100).GetDevice<TechnicMediumHubGestureSensor>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string messageAsString, DeviceType expectedType, byte expectedPortId, string expectedHwVersion, string expectedSwVersion)
{
// arrange
Expand Down

0 comments on commit ba95de8

Please sign in to comment.