2015-11-28 06:47:26 +01:00
using System ;
namespace DS4Windows
{
public class SixAxisEventArgs : EventArgs
{
public readonly SixAxis sixAxis ;
2017-06-22 03:07:21 +02:00
public readonly DateTime timeStamp ;
public SixAxisEventArgs ( DateTime utcTimestamp , SixAxis sa )
2015-11-28 06:47:26 +01:00
{
sixAxis = sa ;
2017-06-22 03:07:21 +02:00
timeStamp = utcTimestamp ;
2015-11-28 06:47:26 +01:00
}
}
public class SixAxis
{
public readonly int gyroX , gyroY , gyroZ , deltaX , deltaY , deltaZ , accelX , accelY , accelZ ;
2017-06-22 03:07:21 +02:00
public readonly int gyroXFull , gyroYFull ;
2015-11-28 06:47:26 +01:00
public readonly byte touchID ;
public readonly SixAxis previousAxis ;
public SixAxis ( int X , int Y , int Z , int aX , int aY , int aZ , SixAxis prevAxis = null )
{
2017-06-22 03:07:21 +02:00
gyroX = X / 256 ;
gyroY = Y / 256 ;
gyroZ = Z / 256 ;
gyroXFull = X ;
gyroYFull = Y ;
accelX = aX / 64 ;
accelY = aY / 64 ;
accelZ = aZ / 64 ;
2015-11-28 06:47:26 +01:00
previousAxis = prevAxis ;
if ( previousAxis ! = null )
{
2017-06-22 03:07:21 +02:00
deltaX = aX - previousAxis . gyroX ;
deltaY = aY - previousAxis . gyroY ;
deltaZ = aZ - previousAxis . gyroZ ;
2015-11-28 06:47:26 +01:00
}
}
}
public class DS4SixAxis
{
2017-06-22 03:07:21 +02:00
//public event EventHandler<SixAxisEventArgs> SixAxisMoved = null; // deltaX/deltaY are set because one or both fingers were already down on a prior sensor reading
2015-11-28 06:47:26 +01:00
public event EventHandler < SixAxisEventArgs > SixAccelMoved = null ; // no status change for the touchpad itself... but other sensors may have changed, or you may just want to do some processing
internal int lastGyroX , lastGyroY , lastGyroZ , lastAX , lastAY , lastAZ ; // tracks 0, 1 or 2 touches; we maintain touch 1 and 2 separately
internal byte [ ] previousPacket = new byte [ 8 ] ;
public void handleSixaxis ( byte [ ] gyro , byte [ ] accel , DS4State state )
{
//bool touchPadIsDown = sensors.TouchButton;
/ * if ( ! PacketChanged ( data , touchPacketOffset ) & & touchPadIsDown = = lastTouchPadIsDown )
{
if ( SixAxisUnchanged ! = null )
SixAxisUnchanged ( this , EventArgs . Empty ) ;
return ;
} * /
/ * byte touchID1 = ( byte ) ( data [ 0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset ] & 0x7F ) ;
byte touchID2 = ( byte ) ( data [ 4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset ] & 0x7F ) ; * /
2017-06-22 03:07:21 +02:00
int currentX = ( short ) ( ( ushort ) ( gyro [ 3 ] < < 8 ) | gyro [ 2 ] ) ; // Gyro Pitch
int currentY = ( short ) ( ( ushort ) ( gyro [ 1 ] < < 8 ) | gyro [ 0 ] ) ; // Gyro Yaw
int currentZ = ( short ) ( ( ushort ) ( gyro [ 5 ] < < 8 ) | gyro [ 4 ] ) ; // Gyro Roll
int AccelX = ( short ) ( ( ushort ) ( accel [ 1 ] < < 8 ) | accel [ 0 ] ) ; // Accel Pitch
int AccelY = ( short ) ( ( ushort ) ( accel [ 3 ] < < 8 ) | accel [ 2 ] ) ; // Accel Roll
int AccelZ = ( short ) ( ( ushort ) ( accel [ 5 ] < < 8 ) | accel [ 4 ] ) ; // Accel Yaw
2015-11-28 06:47:26 +01:00
SixAxisEventArgs args ;
//if (sensors.Touch1 || sensors.Touch2)
{
2015-12-13 22:30:54 +01:00
/ * if ( SixAxisMoved ! = null )
2015-11-28 06:47:26 +01:00
{
SixAxis sPrev , now ;
sPrev = new SixAxis ( lastGyroX , lastGyroY , lastGyroZ , lastAX , lastAY , lastAZ ) ;
now = new SixAxis ( currentX , currentY , currentZ , AccelX , AccelY , AccelZ , sPrev ) ;
args = new SixAxisEventArgs ( state . ReportTimeStamp , now ) ;
SixAxisMoved ( this , args ) ;
}
lastGyroX = currentX ;
lastGyroY = currentY ;
lastGyroZ = currentZ ;
lastAX = AccelX ;
lastAY = AccelY ;
2015-12-13 22:30:54 +01:00
lastAZ = AccelZ ; * /
2015-11-28 06:47:26 +01:00
}
if ( AccelX ! = 0 | | AccelY ! = 0 | | AccelZ ! = 0 )
{
if ( SixAccelMoved ! = null )
{
SixAxis sPrev , now ;
sPrev = new SixAxis ( lastGyroX , lastGyroY , lastGyroZ , lastAX , lastAY , lastAZ ) ;
now = new SixAxis ( currentX , currentY , currentZ , AccelX , AccelY , AccelZ , sPrev ) ;
args = new SixAxisEventArgs ( state . ReportTimeStamp , now ) ;
SixAccelMoved ( this , args ) ;
}
lastGyroX = currentX ;
lastGyroY = currentY ;
lastGyroZ = currentZ ;
lastAX = AccelX ;
lastAY = AccelY ;
lastAZ = AccelZ ;
}
}
}
}