Skip to content

Instantly share code, notes, and snippets.

@tmyt
Forked from runceel/header.hpp
Last active August 29, 2015 14:19
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 tmyt/f8fdd0e20f879f0cb778 to your computer and use it in GitHub Desktop.
Save tmyt/f8fdd0e20f879f0cb778 to your computer and use it in GitHub Desktop.
extern "C"
{
struct PluginVector3
{
double x;
double y;
double z;
};
__declspec (dllexport) HRESULT Initialize();
__declspec (dllexport) HRESULT Uninitialize();
__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();
Sensors.Initialize();
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"), CallingConvention=CallingConvention.Cdecl ]
public static extern uint Initialize();
[DllImport("Sensors.dll"), CallingConvention=CallingConvention.Cdecl ]
public static extern uint Uninitialize();
[DllImport("Sensors.dll"), CallingConvention=CallingConvention.Cdecl ]
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")
#define SAFE_RELEASE(x) {if(x){x->Release(); x=nullptr;}}
ISensorManager* pSensorManager;
ISensor* pMotionSensor;
ISensor* pGyrometerSensor;
HRESULT InitializeAccelerometerSensor()
{
ISensorCollection *pMotionSensorCollection;
if (!SUCCEEDED(pSensorManager->GetSensorsByCategory(SENSOR_TYPE_ACCELEROMETER_3D, &pMotionSensorCollection)))
{
return E_ERROR;
}
if (!SUCCEEDED(pMotionSensorCollection->GetAt(0, &pMotionSensor)))
{
pMotionSensorCollection->Release();
return E_ERROR;
}
pMotionSensorCollection->Release();
return S_OK;
}
HRESULT InitializeGyrometerSensor()
{
ISensorCollection *pGyrometerSensorCollection;
if (!SUCCEEDED(pSensorManager->GetSensorsByCategory(SENSOR_TYPE_GYROMETER_3D, &pGyrometerSensorCollection)))
{
return E_ERROR;
}
if (!SUCCEEDED(pGyrometerSensorCollection->GetAt(0, &pGyrometerSensor)))
{
pGyrometerSensorCollection->Release();
return E_ERROR;
}
pGyrometerSensorCollection->Release();
return S_OK;
}
__declspec (dllexport) HRESULT Initialize()
{
// Initialize ISensorManager
if (!SUCCEEDED(::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorManager))))
{
return E_ERROR;
}
// Initialize Accelerometer Sensor
if (FAILED(InitializeAccelerometerSensor()))
{
SAFE_RELEASE(pSensorManager);
return E_ERROR;
}
// Initialize Gyrometer Sensor
if (FAILED(InitializeGyrometerSensor()))
{
SAFE_RELEASE(pMotionSensor);
SAFE_RELEASE(pSensorManager);
return E_ERROR;
}
return S_OK;
}
__declspec (dllexport) HRESULT Uninitialize()
{
SAFE_RELEASE(pSensorManager);
SAFE_RELEASE(pMotionSensor);
SAFE_RELEASE(pGyrometerSensor);
return S_OK;
}
__declspec (dllexport) PluginVector3 GetAccelerometer()
{
ISensorDataReport *pData;
if (!pMotionSensor || !SUCCEEDED(pMotionSensor->GetData(&pData)))
{
return PluginVector3();
}
PROPVARIANT x = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &x)))
{
pData->Release();
return PluginVector3();
}
PROPVARIANT y = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &y)))
{
pData->Release();
return PluginVector3();
}
PROPVARIANT z = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &z)))
{
pData->Release();
return PluginVector3();
}
pData->Release();
PluginVector3 v;
v.x = x.dblVal;
v.y = y.dblVal;
v.z = z.dblVal;
return v;
}
__declspec (dllexport) PluginVector3 GetAngularAcceleration()
{
ISensorDataReport *pData;
if (!pGyrometerSensor || !SUCCEEDED(pGyrometerSensor->GetData(&pData)))
{
return{ -4, -4, -4 };
}
PROPVARIANT x = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &x)))
{
pData->Release();
return{ -5, -5, -5 };
}
PROPVARIANT y = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &y)))
{
pData->Release();
return{ -6, -6, -6 };
}
PROPVARIANT z = {};
if (!SUCCEEDED(pData->GetSensorValue(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &z)))
{
pData->Release();
return{ -7, -7, -7 };
}
pData->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