-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
examples/SharpBrick.PoweredUp.Examples/ExampleRemoteControlButton.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 ExampleRemoteControlButton : BaseExample | ||
{ | ||
public override async Task ExecuteAsync() | ||
{ | ||
using (var twoPortHandset = Host.FindByType<TwoPortHandset>()) | ||
{ | ||
var device = twoPortHandset.A; | ||
|
||
await device.SetupNotificationAsync(device.ModeIndexBitField, true, deltaInterval: 1); | ||
|
||
await twoPortHandset.RgbLight.SetRgbColorsAsync(0xFF, 0x00, 0x00); | ||
|
||
Log.LogInformation("Press the '+' button on port A (left)"); | ||
|
||
await device.PlusObservable.Any(x => x); | ||
|
||
Log.LogInformation("Thanks! Now press the 'red' button on port A (left)"); | ||
|
||
await device.RedObservable.Any(x => x); | ||
|
||
Log.LogInformation("Thanks! Now press the '-' button on port A (left)"); | ||
|
||
await device.MinusObservable.Any(x => x); | ||
|
||
Log.LogInformation("Thanks! Now press the '+' and '-' buttons on port A (left) at the same time"); | ||
|
||
await device.ButtonsObservable.Any(x => x.Minus && x.Plus); | ||
|
||
var disposable = device.ButtonsObservable.Subscribe(x => Log.LogWarning($"Buttons: {x.Plus} - {x.Stop} - {x.Minus}")); | ||
var disposable2 = device.PlusObservable.Subscribe(x => Log.LogWarning($"Plus: {x}")); | ||
var disposable3 = device.RedObservable.Subscribe(x => Log.LogWarning($"Red: {x}")); | ||
var disposable4 = device.MinusObservable.Subscribe(x => Log.LogWarning($"Minus: {x}")); | ||
|
||
Log.LogInformation("Thanks! You now have 20 seconds to press any button combinations"); | ||
|
||
await Task.Delay(20_000); | ||
|
||
disposable.Dispose(); | ||
disposable2.Dispose(); | ||
disposable3.Dispose(); | ||
disposable4.Dispose(); | ||
|
||
await twoPortHandset.SwitchOffAsync(); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/SharpBrick.PoweredUp.Examples/ExampleRemoteControlRssi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 ExampleRemoteControlRssi : BaseExample | ||
{ | ||
public override async Task ExecuteAsync() | ||
{ | ||
using (var twoPortHandset = Host.FindByType<TwoPortHandset>()) | ||
{ | ||
var device = twoPortHandset.RemoteControlRssi; | ||
|
||
await device.SetupNotificationAsync(device.ModeIndexRssi, true, deltaInterval: 1); | ||
|
||
var disposable = device.RssiObservable.Subscribe(x => Log.LogWarning($"RSSI: {x}")); | ||
|
||
await twoPortHandset.RgbLight.SetRgbColorsAsync(0x00, 0xFF, 0x00); | ||
|
||
Log.LogInformation("Watching RSSI changes for the next 20 seconds"); | ||
|
||
await Task.Delay(20_000); | ||
|
||
disposable.Dispose(); | ||
|
||
await twoPortHandset.SwitchOffAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using SharpBrick.PoweredUp.Protocol; | ||
using SharpBrick.PoweredUp.Utils; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
|
||
namespace SharpBrick.PoweredUp.Devices | ||
{ | ||
public class RemoteControlButton : Device, IPoweredUpDevice | ||
{ | ||
protected sbyte PlusBitMask = 0b0000_0001; | ||
protected sbyte RedBitMask = 0b0000_0010; | ||
protected sbyte MinusBitMask = 0b0000_0100; | ||
|
||
protected SingleValueMode<sbyte> _keyBitFieldMode; | ||
public byte ModeIndexBitField { get; protected set; } = 3; | ||
|
||
public bool Plus => IsBitMaskSet(_keyBitFieldMode.SI, PlusBitMask); | ||
public bool Red => IsBitMaskSet(_keyBitFieldMode.SI, RedBitMask); | ||
public bool Minus => IsBitMaskSet(_keyBitFieldMode.SI, MinusBitMask); | ||
|
||
public IObservable<(bool Plus, bool Stop, bool Minus)> ButtonsObservable => _keyBitFieldMode.Observable.Select(v => (IsBitMaskSet(_keyBitFieldMode.SI, PlusBitMask), IsBitMaskSet(_keyBitFieldMode.SI, RedBitMask), IsBitMaskSet(_keyBitFieldMode.SI, MinusBitMask))); | ||
public IObservable<bool> PlusObservable => _keyBitFieldMode.Observable.Select(v => IsBitMaskSet(_keyBitFieldMode.SI, PlusBitMask)).DistinctUntilChanged(); | ||
public IObservable<bool> RedObservable => _keyBitFieldMode.Observable.Select(v => IsBitMaskSet(_keyBitFieldMode.SI, RedBitMask)).DistinctUntilChanged(); | ||
public IObservable<bool> MinusObservable => _keyBitFieldMode.Observable.Select(v => IsBitMaskSet(_keyBitFieldMode.SI, MinusBitMask)).DistinctUntilChanged(); | ||
|
||
public RemoteControlButton() | ||
{ } | ||
|
||
public RemoteControlButton(ILegoWirelessProtocol protocol, byte hubId, byte portId) | ||
: base(protocol, hubId, portId) | ||
{ | ||
_keyBitFieldMode = SingleValueMode<sbyte>(ModeIndexBitField); | ||
|
||
ObserveForPropertyChanged(PlusObservable, nameof(Plus)); | ||
ObserveForPropertyChanged(RedObservable, nameof(Red)); | ||
ObserveForPropertyChanged(MinusObservable, nameof(Minus)); | ||
} | ||
|
||
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType) | ||
=> @" | ||
0B-00-43-00-01-02-05-1F-00-00-00 | ||
0C-00-44-00-00-00-52-43-4B-45-59-00 | ||
0E-00-44-00-00-01-00-00-80-BF-00-00-80-3F | ||
0E-00-44-00-00-02-00-00-C8-C2-00-00-C8-42 | ||
0E-00-44-00-00-03-00-00-80-BF-00-00-80-3F | ||
0A-00-44-00-00-04-62-74-6E-00 | ||
08-00-44-00-00-05-18-00 | ||
0A-00-44-00-00-80-01-00-02-00 | ||
0C-00-44-00-01-00-4B-45-59-41-20-00 | ||
0E-00-44-00-01-01-00-00-80-BF-00-00-80-3F | ||
0E-00-44-00-01-02-00-00-C8-C2-00-00-C8-42 | ||
0E-00-44-00-01-03-00-00-80-BF-00-00-80-3F | ||
0A-00-44-00-01-04-62-74-6E-00 | ||
08-00-44-00-01-05-70-00 | ||
0A-00-44-00-01-80-01-00-02-00 | ||
0C-00-44-00-02-00-4B-45-59-52-20-00 | ||
0E-00-44-00-02-01-00-00-80-BF-00-00-80-3F | ||
0E-00-44-00-02-02-00-00-C8-C2-00-00-C8-42 | ||
0E-00-44-00-02-03-00-00-80-BF-00-00-80-3F | ||
0A-00-44-00-02-04-62-74-6E-00 | ||
08-00-44-00-02-05-68-00 | ||
0A-00-44-00-02-80-01-00-02-00 | ||
0C-00-44-00-03-00-4B-45-59-44-20-00 | ||
0E-00-44-00-03-01-00-00-00-00-00-00-E0-40 | ||
0E-00-44-00-03-02-00-00-00-00-00-00-C8-42 | ||
0E-00-44-00-03-03-00-00-00-00-00-00-E0-40 | ||
0A-00-44-00-03-04-62-74-6E-00 | ||
08-00-44-00-03-05-04-00 | ||
0A-00-44-00-03-80-01-00-01-00 | ||
0C-00-44-00-04-00-4B-45-59-53-44-00 | ||
0E-00-44-00-04-01-00-00-00-00-00-00-80-3F | ||
0E-00-44-00-04-02-00-00-00-00-00-00-C8-42 | ||
0E-00-44-00-04-03-00-00-00-00-00-00-80-3F | ||
0A-00-44-00-04-04-62-74-6E-00 | ||
08-00-44-00-04-05-04-00 | ||
0A-00-44-00-04-80-03-00-01-00 | ||
".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s)); | ||
|
||
protected bool IsBitMaskSet(sbyte bitField, sbyte bitMask) | ||
=> (bitField & bitMask) != 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using SharpBrick.PoweredUp.Protocol; | ||
using SharpBrick.PoweredUp.Utils; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
|
||
namespace SharpBrick.PoweredUp.Devices | ||
{ | ||
public class RemoteControlRssi : Device, IPoweredUpDevice | ||
{ | ||
protected SingleValueMode<sbyte> _rssiMode; | ||
public byte ModeIndexRssi { get; protected set; } = 0; | ||
|
||
public sbyte Rssi => _rssiMode.SI; | ||
public IObservable<sbyte> RssiObservable => _rssiMode.Observable.Select(x => x.SI); | ||
|
||
public RemoteControlRssi() | ||
{ } | ||
|
||
public RemoteControlRssi(ILegoWirelessProtocol protocol, byte hubId, byte portId) | ||
: base(protocol, hubId, portId) | ||
{ | ||
_rssiMode = SingleValueMode<sbyte>(ModeIndexRssi); | ||
|
||
ObserveForPropertyChanged(_rssiMode.Observable, nameof(Rssi)); | ||
} | ||
|
||
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType) | ||
=> @" | ||
0B-00-43-3C-01-02-01-01-00-00-00 | ||
0C-00-44-3C-00-00-52-53-53-49-20-00 | ||
0E-00-44-3C-00-01-00-00-A0-C2-00-00-F0-C1 | ||
0E-00-44-3C-00-02-00-00-00-00-00-00-C8-42 | ||
0E-00-44-3C-00-03-00-00-A0-C2-00-00-F0-C1 | ||
0A-00-44-3C-00-04-64-62-6D-00 | ||
08-00-44-3C-00-05-50-00 | ||
0A-00-44-3C-00-80-01-00-03-00 | ||
".Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
using SharpBrick.PoweredUp.Devices; | ||
using SharpBrick.PoweredUp.Protocol; | ||
|
||
using System; | ||
|
||
namespace SharpBrick.PoweredUp | ||
{ | ||
public class TwoPortHandset : Hub | ||
{ | ||
public TwoPortHandset(ILegoWirelessProtocol protocol, IDeviceFactory deviceFactory, ILogger<TwoPortHandset> logger, IServiceProvider serviceProvider = default) | ||
: base(protocol, deviceFactory, logger, serviceProvider, SystemType.LegoSystem_TwoPortHandset, new Port[] { | ||
new Port(0, string.Empty, false, expectedDevice: DeviceType.RemoteControlButton), | ||
new Port(1, string.Empty, false, expectedDevice: DeviceType.RemoteControlButton), | ||
new Port(52, string.Empty, false, expectedDevice: DeviceType.RgbLight), | ||
new Port(59, string.Empty, false, expectedDevice: DeviceType.Voltage), | ||
new Port(60, string.Empty, false, expectedDevice: DeviceType.RemoteControlRssi), | ||
}, | ||
knownProperties: new HubProperty[] { | ||
HubProperty.AdvertisingName, | ||
HubProperty.Button, | ||
HubProperty.FwVersion, | ||
HubProperty.HwVersion, | ||
HubProperty.Rssi, | ||
HubProperty.BatteryVoltage, | ||
HubProperty.BatteryType, | ||
HubProperty.ManufacturerName, | ||
HubProperty.RadioFirmwareVersion, | ||
HubProperty.LegoWirelessProtocolVersion, | ||
HubProperty.SystemTypeId, | ||
HubProperty.HardwareNetworkId, | ||
HubProperty.PrimaryMacAddress, | ||
//HubProperty.SecondaryMacAddress, // unsupported on the two port handset | ||
HubProperty.HardwareNetworkFamily, | ||
}) | ||
{ } | ||
|
||
public RemoteControlButton A => Port(0).GetDevice<RemoteControlButton>(); | ||
public RemoteControlButton B => Port(1).GetDevice<RemoteControlButton>(); | ||
|
||
public RgbLight RgbLight => Port(52).GetDevice<RgbLight>(); | ||
public Voltage Voltage => Port(59).GetDevice<Voltage>(); | ||
public RemoteControlRssi RemoteControlRssi => Port(60).GetDevice<RemoteControlRssi>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters