Support N900 accelerometer
[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.4f + accel.x * 0.6f;
54         accel.y = ay * 0.4f + accel.y * 0.6f;
55     }
56     else
57         has_read = 1;
58
59     return i == 3 ? 1 : 0;
60 }
61
62 int  tilt_get_button(int *b, int *s)
63 {
64     return 0;
65 }
66
67 float tilt_get_x(void)
68 {
69     return asinf(accel.y / 1000.0f) * 180.0f / M_PI;
70 }
71
72 float tilt_get_z(void)
73 {
74     return asinf(-accel.x / 1000.0f) * 180.0f / M_PI;
75 }