Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wiimote/src/external/wiicpp/wiic/classic.c @ 9780

Last change on this file since 9780 was 9780, checked in by georgr, 10 years ago

WiiCpp library successfully (?) added - won't work without libbluetooth-dev

  • Property svn:executable set to *
File size: 4.9 KB
Line 
1/*
2 *    classic.c
3 *
4 *        This file is part of WiiC, written by:
5 *              Gabriele Randelli
6 *              Email: randelli@dis.uniroma1.it
7 *
8 *    Copyright 2010
9 *             
10 *        This file is based on Wiiuse, written By:
11 *              Michael Laforest        < para >
12 *              Email: < thepara (--AT--) g m a i l [--DOT--] com >
13 *
14 *        Copyright 2006-2007
15 *
16 *    This program is free software; you can redistribute it and/or modify
17 *    it under the terms of the GNU General Public License as published by
18 *    the Free Software Foundation; either version 3 of the License, or
19 *    (at your option) any later version.
20 *
21 *    This program is distributed in the hope that it will be useful,
22 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
23 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 *    GNU General Public License for more details.
25 *
26 *    You should have received a copy of the GNU General Public License
27 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 *
29 *        $Header$
30 */
31
32/**
33 *      @file
34 *      @brief Classic controller expansion device.
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <math.h>
40
41#include "definitions.h"
42#include "wiic_internal.h"
43#include "dynamics.h"
44#include "events.h"
45#include "classic.h"
46
47static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now);
48
49/**
50 *      @brief Handle the handshake data from the classic controller.
51 *
52 *      @param cc               A pointer to a classic_ctrl_t structure.
53 *      @param data             The data read in from the device.
54 *      @param len              The length of the data block, in bytes.
55 *
56 *      @return Returns 1 if handshake was successful, 0 if not.
57 */
58int classic_ctrl_handshake(struct wiimote_t* wm, struct classic_ctrl_t* cc, byte* data, unsigned short len) {
59        int i;
60        int offset = 0;
61
62        cc->btns = 0;
63        cc->btns_held = 0;
64        cc->btns_released = 0;
65        cc->r_shoulder = 0;
66        cc->l_shoulder = 0;
67
68        /* decrypt data */
69        for (i = 0; i < len; ++i)
70                data[i] = (data[i] ^ 0x17) + 0x17;
71
72        if (data[offset] == 0xFF) {
73                /*
74                 *      Sometimes the data returned here is not correct.
75                 *      This might happen because the wiimote is lagging
76                 *      behind our initialization sequence.
77                 *      To fix this just request the handshake again.
78                 *
79                 *      Other times it's just the first 16 bytes are 0xFF,
80                 *      but since the next 16 bytes are the same, just use
81                 *      those.
82                 */
83                if (data[offset + 16] == 0xFF) {
84                        /* get the calibration data */
85                        byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte));
86
87                        WIIC_DEBUG("Classic controller handshake appears invalid, trying again.");
88                        wiic_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN);
89
90                        return 0;
91                } else
92                        offset += 16;
93        }
94
95
96        /* joystick stuff */
97        cc->ljs.max.x = data[0 + offset] / 4;
98        cc->ljs.min.x = data[1 + offset] / 4;
99        cc->ljs.center.x = data[2 + offset] / 4;
100        cc->ljs.max.y = data[3 + offset] / 4;
101        cc->ljs.min.y = data[4 + offset] / 4;
102        cc->ljs.center.y = data[5 + offset] / 4;
103
104        cc->rjs.max.x = data[6 + offset] / 8;
105        cc->rjs.min.x = data[7 + offset] / 8;
106        cc->rjs.center.x = data[8 + offset] / 8;
107        cc->rjs.max.y = data[9 + offset] / 8;
108        cc->rjs.min.y = data[10 + offset] / 8;
109        cc->rjs.center.y = data[11 + offset] / 8;
110
111        /* handshake done */
112        wm->exp.type = EXP_CLASSIC;
113
114        return 1;
115}
116
117
118/**
119 *      @brief The classic controller disconnected.
120 *
121 *      @param cc               A pointer to a classic_ctrl_t structure.
122 */
123void classic_ctrl_disconnected(struct classic_ctrl_t* cc) {
124        memset(cc, 0, sizeof(struct classic_ctrl_t));
125}
126
127
128
129/**
130 *      @brief Handle classic controller event.
131 *
132 *      @param cc               A pointer to a classic_ctrl_t structure.
133 *      @param msg              The message specified in the event packet.
134 */
135void classic_ctrl_event(struct classic_ctrl_t* cc, byte* msg) {
136        int i, lx, ly, rx, ry;
137        byte l, r;
138
139        /* decrypt data */
140        for (i = 0; i < 6; ++i)
141                msg[i] = (msg[i] ^ 0x17) + 0x17;
142
143        classic_ctrl_pressed_buttons(cc, BIG_ENDIAN_SHORT(*(short*)(msg + 4)));
144
145        /* left/right buttons */
146        l = (((msg[2] & 0x60) >> 2) | ((msg[3] & 0xE0) >> 5));
147        r = (msg[3] & 0x1F);
148
149        /*
150         *      TODO - LR range hardcoded from 0x00 to 0x1F.
151         *      This is probably in the calibration somewhere.
152         */
153        cc->r_shoulder = ((float)r / 0x1F);
154        cc->l_shoulder = ((float)l / 0x1F);
155
156        /* calculate joystick orientation */
157        lx = (msg[0] & 0x3F);
158        ly = (msg[1] & 0x3F);
159        rx = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7);
160        ry = (msg[2] & 0x1F);
161
162        calc_joystick_state(&cc->ljs, lx, ly);
163        calc_joystick_state(&cc->rjs, rx, ry);
164}
165
166
167/**
168 *      @brief Find what buttons are pressed.
169 *
170 *      @param cc               A pointer to a classic_ctrl_t structure.
171 *      @param msg              The message byte specified in the event packet.
172 */
173static void classic_ctrl_pressed_buttons(struct classic_ctrl_t* cc, short now) {
174        /* message is inverted (0 is active, 1 is inactive) */
175        now = ~now & CLASSIC_CTRL_BUTTON_ALL;
176
177        /* pressed now & were pressed, then held */
178        cc->btns_held = (now & cc->btns);
179
180        /* were pressed or were held & not pressed now, then released */
181        cc->btns_released = ((cc->btns | cc->btns_held) & ~now);
182
183        /* buttons pressed now */
184        cc->btns = now;
185}
Note: See TracBrowser for help on using the repository browser.