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 "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
00083 ModelManager manager("Test wiimote");
00084
00085 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00086 manager.addSubComponent(ofs);
00087
00088
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
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
00108
00109
00110 wiimote.led.one = 1;
00111
00112
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
00126
00127
00128
00129 if (wiimote_update(&wiimote) < 0) {
00130 wiimote_disconnect(&wiimote);
00131 break;
00132 }
00133
00134
00135
00136
00137 if (wiimote.keys.home) {
00138 wiimote_disconnect(&wiimote);
00139 }
00140
00141
00142
00143 wiimote.mode.acc = 1;
00144
00145
00146
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:
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:
00187 motor1_dir = 4;
00188 motor2_dir = 4;
00189 break;
00190
00191 }
00192 }
00193
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
00229 manager.stop();
00230 #else
00231 LFATAL("Need the libwiimote");
00232 #endif
00233
00234
00235 return 0;
00236 }
00237
00238
00239
00240
00241
00242