Skip to content

Commit

Permalink
Added Objective-C polyfills for events that cannot be used through sw…
Browse files Browse the repository at this point in the history
…ift alone
  • Loading branch information
yagudaev committed Oct 26, 2014
1 parent 7d404c9 commit 5b4f6d8
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 6 deletions.
6 changes: 6 additions & 0 deletions HelloMyoSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
270BA99719FD04970066528B /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 270BA99619FD04970066528B /* README.md */; };
270BA9A019FD18CB0066528B /* GLKitPolyfill.m in Sources */ = {isa = PBXBuildFile; fileRef = 270BA99F19FD18CB0066528B /* GLKitPolyfill.m */; };
278DAE4919FCAEC500EB804B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 278DAE4819FCAEC500EB804B /* AppDelegate.swift */; };
278DAE4C19FCAEC500EB804B /* HelloMyoSwift.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 278DAE4A19FCAEC500EB804B /* HelloMyoSwift.xcdatamodeld */; };
278DAE4E19FCAEC500EB804B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 278DAE4D19FCAEC500EB804B /* ViewController.swift */; };
Expand All @@ -19,6 +20,8 @@

/* Begin PBXFileReference section */
270BA99619FD04970066528B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
270BA99E19FD18CB0066528B /* GLKitPolyfill.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLKitPolyfill.h; sourceTree = "<group>"; };
270BA99F19FD18CB0066528B /* GLKitPolyfill.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GLKitPolyfill.m; sourceTree = "<group>"; };
278DAE4319FCAEC500EB804B /* HelloMyoSwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloMyoSwift.app; sourceTree = BUILT_PRODUCTS_DIR; };
278DAE4719FCAEC500EB804B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
278DAE4819FCAEC500EB804B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -68,6 +71,8 @@
278DAE4519FCAEC500EB804B /* HelloMyoSwift */ = {
isa = PBXGroup;
children = (
270BA99E19FD18CB0066528B /* GLKitPolyfill.h */,
270BA99F19FD18CB0066528B /* GLKitPolyfill.m */,
278DAE4819FCAEC500EB804B /* AppDelegate.swift */,
278DAE4D19FCAEC500EB804B /* ViewController.swift */,
278DAE4F19FCAEC500EB804B /* Main.storyboard */,
Expand Down Expand Up @@ -211,6 +216,7 @@
files = (
278DAE4E19FCAEC500EB804B /* ViewController.swift in Sources */,
278DAE4C19FCAEC500EB804B /* HelloMyoSwift.xcdatamodeld in Sources */,
270BA9A019FD18CB0066528B /* GLKitPolyfill.m in Sources */,
270BA99719FD04970066528B /* README.md in Sources */,
278DAE4919FCAEC500EB804B /* AppDelegate.swift in Sources */,
);
Expand Down
41 changes: 41 additions & 0 deletions HelloMyoSwift.xcworkspace/xcshareddata/HelloMyoSwift.xccheckout
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>FD45879A-1C50-439F-8095-ECAADA55F90F</string>
<key>IDESourceControlProjectName</key>
<string>HelloMyoSwift</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>C5799279B946F5200577EF4B07E5197F26088DCB</key>
<string>github.com:yagudaev/HelloMyoSwift.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>HelloMyoSwift.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>C5799279B946F5200577EF4B07E5197F26088DCB</key>
<string>..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>github.com:yagudaev/HelloMyoSwift.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>C5799279B946F5200577EF4B07E5197F26088DCB</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>C5799279B946F5200577EF4B07E5197F26088DCB</string>
<key>IDESourceControlWCCName</key>
<string>HelloMyoSwift</string>
</dict>
</array>
</dict>
</plist>
15 changes: 14 additions & 1 deletion HelloMyoSwift/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@
<nil key="highlightedColor"/>
</label>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Acceleration" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="xg5-vp-cjb">
<rect key="frame" x="123" y="35" width="96" height="21"/>
<rect key="frame" x="16" y="35" width="288" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="w4e-Bt-NHp">
<rect key="frame" x="22" y="463" width="282" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uc2-TY-5wv">
<rect key="frame" x="16" y="463" width="293" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
Expand All @@ -55,6 +67,7 @@
<outlet property="accelerationLabel" destination="xg5-vp-cjb" id="p3z-iC-ZDT"/>
<outlet property="accelerationProgressBar" destination="vkn-l2-C7Y" id="FK6-SN-VkV"/>
<outlet property="armLabel" destination="heX-Kw-XKs" id="epX-fC-Rz5"/>
<outlet property="gyroscopeLabel" destination="uc2-TY-5wv" id="GbH-di-kvI"/>
<outlet property="helloLabel" destination="lmv-QN-5ba" id="jbg-p4-TI9"/>
</connections>
</viewController>
Expand Down
48 changes: 48 additions & 0 deletions HelloMyoSwift/GLKitPolyfill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// GLKitPolyfill.h
// HelloMyoSwift
//
// Created by Michael Yagudaev on 2014-10-26.
// Copyright (c) 2014 Michael Yagudaev. All rights reserved.
//

#ifndef HelloMyoSwift_GLKitPolyfill_h
#define HelloMyoSwift_GLKitPolyfill_h

#import <MyoKit/MyoKit.h>

@interface AccelerationData: NSObject
@property (nonatomic) float magnitude;
@property (nonatomic) float x;
@property (nonatomic) float y;
@property (nonatomic) float z;
@end

@implementation AccelerationData
@end

@interface OrientationData: NSObject
@property (nonatomic) TLMAngle *pitch;
@property (nonatomic) TLMAngle *yaw;
@property (nonatomic) TLMAngle *roll;
@end

@implementation OrientationData
@end

@interface GyroData: NSObject
@property (nonatomic) float x;
@property (nonatomic) float y;
@property (nonatomic) float z;
@end

@implementation GyroData
@end

@interface GLKitPolyfill: NSObject
+ (AccelerationData *) getAcceleration:(TLMAccelerometerEvent *)accelerometerEvent;
+ (OrientationData *) getOrientation:(TLMOrientationEvent *)orientationEvent;
+ (GyroData *) getGyro:(TLMGyroscopeEvent *)gyroEvent;
@end

#endif
50 changes: 50 additions & 0 deletions HelloMyoSwift/GLKitPolyfill.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// GLKitPolyfill.m
// HelloMyoSwift
//
// Created by Michael Yagudaev on 2014-10-26.
// Copyright (c) 2014 Michael Yagudaev. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GLKitPolyfill.h"

@implementation GLKitPolyfill

+ (AccelerationData *) getAcceleration:(TLMAccelerometerEvent *)accelerometerEvent {
AccelerationData *result = [AccelerationData new];

// Get the acceleration vector from the accelerometer event.
GLKVector3 accelerationVector = accelerometerEvent.vector;

// Calculate the magnitude of the acceleration vector.
result.magnitude = GLKVector3Length(accelerationVector);
result.x = accelerationVector.x;
result.y = accelerationVector.y;
result.z = accelerationVector.z;

return result;
}

+ (OrientationData *) getOrientation:(TLMOrientationEvent *)orientationEvent {
TLMEulerAngles *angles = [TLMEulerAngles anglesWithQuaternion:orientationEvent.quaternion];

OrientationData *result = [OrientationData new];
result.pitch = angles.pitch;
result.yaw = angles.yaw;
result.roll = angles.roll;

return result;
}

+ (GyroData *) getGyro:(TLMGyroscopeEvent *)gyroEvent {
GLKVector3 vector = gyroEvent.vector;
GyroData *result = [GyroData new];
result.x = vector.x;
result.y = vector.y;
result.z = vector.z;

return result;
}

@end
30 changes: 25 additions & 5 deletions HelloMyoSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import UIKit

class ViewController: UIViewController {

@IBOutlet weak var accelerationProgressBar: UIView!
@IBOutlet weak var accelerationProgressBar: UIProgressView!
@IBOutlet weak var helloLabel: UILabel!
@IBOutlet weak var accelerationLabel: UILabel!
@IBOutlet weak var armLabel: UILabel!

@IBOutlet weak var gyroscopeLabel: UILabel!

var currentPose: TLMPose!

override func viewDidLoad() {
Expand Down Expand Up @@ -83,14 +84,28 @@ class ViewController: UIViewController {
let eventData = notification.userInfo as Dictionary<NSString, TLMOrientationEvent>
let orientationEvent = eventData[kTLMKeyOrientationEvent]!

// TODO: NEED SOME SWIFT MAGIC TO DO ORIENTATION? MAYBE HYBRID OBJECTIVE C CODE?
let angles = GLKitPolyfill.getOrientation(orientationEvent)
let pitch = CGFloat(angles.pitch.radians)
let yaw = CGFloat(angles.yaw.radians)
let roll = CGFloat(angles.roll.radians)
let rotationAndPerspectiveTransform:CATransform3D = CATransform3DConcat(CATransform3DConcat(CATransform3DRotate (CATransform3DIdentity, pitch, -1.0, 0.0, 0.0), CATransform3DRotate(CATransform3DIdentity, yaw, 0.0, 1.0, 0.0)), CATransform3DRotate(CATransform3DIdentity, roll, 0.0, 0.0, -1.0))

// Apply the rotation and perspective transform to helloLabel.
helloLabel.layer.transform = rotationAndPerspectiveTransform
}

func didRecieveAccelerationEvent(notification: NSNotification) {
let eventData = notification.userInfo as Dictionary<NSString, TLMAccelerometerEvent>
let accelerometerEvent = eventData[kTLMKeyAccelerometerEvent]!

// TODO: NEED SOME SWIFT MAGIC TO DO ACCELERATION? MAYBE HYBRID OBJECTIVE C CODE?
let acceleration = GLKitPolyfill.getAcceleration(accelerometerEvent);
accelerationProgressBar.progress = acceleration.magnitude / 4.0;

// Uncomment to show direction of acceleration
// let x = acceleration.x
// let y = acceleration.y
// let z = acceleration.z
// accelerationLabel.text = "Acceleration (\(x), \(y), \(z))"
}

func didChangePose(notification: NSNotification) {
Expand Down Expand Up @@ -129,7 +144,12 @@ class ViewController: UIViewController {
let eventData = notification.userInfo as Dictionary<NSString, TLMGyroscopeEvent>
let gyroEvent = eventData[kTLMKeyGyroscopeEvent]!

// TODO: NEED SOME SWIFT MAGIC TO DO GYROSCOPE MAGIC? MAYBE HYBRID OBJECTIVE C CODE?
let gyroData = GLKitPolyfill.getGyro(gyroEvent)
// Uncomment to display the gyro values
// let x = gyroData.x
// let y = gyroData.y
// let z = gyroData.z
// gyroscopeLabel.text = "Gyro: (\(x), \(y), \(z))"
}
}

1 change: 1 addition & 0 deletions bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#define HelloMyoSwift_bridge_h

#import <MyoKit/MyoKit.h>
#import "HelloMyoSwift/GLKitPolyfill.h"

#endif

0 comments on commit 5b4f6d8

Please sign in to comment.