summaryrefslogtreecommitdiff
path: root/sdl2
diff options
context:
space:
mode:
Diffstat (limited to 'sdl2')
-rw-r--r--sdl2/bindings.scm62
-rw-r--r--sdl2/input/sensor.scm127
2 files changed, 189 insertions, 0 deletions
diff --git a/sdl2/bindings.scm b/sdl2/bindings.scm
index 480cf71..02988ad 100644
--- a/sdl2/bindings.scm
+++ b/sdl2/bindings.scm
@@ -1065,6 +1065,68 @@ RETURN-TYPE and accept arguments of ARG-TYPES."
;;;
+;;; Sensors
+;;;
+
+(define-public SDL_SENSOR_INVALID -1)
+(define-public SDL_SENSOR_UNKNOWN 0)
+(define-public SDL_SENSOR_ACCEL 1)
+(define-public SDL_SENSOR_GYRO 2)
+(define-public SDL_SENSOR_ACCEL_L 3)
+(define-public SDL_SENSOR_GYRO_L 4)
+(define-public SDL_SENSOR_ACCEL_R 5)
+(define-public SDL_SENSOR_GYRO_R 6)
+
+(define-public SDL_STANDARD_GRAVITY 9.80665)
+
+(define-foreign sdl-num-sensors
+ int "SDL_NumSensors" '())
+
+(define-foreign sdl-sensor-get-device-name
+ '* "SDL_SensorGetDeviceName" (list int))
+
+(define-foreign sdl-sensor-get-device-type
+ int "SDL_SensorGetDeviceType" (list int))
+
+(define-foreign sdl-sensor-get-device-non-portable-type
+ int "SDL_SensorGetDeviceNonPortableType" (list int))
+
+(define-foreign sdl-sensor-get-device-instance-id
+ int "SDL_SensorGetDeviceInstanceID" (list int))
+
+(define-foreign sdl-sensor-open
+ '* "SDL_SensorOpen" (list int))
+
+(define-foreign sdl-sensor-from-instance-id
+ '* "SDL_SensorFromInstanceID" (list int))
+
+(define-foreign sdl-sensor-get-name
+ '* "SDL_SensorGetName" '(*))
+
+(define-foreign sdl-sensor-get-type
+ int "SDL_SensorGetType" '(*))
+
+(define-foreign sdl-sensor-get-non-portable-type
+ int "SDL_SensorGetNonPortableType" '(*))
+
+(define-foreign sdl-sensor-get-instance-id
+ int "SDL_SensorGetInstanceID" '(*))
+
+(define-foreign sdl-sensor-get-data
+ int "SDL_SensorGetData" (list '* '* int))
+
+;; Only available in very recent versions of SDL2.
+;; (define-foreign sdl-sensor-get-data-with-timestamp
+;; int "SDL_SensorGetDataWithTimestamp" (list '* uint64 '* int))
+
+(define-foreign sdl-sensor-close
+ void "SDL_SensorClose" '(*))
+
+(define-foreign sdl-sensor-update
+ void "SDL_SensorUpdate" '())
+
+
+;;;
;;; Timer
;;;
diff --git a/sdl2/input/sensor.scm b/sdl2/input/sensor.scm
new file mode 100644
index 0000000..3b1a1be
--- /dev/null
+++ b/sdl2/input/sensor.scm
@@ -0,0 +1,127 @@
+;;; guile-sdl2 --- FFI bindings for SDL2
+;;; Copyright © 2022 David Thompson <davet@gnu.org>
+;;;
+;;; This file is part of guile-sdl2.
+;;;
+;;; Guile-sdl2 is free software; you can redistribute it and/or modify
+;;; it under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation; either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Guile-sdl2 is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with guile-sdl2. If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; Accelerometer/gyroscope input.
+;;
+;;; Code:
+
+(define-module (sdl2 input sensor)
+ #:use-module (sdl2)
+ #:use-module ((sdl2 bindings) #:prefix ffi:)
+ #:use-module (system foreign)
+ #:export (instance-id->sensor
+ num-sensors
+ open-sensor
+ sensor-close
+ sensor-device-instance-id
+ sensor-device-name
+ sensor-device-non-portable-type
+ sensor-device-type
+ sensor-instance-id
+ sensor-name
+ sensor-non-portable-type
+ sensor-type
+ update-sensors))
+
+(define-wrapped-pointer-type <sensor>
+ sensor?
+ wrap-sensor unwrap-sensor
+ (lambda (sensor port)
+ (format port "#<sensor ~a>"
+ (sensor-name sensor))))
+
+(define (num-sensors)
+ (ffi:sdl-num-sensors))
+
+(define (sensor-device-name device-index)
+ "Return the name of the sensor at DEVICE-INDEX, or #f if there is no
+sensor with that index."
+ (let ((ptr (ffi:sdl-sensor-get-device-name device-index)))
+ (if (null-pointer? ptr)
+ #f
+ (pointer->string ptr))))
+
+(define (type-enum->symbol type)
+ (cond
+ ((= type ffi:SDL_SENSOR_INVALID) #f)
+ ((= type ffi:SDL_SENSOR_UNKNOWN) 'unknown)
+ ((= type ffi:SDL_SENSOR_ACCEL) 'accelerometer)
+ ((= type ffi:SDL_SENSOR_GYRO) 'gyroscope)
+ ((= type ffi:SDL_SENSOR_ACCEL_L) 'accelerometer-left)
+ ((= type ffi:SDL_SENSOR_GYRO_L) 'gyroscope-left)
+ ((= type ffi:SDL_SENSOR_ACCEL_R) 'accelerometer-right)
+ ((= type ffi:SDL_SENSOR_GYRO_R) 'gyroscope-right)))
+
+(define (sensor-device-type device-index)
+ "Return the type of the sensor at DEVICE-INDEX, or #f if there is no
+sensor with that index."
+ (type-enum->symbol (ffi:sdl-sensor-get-device-type device-index)))
+
+(define (sensor-device-non-portable-type device-index)
+ "Return the non-portable device type of the sensor at DEVICE-INDEX, or
+#f if there is no sensor with that index."
+ (let ((type (ffi:sdl-sensor-get-device-non-portable-type device-index)))
+ (if (= type -1) #f type)))
+
+(define (sensor-device-instance-id device-index)
+ "Return the instance ID of the sensor at DEVICE-INDEX, or #f if there
+is no sensor with that index."
+ (let ((id (ffi:sdl-sensor-get-device-instance-id device-index)))
+ (if (= id -1) #f id)))
+
+(define (open-sensor device-index)
+ "Return a sensor object for the sensor at DEVICE-INDEX."
+ (let ((ptr (ffi:sdl-sensor-open device-index)))
+ (if (null-pointer? ptr)
+ (sdl-error "open-sensor" "failed to open sensor")
+ (wrap-sensor ptr))))
+
+(define (instance-id->sensor instance-id)
+ "Return the sensor object associated with INSTANCE-ID."
+ (let ((ptr (ffi:sdl-sensor-from-instance-id instance-id)))
+ (if (null-pointer? ptr)
+ (sdl-error "instance-id->sensor" "failed to get sensor")
+ (wrap-sensor ptr))))
+
+(define (sensor-name sensor)
+ "Return the name of SENSOR."
+ (pointer->string (ffi:sdl-sensor-get-name (unwrap-sensor sensor))))
+
+(define (sensor-type sensor)
+ "Return the type of SENSOR."
+ (type-enum->symbol (ffi:sdl-sensor-get-type (unwrap-sensor sensor))))
+
+(define (sensor-non-portable-type sensor)
+ "Return the type of SENSOR."
+ (let ((type (ffi:sdl-sensor-get-non-portable-type (unwrap-sensor sensor))))
+ (if (= type -1) #f type)))
+
+(define (sensor-instance-id sensor)
+ "Return the instance ID of SENSOR."
+ (ffi:sdl-sensor-get-instance-id (unwrap-sensor sensor)))
+
+(define (sensor-close sensor)
+ "Close SENSOR."
+ (ffi:sdl-sensor-close (unwrap-sensor sensor)))
+
+(define (update-sensors)
+ "Update the current state of the open sensors."
+ (ffi:sdl-sensor-update))