Skip to content

Commit

Permalink
Add Protocol Only Example
Browse files Browse the repository at this point in the history
#199 non-breaking
  • Loading branch information
tthiery committed Feb 11, 2024
1 parent 1a5b2cf commit 0279c7e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": "example app - protocol",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/bin/Debug/net8.0-windows10.0.19041.0/SharpBrick.PoweredUp.ExampplesOnProtocol.dll",
"args": [],
"cwd": "${workspaceFolder}/examples/SharpBrick.PoweredUp.ExampplesOnProtocol",
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": "poweredup device list",
"type": "coreclr",
Expand Down
51 changes: 51 additions & 0 deletions examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs
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 =>
{
if (!tcs.Task.IsCompleted)
{
var p = host.CreateProtocol(deviceInfo);
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();
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>
15 changes: 15 additions & 0 deletions powered-up.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpBrick.PoweredUp.BlueGi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBrick.PoweredUp.TestScript", "test\SharpBrick.PoweredUp.TestScript\SharpBrick.PoweredUp.TestScript.csproj", "{2A100817-6E86-4E58-8183-0EA7F49C0848}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBrick.PoweredUp.ExampplesOnProtocol", "examples\SharpBrick.PoweredUp.ExampplesOnProtocol\SharpBrick.PoweredUp.ExampplesOnProtocol.csproj", "{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -130,6 +132,18 @@ Global
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x64.Build.0 = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.ActiveCfg = Release|Any CPU
{2A100817-6E86-4E58-8183-0EA7F49C0848}.Release|x86.Build.0 = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x64.ActiveCfg = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x64.Build.0 = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x86.ActiveCfg = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Debug|x86.Build.0 = Debug|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|Any CPU.Build.0 = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x64.ActiveCfg = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x64.Build.0 = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x86.ActiveCfg = Release|Any CPU
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -143,6 +157,7 @@ Global
{F66A2B09-84B6-477D-9B15-926E771C7D80} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
{E0DC0096-D7F1-4995-833D-9A6C0C3F8F98} = {62C31C3D-8ACF-4ED3-A3D8-225536F3AC6D}
{2A100817-6E86-4E58-8183-0EA7F49C0848} = {39B30145-497F-4AEB-A014-BBF27DA0651A}
{3B91CDE6-66AA-47F5-B65A-DC0D580F202B} = {F4D84196-5E85-41FE-9C39-38BD8F78E84D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {34AA1641-ACED-43ED-A0DD-BA88E43A67A8}
Expand Down

0 comments on commit 0279c7e

Please sign in to comment.