Merge commit 'origin/svn-head' into dhewg
[libogc.git] / wiiuse / wiiboard.c
blob5ee0ccad6ca89cca86f6371b16a9b6db1acbe32e
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: /cvsroot/devkitpro/libogc/wiiuse/wiiboard.c,v 1.6 2008/05/26 19:24:53 shagkur Exp $
29 /**
30 * @file
31 * @brief Wiiboard 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 "wiiboard.h"
48 #include "io.h"
50 /**
51 * @brief Handle the handshake data from the wiiboard.
53 * @param wb A pointer to a wii_board_t structure.
54 * @param data The data read in from the device.
55 * @param len The length of the data block, in bytes.
57 * @return Returns 1 if handshake was successful, 0 if not.
59 int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, ubyte* data, uword len)
61 int offset = 0;
63 if (data[offset]==0xff) {
64 if (data[offset+16]==0xff) {
65 WIIUSE_DEBUG("Wii Balance Board handshake appears invalid, trying again.");
66 wiiuse_read_data(wm, data, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN, wiiuse_handshake_expansion);
67 return 0;
69 offset += 16;
72 wb->ctr[0] = (data[offset+4]<<8)|data[offset+5];
73 wb->cbr[0] = (data[offset+6]<<8)|data[offset+7];
74 wb->ctl[0] = (data[offset+8]<<8)|data[offset+9];
75 wb->cbl[0] = (data[offset+10]<<8)|data[offset+11];
77 wb->ctr[1] = (data[offset+12]<<8)|data[offset+13];
78 wb->cbr[1] = (data[offset+14]<<8)|data[offset+15];
79 wb->ctl[1] = (data[offset+16]<<8)|data[offset+17];
80 wb->cbl[1] = (data[offset+18]<<8)|data[offset+19];
82 wb->ctr[2] = (data[offset+20]<<8)|data[offset+21];
83 wb->cbr[2] = (data[offset+22]<<8)|data[offset+23];
84 wb->ctl[2] = (data[offset+24]<<8)|data[offset+25];
85 wb->cbl[2] = (data[offset+26]<<8)|data[offset+27];
87 /* handshake done */
88 wm->event = WIIUSE_WII_BOARD_INSERTED;
89 wm->exp.type = EXP_WII_BOARD;
91 return 1;
95 /**
96 * @brief The wii board disconnected.
98 * @param cc A pointer to a classic_ctrl_t structure.
100 void wii_board_disconnected(struct wii_board_t* wb)
102 memset(wb, 0, sizeof(struct wii_board_t));
107 * @brief Handle wii board event.
109 * @param wb A pointer to a wii_board_t structure.
110 * @param msg The message specified in the event packet.
112 void wii_board_event(struct wii_board_t* wb, ubyte* msg)
114 wb->rtr = (msg[0]<<8)|msg[1];
115 wb->rbr = (msg[2]<<8)|msg[3];
116 wb->rtl = (msg[4]<<8)|msg[5];
117 wb->rbl = (msg[6]<<8)|msg[7];