Created
June 26, 2012 15:47
-
-
Save larsgk/2996571 to your computer and use it in GitHub Desktop.
Very quick (and VERY dirty) hack to get WiiMote input in qt5/qtsensors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/plugins/sensors/dummy/dummy.pro b/src/plugins/sensors/dummy/dummy.pro | |
index 2cf1392..776458b 100644 | |
--- a/src/plugins/sensors/dummy/dummy.pro | |
+++ b/src/plugins/sensors/dummy/dummy.pro | |
@@ -22,3 +22,7 @@ unix:!mac:LIBS+=-lrt | |
target.path += $$[QT_INSTALL_PLUGINS]/sensors | |
INSTALLS += target | |
+ | |
+ | |
+INCLUDEPATH += /usr/local/include/wiic | |
+LIBS += -L/usr/local/lib -lwiicpp | |
diff --git a/src/plugins/sensors/dummy/dummyaccelerometer.cpp b/src/plugins/sensors/dummy/dummyaccelerometer.cpp | |
index 4a7ba7d..1d96cd4 100644 | |
--- a/src/plugins/sensors/dummy/dummyaccelerometer.cpp | |
+++ b/src/plugins/sensors/dummy/dummyaccelerometer.cpp | |
@@ -43,23 +43,94 @@ | |
#include <QDebug> | |
#include <QtGlobal> | |
+ | |
+#include <stdio.h> | |
+ | |
char const * const dummyaccelerometer::id("dummy.accelerometer"); | |
+std::vector<CWiimote> wiimotes; | |
+bool firstTime = true; | |
+ | |
+ | |
dummyaccelerometer::dummyaccelerometer(QSensor *sensor) | |
- : dummycommon(sensor) | |
+ : dummycommon(sensor), | |
+ reloadWiimotes(0) | |
{ | |
setReading<QAccelerometerReading>(&m_reading); | |
addDataRate(100, 100); // 100Hz | |
+ | |
+ if(firstTime) { | |
+ // Find and connect to the wiimotes | |
+ wiimotes = wii.FindAndConnect(); | |
+ | |
+ if (!wiimotes.size()) { | |
+ printf("No wiimotes found.\n"); | |
+ //return 0; | |
+ } else { | |
+ printf("Wiimotes found: %i\n", wiimotes.size()); | |
+ if(wiimotes.size()>0) | |
+ wiimotes[0].SetMotionSensingMode(CWiimote::ON); | |
+ } | |
+ firstTime = false; | |
+ } | |
+ | |
+ // Setup the wiimotes | |
+ /*for(index = 0, i = wiimotes.begin(); i != wiimotes.end(); ++i, ++index) { | |
+ // Use a reference to make working with the iterator handy. | |
+ CWiimote & wiimote = *i; | |
+ | |
+ //Set Leds | |
+ wiimote.SetLEDs(LED_MAP[index]); | |
+ }*/ | |
+ | |
} | |
void dummyaccelerometer::poll() | |
{ | |
m_reading.setTimestamp(getTimestamp()); | |
+ | |
+ if(reloadWiimotes) { | |
+ // Regenerate the list of wiimotes | |
+ wiimotes = wii.GetWiimotes(); | |
+ | |
+ reloadWiimotes = 0; | |
+ } | |
+ | |
+ if(wii.Poll()) { | |
+ for(i = wiimotes.begin(); i != wiimotes.end(); ++i) { | |
+ // Use a reference to make working with the iterator handy. | |
+ CWiimote & wiimote = *i; | |
+ switch(wiimote.GetEvent()) { | |
+ case CWiimote::EVENT_EVENT: | |
+ // If the accelerometer is turned on then print angles | |
+ if(wiimote.isUsingACC()) { | |
+ float x,y,z; | |
+ wiimote.Accelerometer.GetGravityVector(x, y, z); | |
+ printf("[Wiimote id %i] x = %f y = %f z = %f\n", wiimote.GetID(), x, y, z); | |
+ m_reading.setX(x*10.0); | |
+ m_reading.setY(y*10.0); // facing the user, gravity goes here | |
+ m_reading.setZ(z*10.0); | |
+ } | |
+ break; | |
+ | |
+ case CWiimote::EVENT_DISCONNECT: | |
+ case CWiimote::EVENT_UNEXPECTED_DISCONNECT: | |
+ printf("--- DISCONNECTED [Wiimote id %i] ---\n", wiimote.GetID()); | |
+ reloadWiimotes = 1; | |
+ break; | |
+ | |
+ default: | |
+ break; | |
+ } | |
+ } | |
+ } | |
+ | |
// Your average desktop computer doesn't move :) | |
- m_reading.setX(0); | |
- m_reading.setY(9.8); // facing the user, gravity goes here | |
- m_reading.setZ(0); | |
+ //m_reading.setX(0); | |
+ //m_reading.setY(9.8); // facing the user, gravity goes here | |
+ //m_reading.setZ(0); | |
+ | |
+ | |
newReadingAvailable(); | |
} | |
- | |
diff --git a/src/plugins/sensors/dummy/dummyaccelerometer.h b/src/plugins/sensors/dummy/dummyaccelerometer.h | |
index 0e7bd0b..b0628bc 100644 | |
--- a/src/plugins/sensors/dummy/dummyaccelerometer.h | |
+++ b/src/plugins/sensors/dummy/dummyaccelerometer.h | |
@@ -45,6 +45,8 @@ | |
#include "dummycommon.h" | |
#include <qaccelerometer.h> | |
+#include <wiicpp.h> | |
+ | |
class dummyaccelerometer : public dummycommon | |
{ | |
public: | |
@@ -55,6 +57,13 @@ public: | |
void poll(); | |
private: | |
QAccelerometerReading m_reading; | |
+ | |
+ CWii wii; // Defaults to 4 remotes | |
+ std::vector<CWiimote>::iterator i; | |
+ int reloadWiimotes; | |
+ int index; | |
+ | |
+ | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment