I wrote this code to show the sensors data and to read and show them in swiftui, it doesn't work unfortunately. can someone help me ?? there's some bugs in swiftui and I have some errors but the most important is first to get the sensor data. is it the correct code to read data for accelerometer and gyroscope ?
'''
HStack{
Button("Start") {
// start()
} .padding()
Button("Stop") {
// start()
} .padding()
}
}
}
func start(){
self.motionManager.gyroUpdateInterval = 0.5
motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
print(data as Any)
if let data = self.motionManager.gyroData {
let xG = data.rotationRate.x
let yG = data.rotationRate.y
let zG = data.rotationRate.z
self.appendReadingGyroscope(x: xG, y: yG, z: zG)
// Use the gyroscope data in your app.
}
}
self.motionManager.accelerometerUpdateInterval = 0.5
motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
print(data as Any)
if let data = self.motionManager.accelerometerData {
let xA = data.acceleration.x
let yA = data.acceleration.y
let zA = data.acceleration.z
self.appendReadingAccelerometer(x: xA, y: yA, z: zA)
// Use the accelerometer data in your app.
}
}
}
func stop() {
self.motionManager.stopGyroUpdates()
self.motionManager.stopAccelerometerUpdates()
}
'''