Reads rotation sensor on timer instead whenever new reading arrives
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Fri, 21 Oct 2011 11:30:37 +0000 (14:30 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Fri, 21 Oct 2011 11:30:37 +0000 (14:30 +0300)
No longer vulnerable to other applications changing the sensor speed.
Speed has changed, needs calibrating back to what it was.
Bug: Saw the ship hopping.
Using animation no longer makes sense, needs to be removed (perhaps the
cause of the hop too...)

orientationcontrolledgraphicspixmapobject.cpp
orientationcontrolledgraphicspixmapobject.h

index 3c71e87..d5bc388 100644 (file)
@@ -36,15 +36,12 @@ OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapOb
     QObject(), QGraphicsPixmapItem (pixmap, parent)
 {
 
-    connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
+    rotationReadingInitialized_ = false;
+    rotationReadingTimer_.setInterval(100);
+    connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(rotationSensorReady()));
+
 
-//    qrangelist rangelist = rotationSensor_.availableDataRates();
 
-//    qDebug() << rangelist.length() << "ranges found";
-//    foreach (qrange range, rangelist)
-//    {
-//        qDebug() << "Rotation sensor: " << range.first <<", " << "range.second";
-//    }
 }
 
 void OrientationControlledGraphicsPixmapObject::startMoving()
@@ -58,6 +55,8 @@ void OrientationControlledGraphicsPixmapObject::startMoving()
 void OrientationControlledGraphicsPixmapObject::stopMoving()
 {
     rotationSensor_.stop();
+    rotationReadingInitialized_ = false;
+    rotationReadingTimer_.stop();
 //    qDebug () << "trying to stop the sensor";
 }
 
@@ -66,6 +65,17 @@ void OrientationControlledGraphicsPixmapObject::readRotationSensor()
     if (!scene()) //no movement if this item does not belong to a scene
         return;
 
+
+//Test reading sensor information. Since qDebug has ceased to work, uses qCritical instead...
+//        qrangelist rangelist = rotationSensor_.availableDataRates();
+
+//        qCritical() << rangelist.length() << "ranges found";
+//        foreach (qrange range, rangelist)
+//        {
+//            qCritical() << "Rotation sensor: " << range.first <<", " << "range.second";
+//        }
+//        qCritical() << "Current data date is " << rotationSensor_.dataRate();
+
     QRect sceneRectangle = scene()->sceneRect().toRect();
 
 
@@ -136,3 +146,15 @@ void OrientationControlledGraphicsPixmapObject::setPos(const QPointF &pos)
     }
 
 }
+
+
+void OrientationControlledGraphicsPixmapObject::rotationSensorReady()
+{
+
+    if (!rotationReadingInitialized_)
+    {
+        connect(&rotationReadingTimer_,SIGNAL(timeout()),this,SLOT(readRotationSensor()));
+        rotationReadingInitialized_ = true;
+        rotationReadingTimer_.start();
+    }
+}
index 6bfd1c1..802766b 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <QGraphicsPixmapItem>
 #include <QRotationSensor>
+#include <QTimer>
 
 QTM_USE_NAMESPACE
 
@@ -48,7 +49,8 @@ public slots:
     void startMoving();
     void stopMoving();
     void readRotationSensor();
-      virtual void setPos(const QPointF &pos);
+    virtual void setPos(const QPointF &pos);
+    void rotationSensorReady();
 
 
 protected:
@@ -62,6 +64,8 @@ private:
 
     QRotationSensor rotationSensor_;
     QPointF oldOldPos_;
+    QTimer rotationReadingTimer_;
+    bool rotationReadingInitialized_;