Revision: 9543 http://pure-data.svn.sourceforge.net/pure-data/?rev=9543&view=rev Author: eighthave Date: 2008-03-07 10:47:45 -0800 (Fri, 07 Mar 2008)
Log Message: ----------- A first step of success, I got the wiiremote successfully connecting and getting one data event into Pd with the first bang. Now I have to figure out how to run the Carbon CFRunLoop into Pd land.
- cleaned up some unused variables - fixed some warnings - folded in the b7 version - cleaned up the port a bit
This code directly compiles and works fine on Max/MSP.
Modified Paths: -------------- trunk/externals/io/wiiremote/aka.wiiremote.c trunk/externals/io/wiiremote/wiiremote-help.pd trunk/externals/io/wiiremote/wiiremote.c trunk/externals/io/wiiremote/wiiremote.h
Added Paths: ----------- trunk/externals/io/wiiremote/TODO
Added: trunk/externals/io/wiiremote/TODO =================================================================== --- trunk/externals/io/wiiremote/TODO (rev 0) +++ trunk/externals/io/wiiremote/TODO 2008-03-07 18:47:45 UTC (rev 9543) @@ -0,0 +1,7 @@ + + + - try out IOBluetoothLocalDeviceAvailable(wiiremote->inquiry); to see if that will set up the event loops (http://lists.apple.com/archives/bluetooth-dev/2006/Dec/msg00001.html) + + +- try CFRunLoopRun(); then CFRunLoopStop(CFRunLoopGetCurrent()); in akawiiremote_clock() and akawiiremote_bang() +
Modified: trunk/externals/io/wiiremote/aka.wiiremote.c =================================================================== --- trunk/externals/io/wiiremote/aka.wiiremote.c 2008-03-07 00:49:08 UTC (rev 9542) +++ trunk/externals/io/wiiremote/aka.wiiremote.c 2008-03-07 18:47:45 UTC (rev 9543) @@ -13,6 +13,8 @@ #define SETSYM SETSYMBOL #define SETLONG SETFLOAT #define method t_method +//#define addbang(x) class_addbang(wiiremote_class, (x)) +//#define addmess(x) class_addmessage(class_wiiremote, (x)) static t_class *wiiremote_class; #else /* Max */ #include "ext.h" @@ -20,6 +22,7 @@
#include "wiiremote.h" #include <stdio.h> +#include <string.h>
#define kInterval 100 #define kMaxTrial 100 @@ -39,7 +42,7 @@ Boolean connected; void *statusOut; - void *dataOut + void *dataOut; } t_akawiiremote;
void *akawiiremote_class; // the number of instance of this object @@ -53,6 +56,7 @@ void akawiiremote_vibration(t_akawiiremote *x, long enable); void akawiiremote_led(t_akawiiremote *x, long enable1, long enable2, long enable3, long enable4); void akawiiremote_expansion(t_akawiiremote *x, long enable); +void akawiiremote_extraoutput(t_akawiiremote *x, long enable);
void akawiiremote_getbattery(t_akawiiremote *x); void akawiiremote_getexpansion(t_akawiiremote *x); @@ -78,11 +82,11 @@ NumVersion outSoftwareVersion; BluetoothHCIVersionInfo outHardwareVersion; - post("aka.wiiremote 1.0B6-UB by Masayuki Akamatsu"); + post("aka.wiiremote 1.0B7-UB by Masayuki Akamatsu");
- if (IOBluetoothGetVersion(&outSoftwareVersion, &outHardwareVersion)==kIOReturnSuccess) + if (IOBluetoothGetVersion(&outSoftwareVersion, &outHardwareVersion)==kIOReturnSuccess) // B7 { - if (outSoftwareVersion.majorRev < 1 || outSoftwareVersion.minorAndBugRev < 0x63) + if (outSoftwareVersion.majorRev < 1 && outSoftwareVersion.minorAndBugRev < 0x63) { error("requires Blutooth version 1.6.3 or later."); return; @@ -105,18 +109,22 @@ A_GIMME,0);
class_addbang(wiiremote_class,(t_method)akawiiremote_bang); + class_addmethod(wiiremote_class,(t_method)akawiiremote_address,gensym("address"),A_DEFSYMBOL, 0); class_addmethod(wiiremote_class,(t_method)akawiiremote_connect,gensym("connect"),0); class_addmethod(wiiremote_class,(t_method)akawiiremote_disconnect,gensym("disconnect"),0); class_addmethod(wiiremote_class,(t_method)akawiiremote_motionsensor,gensym("motion"), A_DEFFLOAT, 0); class_addmethod(wiiremote_class,(t_method)akawiiremote_irsensor,gensym("ir"), A_DEFFLOAT, 0); class_addmethod(wiiremote_class,(t_method)akawiiremote_vibration,gensym("vibration"), A_DEFFLOAT, 0); class_addmethod(wiiremote_class,(t_method)akawiiremote_led,gensym("led"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); + class_addmethod(wiiremote_class,(t_method)akawiiremote_expansion,gensym("expansion"), A_DEFFLOAT, 0); + class_addmethod(wiiremote_class,(t_method)akawiiremote_expansion,gensym("nunchuk"), A_DEFFLOAT, 0); + class_addmethod(wiiremote_class,(t_method)akawiiremote_extraoutput,gensym("extraoutput"), A_DEFFLOAT, 0); // B7
class_addmethod(wiiremote_class,(t_method)akawiiremote_getbattery,gensym("getbattery"),0); class_addmethod(wiiremote_class,(t_method)akawiiremote_getexpansion,gensym("getexpansion"),0); class_addmethod(wiiremote_class,(t_method)akawiiremote_getled,gensym("getled"),0); - - class_addmethod(wiiremote_class,(t_method)akawiiremote_assist,gensym("assist"),A_CANT,0); + class_addmethod(wiiremote_class,(t_method)akawiiremote_getaddress,gensym("getaddress"),0); + class_addmethod(wiiremote_class,(t_method)akawiiremote_getcalibration,gensym("getcalibration"), 0); #else /* Max */ setup((t_messlist **)&akawiiremote_class, (method)akawiiremote_new, (method)akawiiremote_free, (short)sizeof(t_akawiiremote), 0L, A_GIMME, 0);
@@ -130,6 +138,7 @@ addmess((method)akawiiremote_led,"led", A_DEFLONG, A_DEFLONG, A_DEFLONG, A_DEFLONG, 0); addmess((method)akawiiremote_expansion,"expansion", A_DEFLONG, 0); addmess((method)akawiiremote_expansion,"nunchuk", A_DEFLONG, 0); + addmess((method)akawiiremote_extraoutput,"extraoutput", A_DEFLONG, 0); // B7
addmess((method)akawiiremote_getbattery,"getbattery",0); addmess((method)akawiiremote_getexpansion,"getexpansion",0); @@ -145,15 +154,10 @@
void akawiiremote_bang(t_akawiiremote *x) { - t_atom av[5]; + t_atom av[7]; if (x->wiiremote->device == nil) return; // do nothing - -//#ifdef PD -// outlet_float(x->buttonsOut, (t_float) x->wiiremote->buttonData); -//#else /* Max */ -//#endif /* PD */
if (x->wiiremote->isExpansionPortAttached && x->wiiremote->isExpansionPortEnabled) { @@ -198,6 +202,18 @@ SETLONG(av + 2, x->wiiremote->nStickY); outlet_anything(x->dataOut, gensym(nunchukStr), 3, av); + if (x->wiiremote->isExtraOutputEnabled) + { + SETSYM(av, gensym("stick_calibration")); + SETLONG(av + 1, x->wiiremote->nunchukJoyStickCalibData.x_min); + SETLONG(av + 2, x->wiiremote->nunchukJoyStickCalibData.x_max); + SETLONG(av + 3, x->wiiremote->nunchukJoyStickCalibData.x_center); + SETLONG(av + 4, x->wiiremote->nunchukJoyStickCalibData.y_min); + SETLONG(av + 5, x->wiiremote->nunchukJoyStickCalibData.y_max); + SETLONG(av + 6, x->wiiremote->nunchukJoyStickCalibData.y_center); + outlet_anything(x->dataOut, gensym(nunchukStr), 7, av); + } + // Motion Sensor if (x->wiiremote->isMotionSensorEnabled) { @@ -207,6 +223,18 @@ SETLONG(av + 3, x->wiiremote->nAccZ); SETLONG(av + 4, x->wiiremote->nOrientation); outlet_anything(x->dataOut, gensym(nunchukStr), 5, av); + + if (x->wiiremote->isExtraOutputEnabled) + { + SETSYM(av, gensym("motion_calibration")); + SETLONG(av + 1, x->wiiremote->nunchukCalibData.accX_zero); + SETLONG(av + 2, x->wiiremote->nunchukCalibData.accY_zero); + SETLONG(av + 3, x->wiiremote->nunchukCalibData.accZ_zero); + SETLONG(av + 4, x->wiiremote->nunchukCalibData.accX_1g); + SETLONG(av + 5, x->wiiremote->nunchukCalibData.accY_1g); + SETLONG(av + 6, x->wiiremote->nunchukCalibData.accZ_1g); + outlet_anything(x->dataOut, gensym(nunchukStr), 7, av); + } } } } @@ -227,6 +255,31 @@ SETFLOAT(av + 3, x->wiiremote->angle); SETLONG (av + 4, x->wiiremote->tracking); outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + + if (x->wiiremote->isExtraOutputEnabled) // B7 + { + SETSYM(av, gensym("irraw")); + SETLONG(av + 1, 0); + SETLONG(av + 2, x->wiiremote->irData[0].x); + SETLONG(av + 3, x->wiiremote->irData[0].y); + SETLONG(av + 4, x->wiiremote->irData[0].s); + outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + SETLONG(av + 1, 1); + SETLONG(av + 2, x->wiiremote->irData[1].x); + SETLONG(av + 3, x->wiiremote->irData[1].y); + SETLONG(av + 4, x->wiiremote->irData[1].s); + outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + SETLONG(av + 1, 2); + SETLONG(av + 2, x->wiiremote->irData[2].x); + SETLONG(av + 3, x->wiiremote->irData[2].y); + SETLONG(av + 4, x->wiiremote->irData[2].s); + outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + SETLONG(av + 1, 3); + SETLONG(av + 2, x->wiiremote->irData[3].x); + SETLONG(av + 3, x->wiiremote->irData[3].y); + SETLONG(av + 4, x->wiiremote->irData[3].s); + outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + } }
// Motion Sensor @@ -238,6 +291,18 @@ SETLONG(av + 3, x->wiiremote->accZ); SETLONG(av + 4, x->wiiremote->orientation); outlet_anything(x->dataOut, gensym(remoteStr), 5, av); + + if (x->wiiremote->isExtraOutputEnabled) // B7 + { + SETSYM(av, gensym("motion_calibration")); + SETLONG(av + 1, x->wiiremote->wiiCalibData.accX_zero); + SETLONG(av + 2, x->wiiremote->wiiCalibData.accY_zero); + SETLONG(av + 3, x->wiiremote->wiiCalibData.accZ_zero); + SETLONG(av + 4, x->wiiremote->wiiCalibData.accX_1g); + SETLONG(av + 5, x->wiiremote->wiiCalibData.accY_1g); + SETLONG(av + 6, x->wiiremote->wiiCalibData.accZ_1g); + outlet_anything(x->dataOut, gensym(remoteStr), 7, av); + } } }
@@ -255,6 +320,7 @@
void akawiiremote_connect(t_akawiiremote *x) { + post("akawiiremote_connect"); t_atom status; Boolean result;
@@ -278,6 +344,8 @@
void akawiiremote_disconnect(t_akawiiremote *x) { + post("akawiiremote_disconnect"); + Boolean result; t_atom status; @@ -296,7 +364,6 @@ void akawiiremote_motionsensor(t_akawiiremote *x, long enable) { Boolean result; - t_atom status;
result = wiiremote_motionsensor(x->wiiremote, enable); //SETLONG(&status, result); @@ -306,17 +373,20 @@ void akawiiremote_irsensor(t_akawiiremote *x, long enable) { Boolean result; - t_atom status; result = wiiremote_irsensor(x->wiiremote, enable); //SETLONG(&status, result); //outlet_anything(x->statusOut, gensym("ir"), 1, &status); }
+void akawiiremote_extraoutput(t_akawiiremote *x, long enable) // B7 +{ + x->wiiremote->isExtraOutputEnabled = enable; +} + void akawiiremote_expansion(t_akawiiremote *x, long enable) { Boolean result; - t_atom status; result = wiiremote_expansion(x->wiiremote, enable); //SETLONG(&status, result); @@ -326,7 +396,6 @@ void akawiiremote_vibration(t_akawiiremote *x, long enable) { Boolean result; - t_atom status; result = wiiremote_vibration(x->wiiremote, enable); //SETLONG(&status, result); @@ -336,7 +405,6 @@ void akawiiremote_led(t_akawiiremote *x, long enable1, long enable2, long enable3, long enable4) { Boolean result; - t_atom status; result = wiiremote_led(x->wiiremote, enable1, enable2, enable3, enable4); //SETLONG(&status, result); @@ -509,12 +577,27 @@
void *akawiiremote_new(t_symbol *s, short ac, t_atom *av) { +#ifdef PD + t_akawiiremote *x = (t_akawiiremote *)pd_new(wiiremote_class); + t_symbol *first_argument; + + x->statusOut = outlet_new(&x->x_obj, 0); + x->dataOut = outlet_new(&x->x_obj, &s_list); + +/* this sets the device name from the object arguments */ + first_argument = atom_getsymbolarg(0, ac, av); + if(first_argument != &s_) + atom_string(av, x->address, MAXPDSTRING-1); +#else /* Max */ t_akawiiremote *x; -#ifdef PD - x = (t_akawiiremote *)pd_new(wiiremote_class); -#else /* Max */ x = (t_akawiiremote *)newobject(akawiiremote_class); + + x->statusOut = outlet_new(x, 0); + x->dataOut = outlet_new(x, 0); + + if (ac>0 && av[0].a_type == A_SYM) + strcpy(x->address, av[0].a_w.w_sym->s_name); #endif /* PD */ x->wiiremote = (WiiRemoteRef)getbytes(sizeof(WiiRemoteRec)); @@ -529,22 +612,11 @@ x->wiiremote->isLED2Illuminated = false; x->wiiremote->isLED3Illuminated = false; x->wiiremote->isLED4Illuminated = false; + x->wiiremote->isExtraOutputEnabled = false; } x->clock = clock_new(x, (method)akawiiremote_clock); -#ifdef PD - if (ac>0 && av[0].a_type == A_SYMBOL) - strcpy(x->address, av[0].a_w.w_symbol->s_name);
- x->statusOut = outlet_new(&x->x_obj, 0); - x->dataOut = outlet_new(&x->x_obj, 0); -#else /* Max */ - if (ac>0 && av[0].a_type == A_SYM) - strcpy(x->address, av[0].a_w.w_sym->s_name); - - x->statusOut = outlet_new(x, 0); - x->dataOut = outlet_new(x, 0); -#endif /* PD */ x->connected = false;
return x;
Modified: trunk/externals/io/wiiremote/wiiremote-help.pd =================================================================== --- trunk/externals/io/wiiremote/wiiremote-help.pd 2008-03-07 00:49:08 UTC (rev 9542) +++ trunk/externals/io/wiiremote/wiiremote-help.pd 2008-03-07 18:47:45 UTC (rev 9543) @@ -1,21 +1,14 @@ -#N canvas 41 171 577 460 10; -#X obj 451 21 import io; -#X obj 213 388 wiiremote; -#X msg 168 207 connect; -#X msg 185 233 disconnect; -#X msg 286 237 motionsensor $1; -#X obj 286 215 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +#N canvas 287 96 744 626 10; +#X msg 128 207 connect; +#X msg 141 228 disconnect; +#X obj 218 203 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1; -#X obj 406 215 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +#X obj 294 202 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1; -#X msg 406 237 irsensor $1; -#X obj 64 254 metro 100; -#X obj 64 233 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 -; -#X msg 376 290 getbatterylevel; -#X msg 376 310 getledstatus; -#X msg 376 330 getexpansionstatus; -#X msg 129 283 bang; +#X obj 127 285 metro 100; +#X obj 127 264 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X msg 89 283 bang; #X text 49 49 - Use the Bluetooth Setup Assistant to setup the WiiRemote with your computer (only required for the first use).; #X text 49 79 - Press the "Sync" button on the WiiRemote , four LEDs @@ -24,15 +17,64 @@ blinking once its connected; #X text 49 139 - Start the [metro] to get updates from [wiiremote] ; -#X connect 2 0 1 0; -#X connect 3 0 1 0; -#X connect 4 0 1 0; +#X obj 399 526 pddp/print; +#X obj 354 556 pddp/print; +#X obj 354 410 route remote; +#X msg 503 290 getbattery; +#X msg 503 310 getled; +#X msg 503 330 getexpansion; +#X obj 489 431 print RIGHT; +#X obj 253 430 print LEFT; +#X obj 354 476 route motion buttons ir; +#X obj 444 497 pddp/print; +#X msg 294 224 ir $1; +#X msg 218 225 motion $1; +#X obj 344 202 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X msg 344 224 vibration $1; +#X obj 434 202 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X msg 434 224 expansion $1; +#X obj 524 202 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 +1; +#X msg 524 223 extraoutput $1; +#N canvas 254 342 450 300 address 0; +#X obj 176 252 outlet; +#X msg 206 126 getaddress; +#X msg 176 72 address 00-1e-35-4c-e6-f1; +#X connect 1 0 0 0; +#X connect 2 0 0 0; +#X restore 25 462 pd address; +#X msg 503 352 getaddress; +#X obj 631 536 dac~; +#X obj 632 408 pddp/dsp; +#X obj 640 500 osc~ 100; +#X obj 303 388 wiiremote 00-1e-35-4c-e6-f1; +#X connect 0 0 34 0; +#X connect 1 0 34 0; +#X connect 2 0 22 0; +#X connect 3 0 21 0; +#X connect 4 0 34 0; #X connect 5 0 4 0; -#X connect 6 0 7 0; -#X connect 7 0 1 0; -#X connect 8 0 1 0; -#X connect 9 0 8 0; -#X connect 10 0 1 0; -#X connect 11 0 1 0; -#X connect 12 0 1 0; -#X connect 13 0 1 0; +#X connect 6 0 34 0; +#X connect 13 0 19 0; +#X connect 13 1 17 0; +#X connect 14 0 34 0; +#X connect 15 0 34 0; +#X connect 16 0 34 0; +#X connect 19 0 12 0; +#X connect 19 1 11 0; +#X connect 19 2 20 0; +#X connect 21 0 34 0; +#X connect 22 0 34 0; +#X connect 23 0 24 0; +#X connect 24 0 34 0; +#X connect 25 0 26 0; +#X connect 26 0 34 0; +#X connect 27 0 28 0; +#X connect 28 0 34 0; +#X connect 30 0 34 0; +#X connect 33 0 31 0; +#X connect 33 0 31 1; +#X connect 34 0 18 0; +#X connect 34 1 13 0;
Modified: trunk/externals/io/wiiremote/wiiremote.c =================================================================== --- trunk/externals/io/wiiremote/wiiremote.c 2008-03-07 00:49:08 UTC (rev 9542) +++ trunk/externals/io/wiiremote/wiiremote.c 2008-03-07 18:47:45 UTC (rev 9543) @@ -208,15 +208,16 @@
void checkDevice(WiiRemoteRef wiiremote, IOBluetoothDeviceRef device) { + post("checkDevice"); CFStringRef name; CFStringRef address; - + if (wiiremote_isconnected(wiiremote)) return; name = IOBluetoothDeviceGetName(device); address = IOBluetoothDeviceGetAddressString(device); - if (name != nil && address != nil) + if (name != nil && address != nil) { if (CFStringCompare(name, CFSTR("Nintendo RVL-CNT-01"), 0) == kCFCompareEqualTo) { @@ -233,17 +234,20 @@
void myFoundFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device) { + post("myFoundFunc"); checkDevice((WiiRemoteRef)refCon, device); }
void myUpdatedFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOBluetoothDeviceRef device, uint32_t devicesRemaining) { + post("myUpdatedFunc"); + checkDevice((WiiRemoteRef)refCon, device); }
void myCompleteFunc(void *refCon, IOBluetoothDeviceInquiryRef inquiry, IOReturn error, Boolean aborted) { - IOReturn ret; + post("myCompleteFunc");
if (aborted) return; // called by stop ;) @@ -252,14 +256,10 @@ wiiremote_stopsearch((WiiRemoteRef)refCon); return; } - - /* - ret = IOBluetoothDeviceInquiryStart(((WiiRemoteRef)refCon)->inquiry); - if (ret != kIOReturnSuccess) - { - wiiremote_stopsearch((WiiRemoteRef)refCon); - } - */ +#ifdef PD + // PD doesn't use the Carbon loop, so we have to manually control it + CFRunLoopStop( CFRunLoopGetCurrent() ); +#endif }
//-------------------------------------------------------------------------------------------- @@ -274,11 +274,12 @@ Boolean wiiremote_search(WiiRemoteRef wiiremote, char *address) { + post("wiiremote_search"); IOReturn ret; if (wiiremote->inquiry != nil) return true; - + wiiremote->inquiry = IOBluetoothDeviceInquiryCreateWithCallbackRefCon((void *)wiiremote); IOBluetoothDeviceInquirySetDeviceFoundCallback(wiiremote->inquiry, myFoundFunc); IOBluetoothDeviceInquirySetDeviceNameUpdatedCallback(wiiremote->inquiry, myUpdatedFunc); @@ -295,11 +296,15 @@ wiiremote->inquiry = nil; return false; } +#ifdef PD + CFRunLoopRun(); // PD doesn't use the Carbon loop, so we have to manually control it +#endif return true; }
Boolean wiiremote_stopsearch(WiiRemoteRef wiiremote) { + post("wiiremote_stopsearch"); IOReturn ret;
if (wiiremote->inquiry == nil) @@ -841,7 +846,7 @@
Boolean wiiremote_disconnect(WiiRemoteRef wiiremote) { - short i; + short i = 0;
if (wiiremote->cchan) {
Modified: trunk/externals/io/wiiremote/wiiremote.h =================================================================== --- trunk/externals/io/wiiremote/wiiremote.h 2008-03-07 00:49:08 UTC (rev 9542) +++ trunk/externals/io/wiiremote/wiiremote.h 2008-03-07 18:47:45 UTC (rev 9543) @@ -67,7 +67,6 @@ WiiClassicControllerPlusButton };
- typedef UInt16 WiiExpansionPortType; enum{ WiiExpNotAttached, @@ -131,6 +130,7 @@ Boolean isLED2Illuminated; Boolean isLED3Illuminated; Boolean isLED4Illuminated; + Boolean isExtraOutputEnabled; Boolean isExpansionPortAttached;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.