00001 /*!@file AppDevices/test-wiimote.C Test the wiimote */ 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 <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-wiimote.C $ 00035 // $Id: test-wiimote.C 14376 2011-01-11 02:44:34Z pez $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Image/Image.H" 00040 #include "Image/DrawOps.H" 00041 #include "Media/FrameSeries.H" 00042 #include "Transport/FrameInfo.H" 00043 #include "Raster/GenericFrame.H" 00044 #include "Raster/GenericFrame.H" 00045 #include "Media/FrameSeries.H" 00046 #include "Transport/FrameInfo.H" 00047 #include "GUI/ImageDisplayStream.H" 00048 #include "GUI/XWinManaged.H" 00049 00050 00051 #ifdef HAVE_LIBWIIMOTE 00052 extern "C" { 00053 #define _ENABLE_TILT 00054 #define _ENABLE_FORCE 00055 #undef HAVE_CONFIG_H 00056 #include <libcwiimote/wiimote.h> 00057 #include <libcwiimote/wiimote_api.h> 00058 } 00059 #endif 00060 00061 #define KEY_UP 98 00062 #define KEY_DOWN 104 00063 #define KEY_LEFT 100 00064 #define KEY_RIGHT 102 00065 00066 00067 int getKey(nub::ref<OutputFrameSeries> &ofs) 00068 { 00069 const nub::soft_ref<ImageDisplayStream> ids = 00070 ofs->findFrameDestType<ImageDisplayStream>(); 00071 00072 const rutz::shared_ptr<XWinManaged> uiwin = 00073 ids.is_valid() 00074 ? ids->getWindow("Output") 00075 : rutz::shared_ptr<XWinManaged>(); 00076 return uiwin->getLastKeyPress(); 00077 } 00078 00079 00080 int main(int argc, const char **argv) 00081 { 00082 // Instantiate a ModelManager: 00083 ModelManager manager("Test wiimote"); 00084 00085 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager)); 00086 manager.addSubComponent(ofs); 00087 00088 // Parse command-line: 00089 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1); 00090 00091 manager.start(); 00092 00093 #ifdef HAVE_LIBWIIMOTE 00094 wiimote_t wiimote = WIIMOTE_INIT; 00095 //wiimote_report_t report = WIIMOTE_REPORT_INIT; 00096 00097 LINFO("Press buttons 1 and 2 on the wiimote now to connect."); 00098 int nmotes = wiimote_discover(&wiimote, 1); 00099 if (nmotes == 0) 00100 LFATAL("no wiimotes were found"); 00101 00102 LINFO("found: %s\n", wiimote.link.r_addr); 00103 00104 if (wiimote_connect(&wiimote, wiimote.link.r_addr) < 0) 00105 LFATAL("Unable to connect to wiimote"); 00106 00107 /* Activate the first led on the wiimote. It will take effect on the 00108 next call to wiimote_update. */ 00109 00110 wiimote.led.one = 1; 00111 00112 // let's get all our ModelComponent instances started: 00113 LINFO("Open "); 00114 00115 LINFO("Status %i", wiimote_is_open(&wiimote)); 00116 00117 int speed = 100; 00118 int motor1_dir = 0; 00119 00120 int motor2_dir = 0; 00121 00122 Point2D<int> loc(128,128); 00123 while(wiimote_is_open(&wiimote)) 00124 { 00125 /* The wiimote_update function is used to synchronize the wiimote 00126 object with the real wiimote. It should be called as often as 00127 possible in order to minimize latency. */ 00128 00129 if (wiimote_update(&wiimote) < 0) { 00130 wiimote_disconnect(&wiimote); 00131 break; 00132 } 00133 00134 /* The wiimote object has member 'keys' which keep track of the 00135 current key state. */ 00136 00137 if (wiimote.keys.home) { //press home to exit 00138 wiimote_disconnect(&wiimote); 00139 } 00140 00141 /* Activate the accelerometer when the 'A' key is pressed. */ 00142 //if (wiimote.keys.a) { 00143 wiimote.mode.acc = 1; 00144 //} 00145 //else { 00146 // wiimote.mode.acc = 0; 00147 //} 00148 00149 00150 Image<PixRGB<byte> > img(255,255,ZEROS); 00151 00152 drawLine(img, Point2D<int>(128, 128), Point2D<int>(128+(int)(wiimote.force.x*400), 128), PixRGB<byte>(255,0,0),3); 00153 drawLine(img, Point2D<int>(128, 128), Point2D<int>(128, 128+(int)(wiimote.force.y*400)), PixRGB<byte>(0,255,0),3); 00154 drawLine(img, Point2D<int>(128, 128), Point2D<int>(128+(int)(wiimote.force.z*400), 128), PixRGB<byte>(0,0,255),3); 00155 00156 ofs->writeRGB(img, "Output", FrameInfo("output", SRC_POS)); 00157 00158 00159 int key = getKey(ofs); 00160 if (key != -1) 00161 { 00162 switch(key) 00163 { 00164 case 10: //l 00165 speed += 10; 00166 break; 00167 case 24: 00168 speed -= 10; 00169 break; 00170 case KEY_UP: 00171 motor1_dir = 2; 00172 motor2_dir = 2; 00173 break; 00174 case KEY_DOWN: 00175 motor1_dir = 1; 00176 motor2_dir = 1; 00177 break; 00178 case KEY_LEFT: 00179 motor1_dir = 2; 00180 motor2_dir = 1; 00181 break; 00182 case KEY_RIGHT: 00183 motor1_dir = 1; 00184 motor2_dir = 2; 00185 break; 00186 case 65: //space 00187 motor1_dir = 4; 00188 motor2_dir = 4; 00189 break; 00190 00191 } 00192 } 00193 //send the data to the wiimote 00194 wiimote_write_byte(&wiimote, 0x04a40001, motor1_dir); 00195 wiimote_write_byte(&wiimote, 0x04a40002, speed); 00196 wiimote_write_byte(&wiimote, 0x04a40003, motor2_dir); 00197 wiimote_write_byte(&wiimote, 0x04a40004, speed); 00198 00199 00200 00201 LINFO("KEYS %04x one=%d two=%d a=%d b=%d <=%d >=%d ^=%d v=%d h=%d +=%d -=%d\n", 00202 wiimote.keys.bits, 00203 wiimote.keys.one, 00204 wiimote.keys.two, 00205 wiimote.keys.a, 00206 wiimote.keys.b, 00207 wiimote.keys.left, 00208 wiimote.keys.right, 00209 wiimote.keys.up, 00210 wiimote.keys.down, 00211 wiimote.keys.home, 00212 wiimote.keys.plus, 00213 wiimote.keys.minus); 00214 00215 LINFO("TILT x=%.3f y=%.3f z=%.3f\n", 00216 wiimote.tilt.x, 00217 wiimote.tilt.y, 00218 wiimote.tilt.z); 00219 00220 LINFO("FORCE x=%.3f y=%.3f z=%.3f (sum=%.3f)\n", 00221 wiimote.force.x, 00222 wiimote.force.y, 00223 wiimote.force.z, 00224 sqrt(wiimote.force.x*wiimote.force.x+wiimote.force.y*wiimote.force.y+wiimote.force.z*wiimote.force.z)); 00225 00226 } 00227 00228 // stop all our ModelComponents 00229 manager.stop(); 00230 #else 00231 LFATAL("Need the libwiimote"); 00232 #endif 00233 00234 // all done! 00235 return 0; 00236 } 00237 00238 // ###################################################################### 00239 /* So things look consistent in everyone's emacs... */ 00240 /* Local Variables: */ 00241 /* indent-tabs-mode: nil */ 00242 /* End: */