Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Accelerometer Support #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions MonoTouch/Xamarin.Mobile/Motion/MotionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright 2011-2014, Xamarin Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using MonoTouch.CoreMotion;
using MonoTouch.Foundation;

namespace Xamarin {
public class MotionManager {

static CMMotionManager motionManager = new CMMotionManager ();

public event EventHandler<MotionVector> OnMotion;
public event EventHandler<Exception> OnError;

public MotionManager ()
{
}

public void Start ()
{
motionManager.StartAccelerometerUpdates (NSOperationQueue.CurrentQueue, (data, error) => {
if (error != null) {
var ex = new NSErrorException (error);
var errorHandler = OnError;
if (errorHandler != null) {
errorHandler(this, ex);
}
} else {
var vector = new MotionVector () {
X = data.Acceleration.X,
Y = data.Acceleration.Y,
Z = data.Acceleration.Z
};

var motionHandler = OnMotion;
if (motionHandler != null) {
motionHandler(this, vector);
}
}
});
}
}
}

25 changes: 25 additions & 0 deletions MonoTouch/Xamarin.Mobile/Motion/MotionVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright 2011-2014, Xamarin Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;

namespace Xamarin {
public struct MotionVector {
public double X;
public double Y;
public double Z;
}
}

3 changes: 3 additions & 0 deletions MonoTouch/Xamarin.Mobile/Xamarin.Mobile.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@
<Compile Include="..\..\MonoDroid\Xamarin.Mobile\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="Motion\MotionManager.cs" />
<Compile Include="Motion\MotionVector.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="Media\" />
<Folder Include="Properties\" />
<Folder Include="Motion\" />
</ItemGroup>
</Project>