int cwiid_beep(cwiid_wiimote_t *wiimote) { /* unsigned char buf[SOUND_BUF_LEN] = { 0xA0, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33}; */ unsigned char buf[SOUND_BUF_LEN] = { 0xA0, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3 }; int i; int ret = 0; pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t timer_cond = PTHREAD_COND_INITIALIZER; struct timespec t; if (exec_write_seq(wiimote, SEQ_LEN(speaker_enable_seq), speaker_enable_seq)) { cwiid_err(wiimote, "Speaker enable error"); ret = -1; } pthread_mutex_lock(&timer_mutex); for (i=0; i<100; i++) { clock_gettime(CLOCK_REALTIME, &t); t.tv_nsec += 10204081; /* t.tv_nsec += 7000000; */ if (cwiid_send_rpt(wiimote, 0, RPT_SPEAKER_DATA, SOUND_BUF_LEN, buf)) { printf("%d\n", i); cwiid_err(wiimote, "Report send error (speaker data)"); ret = -1; break; } /* TODO: I should be shot for this, but hey, it works. * longterm - find a better wait */ pthread_cond_timedwait(&timer_cond, &timer_mutex, &t); } pthread_mutex_unlock(&timer_mutex); if (exec_write_seq(wiimote, SEQ_LEN(speaker_disable_seq), speaker_disable_seq)) { cwiid_err(wiimote, "Speaker disable error"); ret = -1; } return ret; }
int update_rpt_mode(struct wiimote *wiimote, int8_t rpt_mode) { unsigned char buf[RPT_MODE_BUF_LEN]; uint8_t rpt_type; struct write_seq *ir_enable_seq; int seq_len; /* rpt_mode = bitmask of requested report types */ /* rpt_type = report id sent to the wiimote */ if (pthread_mutex_lock(&wiimote->rpt_mutex)) { cwiid_err(wiimote, "Mutex lock error (rpt mutex)"); return -1; } /* -1 updates the reporting mode using old rpt_mode * (reporting type may change if extensions are * plugged in/unplugged */ if (rpt_mode == -1) { rpt_mode = wiimote->state.rpt_mode; } /* Pick a report mode based on report flags */ if ((rpt_mode & CWIID_RPT_EXT) && ((wiimote->state.ext_type == CWIID_EXT_NUNCHUK) || (wiimote->state.ext_type == CWIID_EXT_CLASSIC))) { if ((rpt_mode & CWIID_RPT_IR) && (rpt_mode & CWIID_RPT_ACC)) { rpt_type = RPT_BTN_ACC_IR10_EXT6; ir_enable_seq = ir_enable10_seq; seq_len = SEQ_LEN(ir_enable10_seq); } else if (rpt_mode & CWIID_RPT_IR) { rpt_type = RPT_BTN_IR10_EXT9; ir_enable_seq = ir_enable10_seq; seq_len = SEQ_LEN(ir_enable10_seq); } else if (rpt_mode & CWIID_RPT_ACC) { rpt_type = RPT_BTN_ACC_EXT16; } else if (rpt_mode & CWIID_RPT_BTN) { rpt_type = RPT_BTN_EXT8; } else { rpt_type = RPT_EXT21; } } else if ((rpt_mode & CWIID_RPT_EXT) && wiimote->state.ext_type == CWIID_EXT_BALANCE) { rpt_type = RPT_BTN_EXT8; } else { if (rpt_mode & CWIID_RPT_IR) { rpt_type = RPT_BTN_ACC_IR12; ir_enable_seq = ir_enable12_seq; seq_len = SEQ_LEN(ir_enable12_seq); } else if (rpt_mode & CWIID_RPT_ACC) { rpt_type = RPT_BTN_ACC; } else { rpt_type = RPT_BTN; } } /* Enable IR */ /* TODO: only do this when necessary (record old IR mode) */ if ((rpt_mode & CWIID_RPT_IR)) { if (exec_write_seq(wiimote, seq_len, ir_enable_seq)) { cwiid_err(wiimote, "IR enable error"); return -1; } } /* Disable IR */ else if ((wiimote->state.rpt_mode & CWIID_RPT_IR) && !(rpt_mode & CWIID_RPT_IR)) { if (exec_write_seq(wiimote, SEQ_LEN(ir_disable_seq), ir_disable_seq)) { cwiid_err(wiimote, "IR disable error"); return -1; } } /* Send SET_REPORT */ buf[0] = (wiimote->flags & CWIID_FLAG_CONTINUOUS) ? 0x04 : 0; buf[1] = rpt_type; if (send_report(wiimote, 0, RPT_RPT_MODE, RPT_MODE_BUF_LEN, buf)) { cwiid_err(wiimote, "Send report error (report mode)"); return -1; } /* clear state for unreported data */ if (CWIID_RPT_BTN & ~rpt_mode & wiimote->state.rpt_mode) { wiimote->state.buttons = 0; } if (CWIID_RPT_ACC & ~rpt_mode & wiimote->state.rpt_mode) { memset(wiimote->state.acc, 0, sizeof wiimote->state.acc); } if (CWIID_RPT_IR & ~rpt_mode & wiimote->state.rpt_mode) { memset(wiimote->state.ir_src, 0, sizeof wiimote->state.ir_src); } if ((wiimote->state.ext_type == CWIID_EXT_NUNCHUK) && (CWIID_RPT_NUNCHUK & ~rpt_mode & wiimote->state.rpt_mode)) { memset(&wiimote->state.ext, 0, sizeof wiimote->state.ext); } else if ((wiimote->state.ext_type == CWIID_EXT_CLASSIC) && (CWIID_RPT_CLASSIC & ~rpt_mode & wiimote->state.rpt_mode)) { memset(&wiimote->state.ext, 0, sizeof wiimote->state.ext); } else if ((wiimote->state.ext_type == CWIID_EXT_BALANCE) && (CWIID_RPT_BALANCE & ~rpt_mode & wiimote->state.rpt_mode)) { memset(&wiimote->state.ext, 0, sizeof wiimote->state.ext); } wiimote->state.rpt_mode = rpt_mode; if (pthread_mutex_unlock(&wiimote->rpt_mutex)) { cwiid_err(wiimote, "Mutex unlock error (rpt mutex) - " "deadlock warning"); return -1; } return 0; }