-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
sensor.go
325 lines (278 loc) · 9.27 KB
/
sensor.go
1
2
3
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
161
162
163
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package sdl
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,0,14))
#if defined(WARN_OUTDATED)
#pragma message("SDL_LockSensors is not supported before SDL 2.0.14")
#pragma message("SDL_UnlockSensors is not supported before SDL 2.0.14")
#endif
static void SDL_LockSensors(void)
{
// do nothing
}
static void SDL_UnlockSensors(void)
{
// do nothing
}
#endif
#if !(SDL_VERSION_ATLEAST(2,0,9))
typedef struct _SDL_Sensor SDL_Sensor;
typedef Sint32 SDL_SensorID;
typedef enum
{
SDL_SENSOR_INVALID = -1,
SDL_SENSOR_UNKNOWN,
SDL_SENSOR_ACCEL,
SDL_SENSOR_GYRO
} SDL_SensorType;
#if defined(WARN_OUTDATED)
#pragma message("SDL_NumSensors is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetDeviceName is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetDeviceType is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetDeviceNonPortableType is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetDeviceInstanceID is not supported before SDL 2.0.9")
#pragma message("SDL_SensorOpen is not supported before SDL 2.0.9")
#pragma message("SDL_SensorFromInstanceID is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetName is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetType is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetNonPortableType is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetInstanceID is not supported before SDL 2.0.9")
#pragma message("SDL_SensorGetData is not supported before SDL 2.0.9")
#pragma message("SDL_SensorClose is not supported before SDL 2.0.9")
#pragma message("SDL_SensorUpdate is not supported before SDL 2.0.9")
#endif
static inline int SDL_NumSensors()
{
return 0;
}
static const char * SDL_SensorGetDeviceName(int device_index)
{
return NULL;
}
static SDL_SensorType SDL_SensorGetDeviceType(int device_index)
{
return SDL_SENSOR_INVALID;
}
static int SDL_SensorGetDeviceNonPortableType(int device_index)
{
return -1;
}
static SDL_SensorID SDL_SensorGetDeviceInstanceID(int device_index)
{
return -1;
}
static SDL_Sensor * SDL_SensorOpen(int device_index)
{
return NULL;
}
static SDL_Sensor * SDL_SensorFromInstanceID(SDL_SensorID instance_id)
{
return NULL;
}
static const char * SDL_SensorGetName(SDL_Sensor *sensor)
{
return NULL;
}
static SDL_SensorType SDL_SensorGetType(SDL_Sensor *sensor)
{
return SDL_SENSOR_INVALID;
}
static int SDL_SensorGetNonPortableType(SDL_Sensor *sensor)
{
return -1;
}
static SDL_SensorID SDL_SensorGetInstanceID(SDL_Sensor *sensor)
{
return -1;
}
static int SDL_SensorGetData(SDL_Sensor *sensor, float *data, int num_values)
{
return -1;
}
static void SDL_SensorClose(SDL_Sensor *sensor)
{
}
static void SDL_SensorUpdate()
{
}
#endif
#if !(SDL_VERSION_ATLEAST(2,26,0))
#if defined(WARN_OUTDATED)
#pragma message("SDL_SensorGetDataWithTimestamp is not supported before SDL 2.26.0")
#endif
static inline int SDL_SensorGetDataWithTimestamp(SDL_Sensor *sensor, Uint64 *timestamp, float *data, int num_values)
{
return -1;
}
#endif
*/
import "C"
import "unsafe"
const (
STANDARD_GRAVITY = 9.80665
)
// The different sensors defined by SDL
//
// Additional sensors may be available, using platform dependent semantics.
//
// Here are the additional Android sensors:
// https://developer.android.com/reference/android/hardware/SensorEvent.html#values
const (
SENSOR_INVALID SensorType = C.SDL_SENSOR_INVALID // Returned for an invalid sensor
SENSOR_UNKNOWN SensorType = C.SDL_SENSOR_UNKNOWN // Unknown sensor type
SENSOR_ACCEL SensorType = C.SDL_SENSOR_ACCEL // Accelerometer
SENSOR_GYRO SensorType = C.SDL_SENSOR_GYRO // Gyroscope
)
type Sensor C.SDL_Sensor
type SensorID int32
type SensorType int
// LockSensors locks sensors for multi-threaded access to the sensor API
// (https://wiki.libsdl.org/SDL_LockSensors)
func LockSensors() {
C.SDL_LockSensors()
}
// UnlockSensors unlocks sensors for multi-threaded access to the sensor API
// (https://wiki.libsdl.org/SDL_UnlockSensors)
func UnlockSensors() {
C.SDL_UnlockSensors()
}
// NumSensors counts the number of sensors attached to the system right now
// (https://wiki.libsdl.org/SDL_NumSensors)
func NumSensors() int {
return int(C.SDL_NumSensors())
}
// SensorGetDeviceName gets the implementation dependent name of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor name, or empty string if deviceIndex is out of range.
// (https://wiki.libsdl.org/SDL_SensorGetDeviceName)
func SensorGetDeviceName(deviceIndex int) (name string) {
name = C.GoString(C.SDL_SensorGetDeviceName(C.int(deviceIndex)))
return
}
// SensorGetDeviceType gets the type of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor type, or SDL_SENSOR_INVALID if deviceIndex is out of range.
// (https://wiki.libsdl.org/SDL_SensorGetDeviceType)
func SensorGetDeviceType(deviceIndex int) (typ SensorType) {
typ = SensorType(C.SDL_SensorGetDeviceType(C.int(deviceIndex)))
return
}
// SensorGetDeviceNonPortableType gets the platform dependent type of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor platform dependent type, or -1 if deviceIndex is out of range.
// (https://wiki.libsdl.org/SDL_SensorGetDeviceNonPortableType)
func SensorGetDeviceNonPortableType(deviceIndex int) (typ int) {
typ = int(C.SDL_SensorGetDeviceNonPortableType(C.int(deviceIndex)))
return
}
// SensorGetDeviceInstanceID gets the instance ID of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor instance ID, or -1 if deviceIndex is out of range.
// (https://wiki.libsdl.org/SDL_SensorGetDeviceInstanceID)
func SensorGetDeviceInstanceID(deviceIndex int) (id SensorID) {
id = SensorID(C.SDL_SensorGetDeviceInstanceID(C.int(deviceIndex)))
return
}
// SensorOpen opens a sensor for use.
//
// The index passed as an argument refers to the N'th sensor on the system.
//
// Returns a sensor identifier, or nil if an error occurred.
// (https://wiki.libsdl.org/SDL_SensorOpen)
func SensorOpen(deviceIndex int) (sensor *Sensor) {
sensor = (*Sensor)(unsafe.Pointer(C.SDL_SensorOpen(C.int(deviceIndex))))
return
}
// SensorFromInstanceID returns the Sensor associated with an instance id.
// (https://wiki.libsdl.org/SDL_SensorFromInstanceID)
func SensorFromInstanceID(id SensorID) (sensor *Sensor) {
sensor = (*Sensor)(unsafe.Pointer(C.SDL_SensorFromInstanceID(C.SDL_SensorID(id))))
return
}
// GetName gets the implementation dependent name of a sensor.
//
// Returns the sensor name, or empty string if the sensor is nil.
// (https://wiki.libsdl.org/SDL_SensorGetName)
func (sensor *Sensor) GetName() (name string) {
name = C.GoString(C.SDL_SensorGetName((*C.SDL_Sensor)(sensor)))
return
}
// GetType gets the type of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor type, or SENSOR_INVALID if the sensor is nil.
// (https://wiki.libsdl.org/SDL_SensorGetType)
func (sensor *Sensor) GetType() (typ SensorType) {
typ = SensorType(C.SDL_SensorGetType((*C.SDL_Sensor)(sensor)))
return
}
// GetNonPortableType gets the platform dependent type of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor platform dependent type, or -1 if the sensor is nil.
// (https://wiki.libsdl.org/SDL_SensorGetNonPortableType)
func (sensor *Sensor) GetNonPortableType() (typ int) {
typ = int(C.SDL_SensorGetNonPortableType((*C.SDL_Sensor)(sensor)))
return
}
// GetInstanceID gets the instance ID of a sensor.
//
// This can be called before any sensors are opened.
//
// Returns the sensor instance ID, or -1 if the sensor is nil.
// (https://wiki.libsdl.org/SDL_SensorGetInstanceID)
func (sensor *Sensor) GetInstanceID() (id SensorID) {
id = SensorID(C.SDL_SensorGetInstanceID((*C.SDL_Sensor)(sensor)))
return
}
// GetData gets the current state of an opened sensor.
//
// The number of values and interpretation of the data is sensor dependent.
// (https://wiki.libsdl.org/SDL_SensorGetData)
func (sensor *Sensor) GetData(data []float32) (err error) {
if data == nil {
return nil
}
_data := (*C.float)(unsafe.Pointer(&data[0]))
_numValues := C.int(len(data))
err = errorFromInt(int(C.SDL_SensorGetData((*C.SDL_Sensor)(sensor), _data, _numValues)))
return
}
// GetDataWithTimestamp gets the current state of an opened sensor with the timestamp of the last update.
//
// The number of values and interpretation of the data is sensor dependent.
// (https://wiki.libsdl.org/SDL_SensorGetDataWithTimestamp)
func (sensor *Sensor) GetDataWithTimestamp(timestamp *uint64, data []float32) (err error) {
if data == nil {
return nil
}
_data := (*C.float)(unsafe.Pointer(&data[0]))
_numValues := C.int(len(data))
_timestamp := (*C.Uint64)(timestamp)
err = errorFromInt(int(C.SDL_SensorGetDataWithTimestamp((*C.SDL_Sensor)(sensor), _timestamp, _data, _numValues)))
return
}
// Close closes a sensor previously opened with SensorOpen()
// (https://wiki.libsdl.org/SDL_SensorClose)
func (sensor *Sensor) Close() {
C.SDL_SensorClose((*C.SDL_Sensor)(sensor))
}
// SensorUpdate updates the current state of the open sensors.
//
// This is called automatically by the event loop if sensor events are enabled.
//
// This needs to be called from the thread that initialized the sensor subsystem.
// (https://wiki.libsdl.org/SDL_SensorUpdate)
func SensorUpdate() {
C.SDL_SensorUpdate()
}