Support N900 accelerometer
authorAlberto Mardegan <mardy@users.sourceforge.net>
Tue, 5 Jul 2011 14:11:33 +0000 (17:11 +0300)
committerAlberto Mardegan <mardy@users.sourceforge.net>
Tue, 12 Jul 2011 12:25:45 +0000 (15:25 +0300)
Makefile
share/tilt_maemo.c [new file with mode: 0644]

index f2cda95..3b88369 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -320,9 +320,13 @@ else
 ifeq ($(ENABLE_TILT),loop)
 BALL_OBJS += share/tilt_loop.o
 else
+ifeq ($(PLATFORM),maemo)
+BALL_OBJS += share/tilt_maemo.o
+else
 BALL_OBJS += share/tilt_null.o
 endif
 endif
+endif
 
 ifeq ($(PLATFORM),mingw)
 BALL_OBJS += neverball.ico.o
diff --git a/share/tilt_maemo.c b/share/tilt_maemo.c
new file mode 100644 (file)
index 0000000..efac68a
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2003 Robert Kooima
+ *
+ * NEVERBALL is  free software; you can redistribute  it and/or modify
+ * it under the  terms of the GNU General  Public License as published
+ * by the Free  Software Foundation; either version 2  of the License,
+ * or (at your option) any later version.
+ *
+ * This program 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.
+ */
+
+#define _ISOC99_SOURCE
+#include <math.h>
+#include <stdio.h>
+
+#define M_PI       3.14159265358979323846
+#define ACCEL_FILENAME "/sys/class/i2c-adapter/i2c-3/3-001d/coord"
+
+static struct _accel {
+    float x;
+    float y;
+    float z;
+} accel;
+
+void tilt_init(void)
+{
+}
+
+void tilt_free(void)
+{
+}
+
+int tilt_stat(void)
+{
+    FILE *fd;
+    int i, ax, ay, az;
+    static int has_read = 0;
+
+    fd = fopen(ACCEL_FILENAME, "r");
+    if (!fd) return 0;
+
+    i = fscanf(fd, "%i %i %i", &ax, &ay, &az);
+    fclose(fd);
+
+    if (i != 3) return 0;
+
+    /* Combine with previous reading */
+    if (has_read)
+    {
+        accel.x = ax * 0.4f + accel.x * 0.6f;
+        accel.y = ay * 0.4f + accel.y * 0.6f;
+    }
+    else
+        has_read = 1;
+
+    return i == 3 ? 1 : 0;
+}
+
+int  tilt_get_button(int *b, int *s)
+{
+    return 0;
+}
+
+float tilt_get_x(void)
+{
+    return asinf(accel.y / 1000.0f) * 180.0f / M_PI;
+}
+
+float tilt_get_z(void)
+{
+    return asinf(-accel.x / 1000.0f) * 180.0f / M_PI;
+}