Removed tight coupling of target pin widgets with Insight window - moved target pin state toggling into an InsightWorker task.

This commit is contained in:
Nav
2021-09-04 18:03:45 +01:00
parent 1bc881e9ae
commit 85ef2c57e1
15 changed files with 114 additions and 85 deletions

View File

@@ -77,7 +77,6 @@ void Insight::startup() {
this->connect(this->insightWorker, &InsightWorker::targetProgramCounterUpdated, this->mainWindow, &InsightWindow::onTargetProgramCounterUpdate);
this->connect(this->insightWorker, &InsightWorker::targetIoPortsUpdated, this->mainWindow, &InsightWindow::onTargetIoPortsUpdate);
this->connect(this->mainWindow, &InsightWindow::refreshTargetPinStates, this->insightWorker, &InsightWorker::requestPinStates);
this->connect(this->mainWindow, &InsightWindow::setTargetPinState, this->insightWorker, &InsightWorker::requestPinStateUpdate);
this->mainWindow->setInsightConfig(this->insightConfig);
this->mainWindow->setEnvironmentConfig(this->environmentConfig);

View File

@@ -28,10 +28,6 @@ void InsightWorker::startup() {
std::bind(&InsightWorker::onTargetResumedEvent, this, std::placeholders::_1)
);
this->eventListener->registerCallbackForEventType<Events::TargetPinStatesRetrieved>(
std::bind(&InsightWorker::onTargetPinStatesRetrievedEvent, this, std::placeholders::_1)
);
this->eventListener->registerCallbackForEventType<Events::TargetIoPortsUpdated>(
std::bind(&InsightWorker::onTargetIoPortsUpdatedEvent, this, std::placeholders::_1)
);
@@ -81,14 +77,6 @@ void InsightWorker::requestPinStates(int variantId) {
this->targetControllerConsole.requestPinStates(variantId);
}
void InsightWorker::requestPinStateUpdate(
int variantId,
Bloom::Targets::TargetPinDescriptor pinDescriptor,
Bloom::Targets::TargetPinState pinState
) {
this->targetControllerConsole.setPinState(variantId, pinDescriptor, pinState);
}
void InsightWorker::onTargetStoppedEvent(const Events::TargetExecutionStopped& event) {
/*
* When we report a target halt to Insight, Insight will immediately seek more data from the target (such as GPIO
@@ -125,10 +113,6 @@ void InsightWorker::onTargetResumedEvent(const Events::TargetExecutionResumed& e
emit this->targetStateUpdated(TargetState::RUNNING);
}
void InsightWorker::onTargetPinStatesRetrievedEvent(const Events::TargetPinStatesRetrieved& event) {
emit this->targetPinStatesUpdated(event.variantId, event.pinSatesByNumber);
}
void InsightWorker::onTargetIoPortsUpdatedEvent(const Events::TargetIoPortsUpdated& event) {
emit this->targetIoPortsUpdated();
}

View File

@@ -39,7 +39,6 @@ namespace Bloom
void onTargetStoppedEvent(const Events::TargetExecutionStopped& event);
void onTargetResumedEvent(const Events::TargetExecutionResumed& event);
void onTargetPinStatesRetrievedEvent(const Events::TargetPinStatesRetrieved& event);
void onTargetIoPortsUpdatedEvent(const Events::TargetIoPortsUpdated& event);
void onTargetControllerStateReported(const Events::TargetControllerStateReported& event);
@@ -58,17 +57,11 @@ namespace Bloom
public slots:
void startup();
void requestPinStates(int variantId);
void requestPinStateUpdate(
int variantId,
Bloom::Targets::TargetPinDescriptor pinDescriptor,
Bloom::Targets::TargetPinState pinState
);
signals:
void taskQueued();
void targetStateUpdated(Bloom::Targets::TargetState newState);
void targetProgramCounterUpdated(quint32 programCounter);
void targetPinStatesUpdated(int variantId, Bloom::Targets::TargetPinStateMappingType pinStatesByNumber);
void targetIoPortsUpdated();
void targetControllerSuspended();
void targetControllerResumed(const Bloom::Targets::TargetDescriptor& targetDescriptor);

View File

@@ -0,0 +1,7 @@
#include "SetTargetPinState.hpp"
using namespace Bloom;
void SetTargetPinState::run(TargetControllerConsole& targetControllerConsole) {
targetControllerConsole.setPinState(this->pinDescriptor, this->pinState);
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "InsightWorkerTask.hpp"
#include "src/Targets/TargetPinDescriptor.hpp"
namespace Bloom
{
class SetTargetPinState: public InsightWorkerTask
{
Q_OBJECT
private:
Targets::TargetPinDescriptor pinDescriptor;
Targets::TargetPinState pinState;
protected:
void run(TargetControllerConsole& targetControllerConsole) override;
public:
SetTargetPinState(const Targets::TargetPinDescriptor& pinDescriptor, const Targets::TargetPinState& pinState):
InsightWorkerTask(), pinDescriptor(pinDescriptor), pinState(pinState) {}
};
}

View File

@@ -333,9 +333,14 @@ void InsightWindow::selectVariant(const TargetVariant* variant) {
}
if (this->targetPackageWidget != nullptr) {
this->targetPackageWidget->setTargetState(this->targetState);
if (this->targetState == TargetState::STOPPED) {
this->toggleUi(true);
emit this->refreshTargetPinStates(variant->id);
this->targetPackageWidget->refreshPinStates([this] {
if (this->targetState == TargetState::STOPPED) {
this->targetPackageWidget->setDisabled(false);
}
});
}
this->targetPackageWidget->show();
@@ -441,17 +446,6 @@ void InsightWindow::onTargetIoPortsUpdate() {
}
}
void InsightWindow::togglePinIoState(InsightTargetWidgets::TargetPinWidget* pinWidget) {
auto pinState = pinWidget->getPinState();
// Currently, we only allow users to toggle the IO state of output pins
if (pinState.has_value()
&& pinState.value().ioDirection == TargetPinState::IoDirection::OUTPUT
&& this->selectedVariant != nullptr
) {
auto& pinDescriptor = pinWidget->getPinDescriptor();
pinState.value().ioState = (pinState.value().ioState == TargetPinState::IoState::HIGH) ?
TargetPinState::IoState::LOW : TargetPinState::IoState::HIGH;
emit this->setTargetPinState(this->selectedVariant->id, pinDescriptor, pinState.value());
}
}

View File

@@ -87,14 +87,8 @@ namespace Bloom
void openReportIssuesUrl();
static void openGettingStartedUrl();
void openAboutWindow();
void togglePinIoState(Widgets::InsightTargetWidgets::TargetPinWidget* pinWidget);
signals:
void refreshTargetPinStates(int variantId);
void setTargetPinState(
int variantId,
Bloom::Targets::TargetPinDescriptor pinDescriptor,
Bloom::Targets::TargetPinState pinState
);
};
}

View File

@@ -48,7 +48,7 @@ DualInlinePackageWidget::DualInlinePackageWidget(
assert(insightWindow != nullptr);
for (const auto& [targetPinNumber, targetPinDescriptor]: targetVariant.pinDescriptorsByNumber) {
auto pinWidget = new PinWidget(this, targetPinDescriptor, targetVariant);
auto pinWidget = new PinWidget(targetPinDescriptor, targetVariant, insightWorker, this);
this->pinWidgets.push_back(pinWidget);
if (targetPinNumber <= (targetVariant.pinDescriptorsByNumber.size() / 2)) {
@@ -56,8 +56,6 @@ DualInlinePackageWidget::DualInlinePackageWidget(
} else {
this->topPinLayout->addWidget(pinWidget, 0, Qt::AlignmentFlag::AlignRight);
}
connect(pinWidget, &TargetPinWidget::toggleIoState, insightWindow, &InsightWindow::togglePinIoState);
}
this->layout->addLayout(this->topPinLayout);

View File

@@ -11,8 +11,12 @@
using namespace Bloom::Widgets::InsightTargetWidgets::Dip;
using namespace Bloom::Targets;
PinWidget::PinWidget(QWidget* parent, const TargetPinDescriptor& pinDescriptor, const TargetVariant& targetVariant):
TargetPinWidget(parent, pinDescriptor, targetVariant) {
PinWidget::PinWidget(
const TargetPinDescriptor& pinDescriptor,
const TargetVariant& targetVariant,
InsightWorker& insightWorker,
QWidget* parent
): TargetPinWidget(pinDescriptor, targetVariant, insightWorker, parent) {
this->layout = new QVBoxLayout();
this->layout->setContentsMargins(0, 0, 0, 0);
this->layout->setSpacing(0);

View File

@@ -34,9 +34,10 @@ namespace Bloom::Widgets::InsightTargetWidgets::Dip
+ (PinWidget::LABEL_HEIGHT * PinWidget::MAXIMUM_LABEL_COUNT);
PinWidget(
QWidget* parent,
const Targets::TargetPinDescriptor& pinDescriptor,
const Targets::TargetVariant& targetVariant
const Targets::TargetVariant& targetVariant,
InsightWorker& insightWorker,
QWidget* parent
);
void updatePinState(const Targets::TargetPinState& pinState) override {

View File

@@ -11,8 +11,12 @@
using namespace Bloom::Widgets::InsightTargetWidgets::Qfp;
using namespace Bloom::Targets;
PinWidget::PinWidget(QWidget* parent, const TargetPinDescriptor& pinDescriptor, const TargetVariant& targetVariant):
TargetPinWidget(parent, pinDescriptor, targetVariant) {
PinWidget::PinWidget(
const TargetPinDescriptor& pinDescriptor,
const TargetVariant& targetVariant,
InsightWorker& insightWorker,
QWidget* parent
): TargetPinWidget(pinDescriptor, targetVariant, insightWorker, parent) {
this->layout = new QBoxLayout(QBoxLayout::TopToBottom);
this->layout->setContentsMargins(0, 0, 0, 0);
this->layout->setSpacing(0);

View File

@@ -42,9 +42,10 @@ namespace Bloom::Widgets::InsightTargetWidgets::Qfp
static const int MAXIMUM_VERTICAL_WIDTH = PinBodyWidget::WIDTH;
PinWidget(
QWidget* parent,
const Targets::TargetPinDescriptor& pinDescriptor,
const Targets::TargetVariant& targetVariant
const Targets::TargetVariant& targetVariant,
InsightWorker& insightWorker,
QWidget* parent
);
void updatePinState(const Targets::TargetPinState& pinState) override;

View File

@@ -61,7 +61,7 @@ QuadFlatPackageWidget::QuadFlatPackageWidget(
auto pinCountPerLayout = (targetVariant.pinDescriptorsByNumber.size() / 4);
for (const auto& [targetPinNumber, targetPinDescriptor]: targetVariant.pinDescriptorsByNumber) {
auto pinWidget = new PinWidget(this, targetPinDescriptor, targetVariant);
auto pinWidget = new PinWidget(targetPinDescriptor, targetVariant, insightWorker, this);
this->pinWidgets.push_back(pinWidget);
if (targetPinNumber <= pinCountPerLayout) {
@@ -76,8 +76,6 @@ QuadFlatPackageWidget::QuadFlatPackageWidget(
} else if (targetPinNumber > (pinCountPerLayout * 3) && targetPinNumber <= (pinCountPerLayout * 4)) {
this->topPinLayout->addWidget(pinWidget, 0, Qt::AlignmentFlag::AlignBottom);
}
connect(pinWidget, &TargetPinWidget::toggleIoState, insightWindow, &InsightWindow::togglePinIoState);
}
this->bodyWidget = new BodyWidget(this);

View File

@@ -0,0 +1,50 @@
#include "TargetPinWidget.hpp"
#include "src/Insight/InsightWorker/Tasks/SetTargetPinState.hpp"
using namespace Bloom;
using namespace Bloom::Widgets::InsightTargetWidgets;
using Bloom::Targets::TargetVariant;
using Bloom::Targets::TargetPinDescriptor;
using Bloom::Targets::TargetPinType;
using Bloom::Targets::TargetPinState;
TargetPinWidget::TargetPinWidget(
Targets::TargetPinDescriptor pinDescriptor,
Targets::TargetVariant targetVariant,
InsightWorker& insightWorker,
QWidget* parent
): QWidget(parent),
insightWorker(insightWorker),
targetVariant(std::move(targetVariant)),
pinDescriptor(std::move(pinDescriptor)) {
if (this->pinDescriptor.type == TargetPinType::UNKNOWN) {
this->setDisabled(true);
}
}
void TargetPinWidget::onWidgetBodyClicked() {
// Currently, we only allow users to toggle the IO state of output pins
if (this->pinState.has_value()
&& this->pinState.value().ioDirection == TargetPinState::IoDirection::OUTPUT
) {
this->setDisabled(true);
auto pinState = this->pinState.value();
pinState.ioState = (pinState.ioState == TargetPinState::IoState::HIGH) ?
TargetPinState::IoState::LOW : TargetPinState::IoState::HIGH;
auto setPinStateTask = new SetTargetPinState(this->pinDescriptor, pinState);
this->connect(setPinStateTask, &InsightWorkerTask::completed, this, [this, pinState] {
this->updatePinState(pinState);
this->setDisabled(false);
});
this->connect(setPinStateTask, &InsightWorkerTask::failed, this, [this] {
this->setDisabled(false);
});
this->insightWorker.queueTask(setPinStateTask);
}
}

View File

@@ -3,6 +3,7 @@
#include <QWidget>
#include <utility>
#include "src/Insight/InsightWorker/InsightWorker.hpp"
#include "src/Targets/TargetVariant.hpp"
#include "src/Targets/TargetPinDescriptor.hpp"
@@ -12,6 +13,8 @@ namespace Bloom::Widgets::InsightTargetWidgets
{
Q_OBJECT
protected:
InsightWorker& insightWorker;
Targets::TargetVariant targetVariant;
Targets::TargetPinDescriptor pinDescriptor;
std::optional<Targets::TargetPinState> pinState;
@@ -19,25 +22,16 @@ namespace Bloom::Widgets::InsightTargetWidgets
public:
TargetPinWidget(
QWidget* parent,
Targets::TargetPinDescriptor pinDescriptor,
Targets::TargetVariant targetVariant
): QWidget(parent), targetVariant(std::move(targetVariant)), pinDescriptor(std::move(pinDescriptor)) {
this->setDisabled(false);
};
Targets::TargetVariant targetVariant,
InsightWorker& insightWorker,
QWidget* parent
);
int getPinNumber() const {
return this->pinDescriptor.number;
}
const Targets::TargetPinDescriptor& getPinDescriptor() const {
return this->pinDescriptor;
}
std::optional<Targets::TargetPinState> getPinState() const {
return this->pinState;
}
virtual void updatePinState(const Targets::TargetPinState& pinState) {
this->pinStateChanged = !this->pinState.has_value()
|| this->pinState->ioState != pinState.ioState
@@ -46,21 +40,7 @@ namespace Bloom::Widgets::InsightTargetWidgets
this->pinState = pinState;
}
void setDisabled(bool disabled) {
if (pinDescriptor.type != Targets::TargetPinType::UNKNOWN) {
QWidget::setDisabled(disabled);
} else {
QWidget::setDisabled(true);
}
}
public slots:
void onWidgetBodyClicked() {
emit this->toggleIoState(this);
}
signals:
void toggleIoState(TargetPinWidget* pinWidget);
virtual void onWidgetBodyClicked();
};
}