Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wiimote/src/external/wiicpp/wiic/guitar_hero_3.c @ 10256

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

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

  • Property svn:executable set to *
File size: 4.5 KB
Line 
1/*
2 *    guitar_hero_3.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 Guitar Hero 3 expansion device.
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <math.h>
40
41
42#include "definitions.h"
43#include "wiic_internal.h"
44#include "dynamics.h"
45#include "events.h"
46#include "guitar_hero_3.h"
47
48static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now);
49
50/**
51 *      @brief Handle the handshake data from the guitar.
52 *
53 *      @param cc               A pointer to a classic_ctrl_t structure.
54 *      @param data             The data read in from the device.
55 *      @param len              The length of the data block, in bytes.
56 *
57 *      @return Returns 1 if handshake was successful, 0 if not.
58 */
59int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, byte* data, unsigned short len) {
60        int i;
61        int offset = 0;
62
63        /*
64         *      The good fellows that made the Guitar Hero 3 controller
65         *      failed to factory calibrate the devices.  There is no
66         *      calibration data on the device.
67         */
68
69        gh3->btns = 0;
70        gh3->btns_held = 0;
71        gh3->btns_released = 0;
72        gh3->whammy_bar = 0.0f;
73
74        /* decrypt data */
75        for (i = 0; i < len; ++i)
76                data[i] = (data[i] ^ 0x17) + 0x17;
77
78        if (data[offset] == 0xFF) {
79                /*
80                 *      Sometimes the data returned here is not correct.
81                 *      This might happen because the wiimote is lagging
82                 *      behind our initialization sequence.
83                 *      To fix this just request the handshake again.
84                 *
85                 *      Other times it's just the first 16 bytes are 0xFF,
86                 *      but since the next 16 bytes are the same, just use
87                 *      those.
88                 */
89                if (data[offset + 16] == 0xFF) {
90                        /* get the calibration data */
91                        byte* handshake_buf = malloc(EXP_HANDSHAKE_LEN * sizeof(byte));
92
93                        WIIC_DEBUG("Guitar Hero 3 handshake appears invalid, trying again.");
94                        wiic_read_data_cb(wm, handshake_expansion, handshake_buf, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN);
95
96                        return 0;
97                } else
98                        offset += 16;
99        }
100
101        /* joystick stuff */
102        gh3->js.max.x = GUITAR_HERO_3_JS_MAX_X;
103        gh3->js.min.x = GUITAR_HERO_3_JS_MIN_X;
104        gh3->js.center.x = GUITAR_HERO_3_JS_CENTER_X;
105        gh3->js.max.y = GUITAR_HERO_3_JS_MAX_Y;
106        gh3->js.min.y = GUITAR_HERO_3_JS_MIN_Y;
107        gh3->js.center.y = GUITAR_HERO_3_JS_CENTER_Y;
108
109        /* handshake done */
110        wm->exp.type = EXP_GUITAR_HERO_3;
111
112        return 1;
113}
114
115
116/**
117 *      @brief The guitar disconnected.
118 *
119 *      @param cc               A pointer to a classic_ctrl_t structure.
120 */
121void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3) {
122        memset(gh3, 0, sizeof(struct guitar_hero_3_t));
123}
124
125
126
127/**
128 *      @brief Handle guitar event.
129 *
130 *      @param cc               A pointer to a classic_ctrl_t structure.
131 *      @param msg              The message specified in the event packet.
132 */
133void guitar_hero_3_event(struct guitar_hero_3_t* gh3, byte* msg) {
134        int i;
135
136        /* decrypt data */
137        for (i = 0; i < 6; ++i)
138                msg[i] = (msg[i] ^ 0x17) + 0x17;
139
140        guitar_hero_3_pressed_buttons(gh3, BIG_ENDIAN_SHORT(*(short*)(msg + 4)));
141
142        /* whammy bar */
143        gh3->whammy_bar = (msg[3] - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN);
144
145        /* joy stick */
146        calc_joystick_state(&gh3->js, msg[0], msg[1]);
147}
148
149
150/**
151 *      @brief Find what buttons are pressed.
152 *
153 *      @param cc               A pointer to a classic_ctrl_t structure.
154 *      @param msg              The message byte specified in the event packet.
155 */
156static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now) {
157        /* message is inverted (0 is active, 1 is inactive) */
158        now = ~now & GUITAR_HERO_3_BUTTON_ALL;
159
160        /* pressed now & were pressed, then held */
161        gh3->btns_held = (now & gh3->btns);
162
163        /* were pressed or were held & not pressed now, then released */
164        gh3->btns_released = ((gh3->btns | gh3->btns_held) & ~now);
165
166        /* buttons pressed now */
167        gh3->btns = now;
168}
Note: See TracBrowser for help on using the repository browser.