[9780] | 1 | /* |
---|
| 2 | * io.c |
---|
| 3 | * |
---|
| 4 | * This file is part of WiiC, written by: |
---|
| 5 | * Gabriele Randelli |
---|
| 6 | * Email: randelli@dis.uniroma1.it |
---|
| 7 | * |
---|
| 8 | * Copyright 2010 |
---|
| 9 | * |
---|
| 10 | * This file is based on Wiiuse, written By: |
---|
| 11 | * Michael Laforest < para > |
---|
| 12 | * Email: < thepara (--AT--) g m a i l [--DOT--] com > |
---|
| 13 | * |
---|
| 14 | * Copyright 2006-2007 |
---|
| 15 | * |
---|
| 16 | * This program is free software; you can redistribute it and/or modify |
---|
| 17 | * it under the terms of the GNU General Public License as published by |
---|
| 18 | * the Free Software Foundation; either version 3 of the License, or |
---|
| 19 | * (at your option) any later version. |
---|
| 20 | * |
---|
| 21 | * This program is distributed in the hope that it will be useful, |
---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 24 | * GNU General Public License for more details. |
---|
| 25 | * |
---|
| 26 | * You should have received a copy of the GNU General Public License |
---|
| 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 28 | * |
---|
| 29 | * $Header$ |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * @file |
---|
| 34 | * @brief Handles device I/O (non-OS specific). |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | #include <stdio.h> |
---|
| 38 | #include <stdlib.h> |
---|
| 39 | |
---|
| 40 | #include "definitions.h" |
---|
| 41 | #include "wiic_internal.h" |
---|
| 42 | #include "io.h" |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * @brief Get initialization data from the wiimote. |
---|
| 47 | * |
---|
| 48 | * @param wm Pointer to a wiimote_t structure. |
---|
| 49 | * @param data unused |
---|
| 50 | * @param len unused |
---|
| 51 | * |
---|
| 52 | * When first called for a wiimote_t structure, a request |
---|
| 53 | * is sent to the wiimote for initialization information. |
---|
| 54 | * This includes factory set accelerometer data. |
---|
| 55 | * The handshake will be concluded when the wiimote responds |
---|
| 56 | * with this data. |
---|
| 57 | */ |
---|
| 58 | void wiic_handshake(struct wiimote_t* wm, byte* data, unsigned short len) { |
---|
| 59 | if (!wm) return; |
---|
| 60 | |
---|
| 61 | switch (wm->handshake_state) { |
---|
| 62 | case 0: |
---|
| 63 | { |
---|
| 64 | /* send request to wiimote for accelerometer calibration */ |
---|
| 65 | byte* buf; |
---|
| 66 | |
---|
| 67 | WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); |
---|
| 68 | wiic_set_leds(wm, WIIMOTE_LED_NONE); |
---|
| 69 | |
---|
| 70 | buf = (byte*)malloc(sizeof(byte) * 8); |
---|
| 71 | wiic_read_data_cb(wm, wiic_handshake, buf, WM_MEM_OFFSET_CALIBRATION, 7); |
---|
| 72 | wm->handshake_state++; |
---|
| 73 | |
---|
| 74 | wiic_set_leds(wm, WIIMOTE_LED_NONE); |
---|
| 75 | |
---|
| 76 | break; |
---|
| 77 | } |
---|
| 78 | case 1: |
---|
| 79 | { |
---|
| 80 | struct read_req_t* req = wm->read_req; |
---|
| 81 | struct accel_t* accel = &wm->accel_calib; |
---|
| 82 | |
---|
| 83 | /* received read data */ |
---|
| 84 | accel->cal_zero.x = req->buf[0]; |
---|
| 85 | accel->cal_zero.y = req->buf[1]; |
---|
| 86 | accel->cal_zero.z = req->buf[2]; |
---|
| 87 | |
---|
| 88 | accel->cal_g.x = req->buf[4] - accel->cal_zero.x; |
---|
| 89 | accel->cal_g.y = req->buf[5] - accel->cal_zero.y; |
---|
| 90 | accel->cal_g.z = req->buf[6] - accel->cal_zero.z; |
---|
| 91 | |
---|
| 92 | /* done with the buffer */ |
---|
| 93 | free(req->buf); |
---|
| 94 | |
---|
| 95 | /* handshake is done */ |
---|
| 96 | WIIC_DEBUG("Handshake finished. Calibration: Idle: X=%x Y=%x Z=%x\t+1g: X=%x Y=%x Z=%x", |
---|
| 97 | accel->cal_zero.x, accel->cal_zero.y, accel->cal_zero.z, |
---|
| 98 | accel->cal_g.x, accel->cal_g.y, accel->cal_g.z); |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | /* request the status of the wiimote to see if there is an expansion */ |
---|
| 102 | wiic_status(wm); |
---|
| 103 | |
---|
| 104 | WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); |
---|
| 105 | WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE); |
---|
| 106 | wm->handshake_state++; |
---|
| 107 | |
---|
| 108 | /* now enable IR if it was set before the handshake completed */ |
---|
| 109 | if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR)) { |
---|
| 110 | WIIC_DEBUG("Handshake finished, enabling IR."); |
---|
| 111 | WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_IR); |
---|
| 112 | wiic_set_ir(wm, 1); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | break; |
---|
| 116 | } |
---|
| 117 | default: |
---|
| 118 | { |
---|
| 119 | break; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|