The demo project uses the ANT+ gRPC service to connect to ANT devices and display the data in the Godot project. Logging is output to the Godot editor console for debugging purposes.
- Godot v4.3.stable.mono.official
- Visual Studio Community 2022
- Clone this demo project
- Clone the ANT+ repo
- 2 ANT+ USB Sticks. The second stick is used to simulate ANT devices.
- SimulANT+ for simulating ANT devices
- Install the ANT+ USB sticks and drivers if needed
- Build the ANT+ solution in Visual Studio
- Run the ANT+ project AntGrpcService
- Select the HTTPS option
- Run SimulANT+ and select one of the supported ANT devices, e.g., Heart Rate Monitor or Bike Power
- Run the demo project in Godot
- Create a new Godot project
- Add a top-level node to the scene and name it
Main
- Add a new C# script to the
Main
node - Open the solution in Visual Studio
- Add the following NuGet packages to the project:
SmallEarthTech.AntPlus.Extensions.Hosting
Grpc.Net.Client
Google.Protobuf
Godot.DependencyInjection
- This package adds support for logging and dependency injection
- Right-click Dependencies in the Solution Explorer and select Add Reference.
- Click browse and select AntGrpcShared.dll (netstandard2.1\debug or netstandard2.1\release build) from the cloned AntPlus solution.
- Add the following code to the Main.cs script constructor:
public Main()
{
// create the host
_host = Host.CreateDefaultBuilder(_options).
UseAntPlus(). // add ANT libraries and hosting extensions to services
ConfigureServices(services =>
{
// add the ANT radio service and cancellation token to signal app termination
services.AddSingleton<IAntRadio, AntRadioService>();
services.AddSingleton(_cancellationTokenSource);
// add Godot services
services.AddGodotServices();
}).
Build();
// get the ANT radio service
_antRadioService = _host.Services.GetRequiredService<IAntRadio>() as AntRadioService;
// get the ANT device collection
_antDevices = _host.Services.GetRequiredService<AntCollection>();
// get the _logger
_logger = _host.Services.GetRequiredService<ILogger<Main>>();
}
- Add the following code to the Main.cs script
_Ready
method:
public override void _Ready()
{
// SETUP ANY GODOT NODES HERE
// search for an ANT radio server on the local network
_ = Task.Run(async () =>
{
try
{
await _antRadioService.FindAntRadioServerAsync();
_antDevices.CollectionChanged += AntDevices_CollectionChanged;
// IMPORTANT: Initiate scanning on a background thread.
await _antDevices.StartScanning();
}
catch (OperationCanceledException)
{
_logger.LogInformation("OperationCanceledException: Exiting game.");
}
});
}
- Add a CollectionChanged event handler to the Main.cs script:
private void AntDevices_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// handle the ANT device collection changes
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var device in e.NewItems)
{
// handle the new device
_logger.LogInformation($"New device: {device}");
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (var device in e.OldItems)
{
// handle the removed device
_logger.LogInformation($"Removed device: {device}");
}
break;
case NotifyCollectionChangedAction.Reset:
// handle the reset
_logger.LogInformation("Device collection reset.");
break;
}
}
Provide methods to manage collection changes, such as adding or removing devices.