Added nunchuck acceleration support to UDPWii . Moved some UDPWii stuff to the nunchuck attachment because I needed calibration data for the nunchuck...

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5878 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
dapetcu21 2010-07-14 17:33:14 +00:00
parent 61bd545f80
commit bbb48603be
8 changed files with 97 additions and 32 deletions

View File

@ -66,7 +66,7 @@ THREAD_RETURN UDPWiiThread(void* arg)
UDPWiimote::UDPWiimote(const char *_port) : UDPWiimote::UDPWiimote(const char *_port) :
port(_port), port(_port),
d(new _d) ,x(0),y(0),z(0),nunX(0),nunY(0), d(new _d) ,x(0),y(0),z(0),naX(0),naY(0),naZ(0),nunX(0),nunY(0),
pointerX(-0.1),pointerY(-0.1),nunMask(0),mask(0),time(0) pointerX(-0.1),pointerY(-0.1),nunMask(0),mask(0),time(0)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -212,6 +212,7 @@ UDPWiimote::~UDPWiimote()
#define BUTT_FLAG (1<<1) #define BUTT_FLAG (1<<1)
#define IR_FLAG (1<<2) #define IR_FLAG (1<<2)
#define NUN_FLAG (1<<3) #define NUN_FLAG (1<<3)
#define NUNACCEL_FLAG (1<<4)
int UDPWiimote::pharsePacket(u8 * bf, size_t size) int UDPWiimote::pharsePacket(u8 * bf, size_t size)
{ {
@ -253,6 +254,17 @@ int UDPWiimote::pharsePacket(u8 * bf, size_t size)
nunX=((double)((s32)ntohl(*p)))/1048576; p++; nunX=((double)((s32)ntohl(*p)))/1048576; p++;
nunY=((double)((s32)ntohl(*p)))/1048576; p++; nunY=((double)((s32)ntohl(*p)))/1048576; p++;
} }
if (bf[2]&NUNACCEL_FLAG)
{
if ((size-(((u8*)p)-bf))<12) return -1;
double ux,uy,uz;
ux=(double)((s32)ntohl(*p)); p++;
uy=(double)((s32)ntohl(*p)); p++;
uz=(double)((s32)ntohl(*p)); p++;
naX=ux/1048576; //packet accel data
naY=uy/1048576;
naZ=uz/1048576;
}
return 0; return 0;
} }
@ -298,6 +310,16 @@ void UDPWiimote::getNunchuck(float &_x, float &_y, u8 &_mask)
d->mutex.Leave(); d->mutex.Leave();
} }
void UDPWiimote::getNunchuckAccel(float &_x, float &_y, float &_z)
{
d->mutex.Enter();
_x=(float)naX;
_y=(float)naY;
_z=(float)naZ;
d->mutex.Leave();
}
const char * UDPWiimote::getPort() const char * UDPWiimote::getPort()
{ {
return port.c_str(); return port.c_str();

View File

@ -28,6 +28,7 @@ public:
u32 getButtons(); u32 getButtons();
void getNunchuck(float &x, float &y, u8 &mask); void getNunchuck(float &x, float &y, u8 &mask);
void getIR(float &x, float &y); void getIR(float &x, float &y);
void getNunchuckAccel(float &x, float &y, float &z);
int getErrNo() {return err;}; int getErrNo() {return err;};
const char * getPort(); const char * getPort();
private: private:
@ -37,6 +38,7 @@ private:
struct _d; //using pimpl because Winsock2.h doesen't have include guards -_- struct _d; //using pimpl because Winsock2.h doesen't have include guards -_-
_d *d; _d *d;
double x,y,z; double x,y,z;
double naX,naY,naZ;
double nunX,nunY; double nunX,nunY;
double pointerX,pointerY; double pointerX,pointerY;
u8 nunMask; u8 nunMask;

View File

@ -27,17 +27,21 @@ void UDPWrapper::LoadConfig(IniFile::Section *sec, const std::string& defdev, co
std::string group( base + name ); group += "/"; std::string group( base + name ); group += "/";
int _updAccel,_updIR,_updButt,_udpEn; int _updAccel,_updIR,_updButt,_udpEn,_updNun,_updNunAccel;
sec->Get((group + "Enable").c_str(),&_udpEn, 0); sec->Get((group + "Enable").c_str(),&_udpEn, 0);
sec->Get((group + "Port").c_str(), &port, DefaultPort(index)); sec->Get((group + "Port").c_str(), &port, DefaultPort(index));
sec->Get((group + "Update_Accel").c_str(), &_updAccel, 1); sec->Get((group + "Update_Accel").c_str(), &_updAccel, 1);
sec->Get((group + "Update_IR").c_str(), &_updIR, 1); sec->Get((group + "Update_IR").c_str(), &_updIR, 1);
sec->Get((group + "Update_Butt").c_str(), &_updButt, 1); sec->Get((group + "Update_Butt").c_str(), &_updButt, 1);
sec->Get((group + "Update_Nunchuk").c_str(), &_updNun, 1);
sec->Get((group + "Update_NunchukAccel").c_str(), &_updNunAccel, 0);
udpEn=(_udpEn>0); udpEn=(_udpEn>0);
updAccel=(_updAccel>0); updAccel=(_updAccel>0);
updIR=(_updIR>0); updIR=(_updIR>0);
updButt=(_updButt>0); updButt=(_updButt>0);
updNun=(_updNun>0);
updNunAccel=(_updNunAccel>0);
Refresh(); Refresh();
} }
@ -47,11 +51,13 @@ void UDPWrapper::SaveConfig(IniFile::Section *sec, const std::string& defdev, co
{ {
ControlGroup::SaveConfig(sec,defdev,base); ControlGroup::SaveConfig(sec,defdev,base);
std::string group( base + name ); group += "/"; std::string group( base + name ); group += "/";
sec->Set((group + "Enable").c_str(), (int)udpEn, 0); sec->Set((group + "Enable").c_str(), (int)udpEn);
sec->Set((group + "Port").c_str(), port, DefaultPort(index)); sec->Set((group + "Port").c_str(), port);
sec->Set((group + "Update_Accel").c_str(), (int)updAccel, 1); sec->Set((group + "Update_Accel").c_str(), (int)updAccel);
sec->Set((group + "Update_IR").c_str(), (int)updIR, 1); sec->Set((group + "Update_IR").c_str(), (int)updIR);
sec->Set((group + "Update_Butt").c_str(), (int)updButt, 1); sec->Set((group + "Update_Butt").c_str(), (int)updButt);
sec->Set((group + "Update_Nunchuk").c_str(), (int)updNun);
sec->Set((group + "Update_NunchukAccel").c_str(), (int)updNunAccel);
} }
@ -101,6 +107,8 @@ public:
wxCheckBox * butt; wxCheckBox * butt;
wxCheckBox * accel; wxCheckBox * accel;
wxCheckBox * point; wxCheckBox * point;
wxCheckBox * nun;
wxCheckBox * nunaccel;
wxTextCtrl * port_tbox; wxTextCtrl * port_tbox;
}; };
@ -118,6 +126,9 @@ UDPConfigDiag::UDPConfigDiag(wxWindow * const parent, UDPWrapper * _wrp) :
butt = new wxCheckBox(this,wxID_ANY,wxT("Update Buttons")); butt = new wxCheckBox(this,wxID_ANY,wxT("Update Buttons"));
accel = new wxCheckBox(this,wxID_ANY,wxT("Update Acceleration")); accel = new wxCheckBox(this,wxID_ANY,wxT("Update Acceleration"));
point = new wxCheckBox(this,wxID_ANY,wxT("Update IR Pointer")); point = new wxCheckBox(this,wxID_ANY,wxT("Update IR Pointer"));
nun = new wxCheckBox(this,wxID_ANY,wxT("Update Nunchuk"));
nunaccel = new wxCheckBox(this,wxID_ANY,wxT("Update Nunchuk Acceleration"));
wxButton * ok_butt = new wxButton(this,wxID_ANY,wxT("OK")); wxButton * ok_butt = new wxButton(this,wxID_ANY,wxT("OK"));
@ -130,6 +141,8 @@ UDPConfigDiag::UDPConfigDiag(wxWindow * const parent, UDPWrapper * _wrp) :
_connect_macro_(butt,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); _connect_macro_(butt,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this);
_connect_macro_(accel,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); _connect_macro_(accel,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this);
_connect_macro_(point,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this); _connect_macro_(point,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this);
_connect_macro_(nun,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this);
_connect_macro_(nunaccel,UDPConfigDiag::ChangeUpdateFlags ,wxEVT_COMMAND_CHECKBOX_CLICKED, this);
_connect_macro_(ok_butt,UDPConfigDiag::OKPressed, wxEVT_COMMAND_BUTTON_CLICKED, this); _connect_macro_(ok_butt,UDPConfigDiag::OKPressed, wxEVT_COMMAND_BUTTON_CLICKED, this);
_connect_macro_(port_tbox, UDPConfigDiag::ChangeState, wxEVT_COMMAND_TEXT_UPDATED, this); _connect_macro_(port_tbox, UDPConfigDiag::ChangeState, wxEVT_COMMAND_TEXT_UPDATED, this);
@ -137,13 +150,17 @@ UDPConfigDiag::UDPConfigDiag(wxWindow * const parent, UDPWrapper * _wrp) :
butt->SetValue(wrp->updButt); butt->SetValue(wrp->updButt);
accel->SetValue(wrp->updAccel); accel->SetValue(wrp->updAccel);
point->SetValue(wrp->updIR); point->SetValue(wrp->updIR);
nun->SetValue(wrp->updNun);
nunaccel->SetValue(wrp->updNunAccel);
sizer1->Add(enable,1,wxALL | wxEXPAND,5); sizer1->Add(enable,1,wxALL | wxEXPAND,5);
sizer1->Add(port_sizer, 1,wxDOWN | wxLEFT| wxRIGHT | wxEXPAND,5); sizer1->Add(port_sizer, 1,wxDOWN | wxLEFT| wxRIGHT | wxEXPAND,5);
sizer2->Add(butt,1,wxALL | wxEXPAND,5); sizer2->Add(butt,1,wxALL | wxEXPAND,5);
sizer2->Add(accel,1,wxALL | wxEXPAND,5); sizer2->Add(accel,1,wxALL | wxEXPAND,5);
sizer2->Add(point,1,wxALL | wxEXPAND,5); sizer2->Add(point,1,wxALL | wxEXPAND,5);
sizer2->Add(nun,1,wxALL | wxEXPAND,5);
sizer2->Add(nunaccel,1,wxALL | wxEXPAND,5);
outer_sizer->Add(ok_butt,0, wxDOWN | wxLEFT| wxRIGHT | wxALIGN_RIGHT,10); outer_sizer->Add(ok_butt,0, wxDOWN | wxLEFT| wxRIGHT | wxALIGN_RIGHT,10);
@ -156,6 +173,8 @@ void UDPConfigDiag::ChangeUpdateFlags(wxCommandEvent & event)
wrp->updAccel=accel->GetValue(); wrp->updAccel=accel->GetValue();
wrp->updButt=butt->GetValue(); wrp->updButt=butt->GetValue();
wrp->updIR=point->GetValue(); wrp->updIR=point->GetValue();
wrp->updNun=nun->GetValue();
wrp->updNunAccel=nunaccel->GetValue();
} }
void UDPConfigDiag::ChangeState(wxCommandEvent & event) void UDPConfigDiag::ChangeState(wxCommandEvent & event)

View File

@ -26,7 +26,7 @@ class UDPWrapper : public ControllerEmu::ControlGroup
public: public:
UDPWiimote * inst; UDPWiimote * inst;
int index; int index;
bool updIR,updAccel,updButt,udpEn; //upd from update and udp from... well... UDP bool updIR,updAccel,updButt,updNun,updNunAccel,udpEn; //upd from update and udp from... well... UDP
std::string port; std::string port;
UDPWrapper(int index, const char* const _name); UDPWrapper(int index, const char* const _name);

View File

@ -27,7 +27,7 @@ static const u8 nunchuk_button_bitmasks[] =
Nunchuk::BUTTON_Z, Nunchuk::BUTTON_Z,
}; };
Nunchuk::Nunchuk() : Attachment( "Nunchuk" ) Nunchuk::Nunchuk(UDPWrapper * wrp) : Attachment( "Nunchuk" ) , udpWrap(wrp)
{ {
// buttons // buttons
groups.push_back( m_buttons = new Buttons( "Buttons" ) ); groups.push_back( m_buttons = new Buttons( "Buttons" ) );
@ -80,6 +80,39 @@ void Nunchuk::GetState( u8* const data, const bool focus )
// flip the button bits :/ // flip the button bits :/
ncdata->bt ^= 0x03; ncdata->bt ^= 0x03;
//UDPNunchuk stuff
if (udpWrap->inst)
{
if (udpWrap->updNun)
{
u8 mask;
float x, y;
udpWrap->inst->getNunchuck(x, y, mask);
// buttons
if (mask & UDPWM_NC)
ncdata->bt &= ~WiimoteEmu::Nunchuk::BUTTON_C;
if (mask & UDPWM_NZ)
ncdata->bt &= ~WiimoteEmu::Nunchuk::BUTTON_Z;
// stick
if (ncdata->jx == 0x80 && ncdata->jy == 0x80)
{
ncdata->jx = u8(0x80 + x*127);
ncdata->jy = u8(0x80 + y*127);
}
}
if (udpWrap->updNunAccel)
{
const accel_cal * const calib = (accel_cal*)&reg[0x20];
wm_accel * const accel = (wm_accel*)&ncdata->ax;
float x,y,z;
udpWrap->inst->getNunchuckAccel(x,y,z);
accel->x=u8(x*(calib->one_g.x-calib->zero_g.x)+calib->zero_g.x);
accel->y=u8(y*(calib->one_g.y-calib->zero_g.y)+calib->zero_g.y);
accel->z=u8(z*(calib->one_g.z-calib->zero_g.z)+calib->zero_g.z);
}
}
//End UDPNunchuck
} }

View File

@ -3,13 +3,15 @@
#include "Attachment.h" #include "Attachment.h"
class UDPWrapper;
namespace WiimoteEmu namespace WiimoteEmu
{ {
class Nunchuk : public Attachment class Nunchuk : public Attachment
{ {
public: public:
Nunchuk(); Nunchuk(UDPWrapper * wrp);
virtual void GetState( u8* const data, const bool focus ); virtual void GetState( u8* const data, const bool focus );
enum enum
@ -28,6 +30,8 @@ private:
AnalogStick* m_stick; AnalogStick* m_stick;
unsigned int m_shake_step[3]; unsigned int m_shake_step[3];
UDPWrapper * udpWrap;
}; };
} }

View File

@ -47,7 +47,7 @@ namespace UDPTLayer
*y=-(_y*2-1); *y=-(_y*2-1);
*z=0; *z=0;
} }
} }
#endif #endif

View File

@ -288,7 +288,7 @@ Wiimote::Wiimote( const unsigned int index )
// extension // extension
groups.push_back( m_extension = new Extension( "Extension" ) ); groups.push_back( m_extension = new Extension( "Extension" ) );
m_extension->attachments.push_back( new WiimoteEmu::None() ); m_extension->attachments.push_back( new WiimoteEmu::None() );
m_extension->attachments.push_back( new WiimoteEmu::Nunchuk() ); m_extension->attachments.push_back( new WiimoteEmu::Nunchuk(m_udp) );
m_extension->attachments.push_back( new WiimoteEmu::Classic() ); m_extension->attachments.push_back( new WiimoteEmu::Classic() );
m_extension->attachments.push_back( new WiimoteEmu::Guitar() ); m_extension->attachments.push_back( new WiimoteEmu::Guitar() );
m_extension->attachments.push_back( new WiimoteEmu::Drums() ); m_extension->attachments.push_back( new WiimoteEmu::Drums() );
@ -601,25 +601,10 @@ void Wiimote::Update()
// ---- UDP Wiimote nunchuk stuff // ---- UDP Wiimote nunchuk stuff
// 1 == is hacky, for if nunchuk is attached // 1 == is hacky, for if nunchuk is attached
if (is_focus && 1 == m_extension->active_extension && m_udp->inst) //if (is_focus && 1 == m_extension->active_extension)
{ //{
wm_extension* const ncdata = (wm_extension*)(data + rpt.ext); // UDPTLayer::GetNunchuk(m_udp,(wm_extension*)(data + rpt.ext));
//}
u8 mask;
float x, y;
m_udp->inst->getNunchuck(x, y, mask);
// buttons
if (mask & UDPWM_NC)
ncdata->bt &= ~Nunchuk::BUTTON_C;
if (mask & UDPWM_NZ)
ncdata->bt &= ~Nunchuk::BUTTON_Z;
// stick
if (ncdata->jx == 0x80 && ncdata->jy == 0x80)
{
ncdata->jx = u8(0x80 + x*127);
ncdata->jy = u8(0x80 + y*127);
}
}
// ---- end UDP Wiimote // ---- end UDP Wiimote
// i dont think anything accesses the extension data like this, but ill support it. Indeed, commercial games don't do this. // i dont think anything accesses the extension data like this, but ill support it. Indeed, commercial games don't do this.