-
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.
- Add example (requires cleanup in samples - Add extension mode for IServiceCollection to AddPoweredUp services (like IDeviceFactory) - Made DeviceFactory a singleton instead of static (to allow testing of DynamicDevice on a known device) - Fix console output in DiscoverPorts function - Add WriteDirectModeData to Mode - Add GenericWriteDirectModeDataMessage for byte[] provided by user - Breaking Change due to ServiceCollection need to contain IDeviceFactory #37 breaking
- Loading branch information
Showing
21 changed files
with
293 additions
and
28 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
examples/SharpBrick.PoweredUp.Examples/ExampleDynamicDevice.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,125 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Reactive.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SharpBrick.PoweredUp; | ||
using SharpBrick.PoweredUp.Deployment; | ||
using SharpBrick.PoweredUp.WinRT; | ||
using System.Threading; | ||
using SharpBrick.PoweredUp.Functions; | ||
using SharpBrick.PoweredUp.Devices; | ||
using SharpBrick.PoweredUp.Protocol; | ||
|
||
namespace Example | ||
{ | ||
public class EmptyDeviceFactory : IDeviceFactory | ||
{ | ||
public IPoweredUpDevice Create(DeviceType deviceType) | ||
=> null; | ||
|
||
public IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtocol protocol, byte hubId, byte portId) | ||
=> null; | ||
} | ||
public class ExampleDynamicDevice | ||
{ | ||
public static (PoweredUpHost host, IServiceProvider serviceProvider, Hub selectedHub) CreateHostAndDiscover(bool enableTrace) | ||
{ | ||
var serviceProvider = new ServiceCollection() | ||
// configure your favourite level of logging. | ||
.AddLogging(builder => | ||
{ | ||
builder | ||
.AddConsole(); | ||
if (enableTrace) | ||
{ | ||
builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug); | ||
} | ||
}) | ||
.AddSingleton<IDeviceFactory, EmptyDeviceFactory>() | ||
//.AddPoweredUp() | ||
.BuildServiceProvider(); | ||
|
||
|
||
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger("Main"); | ||
|
||
var poweredUpBluetoothAdapter = new WinRTPoweredUpBluetoothAdapter(); | ||
|
||
var host = new PoweredUpHost(poweredUpBluetoothAdapter, serviceProvider); | ||
|
||
Hub result = null; | ||
|
||
logger.LogInformation("Finding Service"); | ||
var cts = new CancellationTokenSource(); | ||
host.Discover(async hub => | ||
{ | ||
// add this when you are interested in a tracing of the message ("human readable") | ||
if (enableTrace) | ||
{ | ||
var tracer = new TraceMessages(hub.Protocol, serviceProvider.GetService<ILoggerFactory>().CreateLogger<TraceMessages>()); | ||
await tracer.ExecuteAsync(); | ||
} | ||
logger.LogInformation("Connecting to Hub"); | ||
await hub.ConnectAsync(); | ||
result = hub; | ||
logger.LogInformation(hub.AdvertisingName); | ||
logger.LogInformation(hub.SystemType.ToString()); | ||
cts.Cancel(); | ||
logger.LogInformation("Press RETURN to continue to the action"); | ||
}, cts.Token); | ||
|
||
logger.LogInformation("Press RETURN to cancel Scanning"); | ||
Console.ReadLine(); | ||
|
||
cts.Cancel(); | ||
return (host, serviceProvider, result); | ||
} | ||
|
||
public static async Task ExecuteAsync(bool enableTrace) | ||
{ | ||
var (host, serviceProvider, selectedHub) = ExampleDynamicDevice.CreateHostAndDiscover(enableTrace); | ||
|
||
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<ExampleMotorInputAbsolutePosition>(); | ||
|
||
using (var technicMediumHub = host.FindByType<TechnicMediumHub>()) | ||
{ | ||
var model = new DeploymentModelBuilder() | ||
.AddAnyHub(hubBuilder => hubBuilder | ||
.AddAnyDevice(0)) | ||
.Build(); | ||
|
||
model.Verify(technicMediumHub.Protocol); | ||
|
||
var dynamicDeviceWhichIsAMotor = new DynamicDevice(technicMediumHub.Protocol, technicMediumHub.HubId, 0); | ||
await dynamicDeviceWhichIsAMotor.DiscoverAsync(); | ||
|
||
logger.LogInformation("Discovery completed"); | ||
|
||
await dynamicDeviceWhichIsAMotor.TryLockDeviceForCombinedModeNotificationSetupAsync(2, 3); | ||
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(2, true); | ||
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(3, true); | ||
await dynamicDeviceWhichIsAMotor.UnlockFromCombinedModeNotificationSetupAsync(true); | ||
|
||
using var disposable = dynamicDeviceWhichIsAMotor.SingleValueMode<int>(2).Observable.Subscribe(x => logger.LogWarning($"Position: {x.SI} / {x.Pct}")); | ||
using var disposable2 = dynamicDeviceWhichIsAMotor.SingleValueMode<short>(3).Observable.Subscribe(x => logger.LogWarning($"Absolute Position: {x.SI} / {x.Pct}")); | ||
|
||
await dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0).WriteDirectModeDataAsync(0x64); // That is StartPower on a motor | ||
|
||
await Task.Delay(2_000); | ||
|
||
await dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte>(0).WriteDirectModeDataAsync(0x00); // That is Stop on a motor | ||
|
||
await Task.Delay(10_000); | ||
|
||
await technicMediumHub.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
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,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using SharpBrick.PoweredUp.Functions; | ||
using SharpBrick.PoweredUp.Protocol; | ||
using SharpBrick.PoweredUp.Protocol.Messages; | ||
|
||
namespace SharpBrick.PoweredUp | ||
{ | ||
public class DynamicDevice : Device, IPoweredUpDevice | ||
{ | ||
public DynamicDevice() | ||
{ } | ||
|
||
public DynamicDevice(IPoweredUpProtocol protocol, byte hubId, byte portId) | ||
: base(protocol, hubId, portId) | ||
{ } | ||
|
||
public async Task DiscoverAsync() | ||
{ | ||
var discoverPortsFunction = new DiscoverPorts(_protocol); | ||
|
||
await discoverPortsFunction.ExecuteAsync(_portId); | ||
|
||
BuildModes(); | ||
} | ||
|
||
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion) | ||
=> Array.Empty<byte[]>(); | ||
} | ||
} |
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,10 @@ | ||
using SharpBrick.PoweredUp.Protocol; | ||
|
||
namespace SharpBrick.PoweredUp.Devices | ||
{ | ||
public interface IDeviceFactory | ||
{ | ||
IPoweredUpDevice Create(DeviceType deviceType); | ||
IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtocol protocol, byte hubId, byte portId); | ||
} | ||
} |
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,12 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SharpBrick.PoweredUp.Devices; | ||
|
||
namespace SharpBrick.PoweredUp | ||
{ | ||
public static class IServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddPoweredUp(this IServiceCollection self) | ||
=> self | ||
.AddSingleton<IDeviceFactory, DeviceFactory>(); | ||
} | ||
} |
Oops, something went wrong.