Skip to content

Commit

Permalink
Add Mario Accelerometer draft
Browse files Browse the repository at this point in the history
  • Loading branch information
tthiery committed Nov 15, 2020
1 parent b8ac57a commit 289b1a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;

namespace Example
{
public class ExampleMarioAccelerometer : BaseExample
{
public override async Task ExecuteAsync()
{
using (var mario = Host.FindByType<MarioHub>())
{
await mario.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<MarioHub>(hubBuilder => { })
);

var d1 = mario.Accelerometer.GestureObservable.Subscribe(x => Log.LogWarning($"Gesture: {x[0]}/{x[1]}"));

await mario.Accelerometer.SetupNotificationAsync(mario.Accelerometer.ModeIndexGesture, true);

await Task.Delay(20_000);

d1.Dispose();

await mario.SwitchOffAsync();
}
}
}
}
9 changes: 4 additions & 5 deletions examples/SharpBrick.PoweredUp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ static async Task Main(string[] args)
//example = new Example.ExampleTechnicMediumHubGestSensor();
//example = new Example.ExampleRemoteControlButton();
//example = new Example.ExampleRemoteControlRssi();

example = new Example.ExampleTechnicMediumAngularMotorGrey();

// example = new Example.ExampleMarioBarcode();
// example = new Example.ExampleMarioPants();
//example = new Example.ExampleTechnicMediumAngularMotorGrey();
//example = new Example.ExampleMarioBarcode();
//example = new Example.ExampleMarioPants();
example = new Example.ExampleMarioAccelerometer();

// 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: 13 additions & 0 deletions src/SharpBrick.PoweredUp/Devices/MarioHubAccelerometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ namespace SharpBrick.PoweredUp
{
public class MarioHubAccelerometer : Device, IPoweredUpDevice
{
protected MultiValueMode<sbyte> _rawMode;
protected MultiValueMode<short> _gestMode;

public byte ModeIndexRaw { get; protected set; } = 0;
public byte ModeIndexGesture { get; protected set; } = 1;

public IObservable<sbyte[]> RawObservable => _rawMode.Observable.Select(x => x.SI);
public IObservable<short[]> GestureObservable => _gestMode.Observable.Select(x => x.SI);

public MarioHubAccelerometer()
{ }

public MarioHubAccelerometer(ILegoWirelessProtocol protocol, byte hubId, byte portId)
: base(protocol, hubId, portId)
{
_rawMode = MultiValueMode<sbyte>(ModeIndexRaw);
_gestMode = MultiValueMode<short>(ModeIndexGesture);

//ObserveForPropertyChanged(_rawMode.Observable, nameof(Coins));
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
Expand Down

0 comments on commit 289b1a5

Please sign in to comment.