-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,740 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,3 +88,5 @@ fastlane/test_output | |
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ | ||
|
||
*.DS_Store |
11 changes: 11 additions & 0 deletions
11
ios_app/iwatch Watch App/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
ios_app/iwatch Watch App/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "watchos", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// ContentView.swift | ||
// iwatch Watch App | ||
// | ||
// Created by 梁丰洲 on 2024/12/15. | ||
// | ||
|
||
import SwiftUI | ||
import WatchConnectivity | ||
|
||
func quaternionToEulerAngles(w: Double, x: Double, y: Double, z: Double) -> (roll: Double, pitch: Double, yaw: Double) { | ||
let sinr_cosp = 2.0 * (w * x + y * z) | ||
let cosr_cosp = 1.0 - 2.0 * (x * x + y * y) | ||
let roll = atan2(sinr_cosp, cosr_cosp) | ||
|
||
let sinp = 2.0 * (w * y - z * x) | ||
let pitch: Double | ||
if abs(sinp) >= 1 { | ||
pitch = sinp > 0 ? Double.pi / 2 : -Double.pi / 2 | ||
} else { | ||
pitch = asin(sinp) | ||
} | ||
|
||
let siny_cosp = 2.0 * (w * z + x * y) | ||
let cosy_cosp = 1.0 - 2.0 * (y * y + z * z) | ||
let yaw = atan2(siny_cosp, cosy_cosp) | ||
|
||
return (roll, pitch, yaw) | ||
} | ||
|
||
struct ContentView: View { | ||
@State private var logStarting = false | ||
@State private var showAlert = false | ||
@ObservedObject var sensorLogger = WatchSensorManager.shared | ||
|
||
var body: some View { | ||
VStack { | ||
Text("Watch IMU Recorder") | ||
Button(action: { | ||
if !WCSession.default.isReachable { | ||
self.showAlert = true | ||
return | ||
} | ||
|
||
self.logStarting.toggle() | ||
|
||
if self.logStarting { | ||
var samplingFrequency = UserDefaults.standard.integer(forKey: "frequency_preference") | ||
|
||
if samplingFrequency <= 0 { | ||
samplingFrequency = 30 | ||
} | ||
|
||
print("sampling frequency = \(samplingFrequency) on watch") | ||
self.sensorLogger.startUpdate(Double(samplingFrequency)) | ||
} | ||
else { | ||
self.sensorLogger.stopUpdate() | ||
} | ||
}) { | ||
if self.logStarting { | ||
Image(systemName: "pause.circle") | ||
Text("stop pushing") | ||
} | ||
else { | ||
Image(systemName: "play.circle") | ||
Text("Push data") | ||
} | ||
}.alert(isPresented: $showAlert) { | ||
Alert(title: Text("Watch Not Active"), message: Text("Please activate your watch by raising your wrist before pushing data."), dismissButton: .default(Text("OK"))) | ||
} | ||
} | ||
|
||
VStack { | ||
VStack { | ||
Text("Accelerometer").font(.headline) | ||
HStack { | ||
Text(String(format: "%.2f", self.sensorLogger.accX)) | ||
Spacer() | ||
Text(String(format: "%.2f", self.sensorLogger.accY)) | ||
Spacer() | ||
Text(String(format: "%.2f", self.sensorLogger.accZ)) | ||
}.padding(.horizontal) | ||
} | ||
|
||
VStack { | ||
Text("Gyroscope").font(.headline) | ||
HStack { | ||
Text(String(format: "%.2f", self.sensorLogger.gyrX)) | ||
Spacer() | ||
Text(String(format: "%.2f", self.sensorLogger.gyrX)) | ||
Spacer() | ||
Text(String(format: "%.2f", self.sensorLogger.gyrX)) | ||
}.padding(.horizontal) | ||
} | ||
|
||
VStack { | ||
HStack { | ||
let (roll, pitch, yaw) = quaternionToEulerAngles(w: self.sensorLogger.qatW, x: self.sensorLogger.qatX, y: self.sensorLogger.qatY, z: self.sensorLogger.qatZ) | ||
|
||
Text(String(format: "%.2f", roll * 180.0 / Double.pi)) | ||
Spacer() | ||
Text(String(format: "%.2f", pitch * 180.0 / Double.pi)) | ||
Spacer() | ||
Text(String(format: "%.2f", yaw * 180.0 / Double.pi)) | ||
}.padding(.horizontal) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
ContentView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// ExtendedRuntimeManager.swift | ||
// phone | ||
// | ||
// Created by 梁丰洲 on 2024/12/16. | ||
// | ||
|
||
import WatchKit | ||
|
||
class ExtendedRuntimeManager: NSObject, WKExtendedRuntimeSessionDelegate { | ||
static let shared = ExtendedRuntimeManager() | ||
|
||
private var session: WKExtendedRuntimeSession? | ||
|
||
private override init() { | ||
super.init() | ||
} | ||
|
||
func startSession() { | ||
guard session == nil else { return } | ||
|
||
session = WKExtendedRuntimeSession() | ||
session?.delegate = self | ||
session?.start() | ||
|
||
print("Extended Runtime Session started.") | ||
} | ||
|
||
func endSession() { | ||
session?.invalidate() | ||
session = nil | ||
|
||
print("Extended Runtime Session ended.") | ||
} | ||
|
||
// MARK: - WKExtendedRuntimeSessionDelegate | ||
|
||
func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) { | ||
print("Extended Runtime Session did start.") | ||
} | ||
|
||
func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) { | ||
print("Extended Runtime Session will expire.") | ||
} | ||
|
||
func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) { | ||
print("Extended Runtime Session invalidated: \(reason.rawValue), error: \(String(describing: error))") | ||
session = nil | ||
if reason == .expired { | ||
startSession() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// PhoneConnector.swift | ||
// phone | ||
// | ||
// Created by 梁丰洲 on 2024/12/15. | ||
// | ||
|
||
import WatchConnectivity | ||
import WatchKit | ||
|
||
class PhoneConnector: NSObject, WCSessionDelegate { | ||
|
||
override init() { | ||
super.init() | ||
if WCSession.isSupported() { | ||
WCSession.default.delegate = self | ||
WCSession.default.activate() | ||
} | ||
} | ||
|
||
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) { | ||
print("watch activationDidCompleteWith state = \(activationState.rawValue)") | ||
} | ||
|
||
func sessionReachabilityDidChange(_ session: WCSession) { | ||
// 当iPhone或Watch端连通性改变时会调用 | ||
print("watch sessionReachabilityDidChange") | ||
} | ||
|
||
func sendPhoneMessage(data: [String: Any]) { | ||
let session = WCSession.default | ||
if session.isReachable { | ||
session.sendMessage(data, replyHandler: nil, errorHandler: { (error) in | ||
print("watch send message to iPhone error: \(error.localizedDescription)") | ||
}) | ||
} else { | ||
print("watch session is not reachable") | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
ios_app/iwatch Watch App/Preview Content/Preview Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Oops, something went wrong.