|
| 1 | +/* |
| 2 | + * wiiuse |
| 3 | + * |
| 4 | + * Written By: |
| 5 | + * Michael Laforest < para > |
| 6 | + * Email: < thepara (--AT--) g m a i l [--DOT--] com > |
| 7 | + * |
| 8 | + * Copyright 2006-2007 |
| 9 | + * |
| 10 | + * This file is part of wiiuse. |
| 11 | + * |
| 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. |
| 16 | + * |
| 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. |
| 21 | + * |
| 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/>. |
| 24 | + * |
| 25 | + * $Header: /lvm/shared/ds/ds/cvs/devkitpro-cvsbackup/libogc/wiiuse/classic.c,v 1.7 2008-11-14 13:34:57 shagkur Exp $ |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * @file |
| 31 | + * @brief Classic controller expansion device. |
| 32 | + */ |
| 33 | + |
| 34 | +#include "classic.h" |
| 35 | + |
| 36 | +#include "dynamics.h" |
| 37 | + |
| 38 | +#include <stdio.h> |
| 39 | +#include <stdlib.h> |
| 40 | +#include <math.h> |
| 41 | +#include <string.h> |
| 42 | + |
| 43 | +static void fix_bad_calibration_values(struct joystick_t *js, short right_stick) { |
| 44 | + if ((js->min.x >= js->center.x) || (js->max.x <= js->center.x)) { |
| 45 | + js->min.x = 0; |
| 46 | + js->max.x = right_stick ? 32 : 64; |
| 47 | + js->center.x = right_stick ? 16 : 32; |
| 48 | + } |
| 49 | + if ((js->min.y >= js->center.y) || (js->max.y <= js->center.y)) { |
| 50 | + js->min.y = 0; |
| 51 | + js->max.y = right_stick ? 32 : 64; |
| 52 | + js->center.y = right_stick ? 16 : 32; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Find what buttons are pressed. |
| 58 | + * |
| 59 | + * @param cc A pointer to a classic_ctrl_t structure. |
| 60 | + * @param msg The message byte specified in the event packet. |
| 61 | + */ |
| 62 | +static void classic_ctrl_pressed_buttons(struct classic_ctrl_t *cc, const uint8_t *now) { |
| 63 | + u32 buttons = (now[0] << 0x8) | now[1]; |
| 64 | + |
| 65 | + if (cc->type == CLASSIC_TYPE_WIIU) { |
| 66 | + /* append Wii U Pro Controller stick buttons to top 16 bits */ |
| 67 | + buttons |= (now[2] << 0x10); |
| 68 | + |
| 69 | + /* message is inverted (0 is active, 1 is inactive) */ |
| 70 | + buttons = ~buttons & WII_U_PRO_CTRL_BUTTON_ALL; |
| 71 | + } else { |
| 72 | + /* message is inverted (0 is active, 1 is inactive) */ |
| 73 | + buttons = ~buttons & CLASSIC_CTRL_BUTTON_ALL; |
| 74 | + } |
| 75 | + |
| 76 | + /* preserve old btns pressed */ |
| 77 | + cc->btns_last = cc->btns; |
| 78 | + |
| 79 | + /* pressed now & were pressed, then held */ |
| 80 | + cc->btns_held = (buttons & cc->btns); |
| 81 | + |
| 82 | + /* were pressed or were held & not pressed now, then released */ |
| 83 | + cc->btns_released = ((cc->btns | cc->btns_held) & ~buttons); |
| 84 | + |
| 85 | + /* buttons pressed now */ |
| 86 | + cc->btns = buttons; |
| 87 | +} |
| 88 | + |
| 89 | +void _wpad2_classic_calibrate(struct classic_ctrl_t *cc, |
| 90 | + const WpadDeviceExpCalibrationData *cd) |
| 91 | +{ |
| 92 | + const uint8_t *data = cd->data; |
| 93 | + |
| 94 | + cc->btns = 0; |
| 95 | + cc->btns_held = 0; |
| 96 | + cc->btns_released = 0; |
| 97 | + |
| 98 | + /* is this a wiiu pro? */ |
| 99 | + if (cc->type == CLASSIC_TYPE_WIIU) { |
| 100 | + cc->ljs.max.x = cc->ljs.max.y = 208; |
| 101 | + cc->ljs.min.x = cc->ljs.min.y = 48; |
| 102 | + cc->ljs.center.x = cc->ljs.center.y = 0x80; |
| 103 | + |
| 104 | + cc->rjs = cc->ljs; |
| 105 | + } else { |
| 106 | + /* joystick stuff */ |
| 107 | + cc->ljs.max.x = data[0] / 4 == 0 ? 64 : data[0] / 4; |
| 108 | + cc->ljs.min.x = data[1] / 4; |
| 109 | + cc->ljs.center.x = data[2] / 4 == 0 ? 32 : data[2] / 4; |
| 110 | + cc->ljs.max.y = data[3] / 4 == 0 ? 64 : data[3] / 4; |
| 111 | + cc->ljs.min.y = data[4] / 4; |
| 112 | + cc->ljs.center.y = data[5] / 4 == 0 ? 32 : data[5] / 4; |
| 113 | + |
| 114 | + cc->rjs.max.x = data[6] / 8 == 0 ? 32 : data[6] / 8; |
| 115 | + cc->rjs.min.x = data[7] / 8; |
| 116 | + cc->rjs.center.x = data[8] / 8 == 0 ? 16 : data[8] / 8; |
| 117 | + cc->rjs.max.y = data[9] / 8 == 0 ? 32 : data[9] / 8; |
| 118 | + cc->rjs.min.y = data[10] / 8; |
| 119 | + cc->rjs.center.y = data[11] / 8 == 0 ? 16 : data[11] / 8; |
| 120 | + |
| 121 | + fix_bad_calibration_values(&cc->ljs, 0); |
| 122 | + fix_bad_calibration_values(&cc->rjs, 1); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * @brief Handle classic controller event. |
| 128 | + * |
| 129 | + * @param cc A pointer to a classic_ctrl_t structure. |
| 130 | + * @param msg The message specified in the event packet. |
| 131 | + */ |
| 132 | +void _wpad2_classic_event(struct classic_ctrl_t *cc, const uint8_t *msg) { |
| 133 | + if (cc->type == CLASSIC_TYPE_WIIU) { |
| 134 | + classic_ctrl_pressed_buttons(cc, msg + 8); |
| 135 | + |
| 136 | + /* 12-bit little endian values adjusted to 8-bit */ |
| 137 | + cc->ljs.pos.x = (msg[0] >> 4) | (msg[1] << 4); |
| 138 | + cc->rjs.pos.x = (msg[2] >> 4) | (msg[3] << 4); |
| 139 | + cc->ljs.pos.y = (msg[4] >> 4) | (msg[5] << 4); |
| 140 | + cc->rjs.pos.y = (msg[6] >> 4) | (msg[7] << 4); |
| 141 | + |
| 142 | + cc->ls_raw = cc->btns & CLASSIC_CTRL_BUTTON_FULL_L ? 0x1F : 0; |
| 143 | + cc->rs_raw = cc->btns & CLASSIC_CTRL_BUTTON_FULL_R ? 0x1F : 0; |
| 144 | + |
| 145 | + /* Wii U pro controller specific data */ |
| 146 | + cc->charging = !(((msg[10] & WII_U_PRO_CTRL_CHARGING) >> 2) & 1); |
| 147 | + cc->wired = !(((msg[10] & WII_U_PRO_CTRL_WIRED) >> 3) & 1); |
| 148 | + cc->battery = ((msg[10] & WII_U_PRO_CTRL_BATTERY) >> 4) & 7; |
| 149 | + } else { |
| 150 | + classic_ctrl_pressed_buttons(cc, msg + 4); |
| 151 | + |
| 152 | + /* left/right triggers */ |
| 153 | + cc->ls_raw = (((msg[2] & 0x60) >> 2) | ((msg[3] & 0xE0) >> 5)); |
| 154 | + cc->rs_raw = (msg[3] & 0x1F); |
| 155 | + |
| 156 | + /* calculate joystick orientation */ |
| 157 | + cc->ljs.pos.x = (msg[0] & 0x3F); |
| 158 | + cc->ljs.pos.y = (msg[1] & 0x3F); |
| 159 | + cc->rjs.pos.x = ((msg[0] & 0xC0) >> 3) | ((msg[1] & 0xC0) >> 5) | ((msg[2] & 0x80) >> 7); |
| 160 | + cc->rjs.pos.y = (msg[2] & 0x1F); |
| 161 | + } |
| 162 | +} |
0 commit comments