Added the iOS sensor implementation

This commit is contained in:
Sam Lantinga 2018-08-21 17:24:12 -07:00
parent bdbc06b027
commit c6647bf9c9
8 changed files with 320 additions and 6 deletions

View File

@ -195,6 +195,14 @@
AADC5A631FDA10C800960936 /* SDL_shaders_metal_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */; };
AADC5A641FDA10C800960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C98212CD0360047DF2E /* SDL_sensor.h */; };
F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */; };
F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */; };
F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */; };
F3BDD77620F51C3C004ECBF3 /* hid.mm in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD77520F51C3C004ECBF3 /* hid.mm */; };
F3BDD79220F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
F3BDD79320F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
@ -521,6 +529,12 @@
AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_bits.h; sourceTree = "<group>"; };
AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_shaders_metal_ios.h; sourceTree = "<group>"; };
AADC5A621FDA10C800960936 /* SDL_render_metal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_render_metal.m; sourceTree = "<group>"; };
F30D9C98212CD0360047DF2E /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = "<group>"; };
F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor_c.h; sourceTree = "<group>"; };
F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_syssensor.h; sourceTree = "<group>"; };
F30D9C9D212CD0990047DF2E /* SDL_sensor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_sensor.c; sourceTree = "<group>"; };
F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_coremotionsensor.m; sourceTree = "<group>"; };
F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_coremotionsensor.h; sourceTree = "<group>"; };
F3BDD77520F51C3C004ECBF3 /* hid.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = hid.mm; sourceTree = "<group>"; };
F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xbox360.c; sourceTree = "<group>"; };
F3BDD78C20F51CB8004ECBF3 /* SDL_hidapi_switch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch.c; sourceTree = "<group>"; };
@ -814,6 +828,26 @@
path = metal;
sourceTree = "<group>";
};
F30D9C9A212CD0590047DF2E /* sensor */ = {
isa = PBXGroup;
children = (
F30D9CA2212CD09E0047DF2E /* coremotion */,
F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */,
F30D9C9D212CD0990047DF2E /* SDL_sensor.c */,
F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */,
);
path = sensor;
sourceTree = "<group>";
};
F30D9CA2212CD09E0047DF2E /* coremotion */ = {
isa = PBXGroup;
children = (
F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */,
F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */,
);
path = coremotion;
sourceTree = "<group>";
};
F35CEA6E20F51B7F003ECE98 /* hidapi */ = {
isa = PBXGroup;
children = (
@ -976,6 +1010,7 @@
AA7558891595D55500BBD41B /* SDL_revision.h */,
AA75588A1595D55500BBD41B /* SDL_rwops.h */,
AA75588B1595D55500BBD41B /* SDL_scancode.h */,
F30D9C98212CD0360047DF2E /* SDL_sensor.h */,
AA75588C1595D55500BBD41B /* SDL_shape.h */,
AA75588D1595D55500BBD41B /* SDL_stdinc.h */,
AA75588E1595D55500BBD41B /* SDL_surface.h */,
@ -1009,6 +1044,7 @@
FD8BD8150E27E25900B52CD5 /* loadso */,
56ED04DE118A8E9A00A56AA6 /* power */,
041B2CE312FA0F680087D585 /* render */,
F30D9C9A212CD0590047DF2E /* sensor */,
FD3F4A6F0DEA620800C5B771 /* stdlib */,
FD99B9E00DD52EDC00FB1D6B /* thread */,
FD99BA1E0DD52EDC00FB1D6B /* timer */,
@ -1263,6 +1299,7 @@
AA75589C1595D55500BBD41B /* SDL_audio.h in Headers */,
55FFA91A2122302B00D7CBED /* SDL_syspower.h in Headers */,
AA75589D1595D55500BBD41B /* SDL_blendmode.h in Headers */,
F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */,
AA75589E1595D55500BBD41B /* SDL_clipboard.h in Headers */,
AA75589F1595D55500BBD41B /* SDL_config_iphoneos.h in Headers */,
AA7558A01595D55500BBD41B /* SDL_config.h in Headers */,
@ -1277,6 +1314,7 @@
AA7558A71595D55500BBD41B /* SDL_haptic.h in Headers */,
AA7558A81595D55500BBD41B /* SDL_hints.h in Headers */,
566726461DF72CF5001DD3DB /* SDL_dataqueue.h in Headers */,
F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */,
AA7558AA1595D55500BBD41B /* SDL_joystick.h in Headers */,
AA13B34B1FB8B27800D9FEE6 /* SDL_shape_internals.h in Headers */,
AA7558AB1595D55500BBD41B /* SDL_keyboard.h in Headers */,
@ -1284,6 +1322,7 @@
AA7558AC1595D55500BBD41B /* SDL_keycode.h in Headers */,
AA7558AD1595D55500BBD41B /* SDL_loadso.h in Headers */,
AA7558AE1595D55500BBD41B /* SDL_log.h in Headers */,
F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */,
AA7558AF1595D55500BBD41B /* SDL_main.h in Headers */,
AA7558B01595D55500BBD41B /* SDL_mouse.h in Headers */,
AA7558B11595D55500BBD41B /* SDL_mutex.h in Headers */,
@ -1306,6 +1345,7 @@
FAD4F7021BA3C4E8008346CE /* SDL_sysjoystick_c.h in Headers */,
AA7558C11595D55500BBD41B /* SDL_surface.h in Headers */,
AA7558C21595D55500BBD41B /* SDL_system.h in Headers */,
F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */,
AA7558C31595D55500BBD41B /* SDL_syswm.h in Headers */,
AA7558C41595D55500BBD41B /* SDL_thread.h in Headers */,
AA7558C51595D55500BBD41B /* SDL_timer.h in Headers */,
@ -1495,9 +1535,11 @@
AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */,
FAB598561BB5C31600BE72C5 /* SDL_sysloadso.c in Sources */,
FAB598571BB5C31600BE72C5 /* SDL_power.c in Sources */,
F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */,
FAB598581BB5C31600BE72C5 /* SDL_syspower.m in Sources */,
56F9D5601DF73BA400C15B5D /* SDL_dataqueue.c in Sources */,
FAB598591BB5C31600BE72C5 /* SDL_render_gles.c in Sources */,
F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
FAB5985A1BB5C31600BE72C5 /* SDL_render_gles2.c in Sources */,
FAB5985B1BB5C31600BE72C5 /* SDL_shaders_gles2.c in Sources */,
FAB5985D1BB5C31600BE72C5 /* SDL_blendfillrect.c in Sources */,
@ -1570,6 +1612,7 @@
files = (
FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */,
FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */,
F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
FD3F4A7B0DEA620800C5B771 /* SDL_string.c in Sources */,
FD6526660DE8FCDD002AD96B /* SDL_dummyaudio.c in Sources */,
FD6526670DE8FCDD002AD96B /* SDL_audio.c in Sources */,
@ -1667,6 +1710,7 @@
0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */,
04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */,
56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.m in Sources */,
F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */,
F3BDD79420F51CB8004ECBF3 /* SDL_hidapi_switch.c in Sources */,
93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */,
AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */,

View File

@ -139,8 +139,8 @@
#define SDL_JOYSTICK_MFI 1
#define SDL_JOYSTICK_HIDAPI 1
/* Enable the dummy sensor driver */
#define SDL_SENSOR_DUMMY 1
/* Enable the CoreMotion sensor driver */
#define SDL_SENSOR_COREMOTION 1
/* Enable Unix style SO loading */
#define SDL_LOADSO_DLOPEN 1

View File

@ -36,6 +36,9 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
#ifdef SDL_SENSOR_ANDROID
&SDL_ANDROID_SensorDriver,
#endif
#ifdef SDL_SENSOR_COREMOTION
&SDL_COREMOTION_SensorDriver,
#endif
#if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
&SDL_DUMMY_SensorDriver
#endif

View File

@ -93,6 +93,7 @@ typedef struct _SDL_SensorDriver
/* The available sensor drivers */
extern SDL_SensorDriver SDL_ANDROID_SensorDriver;
extern SDL_SensorDriver SDL_COREMOTION_SensorDriver;
extern SDL_SensorDriver SDL_DUMMY_SensorDriver;
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -156,7 +156,7 @@ SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
}
static void
SDL_ANDROID_SensorUpdate(SDL_Sensor* sensor)
SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
{
int events;
ASensorEvent event;
@ -171,7 +171,7 @@ SDL_ANDROID_SensorUpdate(SDL_Sensor* sensor)
}
static void
SDL_ANDROID_SensorClose(SDL_Sensor * sensor)
SDL_ANDROID_SensorClose(SDL_Sensor *sensor)
{
if (sensor->hwdata) {
ASensorEventQueue_disableSensor(sensor->hwdata->eventqueue, sensor->hwdata->asensor);

View File

@ -0,0 +1,30 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
/* The private structure used to keep track of a sensor */
struct sensor_hwdata
{
float data[3];
};
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -0,0 +1,236 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#ifdef SDL_SENSOR_COREMOTION
/* This is the system specific header for the SDL sensor API */
#include <CoreMotion/CoreMotion.h>
#include "SDL_error.h"
#include "SDL_sensor.h"
#include "SDL_coremotionsensor.h"
#include "../SDL_syssensor.h"
#include "../SDL_sensor_c.h"
#define STANDARD_GRAVITY 9.80665f
typedef struct
{
SDL_SensorType type;
SDL_SensorID instance_id;
} SDL_CoreMotionSensor;
static CMMotionManager *SDL_motion_manager;
static SDL_CoreMotionSensor *SDL_sensors;
static int SDL_sensors_count;
static int
SDL_COREMOTION_SensorInit(void)
{
int i, sensors_count = 0;
if (!SDL_motion_manager) {
SDL_motion_manager = [[CMMotionManager alloc] init];
}
if (SDL_motion_manager.accelerometerAvailable) {
++sensors_count;
}
if (SDL_motion_manager.gyroAvailable) {
++sensors_count;
}
if (sensors_count > 0) {
SDL_sensors = (SDL_CoreMotionSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors));
if (!SDL_sensors) {
return SDL_OutOfMemory();
}
i = 0;
if (SDL_motion_manager.accelerometerAvailable) {
SDL_sensors[i].type = SDL_SENSOR_ACCEL;
SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
++i;
}
if (SDL_motion_manager.gyroAvailable) {
SDL_sensors[i].type = SDL_SENSOR_GYRO;
SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
++i;
}
SDL_sensors_count = sensors_count;
}
return 0;
}
static int
SDL_COREMOTION_SensorGetCount(void)
{
return SDL_sensors_count;
}
static void
SDL_COREMOTION_SensorDetect(void)
{
}
static const char *
SDL_COREMOTION_SensorGetDeviceName(int device_index)
{
switch (SDL_sensors[device_index].type) {
case SDL_SENSOR_ACCEL:
return "Accelerometer";
case SDL_SENSOR_GYRO:
return "Gyro";
default:
return "Unknown";
}
}
static SDL_SensorType
SDL_COREMOTION_SensorGetDeviceType(int device_index)
{
return SDL_sensors[device_index].type;
}
static int
SDL_COREMOTION_SensorGetDeviceNonPortableType(int device_index)
{
return SDL_sensors[device_index].type;
}
static SDL_SensorID
SDL_COREMOTION_SensorGetDeviceInstanceID(int device_index)
{
return SDL_sensors[device_index].instance_id;
}
static int
SDL_COREMOTION_SensorOpen(SDL_Sensor *sensor, int device_index)
{
struct sensor_hwdata *hwdata;
hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
if (hwdata == NULL) {
return SDL_OutOfMemory();
}
sensor->hwdata = hwdata;
switch (sensor->type)
{
case SDL_SENSOR_ACCEL:
[SDL_motion_manager startAccelerometerUpdates];
break;
case SDL_SENSOR_GYRO:
[SDL_motion_manager startGyroUpdates];
break;
default:
break;
}
return 0;
}
static void
SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
{
switch (sensor->type)
{
case SDL_SENSOR_ACCEL:
{
CMAccelerometerData *accelerometerData = SDL_motion_manager.accelerometerData;
if (accelerometerData) {
CMAcceleration acceleration = accelerometerData.acceleration;
float data[3];
data[0] = acceleration.x * STANDARD_GRAVITY;
data[1] = acceleration.y * STANDARD_GRAVITY;
data[2] = acceleration.z * STANDARD_GRAVITY;
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
}
}
}
break;
case SDL_SENSOR_GYRO:
{
CMGyroData *gyroData = SDL_motion_manager.gyroData;
if (gyroData) {
CMRotationRate rotationRate = gyroData.rotationRate;
float data[3];
data[0] = rotationRate.x;
data[1] = rotationRate.y;
data[2] = rotationRate.z;
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
}
}
}
break;
default:
break;
}
}
static void
SDL_COREMOTION_SensorClose(SDL_Sensor *sensor)
{
if (sensor->hwdata) {
switch (sensor->type)
{
case SDL_SENSOR_ACCEL:
[SDL_motion_manager stopAccelerometerUpdates];
break;
case SDL_SENSOR_GYRO:
[SDL_motion_manager stopGyroUpdates];
break;
default:
break;
}
SDL_free(sensor->hwdata);
sensor->hwdata = NULL;
}
}
static void
SDL_COREMOTION_SensorQuit(void)
{
}
SDL_SensorDriver SDL_COREMOTION_SensorDriver =
{
SDL_COREMOTION_SensorInit,
SDL_COREMOTION_SensorGetCount,
SDL_COREMOTION_SensorDetect,
SDL_COREMOTION_SensorGetDeviceName,
SDL_COREMOTION_SensorGetDeviceType,
SDL_COREMOTION_SensorGetDeviceNonPortableType,
SDL_COREMOTION_SensorGetDeviceInstanceID,
SDL_COREMOTION_SensorOpen,
SDL_COREMOTION_SensorUpdate,
SDL_COREMOTION_SensorClose,
SDL_COREMOTION_SensorQuit,
};
#endif /* SDL_SENSOR_COREMOTION */
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -76,12 +76,12 @@ SDL_DUMMY_SensorOpen(SDL_Sensor *sensor, int device_index)
}
static void
SDL_DUMMY_SensorUpdate(SDL_Sensor* sensor)
SDL_DUMMY_SensorUpdate(SDL_Sensor *sensor)
{
}
static void
SDL_DUMMY_SensorClose(SDL_Sensor * sensor)
SDL_DUMMY_SensorClose(SDL_Sensor *sensor)
{
}