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