From: Rikhard Kuutti Date: Fri, 5 Mar 2010 09:10:52 +0000 (+0200) Subject: Fixed a bug in calibrate function X-Git-Tag: v0.1~81^2 X-Git-Url: http://git.maemo.org/git/?p=speedfreak;a=commitdiff_plain;h=ab28a83a9c042b54aa682164aeac2e900e709fd8;hp=67d43ca29d2e4165caf0f6f8b637d71ab98281a0 Fixed a bug in calibrate function --- diff --git a/Client/accelerometer.cpp b/Client/accelerometer.cpp index 04a05ce..b2fa1d5 100644 --- a/Client/accelerometer.cpp +++ b/Client/accelerometer.cpp @@ -97,15 +97,21 @@ void Accelerometer::initValues() */ void Accelerometer::calibrate(void) { - QFile file(kFileName); - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; - unsigned int iteration = 0; - QByteArray line; - QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)"); - do { + // Opening the file must be done inside the loop + // or else the values obtained from file.readLine(); + // will be empty for all but the first iteration + QFile file(kFileName); + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + return; + } + + QByteArray line; + QRegExp rx("([0-9-]+) ([0-9-]+) ([0-9-]+)"); + // Read data, parse with regular expressions and process it line = file.readLine(); rx.indexIn(line); @@ -114,18 +120,18 @@ void Accelerometer::calibrate(void) int sampleY = rx.cap(2).toInt(); int sampleZ = rx.cap(3).toInt(); - calibrationX += sampleX; // Accumulate Samples - calibrationY += sampleY; // for all axes. - calibrationZ += sampleZ; + calibrationX = calibrationX + sampleX; // Accumulate Samples + calibrationY = calibrationY + sampleY; // for all axes. + calibrationZ = calibrationZ + sampleZ; iteration++; - } while(iteration!=1024); // 1024 times - calibrationX = calibrationX>>10; // division by 1024 + file.close(); + } while(iteration != 1024); // 1024 times + + calibrationX = calibrationX>>10; // division by 1024 calibrationY = calibrationY>>10; calibrationZ = calibrationZ>>10; - - file.close(); } /**