Skip to content

Commit

Permalink
ControllerEmu: Round input floats instead of casting, part 2
Browse files Browse the repository at this point in the history
Like 3bc4968 but for Wii Remote extensions. I'm doing this to ensure
that TAS input values will still roundtrip after the next commit.
  • Loading branch information
JosJuice committed Sep 29, 2022
1 parent d3beae3 commit 6c8cfcb
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 32 deletions.
14 changes: 7 additions & 7 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Classic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Classic::Classic() : Extension1stParty("Classic", _trans("Classic Controller"))
}

// sticks
constexpr auto gate_radius = ControlState(STICK_GATE_RADIUS) / CAL_STICK_RANGE;
constexpr auto gate_radius = ControlState(STICK_GATE_RADIUS) / CAL_STICK_RADIUS;
groups.emplace_back(m_left_stick =
new ControllerEmu::OctagonAnalogStick(_trans("Left Stick"), gate_radius));
groups.emplace_back(
Expand Down Expand Up @@ -114,8 +114,8 @@ void Classic::Update()
const ControllerEmu::AnalogStick::StateData left_stick_state =
m_left_stick->GetState(m_input_override_function);

const u8 x = static_cast<u8>(LEFT_STICK_CENTER + (left_stick_state.x * LEFT_STICK_RADIUS));
const u8 y = static_cast<u8>(LEFT_STICK_CENTER + (left_stick_state.y * LEFT_STICK_RADIUS));
const u8 x = MapFloat<u8>(left_stick_state.x, LEFT_STICK_CENTER, 0, LEFT_STICK_RANGE);
const u8 y = MapFloat<u8>(left_stick_state.y, LEFT_STICK_CENTER, 0, LEFT_STICK_RANGE);

classic_data.SetLeftStick({x, y});
}
Expand All @@ -125,8 +125,8 @@ void Classic::Update()
const ControllerEmu::AnalogStick::StateData right_stick_data =
m_right_stick->GetState(m_input_override_function);

const u8 x = static_cast<u8>(RIGHT_STICK_CENTER + (right_stick_data.x * RIGHT_STICK_RADIUS));
const u8 y = static_cast<u8>(RIGHT_STICK_CENTER + (right_stick_data.y * RIGHT_STICK_RADIUS));
const u8 x = MapFloat<u8>(right_stick_data.x, RIGHT_STICK_CENTER, 0, RIGHT_STICK_RANGE);
const u8 y = MapFloat<u8>(right_stick_data.y, RIGHT_STICK_CENTER, 0, RIGHT_STICK_RANGE);

classic_data.SetRightStick({x, y});
}
Expand All @@ -139,8 +139,8 @@ void Classic::Update()
m_triggers->GetState(&buttons, classic_trigger_bitmasks.data(), triggers,
m_input_override_function);

const u8 lt = static_cast<u8>(triggers[0] * TRIGGER_RANGE);
const u8 rt = static_cast<u8>(triggers[1] * TRIGGER_RANGE);
const u8 lt = MapFloat<u8>(triggers[0], 0, 0, TRIGGER_RANGE);
const u8 rt = MapFloat<u8>(triggers[1], 0, 0, TRIGGER_RANGE);

classic_data.SetLeftTrigger(lt);
classic_data.SetRightTrigger(rt);
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ class Classic : public Extension1stParty
static constexpr u8 STICK_GATE_RADIUS = 0x61;

static constexpr u8 CAL_STICK_CENTER = 0x80;
static constexpr u8 CAL_STICK_RANGE = 0x7f;
static constexpr u8 CAL_STICK_RADIUS = 0x7f;
static constexpr u8 CAL_STICK_RANGE = 0xff;

static constexpr u8 LEFT_STICK_CENTER = CAL_STICK_CENTER >> (CAL_STICK_BITS - LEFT_STICK_BITS);
static constexpr u8 LEFT_STICK_RADIUS = CAL_STICK_RANGE >> (CAL_STICK_BITS - LEFT_STICK_BITS);
static constexpr u8 LEFT_STICK_RANGE = CAL_STICK_RANGE >> (CAL_STICK_BITS - LEFT_STICK_BITS);

static constexpr u8 RIGHT_STICK_CENTER = CAL_STICK_CENTER >> (CAL_STICK_BITS - RIGHT_STICK_BITS);
static constexpr u8 RIGHT_STICK_RADIUS = CAL_STICK_RANGE >> (CAL_STICK_BITS - RIGHT_STICK_BITS);
static constexpr u8 RIGHT_STICK_RANGE = CAL_STICK_RANGE >> (CAL_STICK_BITS - RIGHT_STICK_BITS);

static constexpr u8 TRIGGER_RANGE = 0x1F;

Expand Down
10 changes: 5 additions & 5 deletions Source/Core/Core/HW/WiimoteEmu/Extension/DrawsomeTablet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ void DrawsomeTablet::Update()
// the "Drawsome" game expects you to go "off screen" a bit to access some menu items.
constexpr u16 MIN_Y = 0x15ff + 0x100;
constexpr u16 MAX_Y = 0x00;
constexpr double CENTER_X = (MAX_X + MIN_X) / 2.0;
constexpr double CENTER_Y = (MAX_Y + MIN_Y) / 2.0;
constexpr u16 CENTER_X = (MAX_X + MIN_X + 1) / 2;
constexpr u16 CENTER_Y = (MAX_Y + MIN_Y + 1) / 2;

const auto stylus_state = m_stylus->GetState(m_input_override_function);
const auto stylus_x = u16(std::lround(CENTER_X + stylus_state.x * (MAX_X - CENTER_X)));
const auto stylus_y = u16(std::lround(CENTER_Y + stylus_state.y * (MAX_Y - CENTER_Y)));
const u16 stylus_x = MapFloat<u16>(stylus_state.x, CENTER_X, MIN_X, MAX_X);
const u16 stylus_y = MapFloat<u16>(-stylus_state.y, CENTER_Y, MAX_Y, MIN_Y);

tablet_data.stylus_x1 = u8(stylus_x);
tablet_data.stylus_x2 = u8(stylus_x >> 8);
Expand All @@ -73,7 +73,7 @@ void DrawsomeTablet::Update()
constexpr u16 MAX_PRESSURE = 0x7ff;

const auto touch_state = m_touch->GetState(m_input_override_function);
const auto pressure = u16(std::lround(touch_state.data[0] * MAX_PRESSURE));
const u16 pressure = MapFloat<u16>(touch_state.data[0], 0, 0, MAX_PRESSURE);

tablet_data.pressure1 = u8(pressure);
tablet_data.pressure2 = u8(pressure >> 8);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Drums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void Drums::Update()
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

drum_data.stick_x = MapFloat(stick_state.x, STICK_CENTER, STICK_MIN, STICK_MAX);
drum_data.stick_y = MapFloat(stick_state.y, STICK_CENTER, STICK_MIN, STICK_MAX);
drum_data.stick_x = MapFloat<u8>(stick_state.x, STICK_CENTER, STICK_MIN, STICK_MAX);
drum_data.stick_y = MapFloat<u8>(stick_state.y, STICK_CENTER, STICK_MIN, STICK_MAX);
}

// Buttons.
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ void Guitar::Update()
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

guitar_data.sx = static_cast<u8>((stick_state.x * STICK_RADIUS) + STICK_CENTER);
guitar_data.sy = static_cast<u8>((stick_state.y * STICK_RADIUS) + STICK_CENTER);
guitar_data.sx = MapFloat<u8>(stick_state.x, STICK_CENTER, 0, STICK_RANGE);
guitar_data.sy = MapFloat<u8>(stick_state.y, STICK_CENTER, 0, STICK_RANGE);
}

// slider bar
Expand All @@ -124,7 +124,7 @@ void Guitar::Update()
// whammy bar
const ControllerEmu::Triggers::StateData whammy_state =
m_whammy->GetState(m_input_override_function);
guitar_data.whammy = static_cast<u8>(whammy_state.data[0] * 0x1F);
guitar_data.whammy = MapFloat<u8>(whammy_state.data[0], 0, 0, 0x1F);

// buttons
m_buttons->GetState(&guitar_data.bt, guitar_button_bitmasks.data(), m_input_override_function);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Guitar.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Guitar : public Extension1stParty

static const u8 STICK_CENTER = 0x20;
static const u8 STICK_RADIUS = 0x1f;
static const u8 STICK_RANGE = 0x3f;

// TODO: Test real hardware. Is this accurate?
static const u8 STICK_GATE_RADIUS = 0x16;
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void Nunchuk::Update()
bool override_occurred = false;
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function, &override_occurred);
nc_data.jx = u8(STICK_CENTER + stick_state.x * STICK_RADIUS);
nc_data.jy = u8(STICK_CENTER + stick_state.y * STICK_RADIUS);
nc_data.jx = MapFloat<u8>(stick_state.x, STICK_CENTER, 0, STICK_RANGE);
nc_data.jy = MapFloat<u8>(stick_state.y, STICK_CENTER, 0, STICK_RANGE);

if (!override_occurred)
{
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Nunchuk.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class Nunchuk : public Extension1stParty

static constexpr u8 STICK_CENTER = 0x80;
static constexpr u8 STICK_RADIUS = 0x7F;
static constexpr u8 STICK_RANGE = 0xFF;

void LoadDefaults(const ControllerInterface& ciface) override;

Expand Down
7 changes: 5 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Shinkansen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ void Shinkansen::Update()
// guesses).
const u8 brake_values[] = {0, 53, 79, 105, 132, 159, 187, 217, 250};
const u8 power_values[] = {255, 229, 208, 189, 170, 153, 135, 118, 101, 85, 68, 51, 35, 17};
ext_data.brake = brake_values[size_t(analog[0] * (sizeof(brake_values) - 1))];
ext_data.power = power_values[size_t(analog[1] * (sizeof(power_values) - 1))];
// Not casting from size_t would trigger a static assert in MapFloat due to its use of llround
ext_data.brake =
brake_values[MapFloat(analog[0], 0, 0, static_cast<int>(sizeof(brake_values) - 1))];
ext_data.power =
power_values[MapFloat(analog[1], 0, 0, static_cast<int>(sizeof(power_values) - 1))];

// Note: This currently assumes a little-endian host.
const u16 button_bitmasks[] = {
Expand Down
12 changes: 6 additions & 6 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ void Turntable::Update()
const ControllerEmu::AnalogStick::StateData stick_state =
m_stick->GetState(m_input_override_function);

tt_data.sx = static_cast<u8>((stick_state.x * STICK_RADIUS) + STICK_CENTER);
tt_data.sy = static_cast<u8>((stick_state.y * STICK_RADIUS) + STICK_CENTER);
tt_data.sx = MapFloat<u8>(stick_state.x, STICK_CENTER, 0, STICK_RANGE);
tt_data.sy = MapFloat<u8>(stick_state.y, STICK_CENTER, 0, STICK_RANGE);
}

// left table
{
const ControllerEmu::Slider::StateData lt = m_left_table->GetState(m_input_override_function);
const s8 tt = static_cast<s8>(lt.value * TABLE_RANGE);
const s8 tt = MapFloat<u8>(lt.value, 0, 0, TABLE_RANGE);

tt_data.ltable1 = tt;
tt_data.ltable2 = tt >> 5;
Expand All @@ -103,7 +103,7 @@ void Turntable::Update()
// right table
{
const ControllerEmu::Slider::StateData rt = m_right_table->GetState(m_input_override_function);
const s8 tt = static_cast<s8>(rt.value * TABLE_RANGE);
const s8 tt = MapFloat<u8>(rt.value, 0, 0, TABLE_RANGE);

tt_data.rtable1 = tt;
tt_data.rtable2 = tt >> 1;
Expand All @@ -114,7 +114,7 @@ void Turntable::Update()
// effect dial
{
const auto dial_state = m_effect_dial->GetState(m_input_override_function);
const u8 dial = static_cast<u8>(dial_state.value * EFFECT_DIAL_RANGE) + EFFECT_DIAL_CENTER;
const u8 dial = MapFloat<u8>(dial_state.value, EFFECT_DIAL_CENTER, 0, EFFECT_DIAL_RANGE);

tt_data.dial1 = dial;
tt_data.dial2 = dial >> 3;
Expand All @@ -124,7 +124,7 @@ void Turntable::Update()
{
const ControllerEmu::Slider::StateData cfs = m_crossfade->GetState(m_input_override_function);

tt_data.slider = static_cast<u8>((cfs.value * CROSSFADE_RANGE) + CROSSFADE_CENTER);
tt_data.slider = MapFloat<u8>(cfs.value, CROSSFADE_CENTER, 0, CROSSFADE_RANGE);
}

// buttons
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/WiimoteEmu/Extension/Turntable.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Turntable : public Extension1stParty
static constexpr int STICK_BIT_COUNT = 6;
static constexpr u8 STICK_CENTER = (1 << STICK_BIT_COUNT) / 2;
static constexpr u8 STICK_RADIUS = STICK_CENTER - 1;
static constexpr u8 STICK_RANGE = (1 << STICK_BIT_COUNT) - 1;
// TODO: Test real hardware. Is this accurate?
static constexpr u8 STICK_GATE_RADIUS = 0x16;

Expand All @@ -85,11 +86,11 @@ class Turntable : public Extension1stParty

static constexpr int EFFECT_DIAL_BIT_COUNT = 5;
static constexpr u8 EFFECT_DIAL_CENTER = (1 << EFFECT_DIAL_BIT_COUNT) / 2;
static constexpr u8 EFFECT_DIAL_RANGE = EFFECT_DIAL_CENTER - 1;
static constexpr u8 EFFECT_DIAL_RANGE = (1 << EFFECT_DIAL_BIT_COUNT) - 1;

static constexpr int CROSSFADE_BIT_COUNT = 4;
static constexpr u8 CROSSFADE_CENTER = (1 << CROSSFADE_BIT_COUNT) / 2;
static constexpr u8 CROSSFADE_RANGE = CROSSFADE_CENTER - 1;
static constexpr u8 CROSSFADE_RANGE = (1 << CROSSFADE_BIT_COUNT) - 1;

private:
ControllerEmu::Buttons* m_buttons;
Expand Down

0 comments on commit 6c8cfcb

Please sign in to comment.