accelerometerclass.cpp 868 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "accelerometerclass.h"
  2. #include <QTimer>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QStringList>
  6. accelerometerClass::accelerometerClass(QObject *parent)
  7. : QThread(parent)
  8. {
  9. }
  10. void accelerometerClass::run()
  11. {
  12. QTimer timer;
  13. connect(&timer, SIGNAL(timeout()), this, SLOT(updateCoords()));
  14. timer.start(20); // 50 Hz update rate
  15. exec();
  16. }
  17. void accelerometerClass::updateCoords()
  18. {
  19. QFile file("/sys/class/i2c-adapter/i2c-3/3-001d/coord");
  20. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  21. return;
  22. QTextStream in(&file);
  23. QString data = in.readAll();
  24. processCoords(data);
  25. }
  26. void accelerometerClass::processCoords(QString &data)
  27. {
  28. QStringList data_splited = data.split(" ");
  29. x = data_splited[0];
  30. y = data_splited[1];
  31. z = data_splited[2];
  32. emit deviceOrientation(x, y, z);
  33. }