You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by he...@apache.org on 2013/04/24 01:41:55 UTC
git commit: added iOS accelerometer code
Updated Branches:
refs/heads/master 8866337a3 -> 2e8eff8d4
added iOS accelerometer code
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/commit/2e8eff8d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/2e8eff8d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/2e8eff8d
Branch: refs/heads/master
Commit: 2e8eff8d482c1f3eb9b5a6a76f32e56aab0a28fa
Parents: 8866337
Author: hermwong <he...@gmail.com>
Authored: Tue Apr 23 16:28:56 2013 -0700
Committer: hermwong <he...@gmail.com>
Committed: Tue Apr 23 16:28:56 2013 -0700
----------------------------------------------------------------------
plugin.xml | 22 ++++++-
src/ios/CDVAccelerometer.h | 39 ++++++++++++
src/ios/CDVAccelerometer.m | 128 +++++++++++++++++++++++++++++++++++++++
3 files changed, 186 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/2e8eff8d/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 35b186a..6b83900 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -1,17 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
-xmlns:android="http://schemas.android.com/apk/res/android"
-id="org.apache.cordova.core">
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ id="org.apache.cordova.core"
version="0.1.0">
+
<name>Device Motion</name>
<!-- android -->
<platform name="android">
+
<config-file target="res/xml/config.xml" parent="/cordova/plugins">
<plugin name="Accelerometer" value="org.apache.cordova.core.AccelListener"/>
</config-file>
<source-file src="AccelListener.java" target-dir="org/apache/cordova/core" />
- </platform>
+
+ </platform>
+
+ <!-- ios -->
+ <platform name="ios">
+
+ <config-file target="config.xml" parent="/cordova/plugins">
+ <plugin name="Accelerometer" value="CDVAccelerometer"/>
+ </config-file>
+
+ <header-file src="CDVAccelerometer.h" target-dir="org/apache/cordova/core" />
+ <source-file src="CDVAccelerometer.m" target-dir="org/apache/cordova/core" />
+
+ </platform>
+
</plugin>
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/2e8eff8d/src/ios/CDVAccelerometer.h
----------------------------------------------------------------------
diff --git a/src/ios/CDVAccelerometer.h b/src/ios/CDVAccelerometer.h
new file mode 100755
index 0000000..044ca53
--- /dev/null
+++ b/src/ios/CDVAccelerometer.h
@@ -0,0 +1,39 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+#import <UIKit/UIKit.h>
+#import "CDVPlugin.h"
+
+@interface CDVAccelerometer : CDVPlugin <UIAccelerometerDelegate>
+{
+ double x;
+ double y;
+ double z;
+ NSTimeInterval timestamp;
+}
+
+@property (readonly, assign) BOOL isRunning;
+@property (nonatomic, strong) NSString* callbackId;
+
+- (CDVAccelerometer*)init;
+
+- (void)start:(CDVInvokedUrlCommand*)command;
+- (void)stop:(CDVInvokedUrlCommand*)command;
+
+@end
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/2e8eff8d/src/ios/CDVAccelerometer.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVAccelerometer.m b/src/ios/CDVAccelerometer.m
new file mode 100755
index 0000000..33093d0
--- /dev/null
+++ b/src/ios/CDVAccelerometer.m
@@ -0,0 +1,128 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+#import "CDVAccelerometer.h"
+
+@interface CDVAccelerometer () {}
+@property (readwrite, assign) BOOL isRunning;
+@end
+
+@implementation CDVAccelerometer
+
+@synthesize callbackId, isRunning;
+
+// defaults to 10 msec
+#define kAccelerometerInterval 40
+// g constant: -9.81 m/s^2
+#define kGravitationalConstant -9.81
+
+- (CDVAccelerometer*)init
+{
+ self = [super init];
+ if (self) {
+ x = 0;
+ y = 0;
+ z = 0;
+ timestamp = 0;
+ self.callbackId = nil;
+ self.isRunning = NO;
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [self stop:nil];
+}
+
+- (void)start:(CDVInvokedUrlCommand*)command
+{
+ NSString* cbId = command.callbackId;
+ NSTimeInterval desiredFrequency_num = kAccelerometerInterval;
+ UIAccelerometer* pAccel = [UIAccelerometer sharedAccelerometer];
+
+ // accelerometer expects fractional seconds, but we have msecs
+ pAccel.updateInterval = desiredFrequency_num / 1000;
+ self.callbackId = cbId;
+ if (!self.isRunning) {
+ pAccel.delegate = self;
+ self.isRunning = YES;
+ }
+}
+
+- (void)onReset
+{
+ [self stop:nil];
+}
+
+- (void)stop:(CDVInvokedUrlCommand*)command
+{
+ UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
+
+ theAccelerometer.delegate = nil;
+ self.isRunning = NO;
+}
+
+/**
+ * Picks up accel updates from device and stores them in this class
+ */
+- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
+{
+ if (self.isRunning) {
+ x = acceleration.x;
+ y = acceleration.y;
+ z = acceleration.z;
+ timestamp = ([[NSDate date] timeIntervalSince1970] * 1000);
+ [self returnAccelInfo];
+ }
+}
+
+- (void)returnAccelInfo
+{
+ // Create an acceleration object
+ NSMutableDictionary* accelProps = [NSMutableDictionary dictionaryWithCapacity:4];
+
+ [accelProps setValue:[NSNumber numberWithDouble:x * kGravitationalConstant] forKey:@"x"];
+ [accelProps setValue:[NSNumber numberWithDouble:y * kGravitationalConstant] forKey:@"y"];
+ [accelProps setValue:[NSNumber numberWithDouble:z * kGravitationalConstant] forKey:@"z"];
+ [accelProps setValue:[NSNumber numberWithDouble:timestamp] forKey:@"timestamp"];
+
+ CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:accelProps];
+ [result setKeepCallback:[NSNumber numberWithBool:YES]];
+ [self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
+}
+
+// TODO: Consider using filtering to isolate instantaneous data vs. gravity data -jm
+
+/*
+ #define kFilteringFactor 0.1
+
+ // Use a basic low-pass filter to keep only the gravity component of each axis.
+ grav_accelX = (acceleration.x * kFilteringFactor) + ( grav_accelX * (1.0 - kFilteringFactor));
+ grav_accelY = (acceleration.y * kFilteringFactor) + ( grav_accelY * (1.0 - kFilteringFactor));
+ grav_accelZ = (acceleration.z * kFilteringFactor) + ( grav_accelZ * (1.0 - kFilteringFactor));
+
+ // Subtract the low-pass value from the current value to get a simplified high-pass filter
+ instant_accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (instant_accelX * (1.0 - kFilteringFactor)) );
+ instant_accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (instant_accelY * (1.0 - kFilteringFactor)) );
+ instant_accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (instant_accelZ * (1.0 - kFilteringFactor)) );
+
+
+ */
+@end