Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding Nunchuk stick calibration
because it's useful for the hybrid Wiimote mode
  • Loading branch information
john-peterson authored and RachelBryk committed Jul 27, 2013
1 parent 4aba013 commit 23f59a8
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 77 deletions.
18 changes: 14 additions & 4 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Attachment.cpp
Expand Up @@ -13,22 +13,32 @@ static const u8 nothing_id[] = { 0x00, 0x00, 0x00, 0x00, 0x2e, 0x2e };
// The id for a partially inserted extension
static const u8 partially_id[] = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };

Attachment::Attachment( const char* const _name ) : name( _name )
Attachment::Attachment( const char* const _name, WiimoteEmu::ExtensionReg& _reg )
: name( _name ), reg( _reg )
{
reg.resize( WIIMOTE_REG_EXT_SIZE, 0);
memset(id, 0, sizeof(id));
memset(calibration, 0, sizeof(calibration));
}

None::None() : Attachment( "None" )
None::None( WiimoteEmu::ExtensionReg& _reg ) : Attachment( "None", _reg )
{
// set up register
memcpy( &reg[0xfa], nothing_id, sizeof(nothing_id) );
memcpy(&id, nothing_id, sizeof(nothing_id));
}

std::string Attachment::GetName() const
{
return name;
}

void Attachment::Reset()
{
// set up register
memset( &reg, 0, WIIMOTE_REG_EXT_SIZE );
memcpy( &reg.constant_id, id, sizeof(id) );
memcpy( &reg.calibration, calibration, sizeof(calibration) );
}

}

void ControllerEmu::Extension::GetState( u8* const data, const bool focus )
Expand Down
12 changes: 8 additions & 4 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Attachment.h
Expand Up @@ -14,19 +14,23 @@ namespace WiimoteEmu
class Attachment : public ControllerEmu
{
public:
Attachment( const char* const _name );
Attachment( const char* const _name, WiimoteEmu::ExtensionReg& _reg );

virtual void GetState( u8* const data, const bool focus = true ) {}
void Reset();
std::string GetName() const;

const char* const name;
std::vector<u8> reg;
const char* const name;
WiimoteEmu::ExtensionReg& reg;

u8 id[6];
u8 calibration[0x10];
};

class None : public Attachment
{
public:
None();
None( WiimoteEmu::ExtensionReg& _reg );
};

}
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Classic.cpp
Expand Up @@ -53,7 +53,7 @@ static const u16 classic_dpad_bitmasks[] =
Classic::PAD_UP, Classic::PAD_DOWN, Classic::PAD_LEFT, Classic::PAD_RIGHT
};

Classic::Classic() : Attachment(_trans("Classic"))
Classic::Classic(WiimoteEmu::ExtensionReg& _reg) : Attachment(_trans("Classic"), _reg)
{
// buttons
groups.push_back(m_buttons = new Buttons("Buttons"));
Expand All @@ -76,9 +76,9 @@ Classic::Classic() : Attachment(_trans("Classic"))

// set up register
// calibration
memcpy(&reg[0x20], classic_calibration, sizeof(classic_calibration));
memcpy(&calibration, classic_calibration, sizeof(classic_calibration));
// id
memcpy(&reg[0xfa], classic_id, sizeof(classic_id));
memcpy(&id, classic_id, sizeof(classic_id));
}

void Classic::GetState(u8* const data, const bool focus)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Classic.h
Expand Up @@ -10,7 +10,7 @@ namespace WiimoteEmu
class Classic : public Attachment
{
public:
Classic();
Classic(WiimoteEmu::ExtensionReg& _reg);
void GetState( u8* const data, const bool focus );

enum
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Drums.cpp
Expand Up @@ -32,7 +32,7 @@ static const u16 drum_button_bitmasks[] =
Drums::BUTTON_PLUS,
};

Drums::Drums() : Attachment(_trans("Drums"))
Drums::Drums(WiimoteEmu::ExtensionReg& _reg) : Attachment(_trans("Drums"), _reg)
{
// pads
groups.push_back(m_pads = new Buttons(_trans("Pads")));
Expand All @@ -49,7 +49,7 @@ Drums::Drums() : Attachment(_trans("Drums"))

// set up register
// id
memcpy(&reg[0xfa], drums_id, sizeof(drums_id));
memcpy(&id, drums_id, sizeof(drums_id));
}

void Drums::GetState(u8* const data, const bool focus)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Drums.h
Expand Up @@ -10,7 +10,7 @@ namespace WiimoteEmu
class Drums : public Attachment
{
public:
Drums();
Drums(WiimoteEmu::ExtensionReg& _reg);
void GetState( u8* const data, const bool focus );

enum
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Guitar.cpp
Expand Up @@ -36,7 +36,7 @@ static const u16 guitar_strum_bitmasks[] =
Guitar::BAR_DOWN,
};

Guitar::Guitar() : Attachment(_trans("Guitar"))
Guitar::Guitar(WiimoteEmu::ExtensionReg& _reg) : Attachment(_trans("Guitar"), _reg)
{
// frets
groups.push_back(m_frets = new Buttons(_trans("Frets")));
Expand All @@ -62,7 +62,7 @@ Guitar::Guitar() : Attachment(_trans("Guitar"))

// set up register
// id
memcpy(&reg[0xfa], guitar_id, sizeof(guitar_id));
memcpy(&id, guitar_id, sizeof(guitar_id));
}

void Guitar::GetState(u8* const data, const bool focus)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Guitar.h
Expand Up @@ -10,7 +10,7 @@ namespace WiimoteEmu
class Guitar : public Attachment
{
public:
Guitar();
Guitar(WiimoteEmu::ExtensionReg& _reg);
void GetState( u8* const data, const bool focus );

enum
Expand Down
41 changes: 34 additions & 7 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Nunchuk.cpp
Expand Up @@ -31,7 +31,8 @@ static const u8 nunchuk_button_bitmasks[] =
Nunchuk::BUTTON_Z,
};

Nunchuk::Nunchuk(UDPWrapper *wrp) : Attachment(_trans("Nunchuk")) , m_udpWrap(wrp)
Nunchuk::Nunchuk(UDPWrapper *wrp, WiimoteEmu::ExtensionReg& _reg)
: Attachment(_trans("Nunchuk"), _reg) , m_udpWrap(wrp)
{
// buttons
groups.push_back(m_buttons = new Buttons("Buttons"));
Expand All @@ -55,9 +56,9 @@ Nunchuk::Nunchuk(UDPWrapper *wrp) : Attachment(_trans("Nunchuk")) , m_udpWrap(wr

// set up register
// calibration
memcpy(&reg[0x20], nunchuck_calibration, sizeof(nunchuck_calibration));
memcpy(&calibration, nunchuck_calibration, sizeof(nunchuck_calibration));
// id
memcpy(&reg[0xfa], nunchuck_id, sizeof(nunchuck_id));
memcpy(&id, nunchuck_id, sizeof(nunchuck_id));

// this should get set to 0 on disconnect, but it isn't, o well
memset(m_shake_step, 0, sizeof(m_shake_step));
Expand All @@ -68,11 +69,37 @@ void Nunchuk::GetState(u8* const data, const bool focus)
wm_extension* const ncdata = (wm_extension*)data;
ncdata->bt = 0;

// stick / not using calibration data for stick, o well
m_stick->GetState(&ncdata->jx, &ncdata->jy, 0x80, focus ? 127 : 0);

// stick
ControlState state[2];
m_stick->GetState(&state[0], &state[1], 0, 1);

nu_cal &cal = *(nu_cal*)&reg.calibration;
nu_js cal_js[2];
cal_js[0] = *&cal.jx;
cal_js[1] = *&cal.jy;

for (int i = 0; i < 2; i++) {
ControlState &s = *&state[i];
nu_js c = *&cal_js[i];
if (s < 0)
s = s * abs(c.min - c.center) + c.center;
else if (s > 0)
s = s * abs(c.max - c.center) + c.center;
else
s = c.center;
}

ncdata->jx = u8(trim(state[0]));
ncdata->jy = u8(trim(state[1]));

if (!focus)
{
ncdata->jx = cal.jx.center;
ncdata->jy = cal.jy.center;
}

AccelData accel;
accel_cal* calib = (accel_cal*)&reg[0x20];
accel_cal* calib = (accel_cal*)&reg.calibration[0];

// tilt
EmulateTilt(&accel, m_tilt, focus);
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Nunchuk.h
Expand Up @@ -15,7 +15,7 @@ namespace WiimoteEmu
class Nunchuk : public Attachment
{
public:
Nunchuk(UDPWrapper * wrp);
Nunchuk(UDPWrapper * wrp, WiimoteEmu::ExtensionReg& _reg);

virtual void GetState( u8* const data, const bool focus );

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Turntable.cpp
Expand Up @@ -30,7 +30,7 @@ static const char* const turntable_button_names[] =
"-", "+", _trans("Euphoria"),
};

Turntable::Turntable() : Attachment(_trans("Turntable"))
Turntable::Turntable(WiimoteEmu::ExtensionReg& _reg) : Attachment(_trans("Turntable"), _reg)
{
// buttons
groups.push_back(m_buttons = new Buttons("Buttons"));
Expand All @@ -53,7 +53,7 @@ Turntable::Turntable() : Attachment(_trans("Turntable"))

// set up register
// id
memcpy(&reg[0xfa], turntable_id, sizeof(turntable_id));
memcpy(&id, turntable_id, sizeof(turntable_id));
}

void Turntable::GetState(u8* const data, const bool focus)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Src/HW/WiimoteEmu/Attachment/Turntable.h
Expand Up @@ -10,7 +10,7 @@ namespace WiimoteEmu
class Turntable : public Attachment
{
public:
Turntable();
Turntable(WiimoteEmu::ExtensionReg& _reg);
void GetState(u8* const data, const bool focus);

enum
Expand Down
7 changes: 2 additions & 5 deletions Source/Core/Core/Src/HW/WiimoteEmu/EmuSubroutines.cpp
Expand Up @@ -851,11 +851,8 @@ void Wiimote::HandleExtensionSwap()
// set the wanted extension
m_extension->active_extension = m_extension->switch_extension;

// set register, I hate this
const std::vector<u8> &reg = ((WiimoteEmu::Attachment*)m_extension->attachments[m_extension->active_extension])->reg;
memset(&m_reg_ext, 0, WIIMOTE_REG_EXT_SIZE);
memcpy(&m_reg_ext, &reg[0], reg.size());

// reset register
((WiimoteEmu::Attachment*)m_extension->attachments[m_extension->active_extension])->Reset();
}
}

Expand Down
12 changes: 6 additions & 6 deletions Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.cpp
Expand Up @@ -286,12 +286,12 @@ Wiimote::Wiimote( const unsigned int index )

// extension
groups.push_back(m_extension = new Extension(_trans("Extension")));
m_extension->attachments.push_back(new WiimoteEmu::None());
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::Guitar());
m_extension->attachments.push_back(new WiimoteEmu::Drums());
m_extension->attachments.push_back(new WiimoteEmu::Turntable());
m_extension->attachments.push_back(new WiimoteEmu::None(m_reg_ext));
m_extension->attachments.push_back(new WiimoteEmu::Nunchuk(m_udp, m_reg_ext));
m_extension->attachments.push_back(new WiimoteEmu::Classic(m_reg_ext));
m_extension->attachments.push_back(new WiimoteEmu::Guitar(m_reg_ext));
m_extension->attachments.push_back(new WiimoteEmu::Drums(m_reg_ext));
m_extension->attachments.push_back(new WiimoteEmu::Turntable(m_reg_ext));

m_extension->settings.push_back(new ControlGroup::Setting(_trans("Motion Plus"), 0, 0, 1));

Expand Down
55 changes: 28 additions & 27 deletions Source/Core/Core/Src/HW/WiimoteEmu/WiimoteEmu.h
Expand Up @@ -17,7 +17,7 @@
#include <vector>
#include <queue>

// Registry sizes
// Registry sizes
#define WIIMOTE_EEPROM_SIZE (16*1024)
#define WIIMOTE_EEPROM_FREE_SIZE 0x1700
#define WIIMOTE_REG_SPEAKER_SIZE 10
Expand Down Expand Up @@ -47,6 +47,30 @@ struct ADPCMState
s32 predictor, step;
};

struct ExtensionReg
{
u8 unknown1[0x08];

// address 0x08
u8 controller_data[0x06];
u8 unknown2[0x12];

// address 0x20
u8 calibration[0x10];
u8 unknown3[0x10];

// address 0x40
u8 encryption_key[0x10];
u8 unknown4[0xA0];

// address 0xF0
u8 encryption;
u8 unknown5[0x9];

// address 0xFA
u8 constant_id[6];
};

extern const ReportFeatures reporting_mode_features[];

void EmulateShake(AccelData* const accel_data
Expand Down Expand Up @@ -143,15 +167,15 @@ friend void Spy(Wiimote* wm_, const void* data_, int size_);
ControlGroup* m_rumble;
Extension* m_extension;
ControlGroup* m_options;

// WiiMote accel data
AccelData m_accel;

// wiimote index, 0-3
const u8 m_index;

double ir_sin, ir_cos; //for the low pass filter

UDPWrapper* m_udp;

bool m_rumble_on;
Expand Down Expand Up @@ -201,30 +225,7 @@ friend void Spy(Wiimote* wm_, const void* data_, int size_);

} m_reg_ir;

struct ExtensionReg
{
u8 unknown1[0x08];

// address 0x08
u8 controller_data[0x06];
u8 unknown2[0x12];

// address 0x20
u8 calibration[0x10];
u8 unknown3[0x10];

// address 0x40
u8 encryption_key[0x10];
u8 unknown4[0xA0];

// address 0xF0
u8 encryption;
u8 unknown5[0x9];

// address 0xFA
u8 constant_id[6];

} m_reg_ext;
ExtensionReg m_reg_ext;

struct SpeakerReg
{
Expand Down

0 comments on commit 23f59a8

Please sign in to comment.