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