From 42a3aaecd0d992490edbb9bd10d6164eef78c4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Mon, 5 Oct 2009 16:47:18 +0000 Subject: probably got balance-calibration to work; renamed everything to "wiimote" internally; added message "report " svn path=/trunk/externals/hardware/wiimote/; revision=12529 --- wiimote.c | 362 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 205 insertions(+), 157 deletions(-) diff --git a/wiimote.c b/wiimote.c index 99af4fb..a13ee76 100644 --- a/wiimote.c +++ b/wiimote.c @@ -1,5 +1,5 @@ // =================================================================== -// Wiiremote external for Puredata +// Wiimote external for Puredata // Written by Mike Wozniewki (Feb 2007), www.mikewoz.com // // Requires the CWiid library (version 0.6.00) by L. Donnie Smith @@ -22,7 +22,7 @@ // ChangeLog: // 2008-04-14 Florian Krebs -// * adapt wiiremote external for the actual version of cwiid (0.6.00) +// * adapt wiimote external for the actual version of cwiid (0.6.00) // 2009-09-14 IOhannes m zmölnig // * made it compile without private cwiid-headers @@ -43,61 +43,62 @@ struct acc { unsigned char z; }; -// class and struct declarations for wiiremote pd external: -static t_class *wiiremote_class; -typedef struct _wiiremote +// class and struct declarations for wiimote pd external: +static t_class *wiimote_class; +typedef struct _wiimote { t_object x_obj; // standard pd object (must be first in struct) - cwiid_wiimote_t *wiiremote; // individual wiiremote handle per pd object, represented in libcwiid + cwiid_wiimote_t *wiimote; // individual wiimote handle per pd object, represented in libcwiid t_float connected; - int wiiremoteID; + int wiimoteID; int reportMode; struct acc_cal acc_cal; /* calibration for built-in accelerometer */ struct acc_cal nc_acc_cal; /* calibration for nunchuk accelerometer */ + struct balance_cal balance_cal; // outlets: t_outlet *outlet_data; -} t_wiiremote; +} t_wiimote; -// For now, we make one global t_wiiremote pointer that we can refer to +// For now, we make one global t_wiimote pointer that we can refer to // in the cwiid_callback. This means we can support maximum of ONE -// wiiremote. ARGH. We'll have to figure out how to have access to the +// wiimote. ARGH. We'll have to figure out how to have access to the // pd object from the callback (without modifying the CWiid code): -#define MAX_WIIREMOTES 14 +#define MAX_WIIMOTES 14 -typedef struct _wiiremoteList { - t_wiiremote*x; +typedef struct _wiimoteList { + t_wiimote*x; int id; - struct _wiiremoteList*next; -} t_wiiremoteList; + struct _wiimoteList*next; +} t_wiimoteList; -t_wiiremoteList*g_wiiremoteList=NULL; +t_wiimoteList*g_wiimoteList=NULL; -static int addWiiremoteObject(t_wiiremote*x, int id) { - t_wiiremoteList*wl=g_wiiremoteList; - t_wiiremoteList*newentry=NULL; +static int addWiimoteObject(t_wiimote*x, int id) { + t_wiimoteList*wl=g_wiimoteList; + t_wiimoteList*newentry=NULL; if(NULL!=wl) { while(wl->next) { if(wl->x == x) { - pd_error(x, "[wiiremote]: already bound to Wii%02d", wl->id); + pd_error(x, "[wiimote]: already bound to Wii%02d", wl->id); return 0; } if(wl->id == id) { - pd_error(x, "[wiiremote]: another object is already bound to Wii%02d", wl->id); + pd_error(x, "[wiimote]: another object is already bound to Wii%02d", wl->id); return 0; } wl=wl->next; } } - newentry=(t_wiiremoteList*)getbytes(sizeof(t_wiiremoteList)); + newentry=(t_wiimoteList*)getbytes(sizeof(t_wiimoteList)); newentry->next=NULL; newentry->x=x; newentry->id=id; @@ -105,13 +106,13 @@ static int addWiiremoteObject(t_wiiremote*x, int id) { if(wl) wl->next=newentry; else - g_wiiremoteList=newentry; + g_wiimoteList=newentry; return 1; } -static t_wiiremote*getWiiremoteObject(const int id) { - t_wiiremoteList*wl=g_wiiremoteList; +static t_wiimote*getWiimoteObject(const int id) { + t_wiimoteList*wl=g_wiimoteList; if(NULL==wl) return NULL; @@ -124,9 +125,9 @@ static t_wiiremote*getWiiremoteObject(const int id) { return NULL; } -static void removeWiiremoteObject(const t_wiiremote*x) { - t_wiiremoteList*wl=g_wiiremoteList; - t_wiiremoteList*last=NULL; +static void removeWiimoteObject(const t_wiimote*x) { + t_wiimoteList*wl=g_wiimoteList; + t_wiimoteList*last=NULL; if(NULL==wl) return; @@ -135,12 +136,12 @@ static void removeWiiremoteObject(const t_wiiremote*x) { if(last) { last->next=wl->next; } else { - g_wiiremoteList=wl->next; + g_wiimoteList=wl->next; } wl->x=NULL; wl->id=0; wl->next=NULL; - freebytes(wl, sizeof(t_wiiremoteList)); + freebytes(wl, sizeof(t_wiimoteList)); return; } @@ -152,11 +153,11 @@ static void removeWiiremoteObject(const t_wiiremote*x) { // ============================================================== -static void wiiremote_debug(t_wiiremote *x) +static void wiimote_debug(t_wiimote *x) { post("\n======================"); - if (x->connected) post("Wiiremote (id: %d) is connected.", x->wiiremoteID); - else post("Wiiremote (id: %d) is NOT connected.", x->wiiremoteID); + if (x->connected) post("Wiimote (id: %d) is connected.", x->wiimoteID); + else post("Wiimote (id: %d) is NOT connected.", x->wiimoteID); post("acceleration: %s", (x->reportMode & CWIID_RPT_ACC)?"ON":"OFF"); post("IR: %s", (x->reportMode & CWIID_RPT_IR)?"ON":"OFF"); post("extensions: %s", (x->reportMode & CWIID_RPT_EXT)?"ON":"OFF"); @@ -174,7 +175,7 @@ static void wiiremote_debug(t_wiiremote *x) // ============================================================== -static void wiiremote_cwiid_battery(t_wiiremote *x, int battery) +static void wiimote_cwiid_battery(t_wiimote *x, int battery) { t_atom ap[1]; t_float bat=(1.f*battery) / CWIID_BATTERY_MAX; @@ -187,7 +188,7 @@ static void wiiremote_cwiid_battery(t_wiiremote *x, int battery) } // Button handler: -static void wiiremote_cwiid_btn(t_wiiremote *x, struct cwiid_btn_mesg *mesg) +static void wiimote_cwiid_btn(t_wiimote *x, struct cwiid_btn_mesg *mesg) { t_atom ap[2]; SETFLOAT(ap+0, (mesg->buttons & 0xFF00)>>8); @@ -196,7 +197,7 @@ static void wiiremote_cwiid_btn(t_wiiremote *x, struct cwiid_btn_mesg *mesg) } -static void wiiremote_cwiid_acc(t_wiiremote *x, struct cwiid_acc_mesg *mesg) +static void wiimote_cwiid_acc(t_wiimote *x, struct cwiid_acc_mesg *mesg) { double a_x, a_y, a_z; t_atom ap[3]; @@ -222,7 +223,7 @@ static void wiiremote_cwiid_acc(t_wiiremote *x, struct cwiid_acc_mesg *mesg) } -static void wiiremote_cwiid_ir(t_wiiremote *x, struct cwiid_ir_mesg *mesg) +static void wiimote_cwiid_ir(t_wiimote *x, struct cwiid_ir_mesg *mesg) { unsigned int i; @@ -239,7 +240,7 @@ static void wiiremote_cwiid_ir(t_wiiremote *x, struct cwiid_ir_mesg *mesg) } } -static void wiiremote_cwiid_nunchuk(t_wiiremote *x, struct cwiid_nunchuk_mesg *mesg) +static void wiimote_cwiid_nunchuk(t_wiimote *x, struct cwiid_nunchuk_mesg *mesg) { t_atom ap[4]; double a_x, a_y, a_z; @@ -280,7 +281,7 @@ static void wiiremote_cwiid_nunchuk(t_wiiremote *x, struct cwiid_nunchuk_mesg *m } #ifdef CWIID_RPT_CLASSIC -static void wiiremote_cwiid_classic(t_wiiremote *x, struct cwiid_classic_mesg *mesg) +static void wiimote_cwiid_classic(t_wiimote *x, struct cwiid_classic_mesg *mesg) { t_atom ap[3]; @@ -318,32 +319,43 @@ static void wiiremote_cwiid_classic(t_wiiremote *x, struct cwiid_classic_mesg *m #ifdef CWIID_RPT_BALANCE #warning Balance ignores calibration data -static void wiiremote_cwiid_balance_output(t_wiiremote *x, t_symbol*s, uint16_t value[3], t_float scale) + +static void wiimote_cwiid_balance_output(t_wiimote *x, t_symbol*s, uint16_t value, uint16_t calibration[3]) { - t_atom ap[4]; - t_float a = scale*value[CWIID_X]; - t_float b = scale*value[CWIID_Y]; - t_float c = scale*value[CWIID_Z]; + /* + 1700 appears to be the step the calibrations are against. + 17kg per sensor is 68kg, 1/2 of the advertised Japanese weight limit. + */ +#define WIIMOTE_BALANCE_CALWEIGHT 1700.f + t_atom ap[2]; + t_float weight=0; + t_float falue=value; + if(calibration) { + if(valueoutlet_data, gensym("balance"), 4, ap); + SETFLOAT (ap+1, weight); + outlet_anything(x->outlet_data, gensym("balance"), 2, ap); } -static void wiiremote_cwiid_balance(t_wiiremote *x, struct cwiid_balance_mesg *mesg) +static void wiimote_cwiid_balance(t_wiimote *x, struct cwiid_balance_mesg *mesg) { - t_float scale = 1.f / ((uint16_t)0xFFFF); - wiiremote_cwiid_balance_output(x, gensym("right_top"), &mesg->right_top, scale); - wiiremote_cwiid_balance_output(x, gensym("right_bottom"), &mesg->right_bottom, scale); - wiiremote_cwiid_balance_output(x, gensym("left_top"), &mesg->left_top, scale); - wiiremote_cwiid_balance_output(x, gensym("left_bottom"), &mesg->left_bottom, scale); + wiimote_cwiid_balance_output(x, gensym("right_top"), mesg->right_top, x->balance_cal.right_top); + wiimote_cwiid_balance_output(x, gensym("right_bottom"), mesg->right_bottom, x->balance_cal.right_bottom); + wiimote_cwiid_balance_output(x, gensym("left_top"), mesg->left_top, x->balance_cal.left_top); + wiimote_cwiid_balance_output(x, gensym("left_bottom"), mesg->left_bottom, x->balance_cal.left_bottom); } #endif #ifdef CWIID_RPT_MOTIONPLUS -static void wiiremote_cwiid_motionplus(t_wiiremote *x, struct cwiid_motionplus_mesg *mesg) +static void wiimote_cwiid_motionplus(t_wiimote *x, struct cwiid_motionplus_mesg *mesg) { t_atom ap[4]; t_float scale = 1.f;// / ((uint16_t)0xFFFF); @@ -379,17 +391,18 @@ static void wiiremote_cwiid_motionplus(t_wiiremote *x, struct cwiid_motionplus_m -static void wiiremote_cwiid_message(t_wiiremote *x, union cwiid_mesg mesg) { +static void wiimote_cwiid_message(t_wiimote *x, union cwiid_mesg mesg) { switch (mesg.type) { case CWIID_MESG_STATUS: - wiiremote_cwiid_battery(x, mesg.status_mesg.battery); + wiimote_cwiid_battery(x, mesg.status_mesg.battery); switch (mesg.status_mesg.ext_type) { case CWIID_EXT_NONE: post("No extension attached"); break; case CWIID_EXT_NUNCHUK: +#ifdef CWIID_RPT_NUNCHCUK post("Nunchuk extension attached"); - if(cwiid_get_acc_cal(x->wiiremote, CWIID_EXT_NUNCHUK, &x->nc_acc_cal)) { + if(cwiid_get_acc_cal(x->wiimote, CWIID_EXT_NUNCHUK, &x->nc_acc_cal)) { post("Unable to retrieve nunchuk calibration"); } else { post("Retrieved nunchuk calibration: zero=(%02d,%02d,%02d) one=(%02d,%02d,%02d)", @@ -401,16 +414,25 @@ static void wiiremote_cwiid_message(t_wiiremote *x, union cwiid_mesg mesg) { x->nc_acc_cal.one [CWIID_Z]); } break; +#endif +#ifdef CWIID_RPT_CLASSIC case CWIID_EXT_CLASSIC: post("Classic controller attached. There is no support for this yet."); break; +#endif +#ifdef CWIID_RPT_BALANCE case CWIID_EXT_BALANCE: - post("Balance controller attached. There is no support for this yet."); + if(cwiid_get_balance_cal(x->wiimote, &x->balance_cal)) { + post("Unable to retrieve balance calibration"); + } break; +#endif +#ifdef CWIID_RPT_MOTIONPLUS case CWIID_EXT_MOTIONPLUS: post("MotionPlus controller attached."); /* no calibration needed for MotionPlus */ break; +#endif case CWIID_EXT_UNKNOWN: post("Unknown extension attached"); break; @@ -420,33 +442,33 @@ static void wiiremote_cwiid_message(t_wiiremote *x, union cwiid_mesg mesg) { } break; case CWIID_MESG_BTN: - wiiremote_cwiid_btn(x, &mesg.btn_mesg); + wiimote_cwiid_btn(x, &mesg.btn_mesg); break; case CWIID_MESG_ACC: - wiiremote_cwiid_acc(x, &mesg.acc_mesg); + wiimote_cwiid_acc(x, &mesg.acc_mesg); break; case CWIID_MESG_IR: - wiiremote_cwiid_ir(x, &mesg.ir_mesg); + wiimote_cwiid_ir(x, &mesg.ir_mesg); break; #ifdef CWIID_RPT_NUNCHUK case CWIID_MESG_NUNCHUK: - wiiremote_cwiid_nunchuk(x, &mesg.nunchuk_mesg); + wiimote_cwiid_nunchuk(x, &mesg.nunchuk_mesg); break; #endif #ifdef CWIID_RPT_CLASSIC case CWIID_MESG_CLASSIC: - wiiremote_cwiid_classic(x, &mesg.classic_mesg); + wiimote_cwiid_classic(x, &mesg.classic_mesg); // todo break; #endif #ifdef CWIID_RPT_MOTIONPLUS case CWIID_MESG_MOTIONPLUS: - wiiremote_cwiid_motionplus(x, &mesg.motionplus_mesg); + wiimote_cwiid_motionplus(x, &mesg.motionplus_mesg); break; #endif #ifdef CWIID_RPT_BALANCE case CWIID_MESG_BALANCE: - wiiremote_cwiid_balance(x, &mesg.balance_mesg); + wiimote_cwiid_balance(x, &mesg.balance_mesg); break; #endif default: @@ -472,24 +494,24 @@ static void print_timestamp(struct timespec*timestamp, struct timespec*reference // The CWiid library invokes a callback function whenever events are -// generated by the wiiremote. This function is specified when connecting -// to the wiiremote (in the cwiid_open function). +// generated by the wiimote. This function is specified when connecting +// to the wiimote (in the cwiid_open function). // Unfortunately, the mesg struct passed as an argument to the -// callback does not have a pointer to the wiiremote instance, and it -// is thus impossible to know which wiiremote-object has invoked the callback. +// callback does not have a pointer to the wiimote instance, and it +// is thus impossible to know which wiimote-object has invoked the callback. // For this case we provide a hard-coded set of wrapper callbacks to -// indicate which Pd wiiremote instance to control. +// indicate which Pd wiimote instance to control. -// So far I have only checked with one wiiremote +// So far I have only checked with one wiimote -/*void cwiid_callback(cwiid_wiiremote_t *wiimt, int mesg_count, union cwiid_mesg *mesg[], struct timespec *timestamp) +/*void cwiid_callback(cwiid_wiimote_t *wiimt, int mesg_count, union cwiid_mesg *mesg[], struct timespec *timestamp) */ -static void cwiid_callback(cwiid_wiimote_t *wiiremote, int mesg_count, +static void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg_array[], struct timespec *timestamp) { int i; - t_wiiremote *x=NULL; + t_wiimote *x=NULL; static struct timespec*ts=NULL; if(NULL==ts) { @@ -498,111 +520,135 @@ static void cwiid_callback(cwiid_wiimote_t *wiiremote, int mesg_count, ts->tv_nsec =timestamp->tv_nsec; } - if(g_wiiremoteList==NULL||wiiremote==NULL) { + if(g_wiimoteList==NULL||wiimote==NULL) { post("no wii's known"); return; } - x=getWiiremoteObject(cwiid_get_id(wiiremote)); + x=getWiimoteObject(cwiid_get_id(wiimote)); if(NULL==x) { - post("no wiiremote loaded: %d%",cwiid_get_id(wiiremote)); + post("no wiimote loaded: %d%",cwiid_get_id(wiimote)); return; } //print_timestamp(timestamp, ts); for (i=0; i < mesg_count; i++) { - wiiremote_cwiid_message(x, mesg_array[i]); + wiimote_cwiid_message(x, mesg_array[i]); } } // ============================================================== -static void wiiremote_status(t_wiiremote *x) +static void wiimote_status(t_wiimote *x) { if(x->connected) { - if (cwiid_request_status(x->wiiremote)) { + if (cwiid_request_status(x->wiimote)) { pd_error(x, "error requesting status message"); } } } -static void wiiremote_resetReportMode(t_wiiremote *x) +static void wiimote_resetReportMode(t_wiimote *x) { if (x->connected) { - verbose(1, "changing report mode for Wii%02d to %d", x->wiiremoteID, x->reportMode); - if (cwiid_command(x->wiiremote, CWIID_CMD_RPT_MODE, x->reportMode)) { - post("wiiremote error: problem setting report mode."); + verbose(1, "changing report mode for Wii%02d to %d", x->wiimoteID, x->reportMode); + if (cwiid_command(x->wiimote, CWIID_CMD_RPT_MODE, x->reportMode)) { + post("wiimote error: problem setting report mode."); } } } -static void wiiremote_setReportMode(t_wiiremote *x, t_floatarg r) +static void wiimote_setReportMode(t_wiimote *x, t_floatarg r) { if (r >= 0) { x->reportMode = (int) r; - wiiremote_resetReportMode(x); + wiimote_resetReportMode(x); } else { return; } } -static void wiiremote_report(t_wiiremote*x, int flag, int onoff) +static void wiimote_report(t_wiimote*x, t_symbol*s, int onoff) { - if(onoff) { - x->reportMode |= flag; - } else { - x->reportMode &= ~flag; - } - wiiremote_resetReportMode(x); + int flag=-1; + if(gensym("status")==s) flag=CWIID_RPT_STATUS; + else if(gensym("button")==s) flag=CWIID_RPT_BTN; + else if(gensym("acceleration")==s) flag=CWIID_RPT_ACC; + else if(gensym("ir")==s) flag=CWIID_RPT_IR; +#ifdef CWIID_RPT_NUNCHUK + else if(gensym("nunchuk")==s) flag=CWIID_RPT_NUNCHUK; +#endif +#ifdef CWIID_RPT_CLASSIC + else if(gensym("classic")==s) flag=CWIID_RPT_CLASSIC; +#endif +#ifdef CWIID_RPT_BALANCE + else if(gensym("balance")==s) flag=CWIID_RPT_BALANCE; +#endif +#ifdef CWIID_RPT_MOTIONPLUS + else if(gensym("motionplus")==s) flag=CWIID_RPT_MOTIONPLUS; +#endif + else if(gensym("ext")==s) flag=CWIID_RPT_EXT; + else { + pd_error(x, "unknown report mode '%s'", s->s_name); + } + + if(flag!=-1) { + if(onoff) { + x->reportMode |= flag; + } else { + x->reportMode &= ~flag; + } + } + wiimote_resetReportMode(x); } -static void wiiremote_reportAcceleration(t_wiiremote *x, t_floatarg f) +static void wiimote_reportAcceleration(t_wiimote *x, t_floatarg f) { - wiiremote_report(x, CWIID_RPT_ACC, f); + wiimote_report(x, gensym("acceleration"), f); } -static void wiiremote_reportIR(t_wiiremote *x, t_floatarg f) +static void wiimote_reportIR(t_wiimote *x, t_floatarg f) { - wiiremote_report(x, CWIID_RPT_IR, f); + wiimote_report(x, gensym("ir"), f); } -static void wiiremote_reportNunchuk(t_wiiremote *x, t_floatarg f) +static void wiimote_reportNunchuk(t_wiimote *x, t_floatarg f) { - wiiremote_report(x, CWIID_RPT_EXT, f); + wiimote_report(x, gensym("nunchuck"), f); } -static void wiiremote_reportMotionplus(t_wiiremote *x, t_floatarg f) +static void wiimote_reportMotionplus(t_wiimote *x, t_floatarg f) { #ifdef CWIID_RPT_MOTIONPLUS int flag=f; if (x->connected) { - verbose(1, "changing motionplus report mode for Wii%02d to %d", x->wiiremoteID, flag); + verbose(1, "changing motionplus report mode for Wii%02d to %d", x->wiimoteID, flag); int err=0; if(flag) { - err=cwiid_enable(x->wiiremote, CWIID_FLAG_MOTIONPLUS); + err=cwiid_enable(x->wiimote, CWIID_FLAG_MOTIONPLUS); } else { - err=cwiid_disable(x->wiiremote, CWIID_FLAG_MOTIONPLUS); + err=cwiid_disable(x->wiimote, CWIID_FLAG_MOTIONPLUS); } if(err) { pd_error(x, "turning %s motionplus returned %d", (flag?"on":"off"), err); } else { - wiiremote_report(x, CWIID_RPT_MOTIONPLUS, f); + wiimote_report(x, gensym("motionplus"), f); } } #endif } -static void wiiremote_setRumble(t_wiiremote *x, t_floatarg f) +static void wiimote_setRumble(t_wiimote *x, t_floatarg f) { if (x->connected) { - if (cwiid_command(x->wiiremote, CWIID_CMD_RUMBLE, f)) post("wiiremote error: problem setting rumble."); + if (cwiid_command(x->wiimote, CWIID_CMD_RUMBLE, f)) post("wiimote error: problem setting rumble."); } } -static void wiiremote_setLED(t_wiiremote *x, t_floatarg f) +static void wiimote_setLED(t_wiimote *x, t_floatarg f) { // some possible values: // CWIID_LED0_ON 0x01 @@ -611,7 +657,7 @@ static void wiiremote_setLED(t_wiiremote *x, t_floatarg f) // CWIID_LED3_ON 0x08 if (x->connected) { - if (cwiid_command(x->wiiremote, CWIID_CMD_LED, f)) post("wiiremote error: problem setting LED."); + if (cwiid_command(x->wiimote, CWIID_CMD_LED, f)) post("wiimote error: problem setting LED."); } } @@ -620,13 +666,13 @@ static void wiiremote_setLED(t_wiiremote *x, t_floatarg f) // ============================================================== -// The following function attempts to connect to a wiiremote at a +// The following function attempts to connect to a wiimote at a // specific address, provided as an argument. eg, 00:19:1D:70:CE:72 // This address can be discovered by running the following command // in a console: // hcitool scan | grep Nintendo -static void wiiremote_doConnect(t_wiiremote *x, t_symbol *addr, t_symbol *dongaddr) +static void wiimote_doConnect(t_wiimote *x, t_symbol *addr, t_symbol *dongaddr) { bdaddr_t bdaddr; unsigned int flags = CWIID_FLAG_MESG_IFC; @@ -657,31 +703,31 @@ static void wiiremote_doConnect(t_wiiremote *x, t_symbol *addr, t_symbol *dongad #if 0 - x->wiiremote = cwiid_open(&bdaddr, dong_bdaddr_ptr, flags); + x->wiimote = cwiid_open(&bdaddr, dong_bdaddr_ptr, flags); #else -#warning multi-dongle support... - x->wiiremote = cwiid_open(&bdaddr, flags); +#warning multi-dongle support missing... + x->wiimote = cwiid_open(&bdaddr, flags); #endif - if(NULL==x->wiiremote) { - post("wiiremote error: unable to connect"); + if(NULL==x->wiimote) { + post("wiimote error: unable to connect"); return; } - if(!addWiiremoteObject(x, cwiid_get_id(x->wiiremote))) { - cwiid_close(x->wiiremote); - x->wiiremote=NULL; + if(!addWiimoteObject(x, cwiid_get_id(x->wiimote))) { + cwiid_close(x->wiimote); + x->wiimote=NULL; return; } - x->wiiremoteID= cwiid_get_id(x->wiiremote); + x->wiimoteID= cwiid_get_id(x->wiimote); - post("wiiremote %i is successfully connected", x->wiiremoteID); + post("wiimote %i is successfully connected", x->wiimoteID); - if(cwiid_get_acc_cal(x->wiiremote, CWIID_EXT_NONE, &x->acc_cal)) { + if(cwiid_get_acc_cal(x->wiimote, CWIID_EXT_NONE, &x->acc_cal)) { post("Unable to retrieve accelerometer calibration"); } else { - post("Retrieved wiiremote calibration: zero=(%02d,%02d,%02d) one=(%02d,%02d,%02d)", + post("Retrieved wiimote calibration: zero=(%02d,%02d,%02d) one=(%02d,%02d,%02d)", x->acc_cal.zero[CWIID_X], x->acc_cal.zero[CWIID_Y], x->acc_cal.zero[CWIID_Z], @@ -694,41 +740,41 @@ static void wiiremote_doConnect(t_wiiremote *x, t_symbol *addr, t_symbol *dongad x->reportMode |= CWIID_RPT_STATUS; x->reportMode |= CWIID_RPT_BTN; - wiiremote_resetReportMode(x); + wiimote_resetReportMode(x); - if (cwiid_set_mesg_callback(x->wiiremote, &cwiid_callback)) { + if (cwiid_set_mesg_callback(x->wiimote, &cwiid_callback)) { pd_error(x, "Unable to set message callback"); } } -// The following function attempts to discover a wiiremote. It requires -// that the user puts the wiiremote into 'discoverable' mode before being +// The following function attempts to discover a wiimote. It requires +// that the user puts the wiimote into 'discoverable' mode before being // called. This is done by pressing the red button under the battery // cover, or by pressing buttons 1 and 2 simultaneously. // TODO: Without pressing the buttons, I get a segmentation error. So far, I don't know why. -static void wiiremote_discover(t_wiiremote *x) +static void wiimote_discover(t_wiimote *x) { - post("Put the wiiremote into discover mode by pressing buttons 1 and 2 simultaneously."); + post("Put the wiimote into discover mode by pressing buttons 1 and 2 simultaneously."); - wiiremote_doConnect(x, NULL, gensym("NULL")); + wiimote_doConnect(x, NULL, gensym("NULL")); if (!(x->connected)) { - post("Error: could not find any wiiremotes. Please ensure that bluetooth is enabled, and that the 'hcitool scan' command lists your Nintendo device."); + post("Error: could not find any wiimotes. Please ensure that bluetooth is enabled, and that the 'hcitool scan' command lists your Nintendo device."); } } -static void wiiremote_doDisconnect(t_wiiremote *x) +static void wiimote_doDisconnect(t_wiimote *x) { if (x->connected) { - if (cwiid_close(x->wiiremote)) { - post("wiiremote error: problems when disconnecting."); + if (cwiid_close(x->wiimote)) { + post("wiimote error: problems when disconnecting."); } else { post("disconnect successfull, resetting values"); - removeWiiremoteObject(x); + removeWiimoteObject(x); x->connected = 0; } } @@ -740,16 +786,16 @@ static void wiiremote_doDisconnect(t_wiiremote *x) // ============================================================== // ============================================================== -static void *wiiremote_new(t_symbol*s, int argc, t_atom *argv) +static void *wiimote_new(t_symbol*s, int argc, t_atom *argv) { - t_wiiremote *x = (t_wiiremote *)pd_new(wiiremote_class); + t_wiimote *x = (t_wiimote *)pd_new(wiimote_class); // create outlets: x->outlet_data = outlet_new(&x->x_obj, NULL); // initialize toggles: x->connected = 0; - x->wiiremoteID = -1; + x->wiimoteID = -1; // connect if user provided an address as an argument: @@ -758,9 +804,9 @@ static void *wiiremote_new(t_symbol*s, int argc, t_atom *argv) post("[%s] connecting to provided address...", s->s_name); if (argv->a_type == A_SYMBOL) { - wiiremote_doConnect(x, NULL, atom_getsymbol(argv)); + wiimote_doConnect(x, NULL, atom_getsymbol(argv)); } else { - error("[wiiremote] expects either no argument, or a bluetooth address as an argument. eg, 00:19:1D:70:CE:72"); + error("[wiimote] expects either no argument, or a bluetooth address as an argument. eg, 00:19:1D:70:CE:72"); return NULL; } } @@ -768,38 +814,40 @@ static void *wiiremote_new(t_symbol*s, int argc, t_atom *argv) } -static void wiiremote_free(t_wiiremote* x) +static void wiimote_free(t_wiimote* x) { - wiiremote_doDisconnect(x); + wiimote_doDisconnect(x); } -void wiiremote_setup(void) +void wiimote_setup(void) { - wiiremote_class = class_new(gensym("wiiremote"), (t_newmethod)wiiremote_new, (t_method)wiiremote_free, sizeof(t_wiiremote), CLASS_DEFAULT, A_GIMME, 0); + wiimote_class = class_new(gensym("wiimote"), (t_newmethod)wiimote_new, (t_method)wiimote_free, sizeof(t_wiimote), CLASS_DEFAULT, A_GIMME, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_debug, gensym("debug"), 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_status, gensym("status"), 0); + class_addmethod(wiimote_class, (t_method) wiimote_debug, gensym("debug"), 0); + class_addmethod(wiimote_class, (t_method) wiimote_status, gensym("status"), 0); /* connection settings */ - class_addmethod(wiiremote_class, (t_method) wiiremote_doConnect, gensym("connect"), A_DEFSYMBOL, A_DEFSYMBOL, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_doDisconnect, gensym("disconnect"), 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_discover, gensym("discover"), 0); + class_addmethod(wiimote_class, (t_method) wiimote_doConnect, gensym("connect"), A_DEFSYMBOL, A_DEFSYMBOL, 0); + class_addmethod(wiimote_class, (t_method) wiimote_doDisconnect, gensym("disconnect"), 0); + class_addmethod(wiimote_class, (t_method) wiimote_discover, gensym("discover"), 0); /* query data */ - class_addmethod(wiiremote_class, (t_method) wiiremote_setReportMode, gensym("setReportMode"), A_DEFFLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_reportAcceleration, gensym("reportAcceleration"), A_DEFFLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_reportIR, gensym("reportIR"), A_DEFFLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_report, gensym("report"), A_SYMBOL, A_FLOAT, 0); + + class_addmethod(wiimote_class, (t_method) wiimote_setReportMode, gensym("setReportMode"), A_FLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_reportAcceleration, gensym("reportAcceleration"), A_FLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_reportIR, gensym("reportIR"), A_FLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_reportNunchuk, gensym("reportNunchuck"), A_DEFFLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_reportNunchuk, gensym("reportNunchuk"), A_DEFFLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_reportMotionplus, gensym("reportMotionplus"), A_DEFFLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_reportNunchuk, gensym("reportNunchuck"), A_FLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_reportNunchuk, gensym("reportNunchuk"), A_FLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_reportMotionplus, gensym("reportMotionplus"), A_FLOAT, 0); /* set things on the wiimote */ - class_addmethod(wiiremote_class, (t_method) wiiremote_setRumble, gensym("setRumble"), A_DEFFLOAT, 0); - class_addmethod(wiiremote_class, (t_method) wiiremote_setLED, gensym("setLED"), A_DEFFLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_setRumble, gensym("setRumble"), A_FLOAT, 0); + class_addmethod(wiimote_class, (t_method) wiimote_setLED, gensym("setLED"), A_FLOAT, 0); } -- cgit v1.2.1