cemu-DS4Windows/DS4Windows/DS4Library/DS4Sixaxis.cs

102 lines
3.6 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-07-18 21:21:03 +02:00
public int gyroYaw, gyroPitch, gyroRoll, deltaX, deltaY, deltaZ, accelX, accelY, accelZ;
2017-07-12 15:04:37 +02:00
public readonly int gyroYawFull, gyroPitchFull, gyroRollFull;
2017-07-18 21:21:03 +02:00
public int accelXFull, accelYFull, accelZFull;
public readonly byte touchID;
public readonly double elapsed;
public readonly SixAxis previousAxis = null;
2017-07-18 21:21:03 +02:00
public SixAxis(int X, int Y, int Z, int aX, int aY, int aZ,
double milliseconds, SixAxis prevAxis = null)
{
2017-07-17 02:42:43 +02:00
gyroYaw = -X / 256;
gyroPitch = Y / 256;
gyroRoll = -Z / 256;
gyroYawFull = -X;
2017-07-12 15:04:37 +02:00
gyroPitchFull = Y;
gyroRollFull = -Z;
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-07-17 02:42:43 +02:00
2017-07-16 14:11:58 +02:00
accelXFull = -aX;
accelYFull = -aY;
accelZFull = aZ;
elapsed = milliseconds;
previousAxis = prevAxis;
if (previousAxis != null)
{
2017-07-17 02:42:43 +02:00
deltaX = gyroYaw - previousAxis.gyroYaw;
deltaY = gyroPitch - previousAxis.gyroPitch;
deltaZ = gyroRoll - previousAxis.gyroRoll;
}
}
}
public class DS4SixAxis
{
2017-07-17 02:42:43 +02:00
// no status change for the touchpad itself... but other sensors may have changed, or you may just want to do some processing
public event EventHandler<SixAxisEventArgs> SixAccelMoved = null;
internal int lastGyroYaw, lastGyroPitch, lastGyroRoll,
lastAX, lastAY, lastAZ; // tracks 0, 1 or 2 touches; we maintain touch 1 and 2 separately
internal double lastMilliseconds;
internal byte[] previousPacket = new byte[8];
public void handleSixaxis(byte[] gyro, byte[] accel, DS4State state, double milliseconds)
{
2017-07-17 02:42:43 +02:00
int currentYaw = (short)((ushort)(gyro[3] << 8) | gyro[2]); // Gyro Yaw
int currentPitch = (short)((ushort)(gyro[1] << 8) | gyro[0]); // Gyro Pitch
int currentRoll = (short)((ushort)(gyro[5] << 8) | gyro[4]); // Gyro Roll
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, lastMilliseconds);
now = new SixAxis(currentYaw, currentPitch, currentRoll,
AccelX, AccelY, AccelZ, milliseconds, sPrev);
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;
lastMilliseconds = milliseconds;
}
}
}
}