From a7c9b25b053ee77a816e1555f1c00c3e4b996396 Mon Sep 17 00:00:00 2001 From: "Joseph W. Joshua" Date: Fri, 30 May 2014 09:56:27 +0300 Subject: Read basic details from dive computer Added classes for reading data from dive computer. This is at the basic level and I will expand it as I go along. Signed-off-by: Joseph W. Joshua Signed-off-by: Thiago Macieira --- qt-ui/configuredivecomputer.cpp | 74 +++++++++++++++++++++++++++++++++++ qt-ui/configuredivecomputer.h | 58 +++++++++++++++++++++++++++ qt-ui/configuredivecomputerdialog.cpp | 48 +++++++++++++++++++++++ qt-ui/configuredivecomputerdialog.h | 11 ++++++ qt-ui/configuredivecomputerdialog.ui | 17 ++++++++ 5 files changed, 208 insertions(+) create mode 100644 qt-ui/configuredivecomputer.cpp create mode 100644 qt-ui/configuredivecomputer.h (limited to 'qt-ui') diff --git a/qt-ui/configuredivecomputer.cpp b/qt-ui/configuredivecomputer.cpp new file mode 100644 index 000000000..d02ceffb7 --- /dev/null +++ b/qt-ui/configuredivecomputer.cpp @@ -0,0 +1,74 @@ +#include "configuredivecomputer.h" +#include "libdivecomputer/hw.h" +#include +ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) : + QObject(parent), + readThread(0) +{ + setState(INITIAL); +} + +void ConfigureDiveComputer::readSettings(device_data_t *data) +{ + setState(READING); + + if (readThread) + readThread->deleteLater(); + + readThread = new ReadSettingsThread(this, data); + connect (readThread, SIGNAL(finished()), + this, SLOT(readThreadFinished()), Qt::QueuedConnection); + connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString))); + + readThread->start(); +} + +void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState) +{ + currentState = newState; + emit stateChanged(currentState); +} + +void ConfigureDiveComputer::setError(QString err) +{ + lastError = err; + emit error(err); +} + +void ConfigureDiveComputer::readHWSettings(device_data_t *data) +{ + +} + +void ConfigureDiveComputer::readThreadFinished() +{ + setState(DONE); + emit deviceSettings(readThread->result); +} + +ReadSettingsThread::ReadSettingsThread(QObject *parent, device_data_t *data) + : QThread(parent), data(data) +{ + +} + +void ReadSettingsThread::run() +{ + QString vendor = data->vendor; + dc_status_t rc; + rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname); + if (rc == DC_STATUS_SUCCESS) { + if (vendor.trimmed() == "Heinrichs Weikamp") { + unsigned char hw_data[10]; + hw_frog_device_version(data->device, hw_data, 10); + QTextStream (&result) << "Device Version: " << hw_data; //just a test. I will work on decoding this + } else { + lastError = tr("This feature is not yet available for the selected dive computer."); + emit error(lastError); + } + dc_device_close(data->device); + } else { + lastError = tr("Could not a establish connection to the dive computer."); + emit error(lastError); + } +} diff --git a/qt-ui/configuredivecomputer.h b/qt-ui/configuredivecomputer.h new file mode 100644 index 000000000..8441c0cec --- /dev/null +++ b/qt-ui/configuredivecomputer.h @@ -0,0 +1,58 @@ +#ifndef CONFIGUREDIVECOMPUTER_H +#define CONFIGUREDIVECOMPUTER_H + +#include +#include +#include "libdivecomputer.h" + +class ReadSettingsThread : public QThread { + Q_OBJECT +public: + ReadSettingsThread(QObject *parent, device_data_t *data); + virtual void run(); + QString result; + QString lastError; +signals: + void error(QString err); +private: + device_data_t *data; +}; + +class ConfigureDiveComputer : public QObject +{ + Q_OBJECT +public: + explicit ConfigureDiveComputer(QObject *parent = 0); + void readSettings(device_data_t *data); + + enum states { + INITIAL, + READING, + WRITING, + CANCELLING, + CANCELLED, + ERROR, + DONE, + }; + + QString lastError; + states currentState; +signals: + void deviceSettings(QString settings); + void message(QString msg); + void error(QString err); + void readFinished(); + void writeFinished(); + void stateChanged(states newState); +private: + ReadSettingsThread *readThread; + void setState(states newState); + + + void readHWSettings(device_data_t *data); +private slots: + void readThreadFinished(); + void setError(QString err); +}; + +#endif // CONFIGUREDIVECOMPUTER_H diff --git a/qt-ui/configuredivecomputerdialog.cpp b/qt-ui/configuredivecomputerdialog.cpp index 0af50b755..53544575c 100644 --- a/qt-ui/configuredivecomputerdialog.cpp +++ b/qt-ui/configuredivecomputerdialog.cpp @@ -6,6 +6,7 @@ #include "../helpers.h" #include "../display.h" #include "../divelist.h" +#include "configuredivecomputer.h" struct product { const char *product; dc_descriptor_t *descriptor; @@ -28,11 +29,17 @@ struct mydescriptor { ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ConfigureDiveComputerDialog), + config(0), vendorModel(0), productModel(0) { ui->setupUi(this); + config = new ConfigureDiveComputer(this); + connect (config, SIGNAL(error(QString)), this, SLOT(configError(QString))); + connect (config, SIGNAL(message(QString)), this, SLOT(configMessage(QString))); + connect (config, SIGNAL(deviceSettings(QString)), ui->availableDetails, SLOT(setText(QString))); + fill_computer_list(); vendorModel = new QStringListModel(vendorList); @@ -46,6 +53,10 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) : } if (default_dive_computer_device) ui->device->setEditText(default_dive_computer_device); + + memset(&device_data, 0, sizeof(device_data)); + + connect (ui->retrieveDetails, SIGNAL(clicked()), this, SLOT(readSettings())); } ConfigureDiveComputerDialog::~ConfigureDiveComputerDialog() @@ -137,3 +148,40 @@ void ConfigureDiveComputerDialog::on_product_currentIndexChanged(const QString & ui->device->setEnabled(false); } } + +void ConfigureDiveComputerDialog::readSettings() +{ + ui->statusLabel->clear(); + ui->errorLabel->clear(); + + getDeviceData(); + config->readSettings(&device_data); +} + +void ConfigureDiveComputerDialog::configMessage(QString msg) +{ + ui->statusLabel->setText(msg); +} + +void ConfigureDiveComputerDialog::configError(QString err) +{ + ui->errorLabel->setText(err); +} + +void ConfigureDiveComputerDialog::getDeviceData() +{ + device_data.devname = strdup(ui->device->currentText().toUtf8().data()); + device_data.vendor = strdup(ui->vendor->currentText().toUtf8().data()); + device_data.product = strdup(ui->product->currentText().toUtf8().data()); + + device_data.descriptor = descriptorLookup[ui->vendor->currentText() + ui->product->currentText()]; + device_data.deviceid = device_data.diveid = 0; + + set_default_dive_computer(device_data.vendor, device_data.product); + set_default_dive_computer_device(device_data.devname); +} + +void ConfigureDiveComputerDialog::on_cancel_clicked() +{ + this->close(); +} diff --git a/qt-ui/configuredivecomputerdialog.h b/qt-ui/configuredivecomputerdialog.h index 4f4fbb36c..116968b79 100644 --- a/qt-ui/configuredivecomputerdialog.h +++ b/qt-ui/configuredivecomputerdialog.h @@ -4,6 +4,9 @@ #include #include #include "../libdivecomputer.h" + +class ConfigureDiveComputer; + namespace Ui { class ConfigureDiveComputerDialog; } @@ -21,9 +24,17 @@ private slots: void on_product_currentIndexChanged(const QString &product); + void readSettings(); + void configMessage(QString msg); + void configError(QString err); + void on_cancel_clicked(); private: Ui::ConfigureDiveComputerDialog *ui; + ConfigureDiveComputer *config; + device_data_t device_data; + void getDeviceData(); + QStringList vendorList; QHash productList; QHash descriptorLookup; diff --git a/qt-ui/configuredivecomputerdialog.ui b/qt-ui/configuredivecomputerdialog.ui index 25d63007f..fe36512b2 100644 --- a/qt-ui/configuredivecomputerdialog.ui +++ b/qt-ui/configuredivecomputerdialog.ui @@ -94,6 +94,23 @@ + + + + color: rgb(242, 19, 25); + + + + + + + + + + + + + -- cgit v1.2.3-70-g09d2