root/libcwiid/interface.c

Revision 2100f14c612471084434b364501e3818c7f4144e, 5.7 kB (checked in by L. Donnie Smith <donnie.smith@…>, 3 years ago)

Various administrative updates

change email addresses
remove individual file changelogs (that's what version control is for)

  • Property mode set to 100644
Line 
1/* Copyright (C) 2007 L. Donnie Smith <donnie.smith@gatech.edu>
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 */
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22#include <pthread.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include "cwiid_internal.h"
26
27int cwiid_get_id(cwiid_wiimote_t *wiimote)
28{
29        return wiimote->id;
30}
31
32int cwiid_set_data(cwiid_wiimote_t *wiimote, const void *data)
33{
34        wiimote->data = data;
35        return 0;
36}
37
38const void *cwiid_get_data(cwiid_wiimote_t *wiimote)
39{
40        return wiimote->data;
41}
42
43int cwiid_enable(cwiid_wiimote_t *wiimote, int flags)
44{
45        unsigned char data;
46
47        if ((flags & CWIID_FLAG_NONBLOCK) &&
48          !(wiimote->flags & CWIID_FLAG_NONBLOCK)) {
49                if (fcntl(wiimote->mesg_pipe[0], F_SETFL, O_NONBLOCK)) {
50                        cwiid_err(wiimote, "File control error (mesg pipe)");
51                        return -1;
52                }
53        }
54        if (flags & CWIID_FLAG_MOTIONPLUS) {
55                data = 0x04;
56                cwiid_write(wiimote, CWIID_RW_REG, 0xA600FE, 1, &data);
57                cwiid_request_status(wiimote);
58        }
59        wiimote->flags |= flags;
60        return 0;
61}
62
63int cwiid_disable(cwiid_wiimote_t *wiimote, int flags)
64{
65        unsigned char data;
66
67        if ((flags & CWIID_FLAG_NONBLOCK) &&
68          (wiimote->flags & CWIID_FLAG_NONBLOCK)) {
69                if (fcntl(wiimote->mesg_pipe[0], F_SETFL, 0)) {
70                        cwiid_err(wiimote, "File control error (mesg pipe)");
71                        return -1;
72                }
73        }
74        if (flags & CWIID_FLAG_MOTIONPLUS) {
75                data = 0x55;
76                cwiid_write(wiimote, CWIID_RW_REG, 0xA400F0, 1, &data);
77                data = 0x00;
78                cwiid_write(wiimote, CWIID_RW_REG, 0xA400FB, 1, &data);
79                cwiid_request_status(wiimote);
80        }
81        wiimote->flags &= ~flags;
82        return 0;
83}
84
85int cwiid_set_mesg_callback(cwiid_wiimote_t *wiimote,
86                            cwiid_mesg_callback_t *callback)
87{
88        if (wiimote->mesg_callback) {
89                if (cancel_mesg_callback(wiimote)) {
90                        /* prints it's own errors */
91                        return -1;
92                }
93        }
94
95        wiimote->mesg_callback = callback;
96
97        if (wiimote->mesg_callback) {
98                if (pthread_create(&wiimote->mesg_callback_thread, NULL,
99                                  (void *(*)(void *))&mesg_callback_thread, wiimote)) {
100                        cwiid_err(wiimote, "Thread creation error (callback thread)");
101                        return -1;
102                }
103        }
104
105        return 0;
106}
107
108int cwiid_get_mesg(cwiid_wiimote_t *wiimote, int *mesg_count,
109                   union cwiid_mesg *mesg[], struct timespec *timestamp)
110{
111        struct mesg_array ma;
112
113        if (read_mesg_array(wiimote->mesg_pipe[0], &ma)) {
114                if (errno == EAGAIN) {
115                        return -1;
116                }
117                else {
118                        cwiid_err(wiimote, "Pipe read error (mesg_pipe)");
119                        return -1;
120                }
121        }
122
123        *mesg_count = ma.count;
124        *timestamp = ma.timestamp;
125
126        if ((*mesg = malloc(ma.count * sizeof ma.array[0])) == NULL) {
127                cwiid_err(wiimote, "Memory allocation error (mesg array)");
128                return -1;
129        }
130
131        memcpy(*mesg, &ma.array, ma.count * sizeof (*mesg)[0]);
132
133        return 0;
134}
135
136int cwiid_get_state(cwiid_wiimote_t *wiimote, struct cwiid_state *state)
137{
138        if (pthread_mutex_lock(&wiimote->state_mutex)) {
139                cwiid_err(wiimote, "Mutex lock error (state mutex)");
140                return -1;
141        }
142
143        memcpy(state, &wiimote->state, sizeof *state);
144
145        if (pthread_mutex_unlock(&wiimote->state_mutex)) {
146                cwiid_err(wiimote, "Mutex unlock error (state mutex) - "
147                                   "deadlock warning");
148                return -1;
149        }
150
151        return 0;
152}
153
154int cwiid_get_acc_cal(cwiid_wiimote_t *wiimote, enum cwiid_ext_type ext_type,
155                      struct acc_cal *acc_cal)
156{
157        uint8_t flags;
158        uint32_t offset;
159        unsigned char buf[7];
160        char *err_str;
161
162        switch (ext_type) {
163        case CWIID_EXT_NONE:
164                flags = CWIID_RW_EEPROM;
165                offset = 0x16;
166                err_str = "";
167                break;
168        case CWIID_EXT_NUNCHUK:
169                flags = CWIID_RW_REG;
170                offset = 0xA40020;
171                err_str = "nunchuk ";
172                break;
173        default:
174                cwiid_err(wiimote, "Unsupported calibration request");
175                return -1;
176        }
177        if (cwiid_read(wiimote, flags, offset, 7, buf)) {
178                cwiid_err(wiimote, "Read error (%scal)", err_str);
179                return -1;
180        }
181
182        acc_cal->zero[CWIID_X] = buf[0];
183        acc_cal->zero[CWIID_Y] = buf[1];
184        acc_cal->zero[CWIID_Z] = buf[2];
185        acc_cal->one[CWIID_X]  = buf[4];
186        acc_cal->one[CWIID_Y]  = buf[5];
187        acc_cal->one[CWIID_Z]  = buf[6];
188
189        return 0;
190}
191
192int cwiid_get_balance_cal(cwiid_wiimote_t *wiimote,
193                          struct balance_cal *balance_cal)
194{
195        unsigned char buf[24];
196
197        if (cwiid_read(wiimote, CWIID_RW_REG, 0xa40024, 24, buf)) {
198                cwiid_err(wiimote, "Read error (balancecal)");
199                return -1;
200        }
201        balance_cal->right_top[0]    = ((uint16_t)buf[0]<<8 | (uint16_t)buf[1]);
202        balance_cal->right_bottom[0] = ((uint16_t)buf[2]<<8 | (uint16_t)buf[3]);
203        balance_cal->left_top[0]     = ((uint16_t)buf[4]<<8 | (uint16_t)buf[5]);
204        balance_cal->left_bottom[0]  = ((uint16_t)buf[6]<<8 | (uint16_t)buf[7]);
205        balance_cal->right_top[1]    = ((uint16_t)buf[8]<<8 | (uint16_t)buf[9]);
206        balance_cal->right_bottom[1] = ((uint16_t)buf[10]<<8 | (uint16_t)buf[11]);
207        balance_cal->left_top[1]     = ((uint16_t)buf[12]<<8 | (uint16_t)buf[13]);
208        balance_cal->left_bottom[1]  = ((uint16_t)buf[14]<<8 | (uint16_t)buf[15]);
209        balance_cal->right_top[2]    = ((uint16_t)buf[16]<<8 | (uint16_t)buf[17]);
210        balance_cal->right_bottom[2] = ((uint16_t)buf[18]<<8 | (uint16_t)buf[19]);
211        balance_cal->left_top[2]     = ((uint16_t)buf[20]<<8 | (uint16_t)buf[21]);
212        balance_cal->left_bottom[2]  = ((uint16_t)buf[22]<<8 | (uint16_t)buf[23]);
213
214        return 0;
215}
Note: See TracBrowser for help on using the browser.