AW: G-Sensor Programmierung
Guck mal hier:
http://blog.enterprisemobile.com/200...-managed-code/
Damit hab ich mal was programmiert (Visual Studio, .NET CF, C#), ging super...
AW: G-Sensor Programmierung
Ich habe den Thread verschoben, hier würdest du mehr Antworten bekommen.
mfg
timsah
AW: G-Sensor Programmierung
AW: G-Sensor Programmierung
Vielen dank fuer die schnelle Hilfe, hab gefunden wonach ich gesucht hab.
Denke es werden jetzt einige lange Naechte folgen ;)
AW: G-Sensor Programmierung
Hallo,
ich versuche ebenfalls den G-Sensor auf einem HTC HD zu programmieren, finde bisher aber nichts dazu im .NET Compact Framework. Die bereits geposteten Links habe ich bereits gefunden, diese beziehen sich aber nur auf C#. Hat das jemand schon in VB realisiert?
AW: G-Sensor Programmierung
Bis auf eine leicht andere Syntax sollte das genauso funktionieren!
AW: G-Sensor Programmierung
Leicht andere Syntax? Ich weiß nicht mal, wie ich mit dem C# Code anfangen muss :(
AW: G-Sensor Programmierung
Das C# Projekt gibt's hier (unter source code). Das enthält Code für die verschiedenen Sensoren für HTC und Omnia.
Da ich nur den G-Sensor für HTC Geräte brauche, habe ich die folgende Klasse erstellt:
Code:
using System;
using System.Runtime.InteropServices;
namespace GSensorTest
{
public class GSensor : IDisposable
{
public struct SensorData
{
public short TiltX; // From -1000 to 1000, 0 is flat
public short TiltY; // From -1000 to 1000, 0 is flat
public short TiltZ; // From -1000 to 1000, 0 = Straight up, -1000 = Flat, 1000 = Upside down
public short Unknown1;
public int AngleY; // From 0 to 359 degrees
public int AngleX; // From 0 to 359 degrees
public int Unknown2;
};
[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);
private IntPtr _handle;
public GSensor()
{
_handle = HTCSensorOpen(1);
IntPtr hEvent = CreateEvent(IntPtr.Zero, true, false, "HTC_GSENSOR_SERVICESTART");
EventModify(hEvent, 3);
CloseHandle(hEvent);
}
public void GetSensorData(ref SensorData data)
{
HTCSensorGetDataOutput(_handle, out data);
}
#region IDisposable Members
public void Dispose()
{
if (_handle != IntPtr.Zero)
{
HTCSensorClose(_handle);
_handle = IntPtr.Zero;
}
IntPtr hEvent = CreateEvent(IntPtr.Zero, true, false, "HTC_GSENSOR_SERVICESTOP");
EventModify(hEvent, 3);
CloseHandle(hEvent);
}
#endregion
}
}
Und für VB.net gibt's diverse freie Konvertierer (z.B. C# - VB.net Konvertierer)
Mit folgendem Resultat:
Code:
Imports System
Imports System.Runtime.InteropServices
Namespace GSensorTest
Public Class GSensor
Implements IDisposable
Public Structure SensorData
Public TiltX As Short
' From -1000 to 1000, 0 is flat
Public TiltY As Short
' From -1000 to 1000, 0 is flat
Public TiltZ As Short
' From -1000 to 1000, 0 = Straight up, -1000 = Flat, 1000 = Upside down
Public Unknown1 As Short
Public AngleY As Integer
' From 0 to 359 degrees
Public AngleX As Integer
' From 0 to 359 degrees
Public Unknown2 As Integer
End Structure
<DllImport("HTCSensorSDK")> _
Private Shared Function HTCSensorGetDataOutput(ByVal handle As IntPtr, ByRef sensorData As SensorData) As IntPtr
End Function
<DllImport("HTCSensorSDK")> _
Private Shared Function HTCSensorOpen(ByVal sensor As Integer) As IntPtr
End Function
<DllImport("HTCSensorSDK")> _
Private Shared Sub HTCSensorClose(ByVal handle As IntPtr)
End Sub
<DllImport("coredll", SetLastError := True)> _
Private Shared Function CreateEvent(ByVal eventAttributes As IntPtr, ByVal manualReset As Boolean, ByVal intialState As Boolean, ByVal name As String) As IntPtr
End Function
<DllImport("coredll", SetLastError := True)> _
Private Shared Function EventModify(ByVal handle As IntPtr, ByVal func As UInteger) As Boolean
End Function
<DllImport("coredll")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
Private _handle As IntPtr
Public Sub New()
_handle = HTCSensorOpen(1)
Dim hEvent As IntPtr = CreateEvent(IntPtr.Zero, True, False, "HTC_GSENSOR_SERVICESTART")
EventModify(hEvent, 3)
CloseHandle(hEvent)
End Sub
Public Sub GetSensorData(ByRef data As SensorData)
HTCSensorGetDataOutput(_handle, data)
End Sub
#Region "IDisposable Members"
Public Sub Dispose()
If _handle <> IntPtr.Zero Then
HTCSensorClose(_handle)
_handle = IntPtr.Zero
End If
Dim hEvent As IntPtr = CreateEvent(IntPtr.Zero, True, False, "HTC_GSENSOR_SERVICESTOP")
EventModify(hEvent, 3)
CloseHandle(hEvent)
End Sub
#End Region
End Class
End Namespace
Da der Emulator diesen Sensor nicht hat (die entsprechende dll fehlt natürlich auch), habe ich im code so etwas wie
Code:
private GSensor _gSensor;
...
// init
if (File.Exists(@"\Windows\HTCSensorSDK.dll"))
{
_gSensor = new GSensor();
}
...
// get sensor data
if (_gSensor != null)
{
GSensor.SensorData sensorData = new GSensor.SensorData();
_gSensor.GetSensorData(ref sensorData);
}
...
// close application
if (_gSensor != null)
{
_gSensor.Dispose();
}
AW: G-Sensor Programmierung
Wow, cool! Ich werde es damit mal ausprobieren! Vielen Dank!
AW: G-Sensor Programmierung
So, habe es nun endlich ausprobieren können und es funktioniert :)
Vielen Dank nochmal :)
AW: G-Sensor Programmierung
Hallo,
könntet Ihr wohl ein Beispiel zum Download bereitstellen? Irgendwie bekomme ich das nicht hin.
Danke im Voraus!
Gruß,
T.u.b.e.
AW: G-Sensor Programmierung
Das ist die GSensor-API-Klasse in VB.NET, so wie ich sie gerade verwende:
Code:
Imports System
Imports System.Runtime.InteropServices
Public Class GSensor
Implements IDisposable
Public Structure SensorData
Public TiltX As Short ' From -1000 to 1000, 0 is flat
Public TiltY As Short ' From -1000 to 1000, 0 is flat
Public TiltZ As Short ' From -1000 to 1000, 0 = Straight up, -1000 = Flat, 1000 = Upside down
Public Unknown1 As Short
Public AngleY As Integer ' From 0 to 359 degrees
Public AngleX As Integer ' From 0 to 359 degrees
Public Unknown2 As Integer
End Structure
<DllImport("HTCSensorSDK")> _
Private Shared Function HTCSensorGetDataOutput(ByVal handle As IntPtr, ByRef sensorData As SensorData) As IntPtr
End Function
<DllImport("HTCSensorSDK")> _
Private Shared Function HTCSensorOpen(ByVal sensor As Integer) As IntPtr
End Function
<DllImport("HTCSensorSDK")> _
Private Shared Sub HTCSensorClose(ByVal handle As IntPtr)
End Sub
<DllImport("coredll", SetLastError:=True)> _
Private Shared Function CreateEvent(ByVal eventAttributes As IntPtr, ByVal manualReset As Boolean, ByVal intialState As Boolean, ByVal name As String) As IntPtr
End Function
<DllImport("coredll", SetLastError:=True)> _
Private Shared Function EventModify(ByVal handle As IntPtr, ByVal func As UInteger) As Boolean
End Function
<DllImport("coredll")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
Private _handle As IntPtr
Public Sub New()
Dim hEvent As IntPtr
Try
_handle = HTCSensorOpen(1)
hEvent = CreateEvent(IntPtr.Zero, True, False, "HTC_GSENSOR_SERVICESTART")
EventModify(hEvent, 3)
CloseHandle(hEvent)
Catch
MsgBox("This is not a HTC Device")
End Try
End Sub
Public Sub GetSensorData(ByRef data As SensorData)
If (_handle <> IntPtr.Zero) Then
HTCSensorGetDataOutput(_handle, data)
End If
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
Dim hEvent As IntPtr
If (_handle <> IntPtr.Zero) Then
HTCSensorClose(_handle)
_handle = IntPtr.Zero
End If
hEvent = CreateEvent(IntPtr.Zero, True, False, "HTC_GSENSOR_SERVICESTOP")
EventModify(hEvent, 3)
CloseHandle(hEvent)
End Sub
End Class