try to fix: built-in gyro doesn't work on iOS12 & iOS13

This commit is contained in:
TrueZhuanjia committed 2025-09-28 10:59:01 +08:00
1 parent b4493707ca
commit f60433ff3f
1 file changed
+111 -85
+111 -85
View File
@@ -101,15 +101,20 @@ static const double MOUSE_SPEED_DIVISOR = 1.25;
[controller.rightTriggerMotor setMotorAmplitude:rightTrigger]; [controller.rightTriggerMotor setMotorAmplitude:rightTrigger];
} }
- (bool)isLandscapeNow {
return CGRectGetWidth([[UIScreen mainScreen]bounds]) > CGRectGetHeight([[UIScreen mainScreen]bounds]);
}
-(void)updateTimerStateForController:(VoidController* )voidController{ -(void)updateTimerStateForController:(VoidController* )voidController{
if (@available(iOS 14.0, tvOS 14.0, *)) { // if (@available(iOS 14.0, tvOS 14.0, *)) {
if (true) {
if(_gyroMode == GyroModeOff){ if(_gyroMode == GyroModeOff){
[self stopTimerForController:voidController]; [self stopTimerForController:voidController];
return; return;
} }
if(voidController.gamepad.motion.hasAttitudeAndRotationRate) [voidController.motionTypes addObject:@(LI_MOTION_TYPE_ACCEL)]; if(voidController.gamepad.motion.hasAttitudeAndRotationRate) [voidController.motionTypes addObject:@(LI_MOTION_TYPE_ACCEL)];
if(voidController.gamepad.motion.hasRotationRate) [voidController.motionTypes addObject:@(LI_MOTION_TYPE_GYRO)]; if (@available(iOS 14.0, *)) if(voidController.gamepad.motion.hasRotationRate) [voidController.motionTypes addObject:@(LI_MOTION_TYPE_GYRO)];
for(NSNumber* motionTypeObj in voidController.motionTypes){ for(NSNumber* motionTypeObj in voidController.motionTypes){
uint8_t motionType = motionTypeObj.intValue; uint8_t motionType = motionTypeObj.intValue;
@@ -147,20 +152,29 @@ static const double MOUSE_SPEED_DIVISOR = 1.25;
voidController.lastDeviceAccelSample = deviceAccelSample; voidController.lastDeviceAccelSample = deviceAccelSample;
// Convert g to m/s^2 // Convert g to m/s^2
if(UIApplication.sharedApplication.windows.firstObject.windowScene.interfaceOrientation == 4){ //check for landscape left or landscape right if (@available(iOS 13.0, *)) {
if(UIApplication.sharedApplication.windows.firstObject.windowScene.interfaceOrientation == 4){ //check for landscape left or landscape right
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_ACCEL,
deviceAccelSample.y * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.z * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.x * -9.80665f * self->_gyroSensitivity);
}
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_ACCEL,
deviceAccelSample.y * +9.80665f * self->_gyroSensitivity,
deviceAccelSample.z * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.x * +9.80665f * self->_gyroSensitivity);
}
}
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_ACCEL, LI_MOTION_TYPE_ACCEL,
deviceAccelSample.y * -9.80665f * self->_gyroSensitivity, deviceAccelSample.y * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.z * -9.80665f * self->_gyroSensitivity, deviceAccelSample.z * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.x * -9.80665f * self->_gyroSensitivity); deviceAccelSample.x * -9.80665f * self->_gyroSensitivity);
} }
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_ACCEL,
deviceAccelSample.y * +9.80665f * self->_gyroSensitivity,
deviceAccelSample.z * -9.80665f * self->_gyroSensitivity,
deviceAccelSample.x * +9.80665f * self->_gyroSensitivity);
}
}]; }];
});} });}
break; break;
@@ -187,20 +201,29 @@ static const double MOUSE_SPEED_DIVISOR = 1.25;
// Convert rad/s to deg/s // Convert rad/s to deg/s
//NSLog(@"sending built-in gyro data, accelSample data 00: %f, playerIndex: %d",deviceGyroSample.x, voidController.playerIndex); //NSLog(@"sending built-in gyro data, accelSample data 00: %f, playerIndex: %d",deviceGyroSample.x, voidController.playerIndex);
if(UIApplication.sharedApplication.windows.firstObject.windowScene.interfaceOrientation == 4){//check for landscape left or landscape right if (@available(iOS 13.0, *)) {
if(UIApplication.sharedApplication.windows.firstObject.windowScene.interfaceOrientation == 4){//check for landscape left or landscape right
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_GYRO,
deviceGyroSample.y * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.z * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.x * 57.2957795f * self->_gyroSensitivity);
}
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_GYRO,
deviceGyroSample.y * -57.2957795f * self->_gyroSensitivity,
deviceGyroSample.z * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.x * -57.2957795f * self->_gyroSensitivity);
}
}
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_GYRO, LI_MOTION_TYPE_GYRO,
deviceGyroSample.y * 57.2957795f * self->_gyroSensitivity, deviceGyroSample.y * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.z * 57.2957795f * self->_gyroSensitivity, deviceGyroSample.z * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.x * 57.2957795f * self->_gyroSensitivity); deviceGyroSample.x * 57.2957795f * self->_gyroSensitivity);
} }
else{
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_GYRO,
deviceGyroSample.y * -57.2957795f * self->_gyroSensitivity,
deviceGyroSample.z * 57.2957795f * self->_gyroSensitivity,
deviceGyroSample.x * -57.2957795f * self->_gyroSensitivity);
}
}]; }];
break; break;
} }
@@ -209,72 +232,75 @@ static const double MOUSE_SPEED_DIVISOR = 1.25;
#endif #endif
else{ else{
NSLog(@"controller obj timer update: controller timer "); NSLog(@"controller obj timer update: controller timer ");
switch (motionType) {
case LI_MOTION_TYPE_ACCEL: if (@available(iOS 14.0, *)) {
[voidController.accelTimer invalidate]; switch (motionType) {
voidController.accelTimer = nil; case LI_MOTION_TYPE_ACCEL:
[voidController.accelTimer invalidate];
if (voidController.reportRateHz && voidController.gamepad.motion.hasGravityAndUserAcceleration) { voidController.accelTimer = nil;
// Reset the last motion sample
GCAcceleration emptyAccelSample = {}; if (voidController.reportRateHz && voidController.gamepad.motion.hasGravityAndUserAcceleration) {
voidController.lastAccelSample = emptyAccelSample; // Reset the last motion sample
NSLog(@"setup controller gyro accelTimer"); GCAcceleration emptyAccelSample = {};
dispatch_sync(dispatch_get_main_queue(), ^{ voidController.lastAccelSample = emptyAccelSample;
voidController.hasAccelerometer = YES; NSLog(@"setup controller gyro accelTimer");
voidController.accelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) { dispatch_sync(dispatch_get_main_queue(), ^{
// Don't send duplicate samples voidController.hasAccelerometer = YES;
GCAcceleration lastAccelSample = voidController.lastAccelSample; voidController.accelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
GCAcceleration accelSample = voidController.gamepad.motion.acceleration; // Don't send duplicate samples
GCAcceleration lastAccelSample = voidController.lastAccelSample;
if (memcmp(&accelSample, &lastAccelSample, sizeof(accelSample)) == 0) { GCAcceleration accelSample = voidController.gamepad.motion.acceleration;
return;
} if (memcmp(&accelSample, &lastAccelSample, sizeof(accelSample)) == 0) {
voidController.lastAccelSample = accelSample; return;
}
// Convert g to m/s^2 voidController.lastAccelSample = accelSample;
//NSLog(@"sending controller gyro data, accelSample data 00: %f, playerIndex: %ld, obj: %@",accelSample.x, (long)voidController.gamepad.playerIndex, voidController);
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, // Convert g to m/s^2
LI_MOTION_TYPE_ACCEL, //NSLog(@"sending controller gyro data, accelSample data 00: %f, playerIndex: %ld, obj: %@",accelSample.x, (long)voidController.gamepad.playerIndex, voidController);
accelSample.x * -9.80665f * self->_gyroSensitivity, LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
accelSample.y * -9.80665f * self->_gyroSensitivity, LI_MOTION_TYPE_ACCEL,
accelSample.z * -9.80665f * self->_gyroSensitivity); accelSample.x * -9.80665f * self->_gyroSensitivity,
}]; accelSample.y * -9.80665f * self->_gyroSensitivity,
}); accelSample.z * -9.80665f * self->_gyroSensitivity);
} }];
break; });
}
case LI_MOTION_TYPE_GYRO: break;
[voidController.gyroTimer invalidate];
voidController.gyroTimer = nil; case LI_MOTION_TYPE_GYRO:
[voidController.gyroTimer invalidate];
if (voidController.reportRateHz && voidController.gamepad.motion.hasRotationRate) { voidController.gyroTimer = nil;
// Reset the last motion sample
GCRotationRate emptyGyroSample = {}; if (voidController.reportRateHz && voidController.gamepad.motion.hasRotationRate) {
voidController.lastGyroSample = emptyGyroSample; // Reset the last motion sample
//dispatch_sync(dispatch_get_main_queue(), ^{ GCRotationRate emptyGyroSample = {};
{dispatch_async(dispatch_get_main_queue(), ^{ voidController.lastGyroSample = emptyGyroSample;
voidController.hasGyroscope = YES; //dispatch_sync(dispatch_get_main_queue(), ^{
voidController.gyroTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) { {dispatch_async(dispatch_get_main_queue(), ^{
// Don't send duplicate samples voidController.hasGyroscope = YES;
GCRotationRate lastGyroSample = voidController.lastGyroSample; voidController.gyroTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
GCRotationRate gyroSample = voidController.gamepad.motion.rotationRate; // Don't send duplicate samples
if (memcmp(&gyroSample, &lastGyroSample, sizeof(gyroSample)) == 0) { GCRotationRate lastGyroSample = voidController.lastGyroSample;
return; GCRotationRate gyroSample = voidController.gamepad.motion.rotationRate;
} if (memcmp(&gyroSample, &lastGyroSample, sizeof(gyroSample)) == 0) {
voidController.lastGyroSample = gyroSample; return;
}
// Convert rad/s to deg/s voidController.lastGyroSample = gyroSample;
// NSLog(@"sending controller gyro data, gyroSample data 00: %f, playerIndex: %ld, obj: %@",gyroSample.x, (long)voidController.gamepad.playerIndex, voidController);
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, // Convert rad/s to deg/s
LI_MOTION_TYPE_GYRO, // NSLog(@"sending controller gyro data, gyroSample data 00: %f, playerIndex: %ld, obj: %@",gyroSample.x, (long)voidController.gamepad.playerIndex, voidController);
gyroSample.x * 57.2957795f * self->_gyroSensitivity, LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
gyroSample.z * 57.2957795f * self->_gyroSensitivity, LI_MOTION_TYPE_GYRO,
gyroSample.y * -57.2957795f * self->_gyroSensitivity); gyroSample.x * 57.2957795f * self->_gyroSensitivity,
}]; gyroSample.z * 57.2957795f * self->_gyroSensitivity,
// }); gyroSample.y * -57.2957795f * self->_gyroSensitivity);
});} }];
} // });
break; });}
}
break;
}
} }
} }
} }
@@ -283,7 +309,7 @@ static const double MOUSE_SPEED_DIVISOR = 1.25;
// Set the motion sensor state if they require manual activation // Set the motion sensor state if they require manual activation
[self updateSensorSateForController:voidController]; [self updateSensorSateForController:voidController];
NSLog(@"sensor active: %d", voidController.gamepad.motion.sensorsActive); // NSLog(@"sensor active: %d", voidController.gamepad.motion.sensorsActive);
} }
} }