HTC Touch Diamond C# Implementation

2012-03-23 by

The GSensor is a piece of hardware in the HTC Touch Diamond that measures movements along the X, Y and Z axis.  This is similar to the features provided by the IPhone.  I have been playing with this SDK in C# and Windows Mobile 6.1 SDK and have come up with some enhancements I would like to show from the original SDK. 

I downloaded the original source from http://blog.enterprisemobile.com/2008/07/using-htc-diamonds-sensor-sdk-from-managed-code/.  After I extracted the Zip file I opened the solution and added some code of my own.  Now I have a more complete SDK, allowing developers to inherit from the base HTCForm object.  This object inherits the Form class from System.Windows.Forms.  This form will automatically react to tilts in the GSensor over a certain degree mark, changing the orientation of the device based on how it is turned.

There are a few known issues with the code, like the dispose feature which you must implement to free up the background threads.  I am continuing to work on the base class until I am happy with the results.

using System;
using GSensorSDK;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace GSensorSDK
{
    public class HTCForm : Form
    {
        public HTCForm()
        {
            try
            {
                mySensor = new HTCGSensor();
                mySensor.SensorChanged += new SensorChangedDelegate(mySensor_SensorChanged);
            }
            catch { }
 
           
            if (mySensor != null)
           {
                _changeTilt = new ChangeTiltDelegate(ChangeTilt);
 
                thMain = new System.Threading.Thread(new System.Threading.ThreadStart(runTiltSensor));
                thMain.Start();
            }
            this.Closing +=new CancelEventHandler(HTCForm_Closing);
            this.Closed += new EventHandler(HTCForm_Closed);
           
       
        }
 
        void HTCForm_Closed(object sender, EventArgs e)
        {
            Destroy();
        }
        private void Destroy()
        {
            try
            {
                thMain.Abort();
            }
            catch { }
            try
            {
                mySensor.Kill();
            }
            catch { }
            try
            {
                mySensor.Dispose();
            }
            catch { }
        }
        void HTCForm_Closing(object sender, EventArgs e)
        {
            Destroy();
        }
        public event SensorChangedDelegate SensorChanged = null;
 
        void mySensor_SensorChanged(HTCGSensorData sensor)
        {
            if (SensorChanged != null)
                SensorChanged(sensor);
        }
      
        void HTCForm_Closing(object sender, CancelEventArgs e)
        {
            Destroy();
        }
        delegate void ChangeTiltDelegate(GVector data);
        ChangeTiltDelegate _changeTilt;
 
        System.Threading.Thread thMain;
        private void runTiltSensor()
        {
            while (true)
            {
                CheckSensorData(mySensor);
                System.Threading.Thread.Sleep(100);
            }
        }
        HTCGSensor mySensor = null;
        [System.ComponentModel.EditorBrowsable( EditorBrowsableState.Always)]
        public bool AllowChangeTilt
        {
            get;
            set;
        }
        public void ChangeTilt(GVector vector)
        {
            //string line = vector.X.ToString() + "," + vector.Y.ToString() + "," + vector.Z.ToString();
            if (vector.X < -4)
            {
                if (Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation != Microsoft.WindowsCE.Forms.ScreenOrientation.Angle270)
                    Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation = Microsoft.WindowsCE.Forms.ScreenOrientation.Angle270;
            }
            else if (vector.X > 4)
            {
                if (Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation != Microsoft.WindowsCE.Forms.ScreenOrientation.Angle90)
                    Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation = Microsoft.WindowsCE.Forms.ScreenOrientation.Angle90;
            }
            else
            {
                if (Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation != Microsoft.WindowsCE.Forms.ScreenOrientation.Angle0)
                    Microsoft.WindowsCE.Forms.SystemSettings.ScreenOrientation = Microsoft.WindowsCE.Forms.ScreenOrientation.Angle0;
 
            }
        }
        void CheckSensorData(IGSensor sender)
        {
           
            if (sender == null)
                return;
            if (!AllowChangeTilt)
                return;
            GVector vector = sender.GetGVector();
            if (this.IsDisposed)
                return;
            this.Invoke(_changeTilt, vector);
        }
 
        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // HTCForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(192F, 192F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(480, 588);
            this.Location = new System.Drawing.Point(0, 52);
            this.Name = "HTCForm";
            this.ResumeLayout(false);
 
        }
 
 
    }
}