-
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.
Support for NAV frequencies, transponder and heading bug (#19)
- Loading branch information
1 parent
2ec9623
commit 71afcd8
Showing
11 changed files
with
614 additions
and
33 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
61 changes: 61 additions & 0 deletions
61
src/CTrue.FsConnect.Managers.Test/AutoPilotManagerTests.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,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)); | ||
} | ||
} | ||
} |
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
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,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; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.