examples/sensors/panorama/tapcontroller.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 
       
     2 
       
     3 #include "tapcontroller.h"
       
     4 #include <QDebug>
       
     5 
       
     6 
       
     7 const qreal TapController::m_timewindow=1000;
       
     8 
       
     9 
       
    10 TapController::TapController(): TimedController(), m_step(20){
       
    11     m_accelerometer.connectToBackend();
       
    12     m_accelerometer.start();
       
    13     connect(&m_accelerometer, SIGNAL(readingChanged()), this, SLOT(updateAcce()));
       
    14 
       
    15     m_tap.connectToBackend();
       
    16     m_tap.start();
       
    17     connect(&m_tap, SIGNAL(readingChanged()), this, SLOT(update()));
       
    18 
       
    19 }
       
    20 
       
    21 TapController::~TapController(){
       
    22     m_tap.stop();
       
    23     disconnect(&m_tap);
       
    24     m_accelerometer.stop();
       
    25     disconnect(&m_accelerometer);
       
    26 }
       
    27 
       
    28 
       
    29 
       
    30 void TapController::update()
       
    31 {
       
    32     int direction = m_tap.reading()->tapDirection();
       
    33     switch(direction){
       
    34     case QTapReading::X:
       
    35     case QTapReading::X_Pos:
       
    36     case QTapReading::X_Neg:
       
    37         m_dy=0;
       
    38         setDx(direction);
       
    39         break;
       
    40     case QTapReading::Y:
       
    41     case QTapReading::Y_Pos:
       
    42     case QTapReading::Y_Neg:
       
    43         m_dx = 0;
       
    44         setDy(direction);
       
    45         break;
       
    46     case QTapReading::Undefined:
       
    47     case QTapReading::Z:
       
    48     case QTapReading::Z_Pos:
       
    49     case QTapReading::Z_Neg:
       
    50     default:
       
    51         m_dx = 0; m_dy = 0;
       
    52         return;
       
    53     }    
       
    54 
       
    55 }
       
    56 
       
    57 
       
    58 void TapController::updateAcce()
       
    59 {
       
    60     qreal accX = m_accelerometer.reading()->x();
       
    61     qreal accY= m_accelerometer.reading()->y();
       
    62     m_now = m_accelerometer.reading()->timestamp();
       
    63 
       
    64     checkX(accX);
       
    65     checkY(accY);
       
    66 }
       
    67 
       
    68 
       
    69 void TapController::updateCoordinates(){
       
    70 
       
    71     if (m_now - m_timestampX > m_timewindow){
       
    72         m_absMaxX = 0;
       
    73         m_timestampX = m_now;
       
    74     }
       
    75 
       
    76     if (m_now - m_timestampY > m_timewindow){
       
    77         m_absMaxY = 0;
       
    78         m_timestampY = m_now;
       
    79     }
       
    80 
       
    81     if (m_dx!=0) m_dx = m_dx>0? m_dx-1: m_dx+1;
       
    82     if (m_dy!=0) m_dy = m_dy>0? m_dy-1: m_dy+1;
       
    83 
       
    84     m_x +=m_dx;
       
    85     m_y +=m_dy;
       
    86 }
       
    87 
       
    88 
       
    89 void TapController::checkX(qreal accX){
       
    90     if (qAbs(accX)>qAbs(m_absMaxX)){
       
    91         m_absMaxX = accX;
       
    92         m_timestampX = m_now;
       
    93     }
       
    94 }
       
    95 
       
    96 
       
    97 void TapController::checkY(qreal accY){
       
    98     if (qAbs(accY)>qAbs(m_absMaxY)){
       
    99         m_absMaxY = accY;
       
   100         m_timestampY = m_now;
       
   101     }
       
   102 }
       
   103 
       
   104 
       
   105 void TapController::setDx(int direction){
       
   106     switch(direction){
       
   107     case QTapReading::X:
       
   108         m_dx = m_absMaxX>0?m_step : -m_step;
       
   109         break;
       
   110     case QTapReading::X_Pos:
       
   111         m_dx = m_step;
       
   112         break;
       
   113     case QTapReading::X_Neg:
       
   114         m_dx = -m_step;
       
   115         break;
       
   116     }
       
   117 }
       
   118 
       
   119 void TapController::setDy(int direction){
       
   120     switch(direction){
       
   121     case QTapReading::Y:
       
   122         m_dy = m_absMaxY>0?m_step : - m_step;
       
   123         break;
       
   124     case QTapReading::Y_Pos:
       
   125         m_dy = m_step;
       
   126         break;
       
   127     case QTapReading::Y_Neg:
       
   128         m_dy = -m_step;
       
   129         break;
       
   130     }
       
   131 }
       
   132