next release will be 1.8.8
[libogc.git] / wiiuse / guitar_hero_3.c
blobf55d9d54a09ca233947cfaee3aa8bb4086db30ce
1 /*
2 * wiiuse
4 * Written By:
5 * Michael Laforest < para >
6 * Email: < thepara (--AT--) g m a i l [--DOT--] com >
8 * Copyright 2006-2007
10 * This file is part of wiiuse.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * $Header: /lvm/shared/ds/ds/cvs/devkitpro-cvsbackup/libogc/wiiuse/guitar_hero_3.c,v 1.7 2008-11-14 13:34:57 shagkur Exp $
29 /**
30 * @file
31 * @brief Guitar Hero 3 expansion device.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <math.h>
37 #include <string.h>
39 #ifdef WIN32
40 #include <Winsock2.h>
41 #endif
43 #include "definitions.h"
44 #include "wiiuse_internal.h"
45 #include "dynamics.h"
46 #include "events.h"
47 #include "guitar_hero_3.h"
48 #include "io.h"
50 static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now);
52 /**
53 * @brief Handle the handshake data from the guitar.
55 * @param cc A pointer to a classic_ctrl_t structure.
56 * @param data The data read in from the device.
57 * @param len The length of the data block, in bytes.
59 * @return Returns 1 if handshake was successful, 0 if not.
61 int guitar_hero_3_handshake(struct wiimote_t* wm, struct guitar_hero_3_t* gh3, ubyte* data, uword len) {
63 * The good fellows that made the Guitar Hero 3 controller
64 * failed to factory calibrate the devices. There is no
65 * calibration data on the device.
68 gh3->btns = 0;
69 gh3->btns_held = 0;
70 gh3->btns_released = 0;
71 gh3->wb_raw = 0;
72 gh3->whammy_bar = 0.0f;
73 gh3->tb_raw = 0;
74 gh3->touch_bar = -1;
76 /* joystick stuff */
77 gh3->js.max.x = GUITAR_HERO_3_JS_MAX_X;
78 gh3->js.min.x = GUITAR_HERO_3_JS_MIN_X;
79 gh3->js.center.x = GUITAR_HERO_3_JS_CENTER_X;
80 gh3->js.max.y = GUITAR_HERO_3_JS_MAX_Y;
81 gh3->js.min.y = GUITAR_HERO_3_JS_MIN_Y;
82 gh3->js.center.y = GUITAR_HERO_3_JS_CENTER_Y;
84 /* handshake done */
85 wm->event = WIIUSE_GUITAR_HERO_3_CTRL_INSERTED;
86 wm->exp.type = EXP_GUITAR_HERO_3;
88 #ifdef WIN32
89 wm->timeout = WIIMOTE_DEFAULT_TIMEOUT;
90 #endif
92 return 1;
96 /**
97 * @brief The guitar disconnected.
99 * @param cc A pointer to a classic_ctrl_t structure.
101 void guitar_hero_3_disconnected(struct guitar_hero_3_t* gh3)
103 memset(gh3, 0, sizeof(struct guitar_hero_3_t));
109 * @brief Handle guitar event.
111 * @param cc A pointer to a classic_ctrl_t structure.
112 * @param msg The message specified in the event packet.
114 void guitar_hero_3_event(struct guitar_hero_3_t* gh3, ubyte* msg) {
115 //int i;
117 /* decrypt data */
119 for (i = 0; i < 6; ++i)
120 msg[i] = (msg[i] ^ 0x17) + 0x17;
122 guitar_hero_3_pressed_buttons(gh3, BIG_ENDIAN_SHORT(*(short*)(msg + 4)));
124 gh3->js.pos.x = (msg[0] & GUITAR_HERO_3_JS_MASK);
125 gh3->js.pos.y = (msg[1] & GUITAR_HERO_3_JS_MASK);
126 gh3->tb_raw = (msg[2] & GUITAR_HERO_3_TOUCH_MASK);
127 gh3->wb_raw = (msg[3] & GUITAR_HERO_3_WHAMMY_MASK);
128 #ifndef GEKKO /* Done in wpad.c for GEKKO */
129 /* touch bar */
130 gh3->touch_bar = 0;
131 if (gh3->tb_raw > 0x1B)
132 gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE;
133 else if (gh3->tb_raw > 0x18)
134 gh3->touch_bar = GUITAR_HERO_3_TOUCH_ORANGE | GUITAR_HERO_3_TOUCH_BLUE;
135 else if (gh3->tb_raw > 0x15)
136 gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE;
137 else if (gh3->tb_raw > 0x13)
138 gh3->touch_bar = GUITAR_HERO_3_TOUCH_BLUE | GUITAR_HERO_3_TOUCH_YELLOW;
139 else if (gh3->tb_raw > 0x10)
140 gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW;
141 else if (gh3->tb_raw > 0x0D)
142 gh3->touch_bar = GUITAR_HERO_3_TOUCH_AVAILABLE;
143 else if (gh3->tb_raw > 0x0B)
144 gh3->touch_bar = GUITAR_HERO_3_TOUCH_YELLOW | GUITAR_HERO_3_TOUCH_RED;
145 else if (gh3->tb_raw > 0x08)
146 gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED;
147 else if (gh3->tb_raw > 0x05)
148 gh3->touch_bar = GUITAR_HERO_3_TOUCH_RED | GUITAR_HERO_3_TOUCH_GREEN;
149 else if (gh3->tb_raw > 0x02)
150 gh3->touch_bar = GUITAR_HERO_3_TOUCH_GREEN;
152 /* whammy bar */
153 gh3->whammy_bar = (gh3->wb_raw - GUITAR_HERO_3_WHAMMY_BAR_MIN) / (float)(GUITAR_HERO_3_WHAMMY_BAR_MAX - GUITAR_HERO_3_WHAMMY_BAR_MIN);
155 /* joy stick */
156 calc_joystick_state(&gh3->js, gh3->js.pos.x, gh3->js.pos.y);
157 #endif
162 * @brief Find what buttons are pressed.
164 * @param cc A pointer to a classic_ctrl_t structure.
165 * @param msg The message byte specified in the event packet.
167 static void guitar_hero_3_pressed_buttons(struct guitar_hero_3_t* gh3, short now) {
168 /* message is inverted (0 is active, 1 is inactive) */
169 now = ~now & GUITAR_HERO_3_BUTTON_ALL;
171 /* preserve old btns pressed */
172 gh3->btns_last = gh3->btns;
174 /* pressed now & were pressed, then held */
175 gh3->btns_held = (now & gh3->btns);
177 /* were pressed or were held & not pressed now, then released */
178 gh3->btns_released = ((gh3->btns | gh3->btns_held) & ~now);
180 /* buttons pressed now */
181 gh3->btns = now;