cemu-DS4Windows/DS4Windows/DS4Library/DS4Sixaxis.cs

116 lines
4.1 KiB
C#
Raw Normal View History

using System;
namespace DS4Windows
{
public class SixAxisEventArgs : EventArgs
{
public readonly SixAxis sixAxis;
public readonly DateTime timeStamp;
public SixAxisEventArgs(DateTime utcTimestamp, SixAxis sa)
{
sixAxis = sa;
timeStamp = utcTimestamp;
}
}
public class SixAxis
{
2017-08-09 10:24:24 +02:00
public const int ACC_RES_PER_G = 8192;
private const float F_ACC_RES_PER_G = ACC_RES_PER_G;
public const int GYRO_RES_IN_DEG_SEC = 16;
private const float F_GYRO_RES_IN_DEG_SEC = GYRO_RES_IN_DEG_SEC;
2017-08-09 10:24:24 +02:00
2017-07-20 07:57:14 +02:00
public int gyroYaw, gyroPitch, gyroRoll, accelX, accelY, accelZ;
2017-08-09 03:19:17 +02:00
public int outputAccelX, outputAccelY, outputAccelZ;
2017-08-09 10:24:24 +02:00
public double accelXG, accelYG, accelZG;
public double angVelYaw, angVelPitch, angVelRoll;
2017-07-12 15:04:37 +02:00
public readonly int gyroYawFull, gyroPitchFull, gyroRollFull;
2017-08-09 03:19:17 +02:00
public readonly int accelXFull, accelYFull, accelZFull;
public readonly byte touchID;
public readonly double elapsed;
public readonly SixAxis previousAxis = null;
2017-08-09 03:19:17 +02:00
2017-08-09 10:24:24 +02:00
double recip = 1d / 8192d;
double tempDouble = 0d;
public SixAxis(int X, int Y, int Z,
int aX, int aY, int aZ,
double elapsedDelta, SixAxis prevAxis = null)
{
2017-07-17 02:42:43 +02:00
gyroYaw = -X / 256;
gyroPitch = Y / 256;
gyroRoll = -Z / 256;
2017-08-09 10:24:24 +02:00
gyroYawFull = -X; gyroPitchFull = Y; gyroRollFull = -Z;
accelXFull = -aX; accelYFull = -aY; accelZFull = aZ;
2017-08-16 04:27:11 +02:00
angVelYaw = gyroYawFull / F_GYRO_RES_IN_DEG_SEC;
angVelPitch = gyroPitchFull / F_GYRO_RES_IN_DEG_SEC;
angVelRoll = gyroRollFull / F_GYRO_RES_IN_DEG_SEC;
2017-08-09 10:24:24 +02:00
accelXG = tempDouble = accelXFull / F_ACC_RES_PER_G;
accelYG = tempDouble = accelYFull / F_ACC_RES_PER_G;
accelZG = tempDouble = accelZFull / F_ACC_RES_PER_G;
2017-07-17 02:42:43 +02:00
// Put accel ranges between 0 - 128 abs
2017-07-16 14:11:58 +02:00
accelX = -aX / 64;
accelY = -aY / 64;
accelZ = aZ / 64;
2017-08-09 03:19:17 +02:00
outputAccelX = accelX;
outputAccelY = accelY;
outputAccelZ = accelZ;
2017-07-17 02:42:43 +02:00
elapsed = elapsedDelta;
previousAxis = prevAxis;
}
}
public class DS4SixAxis
{
2017-07-17 02:42:43 +02:00
public event EventHandler<SixAxisEventArgs> SixAccelMoved = null;
private int lastGyroYaw, lastGyroPitch, lastGyroRoll,
2017-07-18 22:37:01 +02:00
lastAX, lastAY, lastAZ;
private double lastElapsedDelta;
private byte[] previousPacket = new byte[8];
public void handleSixaxis(byte[] gyro, byte[] accel, DS4State state,
double elapsedDelta)
{
2017-07-18 22:37:01 +02:00
int currentYaw = (short)((ushort)(gyro[3] << 8) | gyro[2]);
int currentPitch = (short)((ushort)(gyro[1] << 8) | gyro[0]);
int currentRoll = (short)((ushort)(gyro[5] << 8) | gyro[4]);
2017-07-12 15:04:37 +02:00
int AccelX = (short)((ushort)(accel[1] << 8) | accel[0]);
int AccelY = (short)((ushort)(accel[3] << 8) | accel[2]);
int AccelZ = (short)((ushort)(accel[5] << 8) | accel[4]);
SixAxisEventArgs args = null;
if (AccelX != 0 || AccelY != 0 || AccelZ != 0)
{
if (SixAccelMoved != null)
{
SixAxis sPrev = null, now = null;
2017-07-17 02:42:43 +02:00
sPrev = new SixAxis(lastGyroYaw, lastGyroPitch, lastGyroRoll,
lastAX, lastAY, lastAZ, lastElapsedDelta);
2017-07-17 02:42:43 +02:00
now = new SixAxis(currentYaw, currentPitch, currentRoll,
AccelX, AccelY, AccelZ, elapsedDelta, sPrev);
2017-07-17 02:42:43 +02:00
args = new SixAxisEventArgs(state.ReportTimeStamp, now);
2017-07-16 10:30:49 +02:00
state.Motion = now;
SixAccelMoved(this, args);
}
2017-07-17 02:42:43 +02:00
lastGyroYaw = currentYaw;
lastGyroPitch = currentPitch;
lastGyroRoll = currentRoll;
lastAX = AccelX;
lastAY = AccelY;
lastAZ = AccelZ;
lastElapsedDelta = elapsedDelta;
}
}
}
}