Kettler::Kettler(QObject *parent, QString devname) : QObject(parent), m_heartRate(0), m_power(0), m_cadence(0), m_isKettlerConnectionAlive(true) { m_kettlerConnection.setSerialPort(devname); connect(&m_kettlerConnection, SIGNAL(power(quint32)), this, SLOT(newPower(quint32)), Qt::QueuedConnection); connect(&m_kettlerConnection, SIGNAL(cadence(quint32)), this, SLOT(newCadence(quint32)), Qt::QueuedConnection); connect(&m_kettlerConnection, SIGNAL(pulse(quint32)), this, SLOT(newHeartRate(quint32)), Qt::QueuedConnection); connect(&m_kettlerConnection, SIGNAL(speed(double)), this, SLOT(newSpeed(double)), Qt::QueuedConnection); connect(&m_kettlerConnection, SIGNAL(finished()), this, SLOT(onKettlerConnectionFinished()), Qt::QueuedConnection); }
QString Lap::toString() const { QString str = QString("Total Time %1, Distance %2, Calories %3, Heartbeat %4, Max HeartBeat %5, Cadence %6\n") .arg(totalSeconds()) .arg(length()) .arg(calories()) .arg(heartBeats()) .arg(maximumHeartBeats()) .arg(cadence()); foreach ( TrackPointPtr tp, m_Points ) { str += "\t" + tp->toString() + "\n"; }
void MonarkConnection::requestCadence() { m_serial->write("pedal\r"); if (!m_serial->waitForBytesWritten(500)) { // failure to write to device, bail out this->exit(-1); } QByteArray data = readAnswer(500); m_readMutex.lock(); m_cadence = data.toInt(); m_readMutex.unlock(); emit cadence(m_cadence); }
void KettlerConnection::requestAll() { // If something else is blocking mutex, don't start another round of requests if (! m_mutex.tryLock()) return; // Discard any existing data QByteArray discarded = m_serial->readAll(); m_serial->write("st\r\n"); m_serial->waitForBytesWritten(1000); QByteArray data; bool completeReplyRead = false; bool failed = false; int maxRetries = 3; do { if (m_serial->waitForReadyRead(500)) { data.append(m_serial->readAll()); } else { failed = true; } QString dataString = QString(data); QStringList splits = dataString.split(QRegExp("\\s")); // We need to make sure the last split is 3 chars long, otherwise we // might have read a partial power value if (splits.size() >= 8 && (splits.at(7).length() >= 3)) { completeReplyRead = true; failed = false; bool ok; quint32 newHeartrate = splits.at(0).toUInt(&ok); if (ok) { emit pulse(newHeartrate); } quint32 newCadence = splits.at(1).toUInt(&ok); if (ok) { emit cadence(newCadence); } quint32 newSpeed = splits.at(2).toUInt(&ok); if (ok) { emit speed(newSpeed/10); } quint32 newPower = splits.at(7).toUInt(&ok); if (ok) { emit power(newPower); } } else if (splits.size() > 8) { qDebug() << "Kettler: Faulty sample, larger than 8 splits."; failed = true; } if (--maxRetries == 0) { failed = true; } } while ((!completeReplyRead) || failed); if ((m_loadToWrite != m_load)) { QString cmd = QString("pw %1\r\n").arg(m_loadToWrite); m_serial->write(cmd.toStdString().c_str()); if (!m_serial->waitForBytesWritten(500)) { // failure to write to device, bail out this->exit(-1); } m_load = m_loadToWrite; // Ignore reply QByteArray data = m_serial->readAll(); data.append('\0'); } m_mutex.unlock(); }