From ebe59bcae5a8ceeaa27b0b5ecbb1a82e22f0a55c Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Sun, 5 Feb 2023 11:31:01 +0100 Subject: [PATCH] 8.33 kHz spacing support for Microsoft simulators Related to #186 --- .../fsxcommon/simulatorfsxcommon.cpp | 114 ++++++++++++------ .../simulator/fsxcommon/simulatorfsxcommon.h | 13 ++ 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index 7d6e41fe9..fe3c6a6eb 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -143,6 +143,31 @@ namespace BlackSimPlugin::FsxCommon return this->physicallyAddRemoteAircraftImpl(newRemoteAircraft, ExternalCall); } + bool CSimulatorFsxCommon::updateCOMFromSwiftToSimulator(const CFrequency &newFreq, const CFrequency &lastSimFreq, CFrequency &last25kHzSimFreq, EventIds id) + { + if (newFreq == lastSimFreq) { return false; } + + if (CComSystem::isExclusiveWithin8_33kHzChannel(newFreq) && last25kHzSimFreq.isNull()) + { + // Switch from 25 to 8.33 + // Store last 25 kHz frequency and do not send to simulator + last25kHzSimFreq = lastSimFreq; + return false; + } + + if (CComSystem::isWithin25kHzChannel(newFreq)) + { + // Send to simulator + last25kHzSimFreq.setNull(); + SimConnect_TransmitClientEvent(m_hSimConnect, 0, id, + CBcdConversions::comFrequencyToBcdHz(newFreq), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); + return true; + } + + // Already 8.33 -> nothing to do + return false; + } + bool CSimulatorFsxCommon::updateOwnSimulatorCockpit(const CSimulatedAircraft &ownAircraft, const CIdentifier &originator) { if (originator == this->identifier()) { return false; } @@ -154,43 +179,11 @@ namespace BlackSimPlugin::FsxCommon const CTransponder newTransponder = ownAircraft.getTransponder(); bool changed = false; - if (newCom1.getFrequencyActive() != m_simCom1.getFrequencyActive()) - { - const CFrequency newFreq = newCom1.getFrequencyActive(); - SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetCom1Active, - CBcdConversions::comFrequencyToBcdHz(newFreq), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); - changed = true; - } - if (newCom1.getFrequencyStandby() != m_simCom1.getFrequencyStandby()) - { - const CFrequency newFreq = newCom1.getFrequencyStandby(); - SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetCom1Standby, - CBcdConversions::comFrequencyToBcdHz(newFreq), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); - changed = true; - } - - if (newCom2.getFrequencyActive() != m_simCom2.getFrequencyActive()) - { - const CFrequency newFreq = newCom2.getFrequencyActive(); - SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetCom2Active, - CBcdConversions::comFrequencyToBcdHz(newFreq), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); - changed = true; - } - if (newCom2.getFrequencyStandby() != m_simCom2.getFrequencyStandby()) - { - const CFrequency newFreq = newCom2.getFrequencyStandby(); - SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetCom2Standby, - CBcdConversions::comFrequencyToBcdHz(newFreq), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); - changed = true; - } - - if (newTransponder.getTransponderCode() != m_simTransponder.getTransponderCode()) - { - SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetTransponderCode, - CBcdConversions::transponderCodeToBcd(newTransponder), SIMCONNECT_GROUP_PRIORITY_HIGHEST, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); - changed = true; - } + changed |= updateCOMFromSwiftToSimulator(newCom1.getFrequencyActive(), m_simCom1.getFrequencyActive(), m_lastCom1Active, EventSetCom1Active); + changed |= updateCOMFromSwiftToSimulator(newCom1.getFrequencyStandby(), m_simCom1.getFrequencyStandby(), m_lastCom1Standby, EventSetCom1Standby); + changed |= updateCOMFromSwiftToSimulator(newCom2.getFrequencyActive(), m_simCom2.getFrequencyActive(), m_lastCom2Active, EventSetCom2Active); + changed |= updateCOMFromSwiftToSimulator(newCom2.getFrequencyStandby(), m_simCom2.getFrequencyStandby(), m_lastCom2Standby, EventSetCom2Standby); if (newTransponder.getTransponderMode() != m_simTransponder.getTransponderMode()) { @@ -745,7 +738,30 @@ namespace BlackSimPlugin::FsxCommon const int com1Status = qRound(simulatorOwnAircraft.comStatus1); // Radio status flag : -1 =Invalid 0 = OK 1 = Does not exist 2 = No electricity 3 = Failed com1.setTransmitEnabled(com1Status == 0 && com1Transmit); com1.setReceiveEnabled(com1Status == 0 && (comReceiveAll || com1Transmit)); - const bool changedCom1 = myAircraft.getCom1System() != com1; + + const bool changedCom1Active = myAircraft.getCom1System().getFrequencyActive() != com1.getFrequencyActive() && com1.getFrequencyActive() != m_lastCom1Active; + const bool changedCom1Standby = myAircraft.getCom1System().getFrequencyStandby() != com1.getFrequencyStandby() && com1.getFrequencyStandby() != m_lastCom1Standby; + const bool changedCom1 = changedCom1Active || changedCom1Standby; + + // Avoid overwrite of 8.33 kHz frequency with data from simulator + if (!changedCom1Active) + { + com1.setFrequencyActive(myAircraft.getCom1System().getFrequencyActive()); + } + else + { + m_lastCom1Active.setNull(); + } + + if (!changedCom1Standby) + { + com1.setFrequencyStandby(myAircraft.getCom1System().getFrequencyStandby()); + } + else + { + m_lastCom1Standby.setNull(); + } + m_simCom1 = com1; Q_UNUSED(com1Test) @@ -756,7 +772,29 @@ namespace BlackSimPlugin::FsxCommon const int com2Status = qRound(simulatorOwnAircraft.comStatus2); // Radio status flag : -1 =Invalid 0 = OK 1 = Does not exist 2 = No electricity 3 = Failed com2.setTransmitEnabled(com2Status == 0 && com2Transmit); com2.setReceiveEnabled(com2Status == 0 && (comReceiveAll || com2Transmit)); - const bool changedCom2 = myAircraft.getCom2System() != com2; + const bool changedCom2Active = myAircraft.getCom2System().getFrequencyActive() != com2.getFrequencyActive() && com2.getFrequencyActive() != m_lastCom2Active; + const bool changedCom2Standby = myAircraft.getCom2System().getFrequencyStandby() != com2.getFrequencyStandby() && com2.getFrequencyStandby() != m_lastCom2Standby; + const bool changedCom2 = changedCom2Active || changedCom2Standby; + + // Avoid overwrite of 8.33 kHz frequency with data from simulator + if (!changedCom2Active) + { + com2.setFrequencyActive(myAircraft.getCom2System().getFrequencyActive()); + } + else + { + m_lastCom2Active.setNull(); + } + + if (!changedCom2Standby) + { + com2.setFrequencyStandby(myAircraft.getCom2System().getFrequencyStandby()); + } + else + { + m_lastCom2Standby.setNull(); + } + m_simCom2 = com2; Q_UNUSED(com2Test) diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h index f7562233d..2c55dc1b9 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h @@ -27,6 +27,7 @@ #include "blackmisc/statusmessage.h" #include "blackmisc/network/client.h" #include "blackmisc/pixmap.h" +#include "blackmisc/pq/frequency.h" #include "blackconfig/buildconfig.h" #include @@ -558,6 +559,12 @@ namespace BlackSimPlugin::FsxCommon const BlackMisc::Simulation::CSimulatedAircraft &aircraft, DWORD requestId, CSimConnectObject::SimObjectType type, const CSimConnectObject &removedPendingObject = {}); + //! Update simulator COM from swift data. Returns true if simulator frequency was changed + bool updateCOMFromSwiftToSimulator(const BlackMisc::PhysicalQuantities::CFrequency &newFreq, + const BlackMisc::PhysicalQuantities::CFrequency &lastSimFreq, + BlackMisc::PhysicalQuantities::CFrequency &last25kHzSimFreq, + EventIds id); + //! Used for terrain probes static const BlackMisc::Aviation::CAltitude &terrainProbeAltitude(); @@ -629,6 +636,12 @@ namespace BlackSimPlugin::FsxCommon SIMCONNECT_DATA_REQUEST_ID m_requestIdSimObjTerrainProbe = static_cast(RequestSimObjTerrainProbeStart); //!< request id, use obtainRequestIdForSimObjTerrainProbe to get id QTimer m_simObjectTimer; //!< updating of SimObjects awaiting to be added + // Last selected frequencies in simulator before setting 8.33 kHz spacing frequency + BlackMisc::PhysicalQuantities::CFrequency m_lastCom1Active{ 0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit() }; //!< last COM1 active frequency + BlackMisc::PhysicalQuantities::CFrequency m_lastCom1Standby{ 0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit() }; //!< last COM1 standby frequency + BlackMisc::PhysicalQuantities::CFrequency m_lastCom2Active{ 0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit() }; //!< last COM2 active frequency + BlackMisc::PhysicalQuantities::CFrequency m_lastCom2Standby{ 0, BlackMisc::PhysicalQuantities::CFrequencyUnit::nullUnit() }; //!< last COM2 standby frequency + //! Request id to string static QString requestIdToString(DWORD requestId);