Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Creating Motion-sensing application Using Xcode; I currently have 3 Xcode projects available: AirGuitar, Angular Rotation rate, Angle Detect I need to combine this three projects

Creating Motion-sensing application Using Xcode;

I currently have 3 Xcode projects available: AirGuitar, Angular Rotation rate, Angle Detect

I need to combine this three projects to make a prototype app with:

1. Mapping 6 different angles (25, 14, 5, -5, -15, -25 degrees each) to 6 different guitar strings, each string with fixed note

2. Mapping the rotation rate (angular velocity) to the strumming force (velocity)

How could I possibly combine these projects by Threading in Swift? (maybe using dispatch queue?)

/////AirGuitar ViewController.swift code;

import UIKit

class ViewController: UIViewController {

var button1: Button1!

var button2: Button2!

var button3: Button3!

var button4: Button4!

var button5: Button5!

var button6: Sampler1!

override func viewDidLoad() {

super.viewDidLoad()

button1 = Button1()

button2 = Button2()

button3 = Button3()

button4 = Button4()

button5 = Button5()

button6 = Sampler1()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

@IBAction func button1Down(_ sender: UIButton) {

button1.play()

}

@IBAction func button2Down(_ sender: UIButton) {

button2.play()

}

@IBAction func button3Down(_ sender: UIButton) {

button3.play()

}

@IBAction func button4Down(_ sender: UIButton) {

button4.play()

}

@IBAction func button5Down(_ sender: UIButton) {

button5.play()

}

@IBAction func button6Down(_ sender: UIButton) {

button6.play()

}

// @IBAction func sampler1Up(_ sender: UIButton) {

// sampler1.stop()

// }

}

///Angle detect ViewController.swift;

import CoreMotion // Import CoreMotion (header)

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

updateDeviceMotions()

}

// Class vars

var pitch = 0

var attitude: CMAttitude?

var roll = 0

var yaw = 0

let motionManager = CMMotionManager() // Create motionManager instance

// Func to get degrees from a double - use if you want to do something like if your user lifts up device (see below)

func degrees(radians:Double) -> Double {

return 180 / .pi * radians

}

func checkYawPitchRoll(deviceMotion:CMDeviceMotion) {

attitude = deviceMotion.attitude

roll = Int(degrees(radians: attitude!.roll))

yaw = Int(degrees(radians: attitude!.yaw))

pitch = Int(degrees(radians: attitude!.pitch))

print("Yaw: \(yaw) Pitch: \(pitch) Roll: \(roll)")

}

func updateDeviceMotions() {

motionManager.deviceMotionUpdateInterval = 0.01 // Change to whatever suits your app - milli-seconds

motionManager.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {

(deviceMotion, error) -> Void in

if(error == nil) {

self.checkYawPitchRoll(deviceMotion: deviceMotion!)

} else {

// Do something if theres an error

}

})

}

}

////Angular Rotation rate ViewController.swift;

import CoreMotion // Import CoreMotion (header)

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

updateDeviceMotions()

}

// Class vars

var rotationRate: CMRotationRate?

var x = 0

var y = 0

var z = 0

let motionManager = CMMotionManager() // Create motionManager instance

// Func to get degrees from a double - use if you want to do something like if your user lifts up device (see below)

func degrees(radians:Double) -> Double {

return 180 / .pi * radians

}

func checkRotationVelocity(gyro: CMGyroData) {

rotationRate = gyro.rotationRate

x = Int(degrees(radians: (rotationRate?.x)!))

y = Int(degrees(radians: (rotationRate?.y)!))

z = Int(degrees(radians: (rotationRate?.z)!))

print("x: \(x) y: \(y) z: \(z)")

}

func updateDeviceMotions() {

motionManager.deviceMotionUpdateInterval = 0.01 // Change to whatever suits your app - milli-seconds

motionManager.startGyroUpdates(to: OperationQueue.current!, withHandler: {

(gyro, error) -> Void in

if(error == nil) {

self.checkRotationVelocity(gyro: gyro!)

} else {

// Do something if theres an error

}

})

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions

Question

What is a brand? How does branding help both buyers and sellers?

Answered: 1 week ago