forked from sharpbrick/powered-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
107 lines (86 loc) · 3.49 KB
/
Program.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace SharpBrick.PoweredUp.Examples;
class Program
{
// invoke (due to multi-targeting) with ...
// dotnet run -f net5.0 --
// dotnet run -f net5.0-windows10.0.19041.0 --
// use command line parameters
// --EnableTrace true
// --BluetoothAdapter WinRT|BlueGigaBLE
// --COMPortName COM4
// --TraceDebug true
// or configure in poweredup.json
static async Task Main(string[] args)
{
// (1) load a configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("poweredup.json", true)
.AddCommandLine(args)
.Build();
// Example Selection: select the example to execute
var exampleToExecute = configuration["Example"] ?? "Colors";
var typeToExecute = Assembly
.GetExecutingAssembly()
.GetTypes()
.FirstOrDefault(x => x.FullName.StartsWith($"Example.Example{exampleToExecute}"));
if (typeToExecute is null)
{
Console.WriteLine("Could not find example.");
return;
}
var example = Activator.CreateInstance(typeToExecute) as Example.BaseExample;
// (2) build the DI container
var serviceCollection = new ServiceCollection();
// (2a) configure your favourite level of logging.
serviceCollection.AddLogging(builder =>
{
builder
.AddConsole();
if (bool.TryParse(configuration["EnableTrace"], out var enableTrace) && enableTrace)
{
builder.AddFilter("SharpBrick.PoweredUp.Bluetooth.BluetoothKernel", LogLevel.Debug);
}
if (bool.TryParse(configuration["TraceDebug"], out var traceDebug) && traceDebug)
{
builder.AddFilter("SharpBrick.PoweredUp.BlueGigaBLE.BlueGigaBLEPoweredUpBluetoothAdapater", LogLevel.Debug);
}
});
// (2b) add .AddPoweredUp() (we delegate this into the example because some examples need a different setup)
example.Configure(serviceCollection);
// (2c) add your favourite Bluetooth Adapter
var bluetoothAdapter = configuration["BluetoothAdapter"] ?? "WinRT";
#if WINDOWS
if (bluetoothAdapter == "WinRT")
{
serviceCollection.AddWinRTBluetooth();
}
#endif
#if NET5_0_OR_GREATER
if (bluetoothAdapter == "BlueGigaBLE")
{
// config for "COMPortName" and "TraceDebug" (either via command line or poweredup.json)
// on Windows-PCs you can find it under Device Manager --> Ports (COM & LPT) --> Bleugiga Bluetooth Low Energy (COM#) (where # is a number)
// "COMPortName": "COM4",
// setting this option to false supresses the complete LogDebug()-commands; so they will not generated at all
// "TraceDebug": true
serviceCollection.AddBlueGigaBLEBluetooth(configuration);
}
#endif
var serviceProvider = serviceCollection.BuildServiceProvider();
// Examples Initialization
await example.InitExampleAndDiscoverAsync(serviceProvider, configuration);
// Example Execution
if (example.SelectedHub is not null)
{
await example.ExecuteAsync();
}
}
}