Skip to content

Commit

Permalink
Save and load options in dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
BugLight committed Nov 16, 2020
1 parent 4bbba2e commit a24f2c7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 33 deletions.
8 changes: 8 additions & 0 deletions src/candiagnostictool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) {
canBusView = new CanBusView(leftColumn);
leftColumnLayout->addWidget(canBusView);

options.SetRequestCanId(0);
options.SetResponseCanId(1);
options.SetTimeout(1000);
options.SetMaxRetries(5);

diagnosticView = new DiagnosticView(central);
diagnosticView->SetProtocolOptions(options);
mainLayout->addWidget(diagnosticView, 0, 1);

connect(canBusView, &CanBusView::BusConnected, diagnosticView,
Expand All @@ -52,7 +58,9 @@ CanDiagnosticTool::CanDiagnosticTool(QWidget* parent) : QMainWindow(parent) {

void CanDiagnosticTool::ExecOptionsDialog() {
OptionsDialog dialog(this);
dialog.SetOptions(options);
connect(&dialog, &OptionsDialog::OptionsConfigured, diagnosticView,
&DiagnosticView::SetProtocolOptions);
dialog.exec();
options = dialog.GetOptions();
}
2 changes: 2 additions & 0 deletions src/candiagnostictool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QWidget>

#include <canbusview.h>
#include <diagnosticprotocol.h>
#include <diagnosticview.h>

class CanDiagnosticTool : public QMainWindow {
Expand All @@ -20,6 +21,7 @@ class CanDiagnosticTool : public QMainWindow {
void ExecOptionsDialog();

private:
DiagnosticProtocolOptions options;
QAction* readAction;
QAction* resetAction;
QAction* searchAction;
Expand Down
19 changes: 11 additions & 8 deletions src/diagnosticprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ DiagnosticProtocol::DiagnosticProtocol(

void DiagnosticProtocol::SetCanFilter() {
if (!canBusDevicePtr_) return;
// Óñòàíîâêà ôèëüòðà ïî èäåíòèôèêàòîðó ñîîáùåíèé ïðîòîêîëà
QList<QCanBusDevice::Filter> filterList;
QCanBusDevice::Filter filter;
filter.frameId = options_.GetResponseCanId();
filter.frameIdMask = 0x7fffffff;
filter.format = QCanBusDevice::Filter::MatchBaseFormat;
filter.frameIdMask = 0x1fffffffu;
filter.format = QCanBusDevice::Filter::MatchBaseAndExtendedFormat;
filter.type = QCanBusFrame::DataFrame;
canBusDevicePtr_->setConfigurationParameter(
QCanBusDevice::RawFilterKey,
QVariant::fromValue(QList<QCanBusDevice::Filter>{filter}));
filterList.append(filter);
canBusDevicePtr_->setConfigurationParameter(QCanBusDevice::RawFilterKey,
QVariant::fromValue(filterList));
}

QByteArray DiagnosticProtocol::SendRequest(DiagnosticRequestCode code) const {
Expand All @@ -34,8 +34,11 @@ QByteArray DiagnosticProtocol::SendRequest(DiagnosticRequestCode code) const {
QCanBusFrame(options_.GetRequestCanId(), QByteArray(1, currentCode)));
if (!canBusDevicePtr_->waitForFramesReceived(options_.GetTimeout())) break;
while (canBusDevicePtr_->framesAvailable() > 0) {
auto payload = canBusDevicePtr_->readFrame().payload();
if (payload.startsWith(currentCode)) return payload.remove(0, 1);
auto frame = canBusDevicePtr_->readFrame();
if (frame.frameId() == options_.GetResponseCanId()) {
auto payload = frame.payload();
if (payload.startsWith(currentCode)) return payload.remove(0, 1);
}
}
++retries;
} while (retries <= options_.GetMaxRetries());
Expand Down
18 changes: 8 additions & 10 deletions src/diagnosticprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <QCanBus>
#include <QList>

#include <exception>
#include <memory>

Expand All @@ -11,15 +10,15 @@

class DiagnosticProtocolOptions {
public:
std::uint32_t GetRequestCanId() const noexcept { return requestCanId_; }
quint32 GetRequestCanId() const noexcept { return requestCanId_; }

void SetRequestCanId(std::uint32_t requestCanId) noexcept {
void SetRequestCanId(quint32 requestCanId) noexcept {
requestCanId_ = requestCanId;
}

std::uint32_t GetResponseCanId() const noexcept { return responseCanId_; }
quint32 GetResponseCanId() const noexcept { return responseCanId_; }

void SetResponseCanId(std::uint32_t responseCanId) noexcept {
void SetResponseCanId(quint32 responseCanId) noexcept {
responseCanId_ = responseCanId;
}

Expand All @@ -32,8 +31,8 @@ class DiagnosticProtocolOptions {
void SetMaxRetries(int maxRetries) noexcept { maxRetries_ = maxRetries; }

private:
std::uint32_t requestCanId_;
std::uint32_t responseCanId_;
quint32 requestCanId_;
quint32 responseCanId_;
int timeout_;
int maxRetries_;
};
Expand Down Expand Up @@ -71,13 +70,12 @@ class DiagnosticProtocol {

void ResetErrors() const;

void SetCanBusDevice(
std::shared_ptr<QCanBusDevice> canBusDevicePtr) noexcept {
void SetCanBusDevice(std::shared_ptr<QCanBusDevice> canBusDevicePtr) {
canBusDevicePtr_ = canBusDevicePtr;
SetCanFilter();
}

void SetOptions(const DiagnosticProtocolOptions& options) noexcept {
void SetOptions(const DiagnosticProtocolOptions& options) {
options_ = options;
SetCanFilter();
}
Expand Down
10 changes: 1 addition & 9 deletions src/diagnosticview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ DiagnosticView::DiagnosticView(QWidget* parent) : QWidget(parent) {
errorsAreaLayout->setAlignment(Qt::Alignment(Qt::AlignTop));
errorsAreaContents->setLayout(errorsAreaLayout);

DiagnosticProtocolOptions options;
options.SetRequestCanId(0x0);
options.SetResponseCanId(0x1);
options.SetMaxRetries(5);
options.SetTimeout(1000);
protocol.SetOptions(options);
protocol.SetErrorsList(JsonErrorsReader().ReadErrorsList(QStringLiteral("device_errors.json")));
}

Expand All @@ -50,8 +44,6 @@ void DiagnosticView::SetCanBusDevice(

void DiagnosticView::SetProtocolOptions(const DiagnosticProtocolOptions& options) {
protocol.SetOptions(options);
SearchDevice();
ReadErrors();
}

void DiagnosticView::SearchDevice() {
Expand All @@ -68,8 +60,8 @@ void DiagnosticView::SearchDevice() {

void DiagnosticView::ReadErrors() {
try {
auto errors = protocol.ReadErrors();
ClearErrorsArea();
auto errors = protocol.ReadErrors();
for (const auto& error : errors) {
errorsAreaLayout->addWidget(
new DeviceErrorView(error, errorsAreaContents));
Expand Down
20 changes: 14 additions & 6 deletions src/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ OptionsDialog::OptionsDialog(QWidget* parent) : QDialog(parent) {
connect(buttonOk, &QPushButton::clicked, this, &OptionsDialog::accept);
}

void OptionsDialog::SetOptions(
const DiagnosticProtocolOptions& options) noexcept {
options_ = options;
editRequestId->setText(QString::number(options_.GetRequestCanId(), 16));
editResponseId->setText(QString::number(options_.GetResponseCanId(), 16));
spinTimeout->setValue(options_.GetTimeout());
spinMaxRetries->setValue(options_.GetMaxRetries());
}

void OptionsDialog::accept() {
DiagnosticProtocolOptions options;
options.SetRequestCanId(editRequestId->text().toUInt());
options.SetResponseCanId(editResponseId->text().toUInt());
options.SetTimeout(spinTimeout->value());
options.SetMaxRetries(spinMaxRetries->value());
emit OptionsConfigured(options);
options_.SetRequestCanId(editRequestId->text().toUInt(nullptr, 16));
options_.SetResponseCanId(editResponseId->text().toUInt(nullptr, 16));
options_.SetTimeout(spinTimeout->value());
options_.SetMaxRetries(spinMaxRetries->value());
emit OptionsConfigured(options_);
QDialog::accept();
}
5 changes: 5 additions & 0 deletions src/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ class OptionsDialog : public QDialog {
public:
OptionsDialog(QWidget *parent = Q_NULLPTR);

void SetOptions(const DiagnosticProtocolOptions& options) noexcept;

DiagnosticProtocolOptions GetOptions() const noexcept { return options_; }

void accept() override;

signals:
void OptionsConfigured(const DiagnosticProtocolOptions&);

private:
DiagnosticProtocolOptions options_;
HexValidator* hexValidator;
QFormLayout* formLayout;
QLineEdit* editRequestId;
Expand Down

0 comments on commit a24f2c7

Please sign in to comment.