Skip to content

Commit ac3dde4

Browse files
authored
Add Protocol Only Example (#202)
- Add Protocol Only Example #199 non-breaking
1 parent 1a5b2cf commit ac3dde4

5 files changed

Lines changed: 133 additions & 31 deletions

File tree

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
"console": "integratedTerminal",
1919
"stopAtEntry": false
2020
},
21+
{
22+
"name": "example app - protocol",
23+
"type": "coreclr",
24+
"request": "launch",
25+
"preLaunchTask": "build",
26+
"program": "${workspaceFolder}/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/bin/Debug/net8.0-windows10.0.19041.0/SharpBrick.PoweredUp.ExampplesOnProtocol.dll",
27+
"args": [],
28+
"cwd": "${workspaceFolder}/examples/SharpBrick.PoweredUp.ExampplesOnProtocol",
29+
"console": "integratedTerminal",
30+
"stopAtEntry": false
31+
},
2132
{
2233
"name": "poweredup device list",
2334
"type": "coreclr",

README.md

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,50 +150,48 @@ Console.WriteLine($"Or directly read the latest value: {aposMode.SI} / {aposMode
150150

151151
## Connect to Hub and Send a Message and retrieving answers (directly on protocol layer)
152152

153-
***Note**: The `ILegoWirelessProtocol` class was renamed in 3.0. Previously it is known as `IPoweredUpProtocol`.*
154-
155153
````csharp
156-
157154
var serviceProvider = new ServiceCollection()
158155
.AddLogging()
159156
.AddPoweredUp()
160-
.AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT)
157+
.AddWinRTBluetooth()
161158
.BuildServiceProvider();
162159

163-
using (var scope = serviceProvider.CreateScope()) // create a scoped DI container per intented active connection/protocol. If disposed, disposes all disposable artifacts.
164-
{
165-
// init BT layer with right bluetooth address
166-
scope.ServiceProvider.GetService<BluetoothKernel>().BluetoothAddress = bluetoothAddress;
160+
// getting utilities
161+
var bt = serviceProvider.GetService<IPoweredUpBluetoothAdapter>();
162+
var host = serviceProvider.GetService<PoweredUpHost>();
167163

168-
var protocol = scope.GetService<ILegoWirelessProtocol>();
164+
// discover a LWP bluetooth device
165+
var tcs = new TaskCompletionSource<ILegoWirelessProtocol>();
169166

170-
await protocol.ConnectAsync(); // also connects underlying BT connection
171-
172-
using disposable = protocol.UpstreamMessages.Subscribe(message =>
167+
bt.Discover(async deviceInfo =>
168+
{
169+
if (!tcs.Task.IsCompleted)
173170
{
174-
if (message is HubPropertyMessage<string> msg)
175-
{
176-
Console.WriteLine($"Hub Property - {msg.Property}: {msg.Payload}");
177-
}
178-
});
171+
var p = host.CreateProtocol(deviceInfo);
179172

180-
await protocol.SendMessageAsync(new HubPropertyMessage() {
181-
Property = HubProperty.AdvertisingName,
182-
Operation = HubPropertyOperation.RequestUpdate
183-
});
173+
tcs.SetResult(p);
174+
}
175+
});
184176

185-
Console.Readline(); // allow the messages to be processed and displayed. (alternative: SendMessageReceiveResultAsync, SendPortOutputCommandAsync, ..)
177+
var protocol = await tcs.Task;
186178

187-
// fun with light on hub 0 and built-in LED on port 50
188-
var rgbLight = new RgbLight(protocol, 0, 50);
189-
await rgbLight.SetRgbColorsAsync(0x00, 0xff, 0x00);
179+
// connect the protocol
180+
await protocol.ConnectAsync();
190181

191-
// fun with motor on hub 0 and port 0
192-
var motor = new TechnicXLargeLinearMotor(protocol, 0, 0);
193-
await motor.GotoPositionAsync(45, 10, 100, PortOutputCommandSpecialSpeed.Brake);
194-
await Task.Delay(2000);
195-
await motor.GotoPositionAsync(-45, 10, 100, PortOutputCommandSpecialSpeed.Brake);
196-
}
182+
// send a raw message which should work with ANY motor connected to a hub
183+
var response = await protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage(
184+
0, // PORT A
185+
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
186+
100
187+
)
188+
{
189+
HubId = 0, // as if we ever see another one
190+
});
191+
192+
await Task.Delay(2000);
193+
194+
await protocol.DisconnectAsync();
197195
````
198196
## Connecting with other Bluetooth Adapters
199197

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using SharpBrick.PoweredUp;
2+
3+
using Microsoft.Extensions.DependencyInjection; // SharpBrick.PoweredUp uses the DI system
4+
using Microsoft.Extensions.Logging;
5+
using SharpBrick.PoweredUp.Bluetooth;
6+
using SharpBrick.PoweredUp.Protocol;
7+
using SharpBrick.PoweredUp.Protocol.Messages; // SharpBrick.PoweredUp also logs stuff
8+
9+
var serviceProvider = new ServiceCollection()
10+
.AddLogging()
11+
.AddPoweredUp()
12+
#if WINDOWS
13+
.AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT; others are available)
14+
#endif
15+
.BuildServiceProvider();
16+
17+
// getting utilities
18+
var bt = serviceProvider.GetService<IPoweredUpBluetoothAdapter>();
19+
var host = serviceProvider.GetService<PoweredUpHost>();
20+
21+
// discover a LWP bluetooth device
22+
var tcs = new TaskCompletionSource<ILegoWirelessProtocol>();
23+
24+
bt.Discover(async deviceInfo =>
25+
{
26+
if (!tcs.Task.IsCompleted)
27+
{
28+
var p = host.CreateProtocol(deviceInfo);
29+
30+
tcs.SetResult(p);
31+
}
32+
});
33+
34+
var protocol = await tcs.Task;
35+
36+
// connect the protocol
37+
await protocol.ConnectAsync();
38+
39+
// send a raw message which should work with ANY motor connected to a hub
40+
var response = await protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage(
41+
0, // PORT A
42+
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
43+
100
44+
)
45+
{
46+
HubId = 0, // as if we ever see another one
47+
});
48+
49+
await Task.Delay(2000);
50+
51+
await protocol.DisconnectAsync();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net8.0-windows10.0.19041.0;net8.0</TargetFrameworks>
6+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0</TargetFrameworks>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp\SharpBrick.PoweredUp.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0-windows10.0.19041.0' ">
16+
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp.WinRT\SharpBrick.PoweredUp.WinRT.csproj" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
21+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
22+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
24+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
25+
</ItemGroup>
26+
27+
</Project>

powered-up.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpBrick.PoweredUp.BlueGi
2424
EndProject
2525
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBrick.PoweredUp.TestScript", "test\SharpBrick.PoweredUp.TestScript\SharpBrick.PoweredUp.TestScript.csproj", "{2A100817-6E86-4E58-8183-0EA7F49C0848}"
2626
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBrick.PoweredUp.ExampplesOnProtocol", "examples\SharpBrick.PoweredUp.ExampplesOnProtocol\SharpBrick.PoweredUp.ExampplesOnProtocol.csproj", "{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -130,6 +132,18 @@ Global
130132
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x64.Build.0 = Release|Any CPU
131133
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.ActiveCfg = Release|Any CPU
132134
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.Build.0 = Release|Any CPU
135+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
136+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|Any CPU.Build.0 = Debug|Any CPU
137+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x64.ActiveCfg = Debug|Any CPU
138+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x64.Build.0 = Debug|Any CPU
139+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x86.ActiveCfg = Debug|Any CPU
140+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x86.Build.0 = Debug|Any CPU
141+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|Any CPU.ActiveCfg = Release|Any CPU
142+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|Any CPU.Build.0 = Release|Any CPU
143+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x64.ActiveCfg = Release|Any CPU
144+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x64.Build.0 = Release|Any CPU
145+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x86.ActiveCfg = Release|Any CPU
146+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x86.Build.0 = Release|Any CPU
133147
EndGlobalSection
134148
GlobalSection(SolutionProperties) = preSolution
135149
HideSolutionNode = FALSE
@@ -143,6 +157,7 @@ Global
143157
{F66A2B09-84B6-477D-9B15-926E771C7D80} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
144158
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
145159
{2A100817-6E86-4E58-8183-0EA7F49C0848} = {39B30145-497F-4AEB-A014-BBF27DA0651A}
160+
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B} = {F4D84196-5E85-41FE-9C39-38BD8F78E84D}
146161
EndGlobalSection
147162
GlobalSection(ExtensibilityGlobals) = postSolution
148163
SolutionGuid = {34AA1641-ACED-43ED-A0DD-BA88E43A67A8}

0 commit comments

Comments
 (0)