using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public delegate void FPtr(int value);
public class Haptics : MonoBehaviour
{
private int interfaceId;
class EAIWrapper
{
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int InitializeTI();
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Discover(int type);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Connect([MarshalAs(UnmanagedType.LPStr)] string name, int type, FPtr _callback);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Pulse(int deviceId, int _tacNum, int _msDuration, int _delay);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ChangeFreq(int deviceId, int _tacNum, int freqVal, int _delay);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ChangeGain(int deviceID, int _tacNum, int gainval, int _delay);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Close(int interfaceId);
[DllImport("TactorInterface.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ShutdownTI();
}
void Start()
{
EAIWrapper.InitializeTI();
EAIWrapper.Discover(1);
interfaceId = EAIWrapper.Connect("COM5", 1, null);
}
///
/// Vibrate a haptic motor
///
/// Motor: 1-16
/// Intensity range: 0.0-1.0
/// Vibration time in miliseconds
public void Vibrate(int motor, double intensity, int vibrationTime)
{
EAIWrapper.ChangeGain(interfaceId, motor, (int)(intensity * 255), 0);
EAIWrapper.Pulse(interfaceId, motor, vibrationTime, 0);
EAIWrapper.ChangeFreq(interfaceId, motor, 2000, 0);
}
private void OnDestroy()
{
EAIWrapper.Close(interfaceId);
EAIWrapper.ShutdownTI();
}
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
Vibrate(15, 1, 100);
}
if (Input.GetKey(KeyCode.S))
{
Vibrate(16, 1, 100);
}
if (Input.GetKey(KeyCode.D))
{
for (int i = 1; i <= 16; i++)
{
Vibrate(i, 1, 100);
}
}
}
}