root/branches/dev/libcwiid/interface.c

Revision 113, 4.4 kB (checked in by dsmith, 2 years ago)

added cwiid_get_acc_cal

Line 
1 /* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org>
2  *
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  *  ChangeLog:
18  *  2007-05-14 L. Donnie Smith <cwiid@abstrakraft.org>
19  *  * added timestamp to cwiid_get_mesg
20  *  * added cwiid_get_acc_cal
21  *
22  *  2007-04-24 L. Donnie Smith <cwiid@abstrakraft.org>
23  *  * created for API overhaul
24  */
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include "cwiid_internal.h"
33
34 int cwiid_get_id(struct wiimote *wiimote)
35 {
36         return wiimote->id;
37 }
38
39 int cwiid_set_data(struct wiimote *wiimote, const void *data)
40 {
41         wiimote->data = data;
42         return 0;
43 }
44
45 const void *cwiid_get_data(struct wiimote *wiimote)
46 {
47         return wiimote->data;
48 }
49
50 int cwiid_enable(struct wiimote *wiimote, int flags)
51 {
52         if ((flags & CWIID_FLAG_NONBLOCK) &&
53           !(wiimote->flags & CWIID_FLAG_NONBLOCK)) {
54                 if (fcntl(wiimote->mesg_pipe[0], F_SETFL, O_NONBLOCK)) {
55                         cwiid_err(wiimote, "File control error (mesg pipe)");
56                         return -1;
57                 }
58         }
59         wiimote->flags |= flags;
60         return 0;
61 }
62
63 int cwiid_disable(struct wiimote *wiimote, int flags)
64 {
65         if ((flags & CWIID_FLAG_NONBLOCK) &&
66           (wiimote->flags & CWIID_FLAG_NONBLOCK)) {
67                 if (fcntl(wiimote->mesg_pipe[0], F_SETFL, 0)) {
68                         cwiid_err(wiimote, "File control error (mesg pipe)");
69                         return -1;
70                 }
71         }
72         wiimote->flags &= ~flags;
73         return 0;
74 }
75
76 int cwiid_set_mesg_callback(struct wiimote *wiimote,
77                             cwiid_mesg_callback_t *callback)
78 {
79         if (wiimote->mesg_callback) {
80                 if (cancel_mesg_callback(wiimote)) {
81                         /* prints it's own errors */
82                         return -1;
83                 }
84         }
85
86         wiimote->mesg_callback = callback;
87
88         if (wiimote->mesg_callback) {
89                 if (pthread_create(&wiimote->mesg_callback_thread, NULL,
90                                   (void *(*)(void *))&mesg_callback_thread, wiimote)) {
91                         cwiid_err(wiimote, "Thread creation error (callback thread)");
92                         return -1;
93                 }
94         }
95
96         return 0;
97 }
98
99 int cwiid_get_mesg(struct wiimote *wiimote, int *mesg_count,
100                    union cwiid_mesg *mesg[], struct timespec *timestamp)
101 {
102         struct mesg_array ma;
103
104         if (read_mesg_array(wiimote->mesg_pipe[0], &ma)) {
105                 if (errno == EAGAIN) {
106                         return -1;
107                 }
108                 else {
109                         cwiid_err(wiimote, "Pipe read error (mesg_pipe)");
110                         return -1;
111                 }
112         }
113
114         *mesg_count = ma.count;
115         *timestamp = ma.timestamp;
116
117         if ((*mesg = malloc(ma.count * sizeof ma.array[0])) == NULL) {
118                 cwiid_err(wiimote, "Memory allocation error (mesg array)");
119                 return -1;
120         }
121
122         memcpy(*mesg, &ma.array, ma.count * sizeof (*mesg)[0]);
123
124         return 0;
125 }
126
127 int cwiid_get_state(struct wiimote *wiimote, struct cwiid_state *state)
128 {
129         if (pthread_mutex_lock(&wiimote->state_mutex)) {
130                 cwiid_err(wiimote, "Mutex lock error (state mutex)");
131                 return -1;
132         }
133
134         memcpy(state, &wiimote->state, sizeof *state);
135
136         if (pthread_mutex_unlock(&wiimote->state_mutex)) {
137                 cwiid_err(wiimote, "Mutex unlock error (state mutex) - "
138                                    "deadlock warning");
139                 return -1;
140         }
141
142         return 0;
143 }
144
145 int cwiid_get_acc_cal(struct wiimote *wiimote, enum cwiid_ext_type ext_type,
146                       struct acc_cal *acc_cal)
147 {
148         uint8_t flags;
149         uint32_t offset;
150         unsigned char buf[7];
151         char *err_str;
152
153         switch (ext_type) {
154         case CWIID_EXT_NONE:
155                 flags = CWIID_RW_EEPROM;
156                 offset = 0x16;
157                 err_str = "";
158                 break;
159         case CWIID_EXT_NUNCHUK:
160                 flags = CWIID_RW_REG | CWIID_RW_DECODE;
161                 offset = 0xA40020;
162                 err_str = "nunchuk ";
163                 break;
164         default:
165                 cwiid_err(wiimote, "Unsupported calibration request");
166                 return -1;
167         }
168         if (cwiid_read(wiimote, flags, offset, 7, buf)) {
169                 cwiid_err(wiimote, "Read error (%scal)", err_str);
170                 return -1;
171         }
172
173         acc_cal->zero[CWIID_X] = buf[0];
174         acc_cal->zero[CWIID_Y] = buf[1];
175         acc_cal->zero[CWIID_Z] = buf[2];
176         acc_cal->one[CWIID_X]  = buf[4];
177         acc_cal->one[CWIID_Y]  = buf[5];
178         acc_cal->one[CWIID_Z]  = buf[6];
179
180         return 0;
181 }
Note: See TracBrowser for help on using the browser.