Skip to content
L5474 edited this page Mar 24, 2020 · 3 revisions

This page contains some usage examples of the freePieIO library.

Note: Do not include the dll in your software, this makes it harder for the FreePIE team to upgrade the client library, instead read the path to the FreePIE folder from window registry. Load the path using win32 SetDllDirectory.

C# Examples

Reading from one slot:

internal class Program
{
  [StructLayout(LayoutKind.Sequential)]
  private struct Data
  {
    public float Yaw;
    public float Pitch;
    public float Roll;

    public float X;
    public float Y;
    public float Z;
  }

  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool SetDllDirectory(string lpPathName);

  [DllImport("freepie_io.dll")]
  private static extern int freepie_io_6dof_slots();

  [DllImport("freepie_io.dll", CallingConvention = CallingConvention.Cdecl)]
  private static extern int freepie_io_6dof_read(int index, int length, out Data data);

  private const int ReadFromIndex = 0;

  private static void Main(string[] args)
  {
    SetFreePIEDllPath();

    var slots = freepie_io_6dof_slots();

    if (ReadFromIndex >= slots)
      throw new Exception("Trying to read from an invalid slot");

    while (true)
    {
      Data data;
      var result = freepie_io_6dof_read(ReadFromIndex, 1, out data);
      if(result != 0)
      Console.WriteLine("Yaw: {0}, Pitch: {1}, Roll: {2}, X: {3}, Y: {4}, Z: {5}", data.Yaw, data.Pitch,
                          data.Roll, data.X, data.Y, data.Z);
    }
  }

  private static void SetFreePIEDllPath()
  {
    var path = Registry.GetValue(string.Format("{0}\\Software\\{1}", Registry.CurrentUser, "FreePIE"), "path", null) as string;
    SetDllDirectory(path);
  }
}

Reading from all slots:

internal class Program
{
  [StructLayout(LayoutKind.Sequential)]
  private struct Data
  {
    public float Yaw;
    public float Pitch;
    public float Roll;

    public float X;
    public float Y;
    public float Z;
  }

  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool SetDllDirectory(string lpPathName);

  [DllImport("freepie_io.dll")]
  private static extern int freepie_io_6dof_slots();

  [DllImport("freepie_io.dll", CallingConvention = CallingConvention.Cdecl)]
  private static extern int freepie_io_6dof_read(int index, int length, [Out][MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]Data[] data);

  private const int ReadFromIndex = 0;

  private static void Main(string[] args)
  {
    SetFreePIEDllPath();

    var slots = freepie_io_6dof_slots();

    if (ReadFromIndex >= slots)
        throw new Exception("Trying to read from an invalid slot");

    while (true)
    {
      Data[] arr = new Data[slots];
      var result = freepie_io_6dof_read(ReadFromIndex, slots, arr);
      if (result != 0)
      {
        for (int i = 0; i < slots; i++)
        {
          var data = arr[i];
          Console.WriteLine("Slot: {6}, Yaw: {0}, Pitch: {1}, Roll: {2}, X: {3}, Y: {4}, Z: {5}", data.Yaw, data.Pitch,
            data.Roll, data.X, data.Y, data.Z, i);
        }
      }
    }
  }

  private static void SetFreePIEDllPath()
  {
    var path = Registry.GetValue(string.Format("{0}\\Software\\{1}", Registry.CurrentUser, "FreePIE"), "path", null) as string;
    SetDllDirectory(path);
  }
}

Writing to one slot:

internal class Program
{
  [StructLayout(LayoutKind.Sequential)]
  private struct Data
  {
    public float Yaw;
    public float Pitch;
    public float Roll;

    public float X;
    public float Y;
    public float Z;
  }

  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool SetDllDirectory(string lpPathName);

  [DllImport("freepie_io.dll")]
  private static extern int freepie_io_6dof_slots();

  [DllImport("freepie_io.dll", CallingConvention = CallingConvention.Cdecl)]
  private static extern int freepie_io_6dof_write(int index, int length, Data[] data);

  private const int WriteToIndex = 0;

  private static void Main(string[] args)
  {
    SetFreePIEDllPath();

    var yaw = 0f;

    var arr = new Data[1];

    while (true)
    {
      arr[0] = new Data
      {
        Yaw = yaw
      };
      yaw = yaw + 0.1f;
      if (yaw > 180)
        yaw = -180;

      var result = freepie_io_6dof_write(WriteToIndex, 1, arr);
      if(result != 0)
        throw new Exception(string.Format("Could not write to slot {0}", WriteToIndex));
    }
  }

  private static void SetFreePIEDllPath()
  {
    var path = Registry.GetValue(string.Format("{0}\\Software\\{1}", Registry.CurrentUser, "FreePIE"), "path", null) as string;
    SetDllDirectory(path);
  }
}

The result int returned from the read function is a flag. You can use it like this to get a list of slots that have been updated since last read. Example:

private IEnumerable<int> GetUpdatedIndices(int result)
{
  int i = 0;
  while(result > 0)
  {
    if(result & 1)
      yield return i;
      
    result >>= 1;
    i++;
  }
}
     
var result = freepie_io_6dof_read(0, slots, data);
     
foreach(int index in GetUpdatedIndices(result))
    System.Console.WriteLine("updated: " + data[index]);

FreePIE Examples

Writing data to your program from FreePIE:

freePieIO[0].yaw = mouse.deltaX

Reading data in FreePIE written from your program:

def update():
	diagnostics.watch(freePieIO[0].yaw)
	
if starting:
	freePieIO[0].update += update