Skip to content

Latest commit

 

History

History
234 lines (205 loc) · 6.23 KB

SDL_coremotionsensor.m

File metadata and controls

234 lines (205 loc) · 6.23 KB
 
Aug 22, 2018
Aug 22, 2018
1
2
/*
Simple DirectMedia Layer
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
Aug 22, 2018
Aug 22, 2018
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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"
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];
Aug 23, 2018
Aug 23, 2018
161
162
163
data[0] = acceleration.x * SDL_STANDARD_GRAVITY;
data[1] = acceleration.y * SDL_STANDARD_GRAVITY;
data[2] = acceleration.z * SDL_STANDARD_GRAVITY;
Aug 22, 2018
Aug 22, 2018
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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: */