Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Accelerometer abstraction; let platform plugin provide accelerometer

Make sure that no module links against SDL for joystick access.
  • Loading branch information...
commit 5f3e70cc8e62280f22f7d17718b4c3dc577cba30 1 parent 86ef383
@thp authored
View
59 accelerometer/accelerometer.c
@@ -0,0 +1,59 @@
+/**
+ * apkenv Accelerometer
+ * Copyright (c) 2014, Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+#include "accelerometer.h"
+
+static struct Accelerometer *
+g_accelerometer = 0;
+
+void
+apkenv_accelerometer_register(struct Accelerometer *accelerometer)
+{
+ g_accelerometer = accelerometer;
+}
+
+int
+apkenv_accelerometer_init()
+{
+ if (g_accelerometer) {
+ return g_accelerometer->init(g_accelerometer);
+ }
+
+ return 0;
+}
+
+int
+apkenv_accelerometer_get(float *x, float *y, float *z)
+{
+ if (g_accelerometer) {
+ return g_accelerometer->get(g_accelerometer, x, y, z);
+ }
+
+ return 0;
+}
View
46 accelerometer/accelerometer.h
@@ -0,0 +1,46 @@
+/**
+ * apkenv Accelerometer
+ * Copyright (c) 2014, Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+#ifndef APKENV_ACCELEROMETER_H
+#define APKENV_ACCELEROMETER_H
+
+struct Accelerometer {
+ // Initialize/open the accelerometer device
+ int (*init)(struct Accelerometer *accelerometer);
+
+ // Get the current accelerometer values
+ int (*get)(struct Accelerometer *accelerometer, float *x, float *y, float *z);
+};
+
+void apkenv_accelerometer_register(struct Accelerometer *accelerometer);
+
+int apkenv_accelerometer_init();
+int apkenv_accelerometer_get(float *x, float *y, float *z);
+
+#endif /* APKENV_ACCELEROMETER_H */
View
1  makefile
@@ -14,6 +14,7 @@ SOURCES += $(wildcard jni/*.c)
SOURCES += $(wildcard imagelib/*.c)
SOURCES += $(wildcard debug/*.c)
SOURCES += $(wildcard debug/wrappers/*.c)
+SOURCES += $(wildcard accelerometer/*.c)
# Platform-specific targets and configuration
PLATFORM_INSTALL_TARGETS :=
View
17 modules/trg2.c
@@ -35,8 +35,8 @@
**/
#include "common.h"
+#include "../accelerometer/accelerometer.h"
-#include <SDL/SDL.h>
typedef void (*trg2_init_t)(JNIEnv *env, jobject obj) SOFTFP;
typedef void (*trg2_resize_t)(JNIEnv *env, jobject obj, jint width, jint height) SOFTFP;
@@ -51,7 +51,6 @@ struct SupportModulePriv {
trg2_touch_t native_touch;
trg2_render_t native_render;
trg2_accelerometer_t native_accelerometer;
- SDL_Joystick *joystick;
};
static struct SupportModulePriv trg2_priv;
@@ -75,7 +74,7 @@ trg2_try_init(struct SupportModule *self)
static void
trg2_init(struct SupportModule *self, int width, int height, const char *home)
{
- self->priv->joystick = SDL_JoystickOpen(0);
+ apkenv_accelerometer_init();
self->priv->JNI_OnLoad(VM_M, NULL);
self->priv->native_init(ENV_M, GLOBAL_M);
self->priv->native_resize(ENV_M, GLOBAL_M, width, height);
@@ -92,18 +91,12 @@ trg2_key_input(struct SupportModule *self, int event, int keycode, int unicode)
{
}
-#define CONVERT_ACCELEROMETER(x) ((float)(x) / 32768. * 9.81 * 2)
-#define GET_AXIS(j, x) CONVERT_ACCELEROMETER(SDL_JoystickGetAxis(j, x))
-
static void
trg2_update(struct SupportModule *self)
{
- if (self->priv->native_accelerometer != NULL) {
- SDL_JoystickUpdate();
- self->priv->native_accelerometer(ENV_M, GLOBAL_M,
- GET_AXIS(self->priv->joystick, 0),
- GET_AXIS(self->priv->joystick, 1),
- GET_AXIS(self->priv->joystick, 2));
+ float x, y, z;
+ if (apkenv_accelerometer_get(&x, &y, &z)) {
+ self->priv->native_accelerometer(ENV_M, GLOBAL_M, x, y, z);
}
self->priv->native_render(ENV_M, GLOBAL_M);
View
86 platform/common/sdl_accelerometer_impl.h
@@ -0,0 +1,86 @@
+/**
+ * apkenv Accelerometer
+ * Copyright (c) 2014, Thomas Perl <m@thp.io>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ **/
+
+#include <SDL/SDL.h>
+
+#include "../../accelerometer/accelerometer.h"
+
+struct SDLAccelerometer {
+ // Must have the same structure as struct Accelerometer from accelerometer.h
+ int (*init)(struct SDLAccelerometer *accelerometer);
+ int (*get)(struct SDLAccelerometer *Accelerometer, float *x, float *y, float *z);
+
+ // Additional fields can be added after struct Accelerometer here
+ SDL_Joystick *joystick;
+};
+
+static int
+sdl_accelerometer_init(struct SDLAccelerometer *accelerometer)
+{
+ accelerometer->joystick = SDL_JoystickOpen(0);
+
+ return (accelerometer->joystick != 0);
+}
+
+#define CONVERT_ACCELEROMETER(x) ((float)(x) / 32768. * 9.81 * 2)
+#define GET_AXIS(j, x) CONVERT_ACCELEROMETER(SDL_JoystickGetAxis(j, x))
+
+static int
+sdl_accelerometer_get(struct SDLAccelerometer *accelerometer, float *x, float *y, float *z)
+{
+ if (!accelerometer->joystick) {
+ return 0;
+ }
+
+ SDL_JoystickUpdate();
+
+ if (x) {
+ *x = GET_AXIS(accelerometer->joystick, 0);
+ }
+
+ if (y) {
+ *y = GET_AXIS(accelerometer->joystick, 1);
+ }
+
+ if (z) {
+ *z = GET_AXIS(accelerometer->joystick, 2);
+ }
+
+ return 1;
+}
+
+static struct SDLAccelerometer
+g_sdl_accelerometer = {
+ sdl_accelerometer_init,
+ sdl_accelerometer_get,
+ NULL,
+};
+
+static struct Accelerometer *
+sdl_accelerometer = (struct Accelerometer *)(&g_sdl_accelerometer);
View
62 platform/fremantle.c
@@ -29,9 +29,11 @@
#include "../apkenv.h"
+#include "../accelerometer/accelerometer.h"
#include <SDL/SDL.h>
#include <SDL/SDL_gles.h>
+#include <stdio.h>
struct PlatformPriv {
SDL_Surface *screen;
@@ -40,6 +42,63 @@ struct PlatformPriv {
static struct PlatformPriv priv;
+struct N900Accelerometer {
+ // Must have the same structure as struct Accelerometer from accelerometer.h
+ int (*init)(struct N900Accelerometer *accelerometer);
+ int (*get)(struct N900Accelerometer *accelerometer, float *x, float *y, float *z);
+};
+
+static int
+n900_accelerometer_init(struct N900Accelerometer *accelerometer)
+{
+ return 1;
+}
+
+static int
+n900_accelerometer_get(struct N900Accelerometer *accelerometer, float *x, float *y, float *z)
+{
+ FILE *fp = fopen("/sys/class/i2c-adapter/i2c-3/3-001d/coord", "r");
+ int xx = 0, yy = 0, zz = 0;
+ int result = 0;
+
+ if (!fp) {
+ return 0;
+ }
+
+ if (fscanf(fp, "%i %i %i", &xx, &yy, &zz) == 3) {
+ // X and Y are swapped, and X is negative; compare:
+ // http://wiki.maemo.org/N900_accelerometer
+ // http://developer.android.com/reference/android/hardware/SensorEvent.html
+
+ if (x) {
+ *x = 2 * 0.001 * yy;
+ }
+
+ if (y) {
+ *y = 2 * 0.001 * -xx;
+ }
+
+ if (z) {
+ *z = 2 * 0.001 * zz;
+ }
+
+ result = 1;
+ }
+
+ fclose(fp);
+ return result;
+}
+
+static struct N900Accelerometer
+g_n900_accelerometer = {
+ n900_accelerometer_init,
+ n900_accelerometer_get,
+};
+
+static struct Accelerometer *
+n900_accelerometer = (struct Accelerometer *)(&g_n900_accelerometer);
+
+
static int
fremantle_init(int gles_version)
{
@@ -57,6 +116,9 @@ fremantle_init(int gles_version)
}
SDL_ShowCursor(0);
+
+ apkenv_accelerometer_register(n900_accelerometer);
+
return 1;
}
View
5 platform/harmattan.c
@@ -36,6 +36,8 @@
#include <X11/Xlib.h>
#include <X11/Xatom.h>
+#include "common/sdl_accelerometer_impl.h"
+
struct PlatformPriv {
SDL_Surface *screen;
};
@@ -75,6 +77,9 @@ harmattan_init(int gles_version)
PropModeReplace, (unsigned char*)region, 4);
SDL_ShowCursor(0);
+
+ apkenv_accelerometer_register(sdl_accelerometer);
+
return 1;
}
View
6 platform/pandora.c
@@ -37,6 +37,8 @@
#include <SDL/SDL_syswm.h>
#include <SDL/SDL_video.h>
+#include "common/sdl_accelerometer_impl.h"
+
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
@@ -228,6 +230,10 @@ pandora_init(int gles_version)
G_Data = data;
SDL_ShowCursor(0);
+
+ // XXX: Does Pandora expose the accelerometer via SDL?
+ apkenv_accelerometer_register(sdl_accelerometer);
+
return 1;
}
Please sign in to comment.
Something went wrong with that request. Please try again.