00001 /*!@file Devices/WiiMote.C read wiimote data */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/WiiMote.C $ 00035 // $Id: WiiMote.C 10794 2009-02-08 06:21:09Z itti $ 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); // will live in a separate thread 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 //itsWiimote = wiimote_t; 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 /* Activate the first led on the wiimote. It will take effect on the 00092 next call to wiimote_update. */ 00093 00094 itsWiimote.led.one = 1; 00095 itsWiimote.mode.ir = 1; 00096 itsWiimote.mode.acc = 1; 00097 00098 // start thread for run(): 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 // stop our thread: 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 //pthread_mutex_lock(&itsLock); 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 //pthread_mutex_unlock(&itsLock); 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 //// sleep a little: 00168 //usleep(5000); 00169 } 00170 00171 // we got an order to stop: 00172 //running = f; // FIXME: bogus! other thread may unlock too soon 00173 pthread_exit(0); 00174 #endif 00175 } 00176 00177 // ###################################################################### 00178 /* So things look consistent in everyone's emacs... */ 00179 /* Local Variables: */ 00180 /* indent-tabs-mode: nil */ 00181 /* End: */