From 962c5fd412a19bdc9017f6339fddb0e80ec32b57 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Sun, 1 Nov 2020 03:28:30 +0100 Subject: [PATCH] First FFB handling --- WheelAdapter/Program.cs | 152 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 147 insertions(+), 5 deletions(-) diff --git a/WheelAdapter/Program.cs b/WheelAdapter/Program.cs index 0abc92d..210e73d 100644 --- a/WheelAdapter/Program.cs +++ b/WheelAdapter/Program.cs @@ -1,5 +1,5 @@ using System; - +using System.Runtime.InteropServices.ComTypes; using vJoyInterfaceWrap; namespace WheelAdapter @@ -14,8 +14,11 @@ namespace WheelAdapter static System.Threading.Mutex vj_mutex = new System.Threading.Mutex(true, "VJ access lock"); + static byte ifsDeviceState = 0; + static int Main(string[] args) { + Console.WriteLine("Initializing..."); AppDomain.CurrentDomain.ProcessExit += AppDomain_ProcessExit; #region vJoy init @@ -54,13 +57,17 @@ namespace WheelAdapter IForceSerial.QueryHandler handleQueryDelegate = handleQuery; ifs.queryHandler = handleQueryDelegate; - #endregion - // activate wheel packets + vj.FfbRegisterGenCB(handleFfb, null); + + Console.WriteLine("Running, press q, Enter to exit."); + string s=""; + while (!s.StartsWith("q")) + { + s = Console.ReadLine(); + } - Console.WriteLine("Press Enter to exit."); - Console.ReadLine(); cleanup(); return 0; @@ -118,5 +125,140 @@ namespace WheelAdapter Console.WriteLine("Query [" + query.type.ToString() + "]: " + BitConverter.ToString(query.data)); } + private static void _ffb_check(UInt32 result) + { + if (result != 0) + { + throw new Exception("FFB Error: " + result.ToString()); + } + } + + private static void handleFfb(IntPtr data, object userData) + { + int devId = 0; + _ffb_check(vj.Ffb_h_DeviceID(data, ref devId)); + + FFBPType pkt_type = FFBPType.PT_CTRLREP; + _ffb_check(vj.Ffb_h_Type(data, ref pkt_type)); + + uint itype = 0; + int dsize = 0; + byte[] dbuf = new byte[200]; + _ffb_check(vj.Ffb_h_Packet(data, ref itype, ref dsize, ref dbuf)); + + int ebi = 0; + vj.Ffb_h_EBI(data, ref ebi); + + switch (pkt_type) + { + case FFBPType.PT_EFFREP: + { + vJoy.FFB_EFF_REPORT effect = new vJoy.FFB_EFF_REPORT(); + _ffb_check(vj.Ffb_h_Eff_Report(data, ref effect)); + + break; + } + case FFBPType.PT_ENVREP: + { + vJoy.FFB_EFF_ENVLP effect = new vJoy.FFB_EFF_ENVLP(); + _ffb_check(vj.Ffb_h_Eff_Envlp(data, ref effect)); + + break; + } + case FFBPType.PT_CONDREP: + { + vJoy.FFB_EFF_COND effect = new vJoy.FFB_EFF_COND(); + _ffb_check(vj.Ffb_h_Eff_Cond(data, ref effect)); + + break; + } + case FFBPType.PT_PRIDREP: + { + vJoy.FFB_EFF_PERIOD effect = new vJoy.FFB_EFF_PERIOD(); + _ffb_check(vj.Ffb_h_Eff_Period(data, ref effect)); + + break; + } + case FFBPType.PT_CONSTREP: + { + vJoy.FFB_EFF_CONSTANT effect = new vJoy.FFB_EFF_CONSTANT(); + _ffb_check(vj.Ffb_h_Eff_Constant(data, ref effect)); + + break; + } + case FFBPType.PT_RAMPREP: + { + vJoy.FFB_EFF_RAMP effect = new vJoy.FFB_EFF_RAMP(); + _ffb_check(vj.Ffb_h_Eff_Ramp(data, ref effect)); + + break; + } + case FFBPType.PT_CSTMREP: + { + break; + } + case FFBPType.PT_SMPLREP: + { + break; + } + case FFBPType.PT_EFOPREP: + { + vJoy.FFB_EFF_OP effect = new vJoy.FFB_EFF_OP(); + _ffb_check(vj.Ffb_h_EffOp(data, ref effect)); + + break; + } + case FFBPType.PT_BLKFRREP: + { + break; + } + case FFBPType.PT_CTRLREP: + { + FFB_CTRL ctrl = FFB_CTRL.CTRL_DEVRST; + _ffb_check(vj.Ffb_h_DevCtrl(data, ref ctrl)); + switch (ctrl) + { + case FFB_CTRL.CTRL_ENACT: { ifsDeviceState |= (Byte)IForceSerial.DeviceState.ENABLE_FFB; break; } + case FFB_CTRL.CTRL_DISACT: { ifsDeviceState &= (Byte)~IForceSerial.DeviceState.ENABLE_FFB; break; } + case FFB_CTRL.CTRL_STOPALL: { ifsDeviceState |= (Byte)IForceSerial.DeviceState.STOP_ALL; break; } + case FFB_CTRL.CTRL_DEVRST: { ifsDeviceState = 0; break; } + case FFB_CTRL.CTRL_DEVPAUSE: { ifsDeviceState |= (Byte)IForceSerial.DeviceState.PAUSE_FFB; break; } + case FFB_CTRL.CTRL_DEVCONT: { ifsDeviceState &= (Byte)~IForceSerial.DeviceState.PAUSE_FFB; break; } + } + ifs.WritePacket(IForceSerial.Type.SET_DEVICE_STATE, new Byte[] { ifsDeviceState }); + break; + } + case FFBPType.PT_GAINREP: + { + byte gain = 0; + vj.Ffb_h_DevGain(data, ref gain); + gain = (byte)(gain / 255.0 * 0x80); + ifs.WritePacket(IForceSerial.Type.SET_OVERALL, new Byte[] { (Byte)IForceSerial.OverallControlType.GAIN, gain }); + break; + } + case FFBPType.PT_SETCREP: + { + break; + } + case FFBPType.PT_NEWEFREP: + { + FFBEType effect = FFBEType.ET_NONE; + _ffb_check(vj.Ffb_h_EffNew(data, ref effect)); + + break; + } + case FFBPType.PT_BLKLDREP: + { + break; + } + case FFBPType.PT_POOLREP: + { + break; + } + } + + //ref itype, ref dsize, ref dbuf + Console.WriteLine("FFB[" + devId.ToString() + "]: " + pkt_type.ToString()+" ("+itype.ToString()+"@"+ebi.ToString()+"): "+BitConverter.ToString(dbuf)); + } } }