qshake.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "qshake.h"
  2. #include <math.h>
  3. QShake::QShake(QObject * parent) :
  4. QObject(parent)
  5. , module(0)
  6. , m_sensitivity(QShake::Medium)
  7. , m_direction(QShake::All)
  8. {
  9. sensor.addFilter(this);
  10. }
  11. void QShake::start() {
  12. sensor.start();
  13. }
  14. void QShake::stop() {
  15. sensor.stop();
  16. }
  17. float QShake::sensitivity() const{
  18. return this->m_sensitivity;
  19. }
  20. void QShake::setSensitivity(float value){
  21. this->m_sensitivity = value;
  22. }
  23. QShake::Direction QShake::direction() const{
  24. return this->m_direction;
  25. }
  26. void QShake::setDirection(QShake::Direction value){
  27. this->m_direction = value;
  28. }
  29. bool QShake::filter(QAccelerometerReading *reading)
  30. {
  31. qreal rx = reading->x();
  32. qreal ry = reading->y();
  33. qreal rz = reading->z();
  34. qreal c_sensitivity = ( m_direction == QShake::All ) ? m_sensitivity : m_sensitivity*1.5;
  35. qreal cmodule = 0;
  36. switch(m_direction) {
  37. case QShake::UpDown : cmodule = sqrt(ry * ry * 3); break;
  38. case QShake::LeftRight : cmodule = sqrt(rx * rx * 3); break;
  39. case QShake::PushPull : cmodule = sqrt(rz * rz * 3); break;
  40. default: cmodule = sqrt(rx * rx + ry * ry + rz * rz); break;
  41. }
  42. float cshake = sqrt(pow(module,2)+pow(cmodule,2));
  43. if( cshake > c_sensitivity ) {
  44. emit shake(true);
  45. } else {
  46. emit shake(false);
  47. }
  48. module = cmodule;
  49. return false;
  50. }