Skip to content

Instantly share code, notes, and snippets.

@rjth
Created March 3, 2018 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjth/ca27e282f13d63686a7a724d9c4b7b79 to your computer and use it in GitHub Desktop.
Save rjth/ca27e282f13d63686a7a724d9c4b7b79 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
namespace Sensel
{
class SenselContacts : MonoBehaviour
{
private IntPtr handle = IntPtr.Zero;
private SenselFrame frame = new SenselFrame();
private SenselSensorInfo sensorInfo = default(SenselSensorInfo);
private float sensorWidth = default(float);
private float sensorHeight = default(float);
public int InputID = default(int);
public float xPos = default(float);
public float yPos = default(float);
public float TotalForce = default(float);
public float Area = default(float);
private void Start()
{
SenselDeviceList list = new SenselDeviceList();
list.num_devices = 0;
Sensel.senselGetDeviceList(ref list);
Debug.Log("Num Devices: " + list.num_devices);
if (list.num_devices != 0)
{
Sensel.senselOpenDeviceByID(ref handle, list.devices[0].idx);
}
if (handle != IntPtr.Zero)
{
Sensel.senselGetSensorInfo(handle, ref sensorInfo);
Sensel.senselSetFrameContent(handle, 5);
Sensel.senselAllocateFrameData(handle, frame);
Sensel.senselStartScanning(handle);
sensorWidth = sensorInfo.width;
sensorHeight = sensorInfo.height;
}
}
private void LateUpdate()
{
if (handle != IntPtr.Zero)
{
Int32 numframes = 0;
Sensel.senselReadSensor(handle);
Sensel.senselGetNumAvailableFrames(handle, ref numframes);
for (int f = 0; f < numframes; ++f)
{
Sensel.senselGetFrame(handle, frame);
if (f + 1 == numframes)
{
if(frame.n_contacts > 0)
{
xPos = frame.contacts[0].x_pos / sensorWidth;
yPos = frame.contacts[0].y_pos / sensorHeight;
InputID = frame.contacts[0].id;
TotalForce = frame.contacts[0].total_force;
Area = frame.contacts[0].area;
Debug.Log("ID: " + InputID + '\t' + "xPos: " + xPos + '\t' + "yPos: " + yPos + '\t' + "force: " + TotalForce + '\t' + "area: " + Area);
}
}
}
}
}
private void OnApplicationQuit()
{
Sensel.senselClose(handle);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment