Skip to content

Commit ba95de8

Browse files
authored
Add gesture sensor w/ incomplete gesture list (#84)
* Add gesture sensor w/ incomplete gesture list #18 non-breaking (new api)
1 parent 2ea4db1 commit ba95de8

File tree

9 files changed

+89
-13
lines changed

9 files changed

+89
-13
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ Console.WriteLine($"Or directly read the latest value: {aposMode.SI} / {aposMode
158158

159159
## Connect to Hub and Send a Message and retrieving answers (directly on protocol layer)
160160

161+
***Note**: The `ILegoWirelessProtocol` class was renamed in 3.0. Previously it is known as `IPoweredUpProtocol`.*
162+
161163
````csharp
162164

163165
var serviceProvider = new ServiceCollection()
@@ -245,7 +247,7 @@ DI Container Elements
245247
- [X] .NET Core 3.1 (on Windows 10 using WinRT)
246248
- 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)
247249
- Library uses WinRT for communication therefore only Windows 10
248-
- [ ] Xamarin (on iOS / Android using Xamarin.Essentials)
250+
- [ ] Xamarin (on iOS / Android using ?)
249251
- [ ] Blazor (on Browser using WebBluetooth)
250252
- Hub Model
251253
- Hubs
@@ -265,7 +267,7 @@ DI Container Elements
265267
- [X] Technic Medium Hub - Accelerometer
266268
- [X] Technic Medium Hub - Gyro Sensor
267269
- [X] Technic Medium Hub - Tilt Sensor
268-
- [ ] Technic Medium Hub - Gesture Sensor
270+
- [X] Technic Medium Hub - Gesture Sensor (⚠ Usable but Gesture mapping is pending)
269271
- [X] Technic XLarge Motor
270272
- [X] Technic Large Motor
271273
- [ ] Technic Angular Motor (depend on availability of hardware / contributions)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.Reactive.Linq;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using SharpBrick.PoweredUp;
7+
8+
namespace Example
9+
{
10+
public class ExampleTechnicMediumHubGestSensor : BaseExample
11+
{
12+
public override async Task ExecuteAsync()
13+
{
14+
using (var technicMediumHub = Host.FindByType<TechnicMediumHub>())
15+
{
16+
var device = technicMediumHub.GestureSensor;
17+
18+
await device.SetupNotificationAsync(0, true, deltaInterval: 0);
19+
20+
using var _ = device.GestureObservable.Subscribe(x => Log.LogInformation($"Gesture {x}"));
21+
22+
await Task.Delay(30_000);
23+
24+
await technicMediumHub.SwitchOffAsync();
25+
}
26+
}
27+
}
28+
}

examples/SharpBrick.PoweredUp.Examples/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static async Task Main(string[] args)
3232
//example = new Example.ExampleSetHubProperty();
3333
//example = new Example.ExampleHubPropertyObserving();
3434
//example = new Example.ExampleDiscoverByType();
35-
example = new Example.ExampleCalibrationSteering();
35+
//example = new Example.ExampleCalibrationSteering();
36+
example = new Example.ExampleTechnicMediumHubGestSensor();
3637

3738
// NOTE: Examples are programmed object oriented style. Base class implements methods Configure, DiscoverAsync and ExecuteAsync to be overwriten on demand.
3839
await example.InitHostAndDiscoverAsync(enableTrace);

src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType)
3535
DeviceType.RgbLight => typeof(RgbLight),
3636
DeviceType.TechnicLargeLinearMotor => typeof(TechnicLargeLinearMotor),
3737
DeviceType.TechnicXLargeLinearMotor => typeof(TechnicXLargeLinearMotor),
38-
DeviceType.TechnicMediumHubGestSensor => typeof(TechnicMediumHubGestSensor),
38+
DeviceType.TechnicMediumHubGestureSensor => typeof(TechnicMediumHubGestureSensor),
3939
DeviceType.TechnicMediumHubAccelerometer => typeof(TechnicMediumHubAccelerometer),
4040
DeviceType.TechnicMediumHubGyroSensor => typeof(TechnicMediumHubGyroSensor),
4141
DeviceType.TechnicMediumHubTiltSensor => typeof(TechnicMediumHubTiltSensor),
@@ -51,7 +51,7 @@ public static DeviceType GetDeviceTypeFromType(Type type)
5151
nameof(RgbLight) => DeviceType.RgbLight,
5252
nameof(TechnicLargeLinearMotor) => DeviceType.TechnicLargeLinearMotor,
5353
nameof(TechnicXLargeLinearMotor) => DeviceType.TechnicXLargeLinearMotor,
54-
nameof(TechnicMediumHubGestSensor) => DeviceType.TechnicMediumHubGestSensor,
54+
nameof(TechnicMediumHubGestureSensor) => DeviceType.TechnicMediumHubGestureSensor,
5555
nameof(TechnicMediumHubAccelerometer) => DeviceType.TechnicMediumHubAccelerometer,
5656
nameof(TechnicMediumHubGyroSensor) => DeviceType.TechnicMediumHubGyroSensor,
5757
nameof(TechnicMediumHubTiltSensor) => DeviceType.TechnicMediumHubTiltSensor,

src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Reactive.Linq;
67
using SharpBrick.PoweredUp.Protocol;
78
using SharpBrick.PoweredUp.Utils;
89

910
namespace SharpBrick.PoweredUp
1011
{
11-
public class TechnicMediumHubGestSensor : Device, IPoweredUpDevice
12+
public class TechnicMediumHubGestureSensor : Device, IPoweredUpDevice
1213
{
13-
public TechnicMediumHubGestSensor()
14+
protected SingleValueMode<sbyte> _gestureMode;
15+
public byte ModeIndexGesture { get; protected set; } = 0;
16+
17+
public Gesture Gesture => (Gesture)_gestureMode.SI;
18+
public IObservable<Gesture> GestureObservable => _gestureMode.Observable.Select(x => (Gesture)x.SI);
19+
20+
public TechnicMediumHubGestureSensor()
1421
{ }
1522

16-
public TechnicMediumHubGestSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
23+
public TechnicMediumHubGestureSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
1724
: base(protocol, hubId, portId)
18-
{ }
25+
{
26+
_gestureMode = SingleValueMode<sbyte>(ModeIndexGesture);
27+
28+
ObserveForPropertyChanged(_gestureMode.Observable, nameof(Gesture));
29+
}
1930

2031
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion)
2132
=> @"

src/SharpBrick.PoweredUp/Enums/DeviceType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum DeviceType : ushort
2727
TechnicXLargeLinearMotor = 0x002F, // UNSPECED, TECHNIC_XLARGE_LINEAR_MOTOR
2828
TechnicMediumAngularMotor = 0x0030, // UNSPECED, TECHNIC_MEDIUM_ANGULAR_MOTOR (Spike Prime)
2929
TechnicLargeAngularMotor = 0x0031, // UNSPECED, TECHNIC_LARGE_ANGULAR_MOTOR (Spike Prime)
30-
TechnicMediumHubGestSensor = 0x0036, // UNSPECED, TECHNIC_MEDIUM_HUB_GEST_SENSOR
30+
TechnicMediumHubGestureSensor = 0x0036, // UNSPECED, TECHNIC_MEDIUM_HUB_GEST_SENSOR
3131
RemoteControlButton = 0x0037, // UNSPECED, REMOTE_CONTROL_BUTTON
3232
RemoteControlRssi = 0x0038, // UNSPECED, REMOTE_CONTROL_RSSI
3333
TechnicMediumHubAccelerometer = 0x0039, // UNSPECED, TECHNIC_MEDIUM_HUB_ACCELEROMETER
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace SharpBrick.PoweredUp
2+
{
3+
public enum Gesture : sbyte
4+
{
5+
None = 0, // UNSPECED
6+
/// <summary>
7+
/// May be "Tap" gesture.
8+
///
9+
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
10+
/// </summary>
11+
Gesture1 = 1, // UNSPECED
12+
13+
/// <summary>
14+
/// Some undocumented gesture (maybe bounce).
15+
///
16+
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
17+
/// </summary>
18+
Gesture2 = 2,
19+
20+
/// <summary>
21+
/// May be "Shake" gesture.
22+
///
23+
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
24+
/// </summary>
25+
Gesture3 = 3, // UNSPECED
26+
27+
/// <summary>
28+
/// May be "free fall" gesture.
29+
///
30+
/// Mapping is not well understood. Please contribute an issue if you have found an issue, additional insight or documentation.
31+
/// </summary>
32+
Gesture4 = 4, // UNSPECED
33+
}
34+
}

src/SharpBrick.PoweredUp/Hubs/TechnicMediumHub.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public TechnicMediumHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFac
2121
new Port(97, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubAccelerometer),
2222
new Port(98, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGyroSensor),
2323
new Port(99, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubTiltSensor),
24-
new Port(100, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGestSensor),
24+
new Port(100, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGestureSensor),
2525
})
2626
{ }
2727

@@ -38,6 +38,6 @@ public TechnicMediumHub(ILegoWirelessProtocol protocol, IDeviceFactory deviceFac
3838
public TechnicMediumHubAccelerometer Accelerometer => Port(97).GetDevice<TechnicMediumHubAccelerometer>();
3939
public TechnicMediumHubGyroSensor GyroSensor => Port(98).GetDevice<TechnicMediumHubGyroSensor>();
4040
public TechnicMediumHubTiltSensor TiltSensor => Port(99).GetDevice<TechnicMediumHubTiltSensor>();
41-
public TechnicMediumHubGestSensor GestureSensor => Port(100).GetDevice<TechnicMediumHubGestSensor>();
41+
public TechnicMediumHubGestureSensor GestureSensor => Port(100).GetDevice<TechnicMediumHubGestureSensor>();
4242
}
4343
}

test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class HubAttachedIOEncoderTest
1818
[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")]
1919
[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")]
2020
[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")]
21-
[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")]
21+
[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")]
2222
public void HubAttachedIOEncoder_Decode_Attached<T>(string messageAsString, DeviceType expectedType, byte expectedPortId, string expectedHwVersion, string expectedSwVersion)
2323
{
2424
// arrange

0 commit comments

Comments
 (0)