From d1a29a43fd7b24320de14921cc675e4742eda570 Mon Sep 17 00:00:00 2001 From: DonLakeFlyer Date: Sat, 22 Aug 2020 13:30:49 -0700 Subject: [PATCH] Ability to expose MAVLink enums to QMl --- src/QGCApplication.cc | 7 +++++++ src/comm/QGCMAVLink.cc | 6 ++++++ src/comm/QGCMAVLink.h | 32 +++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/QGCApplication.cc b/src/QGCApplication.cc index ba87770d0..7177376d4 100644 --- a/src/QGCApplication.cc +++ b/src/QGCApplication.cc @@ -106,6 +106,7 @@ #include "TerrainProfile.h" #include "ToolStripAction.h" #include "ToolStripActionList.h" +#include "QGCMAVLink.h" #if defined(QGC_ENABLE_PAIRING) #include "PairingManager.h" @@ -168,6 +169,11 @@ static QObject* screenToolsControllerSingletonFactory(QQmlEngine*, QJSEngine*) return screenToolsController; } +static QObject* mavlinkSingletonFactory(QQmlEngine*, QJSEngine*) +{ + return new QGCMAVLink(); +} + static QObject* qgroundcontrolQmlGlobalSingletonFactory(QQmlEngine*, QJSEngine*) { // We create this object as a QGCTool even though it isn't in the toolbox @@ -586,6 +592,7 @@ void QGCApplication::_initCommon() qmlRegisterSingletonType ("QGroundControl", 1, 0, "QGroundControl", qgroundcontrolQmlGlobalSingletonFactory); qmlRegisterSingletonType ("QGroundControl.ScreenToolsController", 1, 0, "ScreenToolsController", screenToolsControllerSingletonFactory); qmlRegisterSingletonType ("QGroundControl.ShapeFileHelper", 1, 0, "ShapeFileHelper", shapeFileHelperSingletonFactory); + qmlRegisterSingletonType ("MAVLink", 1, 0, "MAVLink", mavlinkSingletonFactory); // Although this should really be in _initForNormalAppBoot putting it here allowws us to create unit tests which pop up more easily if(QFontDatabase::addApplicationFont(":/fonts/opensans") < 0) { diff --git a/src/comm/QGCMAVLink.cc b/src/comm/QGCMAVLink.cc index 162803b21..4d6e45dad 100644 --- a/src/comm/QGCMAVLink.cc +++ b/src/comm/QGCMAVLink.cc @@ -23,6 +23,12 @@ constexpr QGCMAVLink::VehicleClass_t QGCMAVLink::VehicleClassMultiRotor; constexpr QGCMAVLink::VehicleClass_t QGCMAVLink::VehicleClassVTOL; constexpr QGCMAVLink::VehicleClass_t QGCMAVLink::VehicleClassGeneric; +QGCMAVLink::QGCMAVLink(QObject* parent) + : QObject(parent) +{ + +} + QList QGCMAVLink::allFirmwareClasses(void) { static const QList classes = { diff --git a/src/comm/QGCMAVLink.h b/src/comm/QGCMAVLink.h index 5a56e0104..beb58efb8 100644 --- a/src/comm/QGCMAVLink.h +++ b/src/comm/QGCMAVLink.h @@ -47,8 +47,14 @@ extern mavlink_status_t m_mavlink_status[MAVLINK_COMM_NUM_BUFFERS]; #define PACKED_STRUCT( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) ) #endif -class QGCMAVLink { +class QGCMAVLink : public QObject +{ + Q_OBJECT + public: + // Creating an instance of QGCMAVLink is only meant to be used for the Qml Singleton + QGCMAVLink(QObject* parent = nullptr); + typedef int FirmwareClass_t; typedef int VehicleClass_t; @@ -83,6 +89,30 @@ public: static QString mavResultToString (MAV_RESULT result); static QString mavSysStatusSensorToString (MAV_SYS_STATUS_SENSOR sysStatusSensor); + + // Expose mavlink enums to Qml. I've tried various way to make this work without duping, but haven't found anything that works. + + enum MAV_BATTERY_FUNCTION { + MAV_BATTERY_FUNCTION_UNKNOWN=0, /* Battery function is unknown | */ + MAV_BATTERY_FUNCTION_ALL=1, /* Battery supports all flight systems | */ + MAV_BATTERY_FUNCTION_PROPULSION=2, /* Battery for the propulsion system | */ + MAV_BATTERY_FUNCTION_AVIONICS=3, /* Avionics battery | */ + MAV_BATTERY_TYPE_PAYLOAD=4, /* Payload battery | */ + }; + Q_ENUM(MAV_BATTERY_FUNCTION) + + enum MAV_BATTERY_CHARGE_STATE + { + MAV_BATTERY_CHARGE_STATE_UNDEFINED=0, /* Low battery state is not provided | */ + MAV_BATTERY_CHARGE_STATE_OK=1, /* Battery is not in low state. Normal operation. | */ + MAV_BATTERY_CHARGE_STATE_LOW=2, /* Battery state is low, warn and monitor close. | */ + MAV_BATTERY_CHARGE_STATE_CRITICAL=3, /* Battery state is critical, return or abort immediately. | */ + MAV_BATTERY_CHARGE_STATE_EMERGENCY=4, /* Battery state is too low for ordinary abort sequence. Perform fastest possible emergency stop to prevent damage. | */ + MAV_BATTERY_CHARGE_STATE_FAILED=5, /* Battery failed, damage unavoidable. | */ + MAV_BATTERY_CHARGE_STATE_UNHEALTHY=6, /* Battery is diagnosed to be defective or an error occurred, usage is discouraged / prohibited. | */ + MAV_BATTERY_CHARGE_STATE_CHARGING=7, /* Battery is charging. | */ + }; + Q_ENUM(MAV_BATTERY_CHARGE_STATE) }; class MavlinkFTP { -- 2.22.0