Polish up on classes

This patch polishes up on all classes added for dive computer
configuration to give a clean workflow. The classes can now
write and read data from the OSTC 3.

Signed-off-by: Joseph W. Joshua <joejoshw@gmail.com>
Signed-off-by: Thiago Macieira <thiago@macieira.org>
This commit is contained in:
Joseph W. Joshua 2014-06-10 18:25:25 +03:00 committed by Thiago Macieira
parent 4fc16b1674
commit 20eb62a98a
7 changed files with 150 additions and 60 deletions

View file

@ -4,31 +4,42 @@
ConfigureDiveComputer::ConfigureDiveComputer(QObject *parent) :
QObject(parent),
readThread(0),
writeThread(0),
m_deviceDetails(0)
writeThread(0)
{
setState(INITIAL);
}
void ConfigureDiveComputer::readSettings(DeviceDetails *deviceDetails, device_data_t *data)
void ConfigureDiveComputer::readSettings(device_data_t *data)
{
setState(READING);
m_deviceDetails = deviceDetails;
if (readThread)
readThread->deleteLater();
readThread = new ReadSettingsThread(this, deviceDetails, data);
readThread = new ReadSettingsThread(this, data);
connect (readThread, SIGNAL(finished()),
this, SLOT(readThreadFinished()), Qt::QueuedConnection);
connect (readThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
connect (readThread, SIGNAL(devicedetails(DeviceDetails*)), this,
SIGNAL(deviceDetailsChanged(DeviceDetails*)));
readThread->start();
}
void ConfigureDiveComputer::saveDeviceDetails()
void ConfigureDiveComputer::saveDeviceDetails(DeviceDetails *details, device_data_t *data)
{
setState(WRITING);
if (writeThread)
writeThread->deleteLater();
writeThread = new WriteSettingsThread(this, data);
connect (writeThread, SIGNAL(finished()),
this, SLOT(writeThreadFinished()), Qt::QueuedConnection);
connect (writeThread, SIGNAL(error(QString)), this, SLOT(setError(QString)));
writeThread->setDeviceDetails(details);
writeThread->start();
}
void ConfigureDiveComputer::setState(ConfigureDiveComputer::states newState)

View file

@ -13,7 +13,7 @@ class ConfigureDiveComputer : public QObject
Q_OBJECT
public:
explicit ConfigureDiveComputer(QObject *parent = 0);
void readSettings(DeviceDetails *deviceDetails, device_data_t *data);
void readSettings(device_data_t *data);
enum states {
INITIAL,
@ -27,9 +27,8 @@ public:
QString lastError;
states currentState;
DeviceDetails *m_deviceDetails;
device_data_t *m_data;
void saveDeviceDetails();
void saveDeviceDetails(DeviceDetails *details, device_data_t *data);
void fetchDeviceDetails();
signals:
@ -38,6 +37,7 @@ signals:
void readFinished();
void writeFinished();
void stateChanged(states newState);
void deviceDetailsChanged(DeviceDetails *newDetails);
private:
ReadSettingsThread *readThread;

View file

@ -8,6 +8,7 @@
#include "../divelist.h"
#include "configuredivecomputer.h"
#include <QInputDialog>
#include <QDebug>
struct product {
const char *product;
@ -43,6 +44,8 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(QWidget *parent) :
connect (config, SIGNAL(error(QString)), this, SLOT(configError(QString)));
connect (config, SIGNAL(message(QString)), this, SLOT(configMessage(QString)));
connect (config, SIGNAL(readFinished()), this, SLOT(deviceReadFinished()));
connect (config, SIGNAL(deviceDetailsChanged(DeviceDetails*)),
this, SLOT(deviceDetailsReceived(DeviceDetails*)));
fill_computer_list();
@ -159,7 +162,7 @@ void ConfigureDiveComputerDialog::readSettings()
ui->errorLabel->clear();
getDeviceData();
config->readSettings(deviceDetails, &device_data);
config->readSettings(&device_data);
}
void ConfigureDiveComputerDialog::configMessage(QString msg)
@ -183,8 +186,6 @@ void ConfigureDiveComputerDialog::getDeviceData()
set_default_dive_computer(device_data.vendor, device_data.product);
set_default_dive_computer_device(device_data.devname);
//deviceDetails->setData(&device_data);
}
void ConfigureDiveComputerDialog::on_cancel_clicked()
@ -194,11 +195,32 @@ void ConfigureDiveComputerDialog::on_cancel_clicked()
void ConfigureDiveComputerDialog::deviceReadFinished()
{
ui->brightnessComboBox->setCurrentIndex(config->m_deviceDetails->brightness());
}
void ConfigureDiveComputerDialog::on_saveSettingsPushButton_clicked()
{
config->saveDeviceDetails();
deviceDetails->setBrightness(ui->brightnessComboBox->currentIndex());
deviceDetails->setLanguage(ui->languageComboBox->currentIndex());
deviceDetails->setDateFormat(ui->dateFormatComboBox->currentIndex());
deviceDetails->setCustomText(ui->customTextLlineEdit->text());
getDeviceData();
config->saveDeviceDetails(deviceDetails, &device_data);
}
void ConfigureDiveComputerDialog::deviceDetailsReceived(DeviceDetails *newDeviceDetails)
{
deviceDetails = newDeviceDetails;
reloadValues();
}
void ConfigureDiveComputerDialog::reloadValues()
{
ui->serialNoLineEdit->setText(deviceDetails->serialNo());
ui->firmwareVersionLineEdit->setText(deviceDetails->firmwareVersion());
ui->customTextLlineEdit->setText(deviceDetails->customText());
ui->brightnessComboBox->setCurrentIndex(deviceDetails->brightness());
ui->languageComboBox->setCurrentIndex(deviceDetails->language());
ui->dateFormatComboBox->setCurrentIndex(deviceDetails->dateFormat());
}

View file

@ -29,6 +29,8 @@ private slots:
void on_cancel_clicked();
void deviceReadFinished();
void on_saveSettingsPushButton_clicked();
void deviceDetailsReceived(DeviceDetails *newDeviceDetails);
void reloadValues();
private:
Ui::ConfigureDiveComputerDialog *ui;

View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>343</width>
<height>365</height>
<height>390</height>
</rect>
</property>
<property name="windowTitle">
@ -93,12 +93,8 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Brightness:</string>
</property>
</widget>
<item row="2" column="1">
<widget class="QLineEdit" name="customTextLlineEdit"/>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="brightnessComboBox">
@ -119,10 +115,10 @@
</item>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="serialNoLineEdit">
<property name="readOnly">
<bool>true</bool>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Firmware Version:</string>
</property>
</widget>
</item>
@ -150,20 +146,17 @@
</item>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="customTextLlineEdit"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Language:</string>
<string>Brightness:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Serial No.</string>
<item row="0" column="1">
<widget class="QLineEdit" name="serialNoLineEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
@ -174,10 +167,17 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Firmware Version:</string>
<string>Serial No.</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Language:</string>
</property>
</widget>
</item>
@ -188,6 +188,32 @@
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Date Format:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="dateFormatComboBox">
<item>
<property name="text">
<string>MMDDYY</string>
</property>
</item>
<item>
<property name="text">
<string>DDMMYY</string>
</property>
</item>
<item>
<property name="text">
<string>YYMMDD</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>

View file

@ -2,8 +2,8 @@
#include "libdivecomputer/hw.h"
#include <QDebug>
ReadSettingsThread::ReadSettingsThread(QObject *parent, DeviceDetails *deviceDetails, device_data_t *data)
: QThread(parent), m_deviceDetails(deviceDetails), m_data(data)
ReadSettingsThread::ReadSettingsThread(QObject *parent, device_data_t *data)
: QThread(parent), m_data(data)
{
}
@ -14,6 +14,7 @@ void ReadSettingsThread::run()
dc_status_t rc;
rc = rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
if (rc == DC_STATUS_SUCCESS) {
DeviceDetails *m_deviceDetails = new DeviceDetails(0);
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_HW_OSTC3:
supported = true;
@ -25,7 +26,8 @@ void ReadSettingsThread::run()
m_deviceDetails->setLanguage(0);
m_deviceDetails->setLastDeco(0);
m_deviceDetails->setSerialNo("");
unsigned char uData[1];
//Read general settings
unsigned char uData[1] = {0};
rc = hw_ostc3_device_config_read(m_data->device, 0x2D, uData, sizeof(uData));
if (rc == DC_STATUS_SUCCESS)
m_deviceDetails->setBrightness(uData[0]);
@ -35,6 +37,20 @@ void ReadSettingsThread::run()
rc = hw_ostc3_device_config_read(m_data->device, 0x33, uData, sizeof(uData));
if (rc == DC_STATUS_SUCCESS)
m_deviceDetails->setDateFormat(uData[0]);
//read firmware settings
unsigned char fData[64] = {0};
rc = hw_ostc3_device_version (m_data->device, fData, sizeof (fData));
if (rc == DC_STATUS_SUCCESS) {
int serial = fData[0] + (fData[1] << 8);
m_deviceDetails->setSerialNo(QString::number(serial));
int fw = (fData[2] << 8) + fData[3];
m_deviceDetails->setFirmwareVersion(QString::number(fw));
QByteArray ar((char *)fData + 4, 60);
m_deviceDetails->setCustomText(ar.trimmed());
}
emit devicedetails(m_deviceDetails);
break;
}
@ -51,31 +67,46 @@ void ReadSettingsThread::run()
}
}
WriteSettingsThread::WriteSettingsThread(QObject *parent, DeviceDetails *deviceDetails, QString settingName, QVariant settingValue)
: QThread(parent), m_deviceDetails(deviceDetails), m_settingName(settingName), m_settingValue(settingValue)
{
WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data)
: QThread(parent), m_data(data) {
}
void WriteSettingsThread::setDeviceDetails(DeviceDetails *details)
{
m_deviceDetails = details;
}
void WriteSettingsThread::run()
{
bool supported = false;
dc_status_t rc;
switch (dc_device_get_type(data->device)) {
case DC_FAMILY_HW_OSTC3:
rc = dc_device_open(&data->device, data->context, data->descriptor, data->devname);
rc = rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
if (rc == DC_STATUS_SUCCESS) {
} else {
lastError = tr("Could not a establish connection to the dive computer.");
emit error(lastError);
}
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_HW_OSTC3:
supported = true;
//write general settings
hw_ostc3_device_customtext(m_data->device, m_deviceDetails->customText().toUtf8().data());
unsigned char data[1] = {0};
data[0] = m_deviceDetails->brightness();
hw_ostc3_device_config_write(m_data->device, 0x2D, data, sizeof(data));
data[0] = m_deviceDetails->language();
hw_ostc3_device_config_write(m_data->device, 0x32, data, sizeof(data));
data[0] = m_deviceDetails->dateFormat();
hw_ostc3_device_config_write(m_data->device, 0x33, data, sizeof(data));
break;
}
dc_device_close(m_data->device);
if (!supported) {
lastError = tr("This feature is not yet available for the selected dive computer.");
emit error(lastError);
}
}
else {
lastError = tr("Could not a establish connection to the dive computer.");
emit error(lastError);
}
}

View file

@ -11,31 +11,29 @@
class ReadSettingsThread : public QThread {
Q_OBJECT
public:
ReadSettingsThread(QObject *parent, DeviceDetails *deviceDetails, device_data_t *data);
ReadSettingsThread(QObject *parent, device_data_t *data);
virtual void run();
QString result;
QString lastError;
signals:
void error(QString err);
void devicedetails(DeviceDetails *newDeviceDetails);
private:
DeviceDetails *m_deviceDetails;
device_data_t *m_data;
};
class WriteSettingsThread : public QThread {
Q_OBJECT
public:
WriteSettingsThread(QObject *parent, DeviceDetails *deviceDetails, QString settingName, QVariant settingValue);
WriteSettingsThread(QObject *parent, device_data_t *data);
void setDeviceDetails(DeviceDetails *details);
virtual void run();
QString result;
QString lastError;
signals:
void error(QString err);
private:
device_data_t *data;
QString m_settingName;
QVariant m_settingValue;
device_data_t *m_data;
DeviceDetails *m_deviceDetails;
};