From d09ea20422561e2942e2eb0f7e1ffeceb8858e0e Mon Sep 17 00:00:00 2001 From: "T. Thiery" Date: Thu, 20 Aug 2020 23:37:53 +0200 Subject: [PATCH] Add gesture sensor w/ incomplete gesture list #18 non-breaking (new api) --- README.md | 2 +- .../ExampleTechnicMediumHubGestSensor.cs | 28 +++++++++++++++++++ .../SharpBrick.PoweredUp.Examples/Program.cs | 3 +- .../Devices/TechnicMediumHubGestSensor.cs | 13 ++++++++- src/SharpBrick.PoweredUp/Enums/Gesture.cs | 12 ++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 examples/SharpBrick.PoweredUp.Examples/ExampleTechnicMediumHubGestSensor.cs create mode 100644 src/SharpBrick.PoweredUp/Enums/Gesture.cs diff --git a/README.md b/README.md index d8b93dc..bc3d4c0 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,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 - [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/TechnicMediumHubGestSensor.cs b/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs index b3f9fca..15df270 100644 --- a/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs +++ b/src/SharpBrick.PoweredUp/Devices/TechnicMediumHubGestSensor.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Linq; using SharpBrick.PoweredUp.Protocol; using SharpBrick.PoweredUp.Utils; @@ -10,12 +11,22 @@ namespace SharpBrick.PoweredUp { public class TechnicMediumHubGestSensor : Device, IPoweredUpDevice { + 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 TechnicMediumHubGestSensor() { } public TechnicMediumHubGestSensor(IPoweredUpProtocol 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/Gesture.cs b/src/SharpBrick.PoweredUp/Enums/Gesture.cs new file mode 100644 index 0000000..3171aeb --- /dev/null +++ b/src/SharpBrick.PoweredUp/Enums/Gesture.cs @@ -0,0 +1,12 @@ +namespace SharpBrick.PoweredUp +{ + // some are free fall, tap, shake, ?? + public enum Gesture : int + { + None = 0, // UNSPECED + Tap = 1, // UNSPECED + Gesture_2 = 2, + Shake = 3, // UNSPECED + FreeFall = 4, // UNSPECED + } +} \ No newline at end of file