| | 1 | /* Copyright (C) 2009 Jonas Kölker <jonaskoelker@gnu.org> |
| | 2 | * |
| | 3 | * This program is free software; you can redistribute it and/or |
| | 4 | * modify it under the terms of the GNU General Public License as |
| | 5 | * published by the Free Software Foundation; either version 2 of the |
| | 6 | * License, or (at your option) any later version. |
| | 7 | * |
| | 8 | * This program is distributed in the hope that it will be useful, |
| | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 11 | * General Public License for more details. |
| | 12 | * |
| | 13 | * You should have received a copy of the GNU General Public License |
| | 14 | * along with this program; if not, write to the Free Software |
| | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| | 16 | * 02110-1301 USA |
| | 17 | */ |
| | 18 | |
| | 19 | #include <math.h> |
| | 20 | |
| | 21 | #include "wmplugin.h" |
| | 22 | #include <cwiid.h> |
| | 23 | #include "nunchuk_mouse.h" |
| | 24 | |
| | 25 | static void process_nunchuk(struct cwiid_nunchuk_mesg *mesg) |
| | 26 | { |
| | 27 | /* st(x|y): stick value along direction */ |
| | 28 | const uint8_t stx = mesg->stick[CWIID_X]; |
| | 29 | const uint8_t sty = mesg->stick[CWIID_Y]; |
| | 30 | /* d(x|y)(n|p): "delta x, direction=negative", etc. */ |
| | 31 | const int dxn = (center_x - DEADZONE) - stx; |
| | 32 | const int dxp = stx - (center_x + DEADZONE); |
| | 33 | const int dyn = (center_y - DEADZONE) - sty; |
| | 34 | const int dyp = sty - (center_y + DEADZONE); |
| | 35 | |
| | 36 | /* TODO: with -REL_Y, we go down slower than we go up. */ |
| | 37 | data.buttons = 0; |
| | 38 | /* += (vs =): to accumulate the result of multiple events. */ |
| | 39 | data.axes[X].value += |
| | 40 | (dxn >= 0)? -(dxn * RANGE / x_neg_range): |
| | 41 | (dxp >= 0)? +(dxp * RANGE / x_pos_range): 0; |
| | 42 | data.axes[Y].value += |
| | 43 | (dyn >= 0)? -(dyn * RANGE / y_neg_range): |
| | 44 | (dyp >= 0)? +(dyp * RANGE / y_pos_range): 0; |
| | 45 | } |
| | 46 | |
| | 47 | static void calibrate_joystick() { |
| | 48 | int ret; |
| | 49 | uint8_t buf[6]; |
| | 50 | |
| | 51 | /* http://abstrakraft.org/cwiid/ticket/40 */ |
| | 52 | ret = cwiid_read(wiimote, CWIID_RW_REG | CWIID_RW_DECODE, |
| | 53 | 0xA40028, sizeof buf, buf); |
| | 54 | if (ret) wmplugin_err(plugin_id, "joystick calibration error"); |
| | 55 | |
| | 56 | center_x = buf[OFF_X + OFF_CENTER]; |
| | 57 | x_neg_range = (center_x - DEADZONE) - buf[OFF_X - OFF_MIN]; |
| | 58 | x_pos_range = buf[OFF_X + OFF_MAX] - (center_x + DEADZONE); |
| | 59 | |
| | 60 | center_y = buf[OFF_Y + OFF_CENTER]; |
| | 61 | y_neg_range = (center_y - DEADZONE) - buf[OFF_Y - OFF_MIN]; |
| | 62 | y_pos_range = buf[OFF_Y + OFF_MAX] - (center_y + DEADZONE); |
| | 63 | } |
| | 64 | |
| | 65 | struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[]) |
| | 66 | { |
| | 67 | int i; |
| | 68 | enum cwiid_ext_type ext_type = CWIID_EXT_NONE; |
| | 69 | struct wmplugin_data *ret = NULL; |
| | 70 | |
| | 71 | data.axes[X].value = data.axes[Y].value = 0; |
| | 72 | for (i=0; i < mesg_count; i++) { |
| | 73 | switch (mesg[i].type) { |
| | 74 | case CWIID_MESG_STATUS: |
| | 75 | if ((mesg[i].status_mesg.ext_type == CWIID_EXT_NUNCHUK) && |
| | 76 | (ext_type != CWIID_EXT_NUNCHUK)) { |
| | 77 | calibrate_joystick(); |
| | 78 | } |
| | 79 | ext_type = mesg[i].status_mesg.ext_type; |
| | 80 | break; |
| | 81 | case CWIID_MESG_NUNCHUK: |
| | 82 | process_nunchuk(&mesg[i].nunchuk_mesg); |
| | 83 | ret = &data; |
| | 84 | break; |
| | 85 | default: |
| | 86 | break; |
| | 87 | } |
| | 88 | } |
| | 89 | return ret; |
| | 90 | } |
| | 91 | |
| | 92 | int wmplugin_init(int id, cwiid_wiimote_t *arg_wiimote) |
| | 93 | { |
| | 94 | plugin_id = id; |
| | 95 | wiimote = arg_wiimote; |
| | 96 | data.buttons = 0; |
| | 97 | data.axes[0].valid = 1; |
| | 98 | data.axes[1].valid = 1; |
| | 99 | if (wmplugin_set_rpt_mode(id, CWIID_RPT_STATUS | CWIID_RPT_NUNCHUK)) { |
| | 100 | return -1; |
| | 101 | } |
| | 102 | |
| | 103 | return 0; |
| | 104 | } |
| | 105 | |
| | 106 | struct wmplugin_info *wmplugin_info() { |
| | 107 | if (info_init) return &info; |
| | 108 | info.button_count = 0; |
| | 109 | info.axis_count = 2; |
| | 110 | |
| | 111 | info.axis_info[X].name = "X"; |
| | 112 | info.axis_info[X].type = WMPLUGIN_REL; |
| | 113 | info.axis_info[X].min = -RANGE; |
| | 114 | info.axis_info[X].max = +RANGE; |
| | 115 | info.axis_info[X].fuzz = 0; |
| | 116 | info.axis_info[X].flat = 0; |
| | 117 | |
| | 118 | info.axis_info[Y].name = "Y"; |
| | 119 | info.axis_info[Y].type = WMPLUGIN_REL; |
| | 120 | info.axis_info[Y].min = -RANGE; |
| | 121 | info.axis_info[Y].max = +RANGE; |
| | 122 | info.axis_info[Y].fuzz = 0; |
| | 123 | info.axis_info[Y].flat = 0; |
| | 124 | |
| | 125 | info_init = 1; |
| | 126 | return &info; |
| | 127 | } |