aboutsummaryrefslogtreecommitdiff
path: root/src/accelerometers.c
blob: 61118fcea04ad6978b0d63ed40688e45d231067f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * accelerometers.c
 *
 * Accelerometer stuff
 *
 * (c) 2008 Thomas White <taw27@srcf.ucam.org>
 *
 * This file is part of OpenMooCow - accelerometer moobox simulator
 *
 * OpenMooCow 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 3 of the License, or
 * (at your option) any later version.
 *
 * OpenMooCow 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 General Public License
 * along with OpenMooCow.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "types.h"
#include "audio.h"

struct input_event {
	struct timeval time;
	uint16_t type;
	uint16_t code;
	int32_t value;
};
#define EV_SYN (0x00)
#define EV_REL (0x02)
#define SYN_REPORT (0x00)
#define REL_X (0x00)
#define REL_Y (0x01)
#define REL_Z (0x02)

AccelHandle *accelerometer_open() {

	AccelHandle *accel;
	
	/* Initialise accelerometer data structure */
	accel = malloc(sizeof(AccelHandle));
	if ( accel == NULL ) return NULL;
	accel->x = 0;
	accel->y = 0;
	accel->z = 0;
	accel->lx = 0;
	accel->ly = 0;
	accel->lz = 0;
	accel->type = ACCEL_UNKNOWN;
	
	/* Determine accelerometer type */
	accel->fd = open("/dev/input/event3", O_RDONLY, O_NONBLOCK);
	if ( accel->fd != -1 ) {
		accel->type = ACCEL_FREERUNNER;
		printf("Neo Freerunner detected\n");
		return accel;
	}
	
	/* Try other types here */
	
	fprintf(stderr, "Couldn't determine accelerometer type\n");
	return accel;

}

void accelerometer_update_freerunner(AccelHandle *accel) {

	struct input_event ev;
	size_t rval;
	
	rval = read(accel->fd, &ev, sizeof(ev));
	if ( rval != sizeof(ev) ) {
		fprintf(stderr, "Couldn't read accelerometer data");
		return;
	}
	
	if ( ev.type == EV_REL ) {
		if ( ev.code == REL_X ) {
			accel->lx = ev.value;
		}
		if ( ev.code == REL_Y ) {
			accel->ly = ev.value;
		}
		if ( ev.code == REL_Z ) {
			accel->lz = ev.value;
		}
	}

	if ( ev.type == EV_SYN ) {
		if ( ev.code == SYN_REPORT ) {
			accel->x = accel->lx;
			accel->y = accel->ly;
			accel->z = accel->lz;
		}
	}

}

void accelerometer_update(AccelHandle *accel) {

	switch ( accel->type ) {
		case ACCEL_UNKNOWN : {
			return;
		}
		case ACCEL_FREERUNNER : {
			accelerometer_update_freerunner(accel);
			break;
		}
		/* Add other types here. You simply need to provide the "y"
		 * component of acceleration in milli-g in the relevant
		 * structure. */
	}

}

/* The accelerometer work thread */
static void *accel_work(void *data) {

	AccelHandle *accel;
	int *finished = data;
	int pos = 0;
	
	accel = accelerometer_open();
	audio_setup();
	
	while ( !(*finished) ) {
	
		accelerometer_update(accel);
		
		if ( (accel->y < -500) && (pos > -1000) ) {
			pos = -1000;
			audio_trigger_moo();
		} else if ( (accel->y > 500) && (pos < 1000) ) {
			pos = 1000;
			audio_trigger_moo();
		}
		
		usleep(25000);
		
	}
	
	audio_shutdown();
	
	return NULL;

}

GThread *accelerometer_start(int *finished) {
	return g_thread_create(accel_work, finished, TRUE, NULL);	
}