2010-04-13 05:15:38 +00:00
|
|
|
#include "Nunchuk.h"
|
|
|
|
|
2010-07-09 22:11:12 +00:00
|
|
|
#include "UDPWrapper.h"
|
|
|
|
#include "UDPWiimote.h"
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
namespace WiimoteEmu
|
|
|
|
{
|
|
|
|
|
2010-04-24 00:44:10 +00:00
|
|
|
static const u8 nunchuck_id[] = { 0x00, 0x00, 0xa4, 0x20, 0x00, 0x00 };
|
2010-04-13 05:15:38 +00:00
|
|
|
/* Default calibration for the nunchuck. It should be written to 0x20 - 0x3f of the
|
|
|
|
extension register. 0x80 is the neutral x and y accelerators and 0xb3 is the
|
|
|
|
neutral z accelerometer that is adjusted for gravity. */
|
|
|
|
static const u8 nunchuck_calibration[] =
|
|
|
|
{
|
|
|
|
0x80, 0x80, 0x80, 0x00, // accelerometer x, y, z neutral
|
|
|
|
0xb3, 0xb3, 0xb3, 0x00, // x, y, z g-force values
|
|
|
|
|
|
|
|
// 0x80 = analog stick x and y axis center
|
|
|
|
0xff, 0x00, 0x80,
|
|
|
|
0xff, 0x00, 0x80,
|
2010-05-28 03:13:33 +00:00
|
|
|
0xec, 0x41 // checksum on the last two bytes
|
2010-04-13 05:15:38 +00:00
|
|
|
};
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
static const u8 nunchuk_button_bitmasks[] =
|
2010-04-13 05:15:38 +00:00
|
|
|
{
|
2010-07-09 22:11:12 +00:00
|
|
|
Nunchuk::BUTTON_C,
|
|
|
|
Nunchuk::BUTTON_Z,
|
2010-04-13 05:15:38 +00:00
|
|
|
};
|
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
Nunchuk::Nunchuk(UDPWrapper *wrp) : Attachment("Nunchuk") , m_udpWrap(wrp)
|
2010-04-13 05:15:38 +00:00
|
|
|
{
|
|
|
|
// buttons
|
2010-07-26 05:30:50 +00:00
|
|
|
groups.push_back(m_buttons = new Buttons("Buttons"));
|
|
|
|
m_buttons->controls.push_back(new ControlGroup::Input("C"));
|
|
|
|
m_buttons->controls.push_back(new ControlGroup::Input("Z"));
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// stick
|
2010-07-26 05:30:50 +00:00
|
|
|
groups.push_back(m_stick = new AnalogStick("Stick"));
|
2010-04-29 18:51:04 +00:00
|
|
|
|
|
|
|
// swing
|
2010-07-26 05:30:50 +00:00
|
|
|
groups.push_back(m_swing = new Force("Swing"));
|
|
|
|
|
|
|
|
// tilt
|
|
|
|
groups.push_back(m_tilt = new Tilt("Tilt"));
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// shake
|
2010-07-26 05:30:50 +00:00
|
|
|
groups.push_back(m_shake = new Buttons("Shake"));
|
|
|
|
m_shake->controls.push_back(new ControlGroup::Input("X"));
|
|
|
|
m_shake->controls.push_back(new ControlGroup::Input("Y"));
|
|
|
|
m_shake->controls.push_back(new ControlGroup::Input("Z"));
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// set up register
|
|
|
|
// calibration
|
2010-07-26 05:30:50 +00:00
|
|
|
memcpy(®[0x20], nunchuck_calibration, sizeof(nunchuck_calibration));
|
2010-04-13 05:15:38 +00:00
|
|
|
// id
|
2010-07-26 05:30:50 +00:00
|
|
|
memcpy(®[0xfa], nunchuck_id, sizeof(nunchuck_id));
|
2010-04-22 07:11:49 +00:00
|
|
|
|
|
|
|
// this should get set to 0 on disconnect, but it isn't, o well
|
2010-04-29 18:51:04 +00:00
|
|
|
memset(m_shake_step, 0, sizeof(m_shake_step));
|
2010-04-13 05:15:38 +00:00
|
|
|
}
|
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
void Nunchuk::GetState(u8* const data, const bool focus)
|
2010-04-13 05:15:38 +00:00
|
|
|
{
|
|
|
|
wm_extension* const ncdata = (wm_extension*)data;
|
2010-07-10 06:48:24 +00:00
|
|
|
ncdata->bt = 0;
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// stick / not using calibration data for stick, o well
|
2010-07-26 05:30:50 +00:00
|
|
|
m_stick->GetState(&ncdata->jx, &ncdata->jy, 0x80, focus ? 127 : 0);
|
2010-08-24 08:48:09 +00:00
|
|
|
|
|
|
|
AccelData accel;
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// tilt
|
2010-08-24 08:48:09 +00:00
|
|
|
EmulateTilt(&accel, m_tilt, focus);
|
2010-04-13 05:15:38 +00:00
|
|
|
|
2010-04-22 07:11:49 +00:00
|
|
|
if (focus)
|
2010-04-13 05:15:38 +00:00
|
|
|
{
|
2010-07-26 05:30:50 +00:00
|
|
|
// swing
|
2010-08-24 08:48:09 +00:00
|
|
|
EmulateSwing(&accel, m_swing);
|
2010-04-29 18:51:04 +00:00
|
|
|
// shake
|
2010-08-25 11:44:01 +00:00
|
|
|
EmulateShake(&accel, m_shake, m_shake_step);
|
2010-04-29 18:51:04 +00:00
|
|
|
// buttons
|
2010-07-26 05:30:50 +00:00
|
|
|
m_buttons->GetState(&ncdata->bt, nunchuk_button_bitmasks);
|
2010-04-29 18:51:04 +00:00
|
|
|
}
|
2010-04-13 05:15:38 +00:00
|
|
|
|
|
|
|
// flip the button bits :/
|
2010-04-29 18:51:04 +00:00
|
|
|
ncdata->bt ^= 0x03;
|
2010-07-14 17:33:14 +00:00
|
|
|
|
|
|
|
//UDPNunchuk stuff
|
2010-07-26 05:30:50 +00:00
|
|
|
if (m_udpWrap->inst)
|
2010-07-14 17:33:14 +00:00
|
|
|
{
|
2010-07-26 05:30:50 +00:00
|
|
|
if (m_udpWrap->updNun)
|
2010-07-14 17:33:14 +00:00
|
|
|
{
|
|
|
|
u8 mask;
|
|
|
|
float x, y;
|
2010-07-26 05:30:50 +00:00
|
|
|
m_udpWrap->inst->getNunchuck(x, y, mask);
|
2010-07-14 17:33:14 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2010-07-26 05:30:50 +00:00
|
|
|
if (m_udpWrap->updNunAccel)
|
2010-07-14 17:33:14 +00:00
|
|
|
{
|
|
|
|
float x,y,z;
|
2010-07-26 05:30:50 +00:00
|
|
|
m_udpWrap->inst->getNunchuckAccel(x,y,z);
|
2010-08-24 08:48:09 +00:00
|
|
|
accel.x=x;
|
|
|
|
accel.y=y;
|
|
|
|
accel.z=z;
|
2010-07-14 17:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//End UDPNunchuck
|
2010-08-24 08:48:09 +00:00
|
|
|
|
|
|
|
wm_accel* dt = (wm_accel*)&ncdata->ax;
|
|
|
|
accel_cal* calib = (accel_cal*)®[0x20];
|
2010-08-25 11:44:01 +00:00
|
|
|
dt->x=u8(trim(accel.x*(calib->one_g.x-calib->zero_g.x)+calib->zero_g.x));
|
|
|
|
dt->y=u8(trim(accel.y*(calib->one_g.y-calib->zero_g.y)+calib->zero_g.y));
|
|
|
|
dt->z=u8(trim(accel.z*(calib->one_g.z-calib->zero_g.z)+calib->zero_g.z));
|
2010-04-13 05:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|