| 1 | /* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 2 | * |
|---|
| 3 | * This program is free software; you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 6 | * (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 |
|---|
| 11 | * GNU 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 02110-1301 USA |
|---|
| 16 | * |
|---|
| 17 | * ChangeLog: |
|---|
| 18 | * 03/05/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 19 | * * added wiimote_err_t definition |
|---|
| 20 | * * added wiimote_set_err prototype |
|---|
| 21 | * |
|---|
| 22 | * 03/01/2007: L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 23 | * * Initial ChangeLog |
|---|
| 24 | * * type audit (stdint, const, char booleans) |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #ifndef WIIMOTE_H |
|---|
| 28 | #define WIIMOTE_H |
|---|
| 29 | |
|---|
| 30 | #include <stdint.h> |
|---|
| 31 | #include <bluetooth/bluetooth.h> /* bdaddr_t */ |
|---|
| 32 | |
|---|
| 33 | /* Report Mode Flags */ |
|---|
| 34 | #define WIIMOTE_RPT_STATUS 0x01 |
|---|
| 35 | #define WIIMOTE_RPT_BTN 0x02 |
|---|
| 36 | #define WIIMOTE_RPT_ACC 0x04 |
|---|
| 37 | #define WIIMOTE_RPT_IR 0x08 |
|---|
| 38 | #define WIIMOTE_RPT_NUNCHUK 0x10 |
|---|
| 39 | #define WIIMOTE_RPT_CLASSIC 0x20 |
|---|
| 40 | #define WIIMOTE_RPT_EXT (WIIMOTE_RPT_NUNCHUK | WIIMOTE_RPT_CLASSIC) |
|---|
| 41 | |
|---|
| 42 | /* LED flags */ |
|---|
| 43 | #define WIIMOTE_LED1_ON 0x01 |
|---|
| 44 | #define WIIMOTE_LED2_ON 0x02 |
|---|
| 45 | #define WIIMOTE_LED3_ON 0x04 |
|---|
| 46 | #define WIIMOTE_LED4_ON 0x08 |
|---|
| 47 | |
|---|
| 48 | /* Button flags */ |
|---|
| 49 | #define WIIMOTE_BTN_2 0x0001 |
|---|
| 50 | #define WIIMOTE_BTN_1 0x0002 |
|---|
| 51 | #define WIIMOTE_BTN_B 0x0004 |
|---|
| 52 | #define WIIMOTE_BTN_A 0x0008 |
|---|
| 53 | #define WIIMOTE_BTN_MINUS 0x0010 |
|---|
| 54 | #define WIIMOTE_BTN_HOME 0x0080 |
|---|
| 55 | #define WIIMOTE_BTN_LEFT 0x0100 |
|---|
| 56 | #define WIIMOTE_BTN_RIGHT 0x0200 |
|---|
| 57 | #define WIIMOTE_BTN_DOWN 0x0400 |
|---|
| 58 | #define WIIMOTE_BTN_UP 0x0800 |
|---|
| 59 | #define WIIMOTE_BTN_PLUS 0x1000 |
|---|
| 60 | |
|---|
| 61 | #define WIIMOTE_NUNCHUK_BTN_Z 0x01 |
|---|
| 62 | #define WIIMOTE_NUNCHUK_BTN_C 0x02 |
|---|
| 63 | |
|---|
| 64 | #define WIIMOTE_CLASSIC_BTN_UP 0x0001 |
|---|
| 65 | #define WIIMOTE_CLASSIC_BTN_LEFT 0x0002 |
|---|
| 66 | #define WIIMOTE_CLASSIC_BTN_ZR 0x0004 |
|---|
| 67 | #define WIIMOTE_CLASSIC_BTN_X 0x0008 |
|---|
| 68 | #define WIIMOTE_CLASSIC_BTN_A 0x0010 |
|---|
| 69 | #define WIIMOTE_CLASSIC_BTN_Y 0x0020 |
|---|
| 70 | #define WIIMOTE_CLASSIC_BTN_B 0x0040 |
|---|
| 71 | #define WIIMOTE_CLASSIC_BTN_ZL 0x0080 |
|---|
| 72 | #define WIIMOTE_CLASSIC_BTN_R 0x0200 |
|---|
| 73 | #define WIIMOTE_CLASSIC_BTN_PLUS 0x0400 |
|---|
| 74 | #define WIIMOTE_CLASSIC_BTN_HOME 0x0800 |
|---|
| 75 | #define WIIMOTE_CLASSIC_BTN_MINUS 0x1000 |
|---|
| 76 | #define WIIMOTE_CLASSIC_BTN_L 0x2000 |
|---|
| 77 | #define WIIMOTE_CLASSIC_BTN_DOWN 0x4000 |
|---|
| 78 | #define WIIMOTE_CLASSIC_BTN_RIGHT 0x8000 |
|---|
| 79 | |
|---|
| 80 | /* Data Read/Write flags */ |
|---|
| 81 | #define WIIMOTE_RW_EEPROM 0x00 |
|---|
| 82 | #define WIIMOTE_RW_REG 0x04 |
|---|
| 83 | #define WIIMOTE_RW_DECODE 0x01 |
|---|
| 84 | |
|---|
| 85 | /* Maximum Data Read Length */ |
|---|
| 86 | #define WIIMOTE_MAX_READ_LEN 0xFFFF |
|---|
| 87 | |
|---|
| 88 | /* IR Defs */ |
|---|
| 89 | #define WIIMOTE_IR_SRC_COUNT 4 |
|---|
| 90 | #define WIIMOTE_IR_X_MAX 1024 |
|---|
| 91 | #define WIIMOTE_IR_Y_MAX 768 |
|---|
| 92 | |
|---|
| 93 | /* Battery */ |
|---|
| 94 | #define WIIMOTE_BATTERY_MAX 0xD0 |
|---|
| 95 | |
|---|
| 96 | /* Classic Controller Maxes */ |
|---|
| 97 | #define WIIMOTE_CLASSIC_L_STICK_MAX 0x3F |
|---|
| 98 | #define WIIMOTE_CLASSIC_R_STICK_MAX 0x1F |
|---|
| 99 | #define WIIMOTE_CLASSIC_LR_MAX 0x1F |
|---|
| 100 | |
|---|
| 101 | /* Environment Variables */ |
|---|
| 102 | #define WIIMOTE_BDADDR "WIIMOTE_BDADDR" |
|---|
| 103 | |
|---|
| 104 | /* Callback Maximum Message Count */ |
|---|
| 105 | #define WIIMOTE_MAX_MESG_COUNT 5 |
|---|
| 106 | |
|---|
| 107 | enum wiimote_command { |
|---|
| 108 | WIIMOTE_CMD_STATUS, |
|---|
| 109 | WIIMOTE_CMD_LED, |
|---|
| 110 | WIIMOTE_CMD_RUMBLE, |
|---|
| 111 | WIIMOTE_CMD_RPT_MODE |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | enum wiimote_mesg_type { |
|---|
| 115 | WIIMOTE_MESG_STATUS, |
|---|
| 116 | WIIMOTE_MESG_BTN, |
|---|
| 117 | WIIMOTE_MESG_ACC, |
|---|
| 118 | WIIMOTE_MESG_IR, |
|---|
| 119 | WIIMOTE_MESG_NUNCHUK, |
|---|
| 120 | WIIMOTE_MESG_CLASSIC, |
|---|
| 121 | WIIMOTE_MESG_UNKNOWN |
|---|
| 122 | }; |
|---|
| 123 | |
|---|
| 124 | enum wiimote_ext_type { |
|---|
| 125 | WIIMOTE_EXT_NONE, |
|---|
| 126 | WIIMOTE_EXT_NUNCHUK, |
|---|
| 127 | WIIMOTE_EXT_CLASSIC, |
|---|
| 128 | WIIMOTE_EXT_UNKNOWN |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | struct wiimote_status_mesg { |
|---|
| 132 | enum wiimote_mesg_type type; |
|---|
| 133 | uint8_t battery; |
|---|
| 134 | enum wiimote_ext_type extension; |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | struct wiimote_btn_mesg { |
|---|
| 138 | enum wiimote_mesg_type type; |
|---|
| 139 | uint16_t buttons; |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | struct wiimote_acc_mesg { |
|---|
| 143 | enum wiimote_mesg_type type; |
|---|
| 144 | uint8_t x; |
|---|
| 145 | uint8_t y; |
|---|
| 146 | uint8_t z; |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | struct wiimote_ir_src { |
|---|
| 150 | int valid; |
|---|
| 151 | uint16_t x; |
|---|
| 152 | uint16_t y; |
|---|
| 153 | int8_t size; |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | struct wiimote_ir_mesg { |
|---|
| 157 | enum wiimote_mesg_type type; |
|---|
| 158 | struct wiimote_ir_src src[WIIMOTE_IR_SRC_COUNT]; |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | struct wiimote_nunchuk_mesg { |
|---|
| 162 | enum wiimote_mesg_type type; |
|---|
| 163 | uint8_t stick_x; |
|---|
| 164 | uint8_t stick_y; |
|---|
| 165 | uint8_t acc_x; |
|---|
| 166 | uint8_t acc_y; |
|---|
| 167 | uint8_t acc_z; |
|---|
| 168 | uint8_t buttons; |
|---|
| 169 | }; |
|---|
| 170 | |
|---|
| 171 | struct wiimote_classic_mesg { |
|---|
| 172 | enum wiimote_mesg_type type; |
|---|
| 173 | uint8_t l_stick_x; |
|---|
| 174 | uint8_t l_stick_y; |
|---|
| 175 | uint8_t r_stick_x; |
|---|
| 176 | uint8_t r_stick_y; |
|---|
| 177 | uint8_t l; |
|---|
| 178 | uint8_t r; |
|---|
| 179 | uint16_t buttons; |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | union wiimote_mesg { |
|---|
| 183 | enum wiimote_mesg_type type; |
|---|
| 184 | struct wiimote_status_mesg status_mesg; |
|---|
| 185 | struct wiimote_btn_mesg btn_mesg; |
|---|
| 186 | struct wiimote_acc_mesg acc_mesg; |
|---|
| 187 | struct wiimote_ir_mesg ir_mesg; |
|---|
| 188 | struct wiimote_nunchuk_mesg nunchuk_mesg; |
|---|
| 189 | struct wiimote_classic_mesg classic_mesg; |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | typedef struct wiimote wiimote_t; |
|---|
| 193 | |
|---|
| 194 | typedef void wiimote_mesg_callback_t(int, int, union wiimote_mesg* []); |
|---|
| 195 | typedef void wiimote_err_t(int, const char *, ...); |
|---|
| 196 | |
|---|
| 197 | #ifdef __cplusplus |
|---|
| 198 | extern "C" { |
|---|
| 199 | #endif |
|---|
| 200 | |
|---|
| 201 | int wiimote_set_err(wiimote_err_t *err); |
|---|
| 202 | wiimote_t *wiimote_connect(bdaddr_t bdaddr, |
|---|
| 203 | wiimote_mesg_callback_t *mesg_callback, |
|---|
| 204 | int *id); |
|---|
| 205 | int wiimote_disconnect(wiimote_t *wiimote); |
|---|
| 206 | int wiimote_command(wiimote_t *wiimote, enum wiimote_command command, |
|---|
| 207 | uint8_t flags); |
|---|
| 208 | int wiimote_read(wiimote_t *wiimote, uint8_t flags, uint32_t offset, |
|---|
| 209 | uint16_t len, void *data); |
|---|
| 210 | int wiimote_write(wiimote_t *wiimote, uint8_t flags, uint32_t offset, |
|---|
| 211 | uint16_t len, const void *data); |
|---|
| 212 | /* int wiimote_beep(wiimote_t *wiimote); */ |
|---|
| 213 | int wiimote_findfirst(bdaddr_t *bdaddr); |
|---|
| 214 | |
|---|
| 215 | #ifdef __cplusplus |
|---|
| 216 | } |
|---|
| 217 | #endif |
|---|
| 218 | |
|---|
| 219 | #endif |
|---|
| 220 | |
|---|