Skip to content

Commit

Permalink
Support for NAV frequencies, transponder and heading bug (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimianHeber authored May 30, 2021
1 parent 2ec9623 commit 71afcd8
Show file tree
Hide file tree
Showing 11 changed files with 614 additions and 33 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,27 @@ FsConnect fsConnect = new FsConnect();
fsConnect.Connect("RadioManagerTest", 0);
RadioManager radioManager = new RadioManager(_fsConnect);
radioManager.SetCom1ActiveFrequency(freq);
```

Supports:
- Setting and getting COM1 & COM2 standby and active frequencies
- Setting and getting NAV1 & NAV2 standby and active frequencies
- Setting and getting transponder code


### Autopilot manager

```csharp

FsConnect fsConnect = new FsConnect();
fsConnect.Connect("AutopilotManagerTest", 0);
AutopilotManager autopilotManager = new AutopilotManager(_fsConnect);
autopilotManager.SetHeadingBug(42);
```

Supports:
- Getting and setting the heading bug.

## Community

* Have you find a bug? Do you have an idea for a new feature? ... [open an issue on GitHub](https://github.com/c-true/FsConnect/issues)
Expand All @@ -404,6 +422,10 @@ radioManager.SetCom1ActiveFrequency(freq);

## Change log

## 1.3.3

* Added support for NAV frequencies, transponder and autopilot heading bug

## 1.3.2

* Added RadioManager, with support for setting and getting COM1 and COM2 standby and active frequencies.
Expand Down
61 changes: 61 additions & 0 deletions src/CTrue.FsConnect.Managers.Test/AutoPilotManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Threading;
using NUnit.Framework;

namespace CTrue.FsConnect.Managers.Test
{
[TestFixture(Explicit = true)]
public class AutopilotManagerTests
{
private FsConnect _fsConnect;
private AutopilotManager _manager;

[SetUp]
public void SetUp()
{
AutoResetEvent resetEvent = new AutoResetEvent(false);

_fsConnect = new FsConnect();
_fsConnect.ConnectionChanged += (sender, b) =>
{
if (b) resetEvent.Set();
};
_fsConnect.FsError += (sender, args) =>
{
Assert.Fail($"MSFS Error: {args.ExceptionDescription}");
};

_fsConnect.Connect("AutopilotManagerIntegrationTest", 0);

bool res = resetEvent.WaitOne(2000);
if (!res) Assert.Fail("Not connected to MSFS within timeout");

_manager = new AutopilotManager(_fsConnect);
_manager.Initialize();
}

[Test]
public void Update()
{
// Arrange
// Act
_manager.Update();

// Assert
Assert.That(_manager.HeadingBug, Is.GreaterThan(0));
}

[Test]
public void SetHeadingBug()
{
// Arrange
double heading = 42;

// Act
_manager.SetHeadingBug(heading);

// Assert
_manager.Update();
Assert.That(_manager.HeadingBug, Is.EqualTo(heading));
}
}
}
117 changes: 117 additions & 0 deletions src/CTrue.FsConnect.Managers.Test/RadioManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public void SetUp()
_manager = new RadioManager(_fsConnect);
}

[Test]
public void Update()
{
// Arrange
// Act
_manager.Update();

// Assert
Assert.That(_manager.Com1StandbyFrequency, Is.GreaterThan(118));
}

[Test]
public void SetCom1StandbyFrequency()
{
Expand Down Expand Up @@ -126,5 +137,111 @@ public void SwapCom2()
_manager.Update();
Assert.That(_manager.Com2ActiveFrequency, Is.EqualTo(freq));
}

#region NAV

[Test]
public void SetNav1StandbyFrequency()
{
// Arrange
double freq = 110.7;

// Act
_manager.SetNav1StandbyFrequency(freq);

// Assert
_manager.Update();
Assert.That(_manager.Nav1StandbyFrequency, Is.EqualTo(freq));
}

[Test]
public void SetNav1ActiveFrequency()
{
// Arrange
double freq = 110.7;

// Act
_manager.SetNav1ActiveFrequency(freq);

// Assert
_manager.Update();
Assert.That(_manager.Nav1ActiveFrequency, Is.EqualTo(freq));
}

[Test]
public void SetNav2StandbyFrequency()
{
// Arrange
double freq = 110.7;

// Act
_manager.SetNav2StandbyFrequency(freq);

// Assert
_manager.Update();
Assert.That(_manager.Nav2StandbyFrequency, Is.EqualTo(freq));
}

[Test]
public void SetNav2ActiveFrequency()
{
// Arrange
double freq = 110.7;

// Act
_manager.SetNav2ActiveFrequency(freq);

// Assert
_manager.Update();
Assert.That(_manager.Nav2ActiveFrequency, Is.EqualTo(freq));
}

[Test]
public void SwapNav1()
{
// Arrange
double freq = 110.7;
_manager.SetNav1StandbyFrequency(freq);
_manager.SetNav1ActiveFrequency(125.000d);

// Act
_manager.Nav1Swap();

// Assert
_manager.Update();
Assert.That(_manager.Nav1ActiveFrequency, Is.EqualTo(freq));
}

[Test]
public void SwapNav2()
{
// Arrange
double freq = 110.7;
_manager.SetNav2StandbyFrequency(freq);
_manager.SetNav2ActiveFrequency(125.000d);

// Act
_manager.Nav2Swap();

// Assert
_manager.Update();
Assert.That(_manager.Nav2ActiveFrequency, Is.EqualTo(freq));
}

#endregion

[Test]
public void TransponderCode()
{
// Arrange
uint code = 7700;

// Act
_manager.SetTransponderCode(code);

// Assert
_manager.Update();
Assert.That(_manager.TransponderCode, Is.EqualTo(code));
}
}
}
6 changes: 6 additions & 0 deletions src/CTrue.FsConnect.Managers/AircraftManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace CTrue.FsConnect.Managers
{
/// <summary>
/// Specifies how to request data from MSFS.
/// </summary>
public enum RequestMethod
{
Poll,
Expand All @@ -26,6 +29,9 @@ public class AircraftInfoUpdatedEventArgs<T> : EventArgs
/// </remarks>
public interface IAircraftManager<T> : IDisposable where T : struct
{
/// <summary>
/// The <see cref="Updated"/> event is raised when the aircraft info is updated.
/// </summary>
event EventHandler<AircraftInfoUpdatedEventArgs<T>> Updated;

/// <summary>
Expand Down
100 changes: 100 additions & 0 deletions src/CTrue.FsConnect.Managers/AutopilotManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace CTrue.FsConnect.Managers
{
/// <summary>
/// The <see cref="IAutoPilotManager"/> controls the autopilot in the current aircraft.
/// </summary>
/// <remarks>
/// Supports:
/// - Get and set heading bug.
///
/// Usage:
/// Call <see cref="Update"/> to refresh properties with latest values from MSFS.
/// </remarks>
public interface IAutoPilotManager : IFsConnectManager
{
/// <summary>
/// Gets the current heading bug, in degrees.
/// </summary>
double HeadingBug { get; }

/// <summary>
/// Sets the autopilot heading bug, in degrees.
/// </summary>
/// <param name="heading"></param>
void SetHeadingBug(double heading);
}

/// <inheritdoc />
public class AutopilotManager : FsConnectManager, IAutoPilotManager
{
private int _eventGroupId;

private AutopilotSimVars _autopilotSimVars = new AutopilotSimVars();
private int _autoPilotManagerSimVarsReqId;
private int _autoPilotManagerSimVarsDefId;

private int _headingBugSetEventId;

/// <inheritdoc />
public double HeadingBug { get; private set; }

/// <summary>
/// Creates a new <see cref="AutopilotManager"/> instance.
/// </summary>
/// <param name="fsConnect"></param>
public AutopilotManager(IFsConnect fsConnect)
: base(fsConnect)
{
}

protected override void RegisterSimVars()
{
_autoPilotManagerSimVarsReqId = _fsConnect.GetNextId();
_autoPilotManagerSimVarsDefId = _fsConnect.RegisterDataDefinition<AutopilotSimVars>();
}

protected override void RegisterEvents()
{
_eventGroupId = _fsConnect.GetNextId();
_headingBugSetEventId = _fsConnect.GetNextId();
_fsConnect.MapClientEventToSimEvent(_eventGroupId, _headingBugSetEventId, FsEventNameId.HeadingBugSet);

_fsConnect.SetNotificationGroupPriority(_eventGroupId);
}

protected override void OnFsDataReceived(object sender, FsDataReceivedEventArgs e)
{
if (e.Data.Count == 0) return;
if (!(e.Data[0] is AutopilotSimVars)) return;

_autopilotSimVars = (AutopilotSimVars)e.Data[0];
_resetEvent.Set();
}

/// <inheritdoc />
public override void Update()
{
_fsConnect.RequestData(_autoPilotManagerSimVarsReqId, _autoPilotManagerSimVarsDefId);
WaitForUpdate();

HeadingBug = _autopilotSimVars.HeadingBug;
}

/// <inheritdoc />
public void SetHeadingBug(double heading)
{
_fsConnect.TransmitClientEvent(_headingBugSetEventId, (uint)heading, _eventGroupId);
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
internal struct AutopilotSimVars
{
[SimVar(NameId = FsSimVar.AutopilotHeadingLockDir, UnitId = FsUnit.Degree)]
public double HeadingBug;
}
}
}
12 changes: 7 additions & 5 deletions src/CTrue.FsConnect.Managers/CTrue.FsConnect.Managers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
- Aircraft information
- Retrieving sim objects
- Setting world time
- Controlling COM radios
- Controlling COM and NAV frequencies
- Controlling transponder code
- Initial Autopilot manager
</Description>
<Authors>C-True</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/c-true/FsConnect</RepositoryUrl>
<PackageTags>msfs flight-simulator simconnect</PackageTags>
<Version>1.3.2</Version>
<Version>1.3.3</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>Added RadioManager for controlling COM radios.</PackageReleaseNotes>
<AssemblyVersion>1.3.2.0</AssemblyVersion>
<FileVersion>1.3.2.0</FileVersion>
<PackageReleaseNotes>Added support for NAV frequencies, transponder and autopilot heading bug</PackageReleaseNotes>
<AssemblyVersion>1.3.3.0</AssemblyVersion>
<FileVersion>1.3.3.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
Loading

0 comments on commit 71afcd8

Please sign in to comment.