forked from sharpbrick/powered-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleDynamicDevice.cs
74 lines (60 loc) · 3.01 KB
/
ExampleDynamicDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;
using SharpBrick.PoweredUp.Devices;
using SharpBrick.PoweredUp.Hubs;
using SharpBrick.PoweredUp.Protocol;
namespace Example;
public class EmptyDeviceFactory : IDeviceFactory
{
public IPoweredUpDevice Create(DeviceType deviceType)
=> null;
public IPoweredUpDevice CreateConnected(DeviceType deviceType, ILegoWirelessProtocol protocol, byte hubId, byte portId)
=> new DynamicDevice(protocol, hubId, portId);
}
public class ExampleDynamicDevice : BaseExample
{
public override void Configure(IServiceCollection services)
{
// pretend we do not know the device and use only dynamic ones
services.AddSingleton<IHubFactory, HubFactory>();
services.AddSingleton<IDeviceFactory, EmptyDeviceFactory>();
services.AddSingleton<PoweredUpHost>();
}
public override async Task ExecuteAsync()
{
using var technicMediumHub = Host.FindByType<TechnicMediumHub>();
// deployment model verification with unknown devices
await technicMediumHub.VerifyDeploymentModelAsync(mb => mb
.AddAnyHub(hubBuilder => hubBuilder
.AddAnyDevice(0))
);
var dynamicDeviceWhichIsAMotor = technicMediumHub.Port(0).GetDevice<DynamicDevice>();
// or also direct from a protocol
//var dynamicDeviceWhichIsAMotor = new DynamicDevice(technicMediumHub.Protocol, technicMediumHub.HubId, 0);
// discover the unknown device using the LWP
await dynamicDeviceWhichIsAMotor.DiscoverAsync();
Log.LogInformation("Discovery completed");
// use combined mode values from the device
await dynamicDeviceWhichIsAMotor.TryLockDeviceForCombinedModeNotificationSetupAsync(2, 3);
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(2, true);
await dynamicDeviceWhichIsAMotor.SetupNotificationAsync(3, true);
await dynamicDeviceWhichIsAMotor.UnlockFromCombinedModeNotificationSetupAsync(true);
// get the individual modes for input and output
var powerMode = dynamicDeviceWhichIsAMotor.SingleValueMode<sbyte, sbyte>(0);
var posMode = dynamicDeviceWhichIsAMotor.SingleValueMode<int, int>(2);
var aposMode = dynamicDeviceWhichIsAMotor.SingleValueMode<short, short>(3);
// use their observables to report values
using var disposable = posMode.Observable.Subscribe(x => Log.LogWarning($"Position: {x.SI} / {x.Pct}"));
using var disposable2 = aposMode.Observable.Subscribe(x => Log.LogWarning($"Absolute Position: {x.SI} / {x.Pct}"));
// or even write to them
await powerMode.WriteDirectModeDataAsync(0x64); // That is StartPower on a motor
await Task.Delay(2_000);
await powerMode.WriteDirectModeDataAsync(0x00); // That is Stop on a motor
await Task.Delay(10_000);
await technicMediumHub.SwitchOffAsync();
}
}