From 0f309e04c7d510956ff9bc07fe07a1014f9945a0 Mon Sep 17 00:00:00 2001 From: Nav Date: Fri, 9 Apr 2021 20:33:24 +0100 Subject: [PATCH] Cleaned up default target variant selection code and some tidying --- src/DebugToolDrivers/USB/HID/hidapi.hpp | 20 +++++----- .../InsightWindow/InsightWindow.cpp | 38 ++++++++----------- .../InsightWindow/InsightWindow.hpp | 2 +- .../TargetWidgets/DIP/BodyWidget.cpp | 1 - .../TargetWidgets/DIP/BodyWidget.hpp | 1 + .../TargetWidgets/QFP/BodyWidget.hpp | 1 + src/TargetController/TargetController.hpp | 1 + 7 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/DebugToolDrivers/USB/HID/hidapi.hpp b/src/DebugToolDrivers/USB/HID/hidapi.hpp index 801c419b..626eee96 100644 --- a/src/DebugToolDrivers/USB/HID/hidapi.hpp +++ b/src/DebugToolDrivers/USB/HID/hidapi.hpp @@ -4,34 +4,34 @@ #include struct hid_device_ { - /* Handle to the actual device. */ + // Handle to the actual device. libusb_device_handle* device_handle; - /* Endpoint information */ + // Endpoint information int input_endpoint; int output_endpoint; int input_ep_max_packet_size; - /* The interface number of the HID */ + // The interface number of the HID int interface; - /* Indexes of Strings */ + // Indexes of Strings int manufacturer_index; int product_index; int serial_index; - /* Whether blocking reads are used */ - int blocking; /* boolean */ + // Whether blocking reads are used + int blocking; // boolean - /* Read thread objects */ + // Read thread objects pthread_t thread; - pthread_mutex_t mutex; /* Protects input_reports */ + pthread_mutex_t mutex; // Protects input_reports pthread_cond_t condition; - pthread_barrier_t barrier; /* Ensures correct startup sequence */ + pthread_barrier_t barrier; // Ensures correct startup sequence int shutdown_thread; int cancelled; struct libusb_transfer* transfer; - /* List of received input reports. */ + // List of received input reports. struct input_report* input_reports; }; diff --git a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp index 4de585c8..62e87f32 100644 --- a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp @@ -82,6 +82,10 @@ void InsightWindow::init( ); if (this->isVariantSupported(targetVariant)) { + this->supportedVariantsByName.insert( + std::pair(QString::fromStdString(targetVariant.name).toLower().toStdString(), targetVariant) + ); + connect( variantAction, &QAction::triggered, @@ -100,33 +104,21 @@ void InsightWindow::init( this->variantMenu->addAction(variantAction); } - std::copy_if( - this->targetDescriptor.variants.begin(), - this->targetDescriptor.variants.end(), - std::back_inserter(this->supportedVariants), - [this](auto variant) { - return this->isVariantSupported(variant); - } - ); + Logger::debug("Number of target variants supported by Insight: " + std::to_string(supportedVariantsByName.size())); - Logger::debug("Number of target variants supported by Insight: " + std::to_string(supportedVariants.size())); + if (!this->supportedVariantsByName.empty()) { + if (!targetConfig.variantName.empty() && this->supportedVariantsByName.contains(targetConfig.variantName)) { + // The user has specified a valid variant name in their config file, so use that as the default + this->selectVariant(&(this->supportedVariantsByName.at(targetConfig.variantName))); - if (!supportedVariants.empty()) { - auto selectedVariant = std::find_if( - supportedVariants.begin(), - supportedVariants.end(), - [&targetConfig](const TargetVariant& variant) { - auto variantName = QString::fromStdString(variant.name).toLower().toStdString(); - return !targetConfig.variantName.empty() && targetConfig.variantName == variantName; + } else { + if (!targetConfig.variantName.empty()) { + Logger::error("Invalid target variant name \"" + targetConfig.variantName + + "\" - no such variant with the given name was found."); } - ); - /* - * If ths user specified a valid variant name in their config file, use that as the default, otherwise just - * use the first supported variant. - */ - this->selectVariant((selectedVariant != supportedVariants.end()) ? &(*selectedVariant) - : &supportedVariants.front()); + this->selectVariant(&(this->supportedVariantsByName.begin()->second)); + } } else { if (this->targetDescriptor.variants.empty()) { diff --git a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.hpp b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.hpp index 2fc7ee00..880bc632 100644 --- a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.hpp @@ -47,7 +47,7 @@ namespace Bloom QLabel* targetStatusLabel = nullptr; QLabel* programCounterValueLabel = nullptr; - std::vector supportedVariants; + std::map supportedVariantsByName; const TargetVariant* selectedVariant = nullptr; bool uiDisabled = false; diff --git a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.cpp index 4f05ca52..d8225794 100644 --- a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.cpp @@ -23,7 +23,6 @@ void BodyWidget::drawWidget(QPainter& painter) { painter.setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true); // Draw target body -// auto targetBodyColor = QColor("#AFB1B3"); auto targetBodyColor = this->getBodyColor(); auto backgroundColor = QColor("#3C3F41"); diff --git a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.hpp index a064d503..bce80d7a 100644 --- a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/DIP/BodyWidget.hpp @@ -11,6 +11,7 @@ namespace Bloom::InsightTargetWidgets::Dip Q_PROPERTY(int disableAlphaLevel READ getDisableAlphaLevel WRITE setDisableAlphaLevel DESIGNABLE true) private: + // These properties can be modified via Qt style sheets (see Stylesheets/DualInlinePackage.qss) QColor bodyColor = QColor("#AFB1B3"); int disableAlphaLevel = 100; diff --git a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/QFP/BodyWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/QFP/BodyWidget.hpp index 8e865042..db4570ee 100644 --- a/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/QFP/BodyWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/TargetWidgets/QFP/BodyWidget.hpp @@ -11,6 +11,7 @@ namespace Bloom::InsightTargetWidgets::Qfp Q_PROPERTY(int disableAlphaLevel READ getDisableAlphaLevel WRITE setDisableAlphaLevel DESIGNABLE true) private: + // These properties can be modified via Qt style sheets (see Stylesheets/QuadFlatPackage.qss) QColor bodyColor = QColor("#AFB1B3"); int disableAlphaLevel = 100; diff --git a/src/TargetController/TargetController.hpp b/src/TargetController/TargetController.hpp index 0fbf2fa7..30d6938c 100644 --- a/src/TargetController/TargetController.hpp +++ b/src/TargetController/TargetController.hpp @@ -193,6 +193,7 @@ namespace Bloom * @param correlationId */ void emitErrorEvent(int correlationId); + public: TargetController(EventManager& eventManager): eventManager(eventManager) {};