Improve Maemo accelerometer handling
[neverball] / share / tilt_maemo.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #define _ISOC99_SOURCE
16 #include <math.h>
17 #include <stdio.h>
18
19 #define M_PI       3.14159265358979323846
20 #define ACCEL_FILENAME "/sys/class/i2c-adapter/i2c-3/3-001d/coord"
21
22 static struct _accel {
23     float x;
24     float y;
25     float z;
26 } accel;
27
28 void tilt_init(void)
29 {
30 }
31
32 void tilt_free(void)
33 {
34 }
35
36 int tilt_stat(void)
37 {
38     FILE *fd;
39     int i, ax, ay, az;
40     static int has_read = 0;
41
42     fd = fopen(ACCEL_FILENAME, "r");
43     if (!fd) return 0;
44
45     i = fscanf(fd, "%i %i %i", &ax, &ay, &az);
46     fclose(fd);
47
48     if (i != 3) return 0;
49
50     /* Combine with previous reading */
51     if (has_read)
52     {
53         accel.x = ax * 0.2f + accel.x * 0.8f;
54         accel.y = ay * 0.2f + accel.y * 0.8f;
55         accel.z = az * 0.2f + accel.z * 0.8f;
56     }
57     else
58     {
59         accel.x = ax;
60         accel.y = ay;
61         accel.z = az;
62         has_read = 1;
63     }
64
65     return i == 3 ? 1 : 0;
66 }
67
68 int  tilt_get_button(int *b, int *s)
69 {
70     return 0;
71 }
72
73 float tilt_get_x(void)
74 {
75     return atan2f(accel.y, -accel.z) * 180.0f / M_PI;
76 }
77
78 float tilt_get_z(void)
79 {
80     return atan2f(-accel.x, -accel.z) * 180.0f / M_PI;
81 }
82