Skip to content

Commit

Permalink
Add gesture sensor w/ incomplete gesture list
Browse files Browse the repository at this point in the history
#18 non-breaking (new api)
  • Loading branch information
tthiery committed Aug 20, 2020
1 parent 2129aeb commit d09ea20
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
13 changes: 12 additions & 1 deletion 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
{
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 TechnicMediumHubGestSensor()
{ }

public TechnicMediumHubGestSensor(IPoweredUpProtocol 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
12 changes: 12 additions & 0 deletions src/SharpBrick.PoweredUp/Enums/Gesture.cs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit d09ea20

Please sign in to comment.