Skip to content

Commit 6b5933b

Browse files
committed
Add VirtualPort Creation and Deletion
- Extracted Samples into own project - Fixed SpeedProfiles and SpecialSpeed enum namespaces Closes sharpbrick#12
1 parent ace053b commit 6b5933b

File tree

17 files changed

+293
-88
lines changed

17 files changed

+293
-88
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ using (var kernel = new BluetoothKernel(poweredUpBluetoothAdapter, bluetoothAddr
144144
- [X] Properties
145145
- [ ] Alerts
146146
- [ ] Actions
147+
- [X] Create Virtual Ports
147148
- [X] Technic Medium Hub
148149
- .. other hubs depend on availability of hardware / contributions
149150
- Devices

examples/SharpBrick.Cli/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static async Task Main(string[] args)
2222
var app = new CommandLineApplication();
2323

2424
app.HelpOption();
25+
app.UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.CollectAndContinue;
2526

2627
app.Command("device", deviceApp =>
2728
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading.Tasks;
2+
using SharpBrick.PoweredUp;
3+
4+
namespace Example
5+
{
6+
public static class ExampleColors
7+
{
8+
public static async Task ExecuteAsync()
9+
{
10+
var (host, serviceProvider, _) = ExampleHubDiscover.CreateHostAndDiscover();
11+
12+
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
13+
{
14+
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
15+
16+
await Task.Delay(2000);
17+
18+
await technicMediumHub.SwitchOffAsync();
19+
}
20+
}
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Threading;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Logging;
5+
using SharpBrick.PoweredUp;
6+
using SharpBrick.PoweredUp.WinRT;
7+
using SharpBrick.PoweredUp.Functions;
8+
9+
namespace Example
10+
{
11+
public static class ExampleHubDiscover
12+
{
13+
public static (PoweredUpHost host, IServiceProvider serviceProvider, Hub selectedHub) CreateHostAndDiscover()
14+
{
15+
var serviceProvider = new ServiceCollection()
16+
.AddLogging(builder => builder
17+
.AddConsole())
18+
.BuildServiceProvider();
19+
20+
21+
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger("Main");
22+
23+
var poweredUpBluetoothAdapter = new WinRTPoweredUpBluetoothAdapter();
24+
25+
var host = new PoweredUpHost(poweredUpBluetoothAdapter, serviceProvider);
26+
27+
logger.LogInformation("Finding Service");
28+
var cts = new CancellationTokenSource();
29+
host.Discover(async hub =>
30+
{
31+
logger.LogInformation("Connecting to Hub");
32+
33+
await hub.ConnectAsync();
34+
35+
logger.LogInformation(hub.AdvertisingName);
36+
logger.LogInformation(hub.SystemType.ToString());
37+
38+
cts.Cancel();
39+
40+
logger.LogInformation("Press RETURN to continue to the action");
41+
}, cts.Token);
42+
43+
logger.LogInformation("Press RETURN to cancel Scanning");
44+
Console.ReadLine();
45+
46+
cts.Cancel();
47+
return (host, serviceProvider, null);
48+
}
49+
}
50+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Threading.Tasks;
2+
using SharpBrick.PoweredUp;
3+
4+
namespace Example
5+
{
6+
public static class ExampleMotorControl
7+
{
8+
public static async Task ExecuteAsync()
9+
{
10+
var (host, serviceProvider, _) = ExampleHubDiscover.CreateHostAndDiscover();
11+
12+
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
13+
{
14+
var motor = technicMediumHub.A.GetDevice<TechnicXLargeLinearMotor>();
15+
16+
await motor.SetAccelerationTimeAsync(3000);
17+
await motor.SetDeccelerationTimeAsync(1000);
18+
await motor.StartSpeedForTimeAsync(6000, 90, 100, SpecialSpeed.Hold, SpeedProfiles.AccelerationProfile | SpeedProfiles.DeccelerationProfile);
19+
20+
await Task.Delay(10_000);
21+
22+
await motor.StartSpeedForDegreesAsync(180, -10, 100, SpecialSpeed.Brake, SpeedProfiles.None);
23+
24+
await Task.Delay(10_000);
25+
26+
await motor.StartSpeedAsync(100, 90, SpeedProfiles.None);
27+
await Task.Delay(2000);
28+
await motor.StartSpeedAsync(127, 90, SpeedProfiles.None);
29+
await motor.StartSpeedAsync(-100, 90, SpeedProfiles.None);
30+
await Task.Delay(2000);
31+
await motor.StartSpeedAsync(0, 90, SpeedProfiles.None);
32+
33+
await technicMediumHub.SwitchOffAsync();
34+
}
35+
}
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using SharpBrick.PoweredUp;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Example
7+
{
8+
public class ExampleMotorInputAbsolutePosition
9+
{
10+
public static async Task ExecuteAsync()
11+
{
12+
var (host, serviceProvider, _) = ExampleHubDiscover.CreateHostAndDiscover();
13+
14+
var logger = serviceProvider.GetService<ILoggerFactory>().CreateLogger<ExampleMotorInputAbsolutePosition>();
15+
16+
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
17+
{
18+
var motor = technicMediumHub.A.GetDevice<TechnicXLargeLinearMotor>();
19+
20+
await motor.SetupNotificationAsync(motor.ModeIndexAbsolutePosition, true);
21+
22+
await motor.StartPowerAsync(80);
23+
24+
logger.LogWarning($"Position: {motor.AbsolutePosition}");
25+
26+
await Task.Delay(2000);
27+
28+
logger.LogWarning($"Position: {motor.AbsolutePosition}");
29+
30+
await Task.Delay(2000);
31+
32+
await motor.StartPowerAsync(0);
33+
34+
await technicMediumHub.SwitchOffAsync();
35+
}
36+
}
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using SharpBrick.PoweredUp;
4+
5+
namespace Example
6+
{
7+
public static class ExampleMotorVirtualPort
8+
{
9+
public static async Task ExecuteAsync()
10+
{
11+
var (host, serviceProvider, _) = ExampleHubDiscover.CreateHostAndDiscover();
12+
13+
using (var technicMediumHub = host.FindByType<TechnicMediumHub>())
14+
{
15+
await technicMediumHub.CreateVirtualPortAsync(0, 2);
16+
await Task.Delay(1000);
17+
18+
var virtualPort = technicMediumHub.Ports.FirstOrDefault(p => p.IsVirtual);
19+
20+
var motorsOnVirtualPort = virtualPort.GetDevice<TechnicXLargeLinearMotor>();
21+
await motorsOnVirtualPort.StartSpeedForTimeAsync(2000, 40, 100, SpecialSpeed.Float, SpeedProfiles.None);
22+
23+
await Task.Delay(3000);
24+
25+
await technicMediumHub.CloseVirtualPortAsync(virtualPort.PortId);
26+
27+
await technicMediumHub.SwitchOffAsync();
28+
}
29+
}
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace SharpBrick.PoweredUp.Examples
5+
{
6+
class Program
7+
{
8+
static async Task Main(string[] args)
9+
{
10+
//await Example.ExampleColors.ExecuteAsync();
11+
//await Example.ExampleMotorControl.ExecuteAsync();
12+
//await Example.ExampleMotorInputAbsolutePosition.ExecuteAsync();
13+
await Example.ExampleMotorVirtualPort.ExecuteAsync();
14+
}
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp\SharpBrick.PoweredUp.csproj" />
11+
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp.WinRT\SharpBrick.PoweredUp.WinRT.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
16+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
18+
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
19+
</ItemGroup>
20+
21+
</Project>

examples/message-trace/Program.cs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4-
using SharpBrick.PoweredUp.Bluetooth;
54
using SharpBrick.PoweredUp.Protocol.Messages;
65
using SharpBrick.PoweredUp.WinRT;
76
using Microsoft.Extensions.Logging;
87
using SharpBrick.PoweredUp.Protocol;
98
using SharpBrick.PoweredUp.Functions;
109
using Microsoft.Extensions.DependencyInjection;
10+
using System.Linq;
1111

1212
namespace SharpBrick.PoweredUp.Examples.MessageTrace
1313
{
@@ -60,6 +60,7 @@ static async Task Main(string[] args)
6060
{
6161
await technicMediumHub.RgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
6262

63+
// simple motor control
6364
var motor = technicMediumHub.A.GetDevice<TechnicXLargeLinearMotor>();
6465

6566
await motor.GotoAbsolutePositionAsync(45, 10, 100, SpecialSpeed.Brake, SpeedProfiles.None);
@@ -68,87 +69,8 @@ static async Task Main(string[] args)
6869

6970
await technicMediumHub.SwitchOffAsync();
7071
}
71-
72-
return;
73-
74-
ulong bluetoothAddress = 158897336311065;
75-
76-
if (bluetoothAddress == 0)
77-
return;
78-
79-
80-
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
81-
using (var kernel = new BluetoothKernel(poweredUpBluetoothAdapter, bluetoothAddress, loggerFactory.CreateLogger<BluetoothKernel>()))
82-
{
83-
var protocol = new PoweredUpProtocol(kernel, loggerFactory.CreateLogger<PoweredUpProtocol>());
84-
85-
await kernel.ConnectAsync();
86-
87-
await protocol.SetupUpstreamObservableAsync();
88-
89-
// virtual port sample
90-
// await protocol.SendMessageAsync(new VirtualPortSetupForConnectedMessage() { SubCommand = VirtualPortSubCommand.Connected, PortAId = 0x01, PortBId = 0x02, });
91-
// await kernel.SendBytesAsync(BytesStringUtil.StringToData("09-00-81-10-11-07-64-64-00")); // 3.27.5
92-
93-
// single motor sample
94-
// await protocol.SendMessageAsync(BytesStringUtil.StringToData("09-00-81-00-11-07-64-64-00")); // 3.27.5
95-
96-
//await protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage() { PortId = 99, Mode = 0x00, DeltaInterval = 5, NotificationEnabled = true });
97-
98-
//await SetupPortInCombinedMode(protocol);
99-
100-
Console.ReadLine();
101-
102-
//await protocol.SendMessageAsync(new HubActionMessage() { Action = HubAction.ResetBusyIndication, });
103-
104-
var motor = new TechnicXLargeLinearMotor(protocol, 0, 0);
105-
// await motor.SetAccelerationTime(3000);
106-
// await motor.SetDeccelerationTime(1000);
107-
// await motor.StartSpeedForTimeAsync(6000, 90, 100, PortOutputCommandSpecialSpeed.Hold, PortOutputCommandSpeedProfile.AccelerationProfile | PortOutputCommandSpeedProfile.DeccelerationProfile);
108-
109-
// await Task.Delay(2000);
110-
111-
//await motor.StartSpeedForDegrees(180, -10, 100, PortOutputCommandSpecialSpeed.Brake, PortOutputCommandSpeedProfile.None);
112-
113-
await Task.Delay(2000);
114-
115-
await motor.SetupNotificationAsync(motor.ModeIndexAbsolutePosition, true);
116-
117-
await motor.StartPowerAsync(80);
118-
119-
logger.LogWarning($"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {motor.AbsolutePosition}");
120-
121-
await Task.Delay(2000);
122-
123-
logger.LogWarning($"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {motor.AbsolutePosition}");
124-
125-
await Task.Delay(2000);
126-
127-
await motor.StartPowerAsync(0);
128-
129-
logger.LogWarning($"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {motor.AbsolutePosition}");
130-
// await motor.StartSpeedAsync(100, 90, PortOutputCommandSpeedProfile.None);
131-
132-
// await Task.Delay(2000);
133-
134-
// await motor.StartSpeedAsync(127, 90, PortOutputCommandSpeedProfile.None);
135-
136-
// await motor.StartSpeedAsync(-100, 90, PortOutputCommandSpeedProfile.None);
137-
// await Task.Delay(2000);
138-
139-
// await motor.StartSpeedAsync(0, 90, PortOutputCommandSpeedProfile.None);
140-
141-
Console.ReadLine();
142-
143-
logger.LogInformation("Switch off device");
144-
await protocol.SendMessageAsync(new HubActionMessage() { Action = HubAction.SwitchOffHub });
145-
146-
Console.ReadLine();
147-
}
14872
}
14973

150-
151-
15274
private static async Task SetupPortInCombinedMode(PoweredUpProtocol protocol)
15375
{
15476
await protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeMessage()

0 commit comments

Comments
 (0)