-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathwiiboard.c
More file actions
127 lines (109 loc) · 3.28 KB
/
Copy pathwiiboard.c
File metadata and controls
127 lines (109 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* wiiuse
*
* Written By:
* Michael Laforest < para >
* Email: < thepara (--AT--) g m a i l [--DOT--] com >
*
* Copyright 2006-2007
*
* This file is part of wiiuse.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* $Header: /cvsroot/devkitpro/libogc/wiiuse/wiiboard.c,v 1.6 2008/05/26 19:24:53 shagkur Exp $
*
*/
/**
* @file
* @brief Wiiboard expansion device.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifdef WIN32
#include <Winsock2.h>
#endif
#include "definitions.h"
#include "wiiuse_internal.h"
#include "dynamics.h"
#include "events.h"
#include "wiiboard.h"
#include "io.h"
static u16 load_be_u16(u8* data)
{
return ((u16)(data[0] << 8)) | ((u16)data[1]);
}
/**
* @brief Handle the handshake data from the wiiboard.
*
* @param wb A pointer to a wii_board_t structure.
* @param data The data read in from the device.
* @param len The length of the data block, in bytes.
*
* @return Returns 1 if handshake was successful, 0 if not.
*/
int wii_board_handshake(struct wiimote_t* wm, struct wii_board_t* wb, ubyte* data, uword len)
{
unsigned offset = 0;
if (data[offset] == 0xff) {
if (data[offset + 0x10] == 0xff) {
WIIUSE_DEBUG("Wii Balance Board handshake appears invalid, trying again.");
wiiuse_read_data(wm, data, WM_EXP_MEM_CALIBR, EXP_HANDSHAKE_LEN, wiiuse_handshake_expansion);
return 0;
}
offset += 0x10;
}
for (unsigned i = 0; i < WII_BOARD_NUM_SENSORS; ++i) {
/* The reference values for 0 Kg */
wb->cal_sensor[i].ref_0 = load_be_u16(data + offset + 0x04 + 2*i);
/* The reference values for 17 Kg */
wb->cal_sensor[i].ref_17 = load_be_u16(data + offset + 0x0c + 2*i);
/* The reference values for 34 Kg */
wb->cal_sensor[i].ref_34 = load_be_u16(data + offset + 0x14 + 2*i);
}
/* The critical battery value (always 0x69). */
wb->cal_bat = data[offset + 0x01];
/* The reference temperature. */
wb->cal_temp = data[offset + 0x40];
/* handshake done */
wm->event = WIIUSE_WII_BOARD_INSERTED;
wm->exp.type = EXP_WII_BOARD;
/* Balance Board only has the first LED, so make sure it's on. */
wiiuse_set_leds(wm, WIIMOTE_LED_1, NULL);
return 1;
}
/**
* @brief The wii board disconnected.
*
* @param cc A pointer to a wii_board_t structure.
*/
void wii_board_disconnected(struct wii_board_t* wb)
{
memset(wb, 0, sizeof(struct wii_board_t));
}
/**
* @brief Handle wii board event.
*
* @param wb A pointer to a wii_board_t structure.
* @param msg The message specified in the event packet.
*/
void wii_board_event(struct wii_board_t* wb, ubyte* msg)
{
for (unsigned i = 0; i < WII_BOARD_NUM_SENSORS; ++i)
wb->raw_sensor[i] = load_be_u16(msg + 2*i);
wb->raw_temp = msg[0x8];
wb->raw_bat = msg[0xa];
}