-
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 Protocol Only Example #199 non-breaking
- Loading branch information
Showing
5 changed files
with
133 additions
and
31 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.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,51 @@ | ||
using SharpBrick.PoweredUp; | ||
|
||
using Microsoft.Extensions.DependencyInjection; // SharpBrick.PoweredUp uses the DI system | ||
using Microsoft.Extensions.Logging; | ||
using SharpBrick.PoweredUp.Bluetooth; | ||
using SharpBrick.PoweredUp.Protocol; | ||
using SharpBrick.PoweredUp.Protocol.Messages; // SharpBrick.PoweredUp also logs stuff | ||
|
||
var serviceProvider = new ServiceCollection() | ||
.AddLogging() | ||
.AddPoweredUp() | ||
#if WINDOWS | ||
.AddWinRTBluetooth() // using WinRT Bluetooth on Windows (separate NuGet SharpBrick.PoweredUp.WinRT; others are available) | ||
#endif | ||
.BuildServiceProvider(); | ||
|
||
// getting utilities | ||
var bt = serviceProvider.GetService<IPoweredUpBluetoothAdapter>(); | ||
var host = serviceProvider.GetService<PoweredUpHost>(); | ||
|
||
// discover a LWP bluetooth device | ||
var tcs = new TaskCompletionSource<ILegoWirelessProtocol>(); | ||
|
||
bt.Discover(async deviceInfo => | ||
Check warning on line 24 in examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs GitHub Actions / build
Check warning on line 24 in examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs GitHub Actions / build
Check warning on line 24 in examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs GitHub Actions / build
Check warning on line 24 in examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs GitHub Actions / build
|
||
{ | ||
if (!tcs.Task.IsCompleted) | ||
{ | ||
var p = host.CreateProtocol(deviceInfo); | ||
Check warning on line 28 in examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs GitHub Actions / build
|
||
tcs.SetResult(p); | ||
} | ||
}); | ||
|
||
var protocol = await tcs.Task; | ||
|
||
// connect the protocol | ||
await protocol.ConnectAsync(); | ||
|
||
// send a raw message which should work with ANY motor connected to a hub | ||
var response = await protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage( | ||
0, // PORT A | ||
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback, | ||
100 | ||
) | ||
{ | ||
HubId = 0, // as if we ever see another one | ||
}); | ||
|
||
await Task.Delay(2000); | ||
|
||
await protocol.DisconnectAsync(); |
27 changes: 27 additions & 0 deletions
27
.../SharpBrick.PoweredUp.ExampplesOnProtocol/SharpBrick.PoweredUp.ExampplesOnProtocol.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net8.0-windows10.0.19041.0;net8.0</TargetFrameworks> | ||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp\SharpBrick.PoweredUp.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0-windows10.0.19041.0' "> | ||
<ProjectReference Include="..\..\src\SharpBrick.PoweredUp.WinRT\SharpBrick.PoweredUp.WinRT.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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