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
+105 -79
+105 -79
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:
[voidController.accelTimer invalidate];
voidController.accelTimer = nil;
if (voidController.reportRateHz && voidController.gamepad.motion.hasGravityAndUserAcceleration) { if (@available(iOS 14.0, *)) {
// Reset the last motion sample switch (motionType) {
GCAcceleration emptyAccelSample = {}; case LI_MOTION_TYPE_ACCEL:
voidController.lastAccelSample = emptyAccelSample; [voidController.accelTimer invalidate];
NSLog(@"setup controller gyro accelTimer"); voidController.accelTimer = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
voidController.hasAccelerometer = YES;
voidController.accelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
// Don't send duplicate samples
GCAcceleration lastAccelSample = voidController.lastAccelSample;
GCAcceleration accelSample = voidController.gamepad.motion.acceleration;
if (memcmp(&accelSample, &lastAccelSample, sizeof(accelSample)) == 0) { if (voidController.reportRateHz && voidController.gamepad.motion.hasGravityAndUserAcceleration) {
return; // Reset the last motion sample
} GCAcceleration emptyAccelSample = {};
voidController.lastAccelSample = accelSample; voidController.lastAccelSample = emptyAccelSample;
NSLog(@"setup controller gyro accelTimer");
dispatch_sync(dispatch_get_main_queue(), ^{
voidController.hasAccelerometer = YES;
voidController.accelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
// Don't send duplicate samples
GCAcceleration lastAccelSample = voidController.lastAccelSample;
GCAcceleration accelSample = voidController.gamepad.motion.acceleration;
// Convert g to m/s^2 if (memcmp(&accelSample, &lastAccelSample, sizeof(accelSample)) == 0) {
//NSLog(@"sending controller gyro data, accelSample data 00: %f, playerIndex: %ld, obj: %@",accelSample.x, (long)voidController.gamepad.playerIndex, voidController); return;
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, }
LI_MOTION_TYPE_ACCEL, voidController.lastAccelSample = accelSample;
accelSample.x * -9.80665f * self->_gyroSensitivity,
accelSample.y * -9.80665f * self->_gyroSensitivity,
accelSample.z * -9.80665f * self->_gyroSensitivity);
}];
});
}
break;
case LI_MOTION_TYPE_GYRO: // Convert g to m/s^2
[voidController.gyroTimer invalidate]; //NSLog(@"sending controller gyro data, accelSample data 00: %f, playerIndex: %ld, obj: %@",accelSample.x, (long)voidController.gamepad.playerIndex, voidController);
voidController.gyroTimer = nil; LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_ACCEL,
accelSample.x * -9.80665f * self->_gyroSensitivity,
accelSample.y * -9.80665f * self->_gyroSensitivity,
accelSample.z * -9.80665f * self->_gyroSensitivity);
}];
});
}
break;
if (voidController.reportRateHz && voidController.gamepad.motion.hasRotationRate) { case LI_MOTION_TYPE_GYRO:
// Reset the last motion sample [voidController.gyroTimer invalidate];
GCRotationRate emptyGyroSample = {}; voidController.gyroTimer = nil;
voidController.lastGyroSample = emptyGyroSample;
//dispatch_sync(dispatch_get_main_queue(), ^{
{dispatch_async(dispatch_get_main_queue(), ^{
voidController.hasGyroscope = YES;
voidController.gyroTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
// Don't send duplicate samples
GCRotationRate lastGyroSample = voidController.lastGyroSample;
GCRotationRate gyroSample = voidController.gamepad.motion.rotationRate;
if (memcmp(&gyroSample, &lastGyroSample, sizeof(gyroSample)) == 0) {
return;
}
voidController.lastGyroSample = gyroSample;
// Convert rad/s to deg/s if (voidController.reportRateHz && voidController.gamepad.motion.hasRotationRate) {
// NSLog(@"sending controller gyro data, gyroSample data 00: %f, playerIndex: %ld, obj: %@",gyroSample.x, (long)voidController.gamepad.playerIndex, voidController); // Reset the last motion sample
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber, GCRotationRate emptyGyroSample = {};
LI_MOTION_TYPE_GYRO, voidController.lastGyroSample = emptyGyroSample;
gyroSample.x * 57.2957795f * self->_gyroSensitivity, //dispatch_sync(dispatch_get_main_queue(), ^{
gyroSample.z * 57.2957795f * self->_gyroSensitivity, {dispatch_async(dispatch_get_main_queue(), ^{
gyroSample.y * -57.2957795f * self->_gyroSensitivity); voidController.hasGyroscope = YES;
}]; voidController.gyroTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / voidController.reportRateHz repeats:YES block:^(NSTimer *timer) {
// }); // Don't send duplicate samples
});} GCRotationRate lastGyroSample = voidController.lastGyroSample;
} GCRotationRate gyroSample = voidController.gamepad.motion.rotationRate;
break; if (memcmp(&gyroSample, &lastGyroSample, sizeof(gyroSample)) == 0) {
return;
}
voidController.lastGyroSample = gyroSample;
// Convert rad/s to deg/s
// NSLog(@"sending controller gyro data, gyroSample data 00: %f, playerIndex: %ld, obj: %@",gyroSample.x, (long)voidController.gamepad.playerIndex, voidController);
LiSendControllerMotionEvent((uint8_t)voidController.controllerNumber,
LI_MOTION_TYPE_GYRO,
gyroSample.x * 57.2957795f * self->_gyroSensitivity,
gyroSample.z * 57.2957795f * self->_gyroSensitivity,
gyroSample.y * -57.2957795f * self->_gyroSensitivity);
}];
// });
});}
}
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);
} }
} }