Skip to content

Instantly share code, notes, and snippets.

@runceel
Created April 13, 2015 05:57
Show Gist options
  • Save runceel/008a3fdc3fedf88d94c6 to your computer and use it in GitHub Desktop.
Save runceel/008a3fdc3fedf88d94c6 to your computer and use it in GitHub Desktop.
extern "C"
{
struct PluginVector3
{
double x;
double y;
double z;
};
__declspec (dllexport) PluginVector3 GetAccelerometer();
__declspec (dllexport) PluginVector3 GetAngularAcceleration();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace GyroWpfApp
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var timer = new DispatcherTimer();
timer.Tick += (_, __) =>
{
var v = Sensors.GetAngularAcceleration();
this.TextBlockX.Text = v.x.ToString();
this.TextBlockY.Text = v.y.ToString();
this.TextBlockZ.Text = v.z.ToString();
};
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
}
public static class Sensors
{
[DllImport("Sensors.dll")]
public static extern PluginVector3 GetAngularAcceleration();
}
[StructLayout(LayoutKind.Sequential)]
public struct PluginVector3
{
public double x;
public double y;
public double z;
}
}
// Win32Project1.cpp : DLL アプリケーション用にエクスポートされる関数を定義します。
//
#include "stdafx.h"
#include <InitGuid.h>
#include <SensorsApi.h>
#include <Sensors.h>
#pragma comment(lib, "Sensorsapi.lib")
__declspec (dllexport) PluginVector3 GetAccelerometer()
{
ISensorManager *pSensorManager;
ISensorCollection *pMotionSensorCollection;
ISensor *pMotionSensor;
if (!SUCCEEDED(::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorManager))))
{
return PluginVector3();
}
if (!SUCCEEDED(pSensorManager->GetSensorsByCategory(SENSOR_TYPE_ACCELEROMETER_3D, &pMotionSensorCollection)))
{
pSensorManager->Release();
return PluginVector3();
}
if (!SUCCEEDED(pMotionSensorCollection->GetAt(0, &pMotionSensor)))
{
pMotionSensorCollection->Release();
pSensorManager->Release();
return PluginVector3();
}
ISensorDataReport *pData;
if (!SUCCEEDED(pMotionSensor->GetData(&pData)))
{
pMotionSensor->Release();
pMotionSensorCollection->Release();
pSensorManager->Release();
return PluginVector3();
}
PROPVARIANT x = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &x)))
{
pData->Release();
pMotionSensor->Release();
pMotionSensorCollection->Release();
pSensorManager->Release();
return PluginVector3();
}
PROPVARIANT y = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &y)))
{
pData->Release();
pMotionSensor->Release();
pMotionSensorCollection->Release();
pSensorManager->Release();
return PluginVector3();
}
PROPVARIANT z = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &z)))
{
pData->Release();
pMotionSensor->Release();
pMotionSensorCollection->Release();
pSensorManager->Release();
return PluginVector3();
}
pData->Release();
pMotionSensor->Release();
pMotionSensorCollection->Release();
pSensorManager->Release();
PluginVector3 v;
v.x = x.dblVal;
v.y = y.dblVal;
v.z = z.dblVal;
return v;
}
__declspec (dllexport) PluginVector3 GetAngularAcceleration()
{
ISensorManager *pSensorManager;
ISensorCollection *pGyrometerSensorCollection;
ISensor *pGyrometerSensor;
if (!SUCCEEDED(::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorManager))))
{
return { -1, -1, -1 };
}
if (!SUCCEEDED(pSensorManager->GetSensorsByCategory(SENSOR_TYPE_GYROMETER_3D, &pGyrometerSensorCollection)))
{
pSensorManager->Release();
return { -2, -2, -2 };
}
if (!SUCCEEDED(pGyrometerSensorCollection->GetAt(0, &pGyrometerSensor)))
{
pGyrometerSensorCollection->Release();
pSensorManager->Release();
return { -3, -3, -3 };
}
ISensorDataReport *pData;
if (!SUCCEEDED(pGyrometerSensor->GetData(&pData)))
{
pGyrometerSensor->Release();
pGyrometerSensorCollection->Release();
pSensorManager->Release();
return { -4, -4, -4 };
}
PROPVARIANT x = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &x)))
{
pData->Release();
pGyrometerSensor->Release();
pGyrometerSensorCollection->Release();
pSensorManager->Release();
return { -5, -5, -5 };
}
PROPVARIANT y = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &y)))
{
pData->Release();
pGyrometerSensor->Release();
pGyrometerSensorCollection->Release();
pSensorManager->Release();
return { -6, -6, -6 };
}
PROPVARIANT z = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &z)))
{
pData->Release();
pGyrometerSensor->Release();
pGyrometerSensorCollection->Release();
pSensorManager->Release();
return { -7, -7, -7 };
}
pData->Release();
pGyrometerSensor->Release();
pGyrometerSensorCollection->Release();
pSensorManager->Release();
PluginVector3 v;
v.x = x.dblVal;
v.y = y.dblVal;
v.z = z.dblVal;
return v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment