このソースの参考ページはこちらです
y::memo weblogさんのHYBRID
W-ZERO3 の加速度センサーをハック
感謝です。
HWZERO_LOCA.ClsAccelerometer cAccela = new HWZERO_LOCA.ClsAccelerometer();
HWZERO_ACCE.ClsAccelerometer.TILTDATA_RAW AcceData =
cAccela.getAcceData();
x = AcceData.x;
y = AcceData.y;
z = AcceData.z;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using HANDLE = System.IntPtr;
namespace HWZERO_ACCE
{
class ClsAccelerometer
{
//加速度センサー
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice, UInt32 dwIoControlCode,
byte[] lpInBuffer, Int32 nInBufferSize,
byte[] lpOutBuffer, Int32 nOutBufferSize,
ref UInt32 lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll", SetLastError = true)]
public static extern IntPtr CreateFile(
String lpFileName, // ファイル名
UInt32 dwDesiredAccess, // アクセスモード
UInt32 dwShareMode, // 共有モード
IntPtr lpSecurityAttributes, // セキュリティ記述子
UInt32 dwCreationDisposition, // 作成方法
UInt32 dwFlagsAndAttributes, // ファイル属性
IntPtr hTemplateFile);// テンプレートファイルのハンドル
[DllImport("coredll.dll", SetLastError = true, CallingConvention =
CallingConvention.Winapi, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(
HANDLE hObject);
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
private const uint FILE_SHARE_READ = 0x1;
private const uint FILE_SHARE_WRITE = 0x2;
private const uint FILE_ANY_ACCESS = 0;
private const uint GENERIC_READ = 0x80000000;
private const uint OPEN_EXISTING = 3;
private const uint METHOD_BUFFERED = 0;
public uint CTL_CODE(uint DeviceType, uint Function, uint Method,
uint Access)
{
return ((DeviceType << 16) | (Access << 14) | (Function << 2)
| Method);
}
/* 定数宣言 */
const string SHARP_GSENSOR_DEVICE_NAME = "LIS1:";
/* 加速度センサーが返すデータ構造 */
public struct TILTDATA_RAW
{
public Int16 x; /* signed short */
public Int16 y; /* signed short */
public Int16 z; /* signed short */
};
TILTDATA_RAW acceData = new TILTDATA_RAW();
static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public TILTDATA_RAW getAcceData()
{
uint IOCTL_SHARP_GSENSOR_REFRESH = CTL_CODE(0x8002, 0x0BBC,
METHOD_BUFFERED, FILE_ANY_ACCESS);
int[] inBuffer;
Int16[] outBuffer;
/* 加速度センサーにアクセスするためのハンドル */
HANDLE hSensorDevice = INVALID_HANDLE_VALUE;
/* 加速度センサーが返すデータ構造 */
TILTDATA_RAW tilt = new TILTDATA_RAW();
/* オープン */
hSensorDevice = CreateFile(
SHARP_GSENSOR_DEVICE_NAME,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (hSensorDevice == (IntPtr)(-1))
throw new InvalidOperationException("Unable to Create File");
inBuffer = new int[0];
outBuffer = new Int16[3];
uint bytesReturned = 0;
int inSize = sizeof(int) * inBuffer.Length;
int outSize = sizeof(Int16) * outBuffer.Length;
byte[] inbyteArray;
inbyteArray = BitConverter.GetBytes((int)0);
byte[] outbyteArray = new byte[outSize];
try
{
if (INVALID_HANDLE_VALUE != hSensorDevice && false !=
DeviceIoControl(
hSensorDevice,
IOCTL_SHARP_GSENSOR_REFRESH,
inbyteArray,
inSize,
outbyteArray,
outSize,
ref bytesReturned,
IntPtr.Zero))
{
byte[] byteArray = { 1, 0, 0, 0 };
acceData.x = BitConverter.ToInt16(outbyteArray, 0);
acceData.y = BitConverter.ToInt16(outbyteArray, 2);
acceData.z = BitConverter.ToInt16(outbyteArray, 4);
}
}
finally
{
//終了
if (INVALID_HANDLE_VALUE != hSensorDevice)
{
CloseHandle(hSensorDevice);
hSensorDevice = INVALID_HANDLE_VALUE;
}
}
return acceData;
}
}
}