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