⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Working with G-Sensor (Accelerometer) on HTC C#

Recently, I was faced with the task (due to the acquisition of the HTC HD2 device) of how to access the accelerometer data in it. And here is the result of my work, a small library for accessing the G-Sensor in HTC (theoretically, it should be suitable for all HTC devices with an accelerometer). As always, the source code is included, feel free to use it in your projects.

The source code of the library:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace htcGS
{
    //Структура данных с сенсора
    public struct SensorData
    {
        public short TiltX;          // От -1000 до 1000, 0 лежит
        public short TiltY;          // От -1000 до 1000, 0 лежит
        public short TiltZ;          // От -1000 до 1000, 0 = стоит, -1000 = лежит, 1000 = перевёрнут
        public short Unknown1;       // Всегда ноль
        public int AngleY;         // От 0 до 359
        public int AngleX;         // От 0 до 359
        public int Unknown2;       // Непонятно что.
    };

    public class HTCGSensor : IDisposable
    {
        [DllImport("HTCSensorSDK")]
        extern static IntPtr HTCSensorGetDataOutput(IntPtr handle, out SensorData sensorData);
        [DllImport("HTCSensorSDK")]
        extern static IntPtr HTCSensorOpen(int sensor);
        [DllImport("HTCSensorSDK")]
        extern static void HTCSensorClose(IntPtr handle);
        [DllImport("coredll", SetLastError = true)]
        extern static IntPtr CreateEvent(IntPtr eventAttributes, bool manualReset, bool intialState, string name);
        [DllImport("coredll", SetLastError = true)]
        extern static bool EventModify(IntPtr handle, uint func);
        [DllImport("coredll")]
        extern static bool CloseHandle(IntPtr handle);



        public void Dispose()
        {
            //Останавливаем...
            IntPtr hEvent = CreateEvent(IntPtr.Zero, true, false, "HTC_GSENSOR_SERVICESTOP");
            CloseHandle(hEvent);
        }

        public void GetSensorData(ref SensorData data)
        {
            //Инициализирует, стартует и принимает данные.
            IntPtr Handle = HTCSensorOpen(1);
            IntPtr hEvent = CreateEvent
            (IntPtr.Zero, true, false, "HTC_GSENSOR_SERVICESTART");
            EventModify(hEvent, 3);
            CloseHandle(hEvent);
            HTCSensorGetDataOutput(Handle, out data);
            return;
        }


    }
}



Now, how to use it, the source code of the program that works with the library:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using htcGS;

namespace SmartDeviceProject3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

//Запускаем таймер...
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            
            

        }

//Останавливаем всю эту беду и выходим из приложения.
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            htcGS.HTCGSensor nw = new HTCGSensor();
            nw.Dispose();
            Application.Exit();
        }

//Это то что выполняем по таймеру (выводим параметры с G-Sensor-а)
        private void timer1_Tick(object sender, EventArgs e)
        {
            SensorData sd = new SensorData();
            htcGS.HTCGSensor nw = new HTCGSensor();
            nw.GetSensorData(ref sd);
            label1.Text = sd.TiltX.ToString();
            label2.Text = sd.TiltY.ToString();
            label3.Text = sd.TiltZ.ToString();
            label4.Text = sd.AngleX.ToString();
            label5.Text = sd.AngleY.ToString();
            label6.Text = sd.Unknown1.ToString();
            label7.Text = sd.Unknown2.ToString();
        }
    }
}


When you click the button1 button, the program starts a timer that begins to receive data from the accelerometer and display it on the screen. When you click the button2 button, the program stops the timer and closes the application.

You can download the application with source code here: htcgsensor.rar · 22 downloads 

Files

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.