core/hid: Fully emulate motion from button

This commit is contained in:
german77 2021-11-01 19:49:14 -06:00 committed by Narr the Reg
parent 77fa4d4bf6
commit 136eb9c4c2
7 changed files with 97 additions and 37 deletions

View file

@ -644,6 +644,7 @@ void EmulatedController::SetMotion(Common::Input::CallbackStatus callback, std::
});
emulated.UpdateRotation(raw_status.delta_timestamp);
emulated.UpdateOrientation(raw_status.delta_timestamp);
force_update_motion = raw_status.force_update;
if (is_configuring) {
TriggerOnChange(ControllerTriggerType::Motion, false);
@ -653,7 +654,7 @@ void EmulatedController::SetMotion(Common::Input::CallbackStatus callback, std::
auto& motion = controller.motion_state[index];
motion.accel = emulated.GetAcceleration();
motion.gyro = emulated.GetGyroscope();
motion.rotation = emulated.GetGyroscope();
motion.rotation = emulated.GetRotations();
motion.orientation = emulated.GetOrientation();
motion.is_at_rest = emulated.IsMoving(motion_sensitivity);
@ -962,6 +963,14 @@ NpadGcTriggerState EmulatedController::GetTriggers() const {
}
MotionState EmulatedController::GetMotions() const {
if (force_update_motion) {
for (auto& device : motion_devices) {
if (!device) {
continue;
}
device->ForceUpdate();
}
}
return controller.motion_state;
}

View file

@ -355,6 +355,7 @@ private:
bool is_connected{false};
bool is_configuring{false};
f32 motion_sensitivity{0.01f};
bool force_update_motion{false};
// Temporary values to avoid doing changes while the controller is on configuration mode
NpadType tmp_npad_type{NpadType::None};

View file

@ -74,45 +74,53 @@ Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatu
Common::Input::MotionStatus status{};
switch (callback.type) {
case Common::Input::InputType::Button: {
Common::Input::AnalogProperties properties{
.deadzone = 0.0f,
.range = 1.0f,
.offset = 0.0f,
};
status.delta_timestamp = 5000;
status.force_update = true;
status.accel.x = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.accel.y = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.accel.z = {
.value = 0.0f,
.raw_value = -1.0f,
.properties = properties,
};
status.gyro.x = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.gyro.y = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.gyro.z = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
if (TransformToButton(callback).value) {
std::random_device device;
std::mt19937 gen(device());
std::uniform_int_distribution<s16> distribution(-1000, 1000);
Common::Input::AnalogProperties properties{
.deadzone = 0.0,
.range = 1.0f,
.offset = 0.0,
};
status.accel.x = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.accel.y = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.accel.z = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.gyro.x = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.gyro.y = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.gyro.z = {
.value = 0,
.raw_value = static_cast<f32>(distribution(gen)) * 0.001f,
.properties = properties,
};
status.accel.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.accel.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.accel.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
}
break;
}

View file

@ -56,15 +56,31 @@ private:
Common::Vec3f integral_error;
Common::Vec3f derivative_error;
// Quaternion containing the device orientation
Common::Quaternion<f32> quat{{0.0f, 0.0f, -1.0f}, 0.0f};
// Number of full rotations in each axis
Common::Vec3f rotations;
// Acceleration vector measurement in G force
Common::Vec3f accel;
// Gyroscope vector measurement in radians/s.
Common::Vec3f gyro;
// Vector to be substracted from gyro measurements
Common::Vec3f gyro_drift;
// Minimum gyro amplitude to detect if the device is moving
f32 gyro_threshold = 0.0f;
// Number of invalid sequential data
u32 reset_counter = 0;
// If the provided data is invalid the device will be autocalibrated
bool reset_enabled = true;
// Use accelerometer values to calculate position
bool only_accelerometer = true;
};