WiiMote.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Devices/WiiMote.H"
00039
00040 #include "Component/ParamMap.H"
00041 #include "Util/Assert.H"
00042 #include "Util/Types.H"
00043 #include "Util/log.H"
00044
00045 #include <cmath>
00046 #include <unistd.h>
00047
00048 void* WiiMote_run(void *r0);
00049
00050
00051 void* WiiMote_run(void *r0)
00052 {
00053 WiiMote *r = (WiiMote *)r0;
00054 r->run(); return NULL;
00055 }
00056
00057
00058 WiiMote::WiiMote(OptionManager& mgr,
00059 const std::string& descrName,
00060 const std::string& tagName) :
00061 ModelComponent(mgr, descrName, tagName)
00062 {
00063 running = false;
00064
00065 pthread_mutex_init(&itsLock, NULL);
00066 }
00067
00068
00069 void WiiMote::start1()
00070 {
00071 }
00072
00073
00074 void WiiMote::start2()
00075 {
00076 }
00077
00078 void WiiMote::init()
00079 {
00080 #ifdef HAVE_LIBWIIMOTE
00081 LINFO("Press buttons 1 and 2 on the wiimote now to connect.");
00082 int nmotes = wiimote_discover(&itsWiimote, 1);
00083 if (nmotes == 0)
00084 LFATAL("no wiimotes were found");
00085
00086 LINFO("found: %s\n", itsWiimote.link.r_addr);
00087
00088 if (wiimote_connect(&itsWiimote, itsWiimote.link.r_addr) < 0)
00089 LFATAL("Unable to connect to wiimote");
00090
00091
00092
00093
00094 itsWiimote.led.one = 1;
00095 itsWiimote.mode.ir = 1;
00096 itsWiimote.mode.acc = 1;
00097
00098
00099 pthread_create(&runner, NULL, &WiiMote_run, (void *)this);
00100 #else
00101 LFATAL("Need the libwiimote");
00102 #endif
00103
00104 }
00105
00106
00107 void WiiMote::stop1()
00108 {
00109
00110 running = false; while(running == false) usleep(5);
00111 usleep(50); running = false;
00112 }
00113
00114
00115 WiiMote::~WiiMote()
00116 {
00117 pthread_mutex_destroy(&itsLock);
00118 }
00119
00120
00121 WiiMote::SensorData WiiMote::getSensorData()
00122 {
00123 SensorData sensorData;
00124 #ifdef HAVE_LIBWIIMOTE
00125
00126 sensorData.tilt.x = itsWiimote.tilt.x;
00127 sensorData.tilt.y = itsWiimote.tilt.y;
00128 sensorData.tilt.z = itsWiimote.tilt.z;
00129
00130 sensorData.force.x = itsWiimote.force.x;
00131 sensorData.force.y = itsWiimote.force.y;
00132 sensorData.force.z = itsWiimote.force.z;
00133
00134 sensorData.IR1 = Point2D<int>(itsWiimote.ir1.x, itsWiimote.ir1.y);
00135 sensorData.ir1Size = itsWiimote.ir1.size;
00136
00137 sensorData.IR2 = Point2D<int>(itsWiimote.ir2.x, itsWiimote.ir2.y);
00138 sensorData.ir2Size = itsWiimote.ir2.size;
00139
00140 sensorData.IR3 = Point2D<int>(itsWiimote.ir3.x, itsWiimote.ir3.y);
00141 sensorData.ir3Size = itsWiimote.ir3.size;
00142
00143 sensorData.IR4 = Point2D<int>(itsWiimote.ir4.x, itsWiimote.ir4.y);
00144 sensorData.ir4Size = itsWiimote.ir4.size;
00145
00146 #endif
00147
00148 return sensorData;
00149 }
00150
00151
00152 void WiiMote::run()
00153 {
00154 #ifdef HAVE_LIBWIIMOTE
00155 running = true;
00156
00157 while(wiimote_is_open(&itsWiimote) && running)
00158 {
00159 pthread_mutex_lock(&itsLock);
00160 int rc = wiimote_update(&itsWiimote);
00161 pthread_mutex_unlock(&itsLock);
00162
00163 if (rc < 0) {
00164 wiimote_disconnect(&itsWiimote);
00165 break;
00166 }
00167
00168
00169 }
00170
00171
00172
00173 pthread_exit(0);
00174 #endif
00175 }
00176
00177
00178
00179
00180
00181