Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Yefancy committed Jan 9, 2025
1 parent ca2823d commit f436f2d
Show file tree
Hide file tree
Showing 27 changed files with 1,740 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

*.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
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
}
}
6 changes: 6 additions & 0 deletions ios_app/iwatch Watch App/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
114 changes: 114 additions & 0 deletions ios_app/iwatch Watch App/ContentView.swift
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()
}
53 changes: 53 additions & 0 deletions ios_app/iwatch Watch App/ExtendedRuntimeManager.swift
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()
}
}
}
40 changes: 40 additions & 0 deletions ios_app/iwatch Watch App/PhoneConnector.swift
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")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit f436f2d

Please sign in to comment.