| 1 | /* Copyright (C) 2007 L. Donnie Smith <donnie.smith@gatech.edu> |
|---|
| 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 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef CWIID_H |
|---|
| 20 | #define CWIID_H |
|---|
| 21 | |
|---|
| 22 | #include <stdarg.h> |
|---|
| 23 | #include <stdint.h> |
|---|
| 24 | #include <time.h> |
|---|
| 25 | #include <bluetooth/bluetooth.h> /* bdaddr_t */ |
|---|
| 26 | |
|---|
| 27 | /* Flags */ |
|---|
| 28 | #define CWIID_FLAG_MESG_IFC 0x01 |
|---|
| 29 | #define CWIID_FLAG_CONTINUOUS 0x02 |
|---|
| 30 | #define CWIID_FLAG_REPEAT_BTN 0x04 |
|---|
| 31 | #define CWIID_FLAG_NONBLOCK 0x08 |
|---|
| 32 | #define CWIID_FLAG_MOTIONPLUS 0x10 |
|---|
| 33 | |
|---|
| 34 | /* Report Mode Flags */ |
|---|
| 35 | #define CWIID_RPT_STATUS 0x01 |
|---|
| 36 | #define CWIID_RPT_BTN 0x02 |
|---|
| 37 | #define CWIID_RPT_ACC 0x04 |
|---|
| 38 | #define CWIID_RPT_IR 0x08 |
|---|
| 39 | #define CWIID_RPT_NUNCHUK 0x10 |
|---|
| 40 | #define CWIID_RPT_CLASSIC 0x20 |
|---|
| 41 | #define CWIID_RPT_BALANCE 0x40 |
|---|
| 42 | #define CWIID_RPT_MOTIONPLUS 0x80 |
|---|
| 43 | #define CWIID_RPT_EXT (CWIID_RPT_NUNCHUK | CWIID_RPT_CLASSIC | \ |
|---|
| 44 | CWIID_RPT_BALANCE | CWIID_RPT_MOTIONPLUS) |
|---|
| 45 | |
|---|
| 46 | /* LED flags */ |
|---|
| 47 | #define CWIID_LED1_ON 0x01 |
|---|
| 48 | #define CWIID_LED2_ON 0x02 |
|---|
| 49 | #define CWIID_LED3_ON 0x04 |
|---|
| 50 | #define CWIID_LED4_ON 0x08 |
|---|
| 51 | |
|---|
| 52 | /* Button flags */ |
|---|
| 53 | #define CWIID_BTN_2 0x0001 |
|---|
| 54 | #define CWIID_BTN_1 0x0002 |
|---|
| 55 | #define CWIID_BTN_B 0x0004 |
|---|
| 56 | #define CWIID_BTN_A 0x0008 |
|---|
| 57 | #define CWIID_BTN_MINUS 0x0010 |
|---|
| 58 | #define CWIID_BTN_HOME 0x0080 |
|---|
| 59 | #define CWIID_BTN_LEFT 0x0100 |
|---|
| 60 | #define CWIID_BTN_RIGHT 0x0200 |
|---|
| 61 | #define CWIID_BTN_DOWN 0x0400 |
|---|
| 62 | #define CWIID_BTN_UP 0x0800 |
|---|
| 63 | #define CWIID_BTN_PLUS 0x1000 |
|---|
| 64 | |
|---|
| 65 | #define CWIID_NUNCHUK_BTN_Z 0x01 |
|---|
| 66 | #define CWIID_NUNCHUK_BTN_C 0x02 |
|---|
| 67 | |
|---|
| 68 | #define CWIID_CLASSIC_BTN_UP 0x0001 |
|---|
| 69 | #define CWIID_CLASSIC_BTN_LEFT 0x0002 |
|---|
| 70 | #define CWIID_CLASSIC_BTN_ZR 0x0004 |
|---|
| 71 | #define CWIID_CLASSIC_BTN_X 0x0008 |
|---|
| 72 | #define CWIID_CLASSIC_BTN_A 0x0010 |
|---|
| 73 | #define CWIID_CLASSIC_BTN_Y 0x0020 |
|---|
| 74 | #define CWIID_CLASSIC_BTN_B 0x0040 |
|---|
| 75 | #define CWIID_CLASSIC_BTN_ZL 0x0080 |
|---|
| 76 | #define CWIID_CLASSIC_BTN_R 0x0200 |
|---|
| 77 | #define CWIID_CLASSIC_BTN_PLUS 0x0400 |
|---|
| 78 | #define CWIID_CLASSIC_BTN_HOME 0x0800 |
|---|
| 79 | #define CWIID_CLASSIC_BTN_MINUS 0x1000 |
|---|
| 80 | #define CWIID_CLASSIC_BTN_L 0x2000 |
|---|
| 81 | #define CWIID_CLASSIC_BTN_DOWN 0x4000 |
|---|
| 82 | #define CWIID_CLASSIC_BTN_RIGHT 0x8000 |
|---|
| 83 | |
|---|
| 84 | /* Send Report flags */ |
|---|
| 85 | #define CWIID_SEND_RPT_NO_RUMBLE 0x01 |
|---|
| 86 | |
|---|
| 87 | /* Data Read/Write flags */ |
|---|
| 88 | #define CWIID_RW_EEPROM 0x00 |
|---|
| 89 | #define CWIID_RW_REG 0x04 |
|---|
| 90 | #define CWIID_RW_DECODE 0x00 |
|---|
| 91 | |
|---|
| 92 | /* Maximum Data Read Length */ |
|---|
| 93 | #define CWIID_MAX_READ_LEN 0xFFFF |
|---|
| 94 | |
|---|
| 95 | /* Array Index Defs */ |
|---|
| 96 | #define CWIID_X 0 |
|---|
| 97 | #define CWIID_Y 1 |
|---|
| 98 | #define CWIID_Z 2 |
|---|
| 99 | #define CWIID_PHI 0 |
|---|
| 100 | #define CWIID_THETA 1 |
|---|
| 101 | #define CWIID_PSI 2 |
|---|
| 102 | |
|---|
| 103 | /* Acc Defs */ |
|---|
| 104 | #define CWIID_ACC_MAX 0xFF |
|---|
| 105 | |
|---|
| 106 | /* IR Defs */ |
|---|
| 107 | #define CWIID_IR_SRC_COUNT 4 |
|---|
| 108 | #define CWIID_IR_X_MAX 1024 |
|---|
| 109 | #define CWIID_IR_Y_MAX 768 |
|---|
| 110 | |
|---|
| 111 | /* Battery */ |
|---|
| 112 | #define CWIID_BATTERY_MAX 0xD0 |
|---|
| 113 | |
|---|
| 114 | /* Classic Controller Maxes */ |
|---|
| 115 | #define CWIID_CLASSIC_L_STICK_MAX 0x3F |
|---|
| 116 | #define CWIID_CLASSIC_R_STICK_MAX 0x1F |
|---|
| 117 | #define CWIID_CLASSIC_LR_MAX 0x1F |
|---|
| 118 | |
|---|
| 119 | /* Environment Variables */ |
|---|
| 120 | #define WIIMOTE_BDADDR "WIIMOTE_BDADDR" |
|---|
| 121 | |
|---|
| 122 | /* Callback Maximum Message Count */ |
|---|
| 123 | #define CWIID_MAX_MESG_COUNT 5 |
|---|
| 124 | |
|---|
| 125 | /* Enumerations */ |
|---|
| 126 | enum cwiid_command { |
|---|
| 127 | CWIID_CMD_STATUS, |
|---|
| 128 | CWIID_CMD_LED, |
|---|
| 129 | CWIID_CMD_RUMBLE, |
|---|
| 130 | CWIID_CMD_RPT_MODE |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | enum cwiid_mesg_type { |
|---|
| 134 | CWIID_MESG_STATUS, |
|---|
| 135 | CWIID_MESG_BTN, |
|---|
| 136 | CWIID_MESG_ACC, |
|---|
| 137 | CWIID_MESG_IR, |
|---|
| 138 | CWIID_MESG_NUNCHUK, |
|---|
| 139 | CWIID_MESG_CLASSIC, |
|---|
| 140 | CWIID_MESG_BALANCE, |
|---|
| 141 | CWIID_MESG_MOTIONPLUS, |
|---|
| 142 | CWIID_MESG_ERROR, |
|---|
| 143 | CWIID_MESG_UNKNOWN |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | enum cwiid_ext_type { |
|---|
| 147 | CWIID_EXT_NONE, |
|---|
| 148 | CWIID_EXT_NUNCHUK, |
|---|
| 149 | CWIID_EXT_CLASSIC, |
|---|
| 150 | CWIID_EXT_BALANCE, |
|---|
| 151 | CWIID_EXT_MOTIONPLUS, |
|---|
| 152 | CWIID_EXT_UNKNOWN |
|---|
| 153 | }; |
|---|
| 154 | |
|---|
| 155 | enum cwiid_error { |
|---|
| 156 | CWIID_ERROR_NONE, |
|---|
| 157 | CWIID_ERROR_DISCONNECT, |
|---|
| 158 | CWIID_ERROR_COMM |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | struct acc_cal { |
|---|
| 162 | uint8_t zero[3]; |
|---|
| 163 | uint8_t one[3]; |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | struct balance_cal { |
|---|
| 167 | uint16_t right_top[3]; |
|---|
| 168 | uint16_t right_bottom[3]; |
|---|
| 169 | uint16_t left_top[3]; |
|---|
| 170 | uint16_t left_bottom[3]; |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | /* Message Structs */ |
|---|
| 174 | struct cwiid_status_mesg { |
|---|
| 175 | enum cwiid_mesg_type type; |
|---|
| 176 | uint8_t battery; |
|---|
| 177 | enum cwiid_ext_type ext_type; |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | struct cwiid_btn_mesg { |
|---|
| 181 | enum cwiid_mesg_type type; |
|---|
| 182 | uint16_t buttons; |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | struct cwiid_acc_mesg { |
|---|
| 186 | enum cwiid_mesg_type type; |
|---|
| 187 | uint8_t acc[3]; |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | struct cwiid_ir_src { |
|---|
| 191 | char valid; |
|---|
| 192 | uint16_t pos[2]; |
|---|
| 193 | int8_t size; |
|---|
| 194 | }; |
|---|
| 195 | |
|---|
| 196 | struct cwiid_ir_mesg { |
|---|
| 197 | enum cwiid_mesg_type type; |
|---|
| 198 | struct cwiid_ir_src src[CWIID_IR_SRC_COUNT]; |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | struct cwiid_nunchuk_mesg { |
|---|
| 202 | enum cwiid_mesg_type type; |
|---|
| 203 | uint8_t stick[2]; |
|---|
| 204 | uint8_t acc[3]; |
|---|
| 205 | uint8_t buttons; |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | struct cwiid_classic_mesg { |
|---|
| 209 | enum cwiid_mesg_type type; |
|---|
| 210 | uint8_t l_stick[2]; |
|---|
| 211 | uint8_t r_stick[2]; |
|---|
| 212 | uint8_t l; |
|---|
| 213 | uint8_t r; |
|---|
| 214 | uint16_t buttons; |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | struct cwiid_balance_mesg { |
|---|
| 218 | enum cwiid_mesg_type type; |
|---|
| 219 | uint16_t right_top; |
|---|
| 220 | uint16_t right_bottom; |
|---|
| 221 | uint16_t left_top; |
|---|
| 222 | uint16_t left_bottom; |
|---|
| 223 | }; |
|---|
| 224 | |
|---|
| 225 | struct cwiid_motionplus_mesg { |
|---|
| 226 | enum cwiid_mesg_type type; |
|---|
| 227 | uint16_t angle_rate[3]; |
|---|
| 228 | uint8_t low_speed[3]; |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | struct cwiid_error_mesg { |
|---|
| 232 | enum cwiid_mesg_type type; |
|---|
| 233 | enum cwiid_error error; |
|---|
| 234 | }; |
|---|
| 235 | |
|---|
| 236 | union cwiid_mesg { |
|---|
| 237 | enum cwiid_mesg_type type; |
|---|
| 238 | struct cwiid_status_mesg status_mesg; |
|---|
| 239 | struct cwiid_btn_mesg btn_mesg; |
|---|
| 240 | struct cwiid_acc_mesg acc_mesg; |
|---|
| 241 | struct cwiid_ir_mesg ir_mesg; |
|---|
| 242 | struct cwiid_nunchuk_mesg nunchuk_mesg; |
|---|
| 243 | struct cwiid_classic_mesg classic_mesg; |
|---|
| 244 | struct cwiid_balance_mesg balance_mesg; |
|---|
| 245 | struct cwiid_motionplus_mesg motionplus_mesg; |
|---|
| 246 | struct cwiid_error_mesg error_mesg; |
|---|
| 247 | }; |
|---|
| 248 | |
|---|
| 249 | /* State Structs */ |
|---|
| 250 | struct nunchuk_state { |
|---|
| 251 | uint8_t stick[2]; |
|---|
| 252 | uint8_t acc[3]; |
|---|
| 253 | uint8_t buttons; |
|---|
| 254 | }; |
|---|
| 255 | |
|---|
| 256 | struct classic_state { |
|---|
| 257 | uint8_t l_stick[2]; |
|---|
| 258 | uint8_t r_stick[2]; |
|---|
| 259 | uint8_t l; |
|---|
| 260 | uint8_t r; |
|---|
| 261 | uint16_t buttons; |
|---|
| 262 | }; |
|---|
| 263 | |
|---|
| 264 | struct balance_state { |
|---|
| 265 | uint16_t right_top; |
|---|
| 266 | uint16_t right_bottom; |
|---|
| 267 | uint16_t left_top; |
|---|
| 268 | uint16_t left_bottom; |
|---|
| 269 | }; |
|---|
| 270 | |
|---|
| 271 | struct motionplus_state { |
|---|
| 272 | uint16_t angle_rate[3]; |
|---|
| 273 | uint8_t low_speed[3]; |
|---|
| 274 | }; |
|---|
| 275 | |
|---|
| 276 | union ext_state { |
|---|
| 277 | struct nunchuk_state nunchuk; |
|---|
| 278 | struct classic_state classic; |
|---|
| 279 | struct balance_state balance; |
|---|
| 280 | struct motionplus_state motionplus; |
|---|
| 281 | }; |
|---|
| 282 | |
|---|
| 283 | struct cwiid_state { |
|---|
| 284 | uint8_t rpt_mode; |
|---|
| 285 | uint8_t led; |
|---|
| 286 | uint8_t rumble; |
|---|
| 287 | uint8_t battery; |
|---|
| 288 | uint16_t buttons; |
|---|
| 289 | uint8_t acc[3]; |
|---|
| 290 | struct cwiid_ir_src ir_src[CWIID_IR_SRC_COUNT]; |
|---|
| 291 | enum cwiid_ext_type ext_type; |
|---|
| 292 | union ext_state ext; |
|---|
| 293 | enum cwiid_error error; |
|---|
| 294 | }; |
|---|
| 295 | |
|---|
| 296 | /* Typedefs */ |
|---|
| 297 | typedef struct wiimote cwiid_wiimote_t; |
|---|
| 298 | |
|---|
| 299 | typedef void cwiid_mesg_callback_t(cwiid_wiimote_t *, int, |
|---|
| 300 | union cwiid_mesg [], struct timespec *); |
|---|
| 301 | typedef void cwiid_err_t(cwiid_wiimote_t *, const char *, va_list ap); |
|---|
| 302 | |
|---|
| 303 | /* get_bdinfo */ |
|---|
| 304 | #define BT_NO_WIIMOTE_FILTER 0x01 |
|---|
| 305 | #define BT_NAME_LEN 32 |
|---|
| 306 | |
|---|
| 307 | struct cwiid_bdinfo { |
|---|
| 308 | bdaddr_t bdaddr; |
|---|
| 309 | uint8_t btclass[3]; |
|---|
| 310 | char name[BT_NAME_LEN]; |
|---|
| 311 | }; |
|---|
| 312 | |
|---|
| 313 | #ifdef __cplusplus |
|---|
| 314 | extern "C" { |
|---|
| 315 | #endif |
|---|
| 316 | |
|---|
| 317 | /* Error reporting (library wide) */ |
|---|
| 318 | int cwiid_set_err(cwiid_err_t *err); |
|---|
| 319 | void cwiid_err_default(struct wiimote *wiimote, const char *str, va_list ap); |
|---|
| 320 | |
|---|
| 321 | /* Connection */ |
|---|
| 322 | #define cwiid_connect cwiid_open |
|---|
| 323 | #define cwiid_disconnect cwiid_close |
|---|
| 324 | cwiid_wiimote_t *cwiid_open(bdaddr_t *bdaddr, int flags); |
|---|
| 325 | cwiid_wiimote_t *cwiid_open_timeout(bdaddr_t *bdaddr, int flags, int timeout); |
|---|
| 326 | cwiid_wiimote_t *cwiid_listen(int flags); |
|---|
| 327 | int cwiid_close(cwiid_wiimote_t *wiimote); |
|---|
| 328 | |
|---|
| 329 | int cwiid_get_id(cwiid_wiimote_t *wiimote); |
|---|
| 330 | int cwiid_set_data(cwiid_wiimote_t *wiimote, const void *data); |
|---|
| 331 | const void *cwiid_get_data(cwiid_wiimote_t *wiimote); |
|---|
| 332 | int cwiid_enable(cwiid_wiimote_t *wiimote, int flags); |
|---|
| 333 | int cwiid_disable(cwiid_wiimote_t *wiimote, int flags); |
|---|
| 334 | |
|---|
| 335 | /* Interfaces */ |
|---|
| 336 | int cwiid_set_mesg_callback(cwiid_wiimote_t *wiimote, |
|---|
| 337 | cwiid_mesg_callback_t *callback); |
|---|
| 338 | int cwiid_get_mesg(cwiid_wiimote_t *wiimote, int *mesg_count, |
|---|
| 339 | union cwiid_mesg *mesg[], struct timespec *timestamp); |
|---|
| 340 | int cwiid_get_state(cwiid_wiimote_t *wiimote, struct cwiid_state *state); |
|---|
| 341 | int cwiid_get_acc_cal(struct wiimote *wiimote, enum cwiid_ext_type ext_type, |
|---|
| 342 | struct acc_cal *acc_cal); |
|---|
| 343 | int cwiid_get_balance_cal(struct wiimote *wiimote, |
|---|
| 344 | struct balance_cal *balance_cal); |
|---|
| 345 | |
|---|
| 346 | /* Operations */ |
|---|
| 347 | int cwiid_command(cwiid_wiimote_t *wiimote, enum cwiid_command command, |
|---|
| 348 | int flags); |
|---|
| 349 | int cwiid_send_rpt(cwiid_wiimote_t *wiimote, uint8_t flags, uint8_t report, |
|---|
| 350 | size_t len, const void *data); |
|---|
| 351 | int cwiid_request_status(cwiid_wiimote_t *wiimote); |
|---|
| 352 | int cwiid_set_led(cwiid_wiimote_t *wiimote, uint8_t led); |
|---|
| 353 | int cwiid_set_rumble(cwiid_wiimote_t *wiimote, uint8_t rumble); |
|---|
| 354 | int cwiid_set_rpt_mode(cwiid_wiimote_t *wiimote, uint8_t rpt_mode); |
|---|
| 355 | int cwiid_read(cwiid_wiimote_t *wiimote, uint8_t flags, uint32_t offset, |
|---|
| 356 | uint16_t len, void *data); |
|---|
| 357 | int cwiid_write(cwiid_wiimote_t *wiimote, uint8_t flags, uint32_t offset, |
|---|
| 358 | uint16_t len, const void *data); |
|---|
| 359 | /* int cwiid_beep(cwiid_wiimote_t *wiimote); */ |
|---|
| 360 | |
|---|
| 361 | /* HCI functions */ |
|---|
| 362 | int cwiid_get_bdinfo_array(int dev_id, unsigned int timeout, int max_bdinfo, |
|---|
| 363 | struct cwiid_bdinfo **bdinfo, uint8_t flags); |
|---|
| 364 | int cwiid_find_wiimote(bdaddr_t *bdaddr, int timeout); |
|---|
| 365 | |
|---|
| 366 | #ifdef __cplusplus |
|---|
| 367 | } |
|---|
| 368 | #endif |
|---|
| 369 | |
|---|
| 370 | #endif |
|---|