Skip to main content

[Qt] Push-ups Counter: Step1-Functionality

I made the first version of my Push-ups counter using Qt. It is based on using the accelerometer to sense the movement of the mobile. In this version:
- It has a settings screen with only one option till now: Mute Sound.
- The next screen counts the push-ups depending on some internally-set variables (the positions of up/down)
- It makes a beep when I reach the up/down position, so I know if I'm up/down enough.
= I noticed that if my arm shakes, it may count extra points.(to be fixed)




The main important points in the application are how to read the device's position and how to count.
To read the position, I read the "x" value of the accelerometer:
I use two classes: QSensor, QSensorReading

QSensor *sensor = new QSensor("QAccelerometer");
sensor->start();
QSensorReading *reading = sensor->reading();
int value = abs(reading->property("x").value<qreal>()/1);

Some notes:
- The abs() method is because the value can be +ve or -ve depending on which arm I use, so I just take the absolute value.
- The value<qreal>()/1 is a simple way of moving any noise. If I get the value as int, it will be in a big range (I can't remember it right now), and it may be useful in sensitive applications but not in this case. The range I get is 0-9. And the division by 1 is to neglect the fractions.


I added two flags to detect if the next calculation to for going up or down. An a timer to calculate the value of the accelerometer every period of time. I know that the QSensor class has a signal of readingChanged(), but it is very rapid and will exhaust the processor.

void CounterWindow::readSensorValue()
{
    int value = abs(reading->property("x").value<qreal>()/1);

    if(nextUp)
    {
        if(value <= upValue)
        {
            if(!muted) QApplication::beep();
            nextUp = false;
            nextDown = true;
            score++;
            counterLabel->setText(QString::number(score));
        }
    }
    else if(nextDown)
    {
        if(value >= downValue)
        {
            if(!muted) QApplication::beep();
            nextUp = true;
            nextDown = false;
        }
    }
}


I also had to add some Symbian code to disable the change of orientation when the mobile rotates and force the Portrait orientation.

[PushupsCounter.pro]
symbian {
    LIBS += -lcone -leikcore -lavkon
}

[main.cpp]
// Needed Symbian specific headers
#ifdef Q_OS_SYMBIAN
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif

and add this code before calling the window
[main.cpp]
StartingWindow w;
#if defined(Q_OS_SYMBIAN)
    CAknAppUi* appUi = dynamic_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi());
        if(appUi){
            QT_TRAP_THROWING(appUi ->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait));
        }
#endif
    w.showMaximized();


So... the next step is for the Look&Feel and fixing any bug that may appear.

Download v1.0



Creative Commons License
Blog Example by Mahmoud Adly Ezzat is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Comments

Popular posts from this blog

Prayer Times Calculations: Pure C++ Code

[25-March-2017] Update: For brothers and sisters in Indonesia or any country below the equator, thanks to Reza's comment ,  the code now works correctly. [18-January-2012 Update: I refined the code to make it easier and more elegant to use] [06-July-2011 Update: I found this page http://praytimes.org/calculation#Implementation ] This idea came to me when a friend of mine was asking for an 'offline database' for prayer times. I searched for algorithms to make my own database and almost all search results lead to the same algorithm. And all codes are divided into two categories: a simple but approximate one, and a complicated but precise one. I tried with the complicated one to understand it but it needs more time because of its nested functions and -unfortunately- it is not a priority right now. So I went with the easy one as a start. One thing I noticed is that every person is trying to make the ultimate use of the framework he is working on (mostly dotNet). But co...

[Qt] Custom QListView delegate with word wrap

Using QListView with a custom item layout has been a very popular demand by many developers. The problem with using  a custom delegate was having some basic features like word wrap depending on the allowed list size. Here is a screenshot from a project prototype I've been working on. The text will wrap around to fit in multiple lines depending on text length and list view width. My custom delegate is about using two important methods: QFontMetrics::boundingRect() and QPainter::drawText() . The first method will return the minimum Rect needed to draw the given text using the given origin and font. The second method draws the given text within the given Rect. Note that the flag Qt::TextWordWrap is given to both methods when called. The code is simple: Get system default font. Process the font to suite your needs. In my case: I have a normal font and another bold one for the header. Create a QFontMetrics instance for each font. Use the QFontMetrics instances to cal...

Qt Folder Compression

I needed a compression module that does not depend on anything but Qt, and I mean a module to compress a complete folder with its children. This is because I wanted the compression module to work on any platform supported by Qt with no other dependencies. So I made this modules that only uses only qCompress() & qUncompress() methods plus some logic. It is mainly a recursive algorithm that serializes data into a single file, each with its relative path to the parent folder. And deserializing is just taking binary data, creating needed subfolders, and saving the file. Download: QtFolderCompressor.zip [FolderCompressor.h] #ifndef FOLDERCOMPRESSOR_H #define FOLDERCOMPRESSOR_H #include <QFile> #include <QObject> #include <QDir> class FolderCompressor : public QObject { Q_OBJECT public: explicit FolderCompressor(QObject *parent = 0); //A recursive function that scans all files inside the source folder //and serializes all files in a row of f...