move calc of d/l status str to PowerTapDevice

...thereby making DownloadRideDialog a little more device-agnostic.
This commit is contained in:
Sean Rhea
2009-08-09 12:43:40 -07:00
parent 563285ab9d
commit 356ee341b2
4 changed files with 33 additions and 48 deletions

View File

@@ -30,7 +30,7 @@
DownloadRideDialog::DownloadRideDialog(MainWindow *mainWindow,
const QDir &home) :
mainWindow(mainWindow), home(home), cancelled(false),
downloadInProgress(false), recIntSecs(0.0), endingOffset(0)
downloadInProgress(false)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle("Download Ride Data");
@@ -117,39 +117,9 @@ DownloadRideDialog::scanCommPorts()
}
bool
DownloadRideDialog::statusCallback(PowerTapDevice::State state)
DownloadRideDialog::statusCallback(const QString &statusText)
{
if (state == PowerTapDevice::STATE_READING_VERSION)
label->setText("Reading version...");
else if (state == PowerTapDevice::STATE_READING_HEADER)
label->setText(label->text() + "done.\nReading header...");
else if (state == PowerTapDevice::STATE_READING_DATA) {
label->setText(label->text() + "done.\nReading ride data...\n");
endingOffset = label->text().length();
}
else {
assert(state == PowerTapDevice::STATE_DATA_AVAILABLE);
unsigned char *buf = records.data();
bool bIsVer81 = PowerTapUtil::is_Ver81(buf);
if (recIntSecs == 0.0) {
for (int i = 0; i < records.size(); i += 6) {
if (PowerTapUtil::is_config(buf + i, bIsVer81)) {
unsigned unused1, unused2, unused3;
PowerTapUtil::unpack_config(buf + i, &unused1, &unused2,
&recIntSecs, &unused3,
bIsVer81);
}
}
}
if (recIntSecs != 0.0) {
int min = (int) round(records.size() / 6 * recIntSecs);
QString existing = label->text();
existing.chop(existing.size() - endingOffset);
existing.append(QString("Ride data read: %1:%2").arg(min / 60)
.arg(min % 60, 2, 10, QLatin1Char('0')));
label->setText(existing);
}
}
label->setText(statusText);
QCoreApplication::processEvents();
return !cancelled;
}

View File

@@ -34,7 +34,7 @@ class DownloadRideDialog : public QDialog
DownloadRideDialog(MainWindow *mainWindow, const QDir &home);
void downloadFinished();
bool statusCallback(PowerTapDevice::State state);
bool statusCallback(const QString &statusText);
private slots:
void downloadClicked();
@@ -52,8 +52,6 @@ class DownloadRideDialog : public QDialog
QVector<CommPortPtr> devList;
bool cancelled, downloadInProgress;
double recIntSecs;
int endingOffset;
QVector<unsigned char> records;
};

View File

@@ -115,7 +115,9 @@ PowerTapDevice::download(CommPortPtr dev, QByteArray &version,
if (!doWrite(dev, 0x56, false, err)) // 'V'
return false;
if (!statusCallback(STATE_READING_VERSION)) {
QString cbtext = "Reading version...";
if (!statusCallback(cbtext)) {
err = "download cancelled";
return false;
}
@@ -144,7 +146,8 @@ PowerTapDevice::download(CommPortPtr dev, QByteArray &version,
bool hwecho = version.indexOf('V') < veridx;
if (PT_DEBUG) printf("hwecho=%s\n", hwecho ? "true" : "false");
if (!statusCallback(STATE_READING_HEADER)) {
cbtext += "done.\nReading header...";
if (!statusCallback(cbtext)) {
err = "download cancelled";
return false;
}
@@ -168,10 +171,13 @@ PowerTapDevice::download(CommPortPtr dev, QByteArray &version,
for (size_t i = 0; i < sizeof(header); ++i)
records.append(header[i]);
if (!statusCallback(STATE_READING_DATA)) {
cbtext += "done.\nReading ride data...\n";
if (!statusCallback(cbtext)) {
err = "download cancelled";
return false;
}
int cbtextlen = cbtext.length();
double recIntSecs = 0.0;
fflush(stdout);
while (true) {
@@ -218,7 +224,25 @@ PowerTapDevice::download(CommPortPtr dev, QByteArray &version,
if (PT_DEBUG) printf("good checksum\n");
for (size_t i = 0; i < sizeof(buf) - 1; ++i)
records.append(buf[i]);
if (!statusCallback(STATE_DATA_AVAILABLE)) {
if (recIntSecs == 0.0) {
unsigned char *data = records.data();
bool bIsVer81 = PowerTapUtil::is_Ver81(data);
for (int i = 0; i < records.size(); i += 6) {
if (PowerTapUtil::is_config(data + i, bIsVer81)) {
unsigned unused1, unused2, unused3;
PowerTapUtil::unpack_config(
data + i, &unused1, &unused2,
&recIntSecs, &unused3, bIsVer81);
}
}
}
if (recIntSecs != 0.0) {
int min = (int) round(records.size() / 6 * recIntSecs);
cbtext.chop(cbtext.size() - cbtextlen);
cbtext.append(QString("Ride data read: %1:%2").arg(min / 60)
.arg(min % 60, 2, 10, QLatin1Char('0')));
}
if (!statusCallback(cbtext)) {
err = "download cancelled";
return false;
}

View File

@@ -24,14 +24,7 @@
struct PowerTapDevice
{
enum State {
STATE_READING_VERSION,
STATE_READING_HEADER,
STATE_READING_DATA,
STATE_DATA_AVAILABLE
};
typedef boost::function<bool (State state)> StatusCallback;
typedef boost::function<bool (const QString &statusText)> StatusCallback;
static bool download(CommPortPtr dev, QByteArray &version,
QVector<unsigned char> &records,