Exemplo n.º 1
0
void wiic_sound( struct wiimote_t* wm ) {
  /*Working buffer*/
  unsigned char buf1[21] = { 0xA0, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
                             0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
                             0xC3, 0xC3, 0xC3, 0xC3, 0xC3 };
  
  int t;
  
  for ( t = 0; t <= 50; t++ ) {
    wiic_send( wm, WM_CMD_SPEAKER_DATA, buf1, 21 );
    usleep( 50000 );
  }
  
  /* set the wiimote report type */
  wiic_set_report_type( wm );
  
  /* wait for the wiimote to catch up */
  usleep( 50000 );
}
Exemplo n.º 2
0
/**
 *	@brief Connect to a wiimote with a known address.
 *
 *	@param wm		Pointer to a wiimote_t structure.
 *	@param address	The address of the device to connect to.
 *					If NULL, use the address in the struct set by wiic_find().
 *
 *	@return 1 on success, 0 on failure
 */
static int wiic_connect_single(struct wiimote_t* wm, char* address) {
    struct sockaddr_l2 addr;
    memset(&addr, 0, sizeof(addr));

    if (!wm || WIIMOTE_IS_CONNECTED(wm))
        return 0;

    addr.l2_family = AF_BLUETOOTH;

    if (address)
        /* use provided address */
        str2ba(address, &addr.l2_bdaddr);
    else
        /* use address of device discovered */
        addr.l2_bdaddr = wm->bdaddr;

    /*
     *	OUTPUT CHANNEL
     */
    wm->out_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    if (wm->out_sock == -1)
        return 0;

    addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);

    /* connect to wiimote */
    if (connect(wm->out_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        perror("connect() output sock");
        return 0;
    }

    /*
     *	INPUT CHANNEL
     */
    wm->in_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    if (wm->in_sock == -1) {
        close(wm->out_sock);
        wm->out_sock = -1;
        return 0;
    }

    addr.l2_psm = htobs(WM_INPUT_CHANNEL);

    /* connect to wiimote */
    if (connect(wm->in_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        perror("connect() interrupt sock");
        close(wm->out_sock);
        wm->out_sock = -1;
        return 0;
    }

    WIIC_INFO("Connected to wiimote [id %i].", wm->unid);

    /* do the handshake */
    WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
    wiic_handshake(wm, NULL, 0);

    wiic_set_report_type(wm);

    return 1;
}
Exemplo n.º 3
0
void wiic_set_speaker( struct wiimote_t* wm, int status ) {
  
  if ( status ) {
    // if already enabled then stop
    if ( WIIMOTE_IS_SET(wm, WIIMOTE_STATE_SPEAKER) ) return;
    
    WIIMOTE_ENABLE_STATE( wm, WIIMOTE_STATE_SPEAKER );
    WIIMOTE_ENABLE_STATE( wm, WIIMOTE_STATE_SPEAKER_MUTE );
    
    byte buf;
    
    /* Initialization Protocol */

    // Enable Speaker
    buf = 0x04;
    wiic_send( wm, WM_CMD_SPEAKER_ENABLE, &buf, 1 );
    
    // Mute Speaker
    buf = 0x04;
    wiic_send( wm, WM_CMD_SPEAKER_MUTE, &buf, 1 );
    
    // Write 0x01 to register 0xa20009
    buf = 0x01;
    wiic_write_data( wm, 0x04a20009, &buf, 1 );
    
    // Write 0x08 to register 0xa20001
    buf = 0x08;
    wiic_write_data( wm, 0x04a20001, &buf, 1 );
    
    // 1st byte for configuration 
    buf = 0x00;
    wiic_write_data( wm, 0x04a20001, &buf, 1 );
    // 2nd byte for configuration
    buf = 0x00;
    wiic_write_data( wm, 0x04a20002, &buf, 1 );
    // 3rd byte for configuration
    buf = 0xD0;
    wiic_write_data( wm, 0x04a20003, &buf, 1 );
    // 4th byte for configuration
    buf = 0x07;
    wiic_write_data( wm, 0x04a20004, &buf, 1 );
    // 5th byte for configuration
    buf = 40;
    wiic_write_data( wm, 0x04a20005, &buf, 1 );
    // 6th byte for configuration
    buf = 0x00;
    wiic_write_data( wm, 0x04a20006, &buf, 1 );
    // 7th byte for configuration
    buf = 0x00;
    wiic_write_data( wm, 0x04a20007, &buf, 1 );
    
    // Write 0x01 to register 0xa20008
    buf = 0x01;
    wiic_write_data( wm, 0x04a20008, &buf, 1 );
    
    // Unmute Speaker
    buf = 0x00;
    wiic_send( wm, WM_CMD_SPEAKER_MUTE, &buf, 1 );
    
    WIIC_DEBUG("Speaker enabled");
  } else {
    // if already disabled then stop 
    if ( !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_SPEAKER) ) return;
    
    WIIMOTE_DISABLE_STATE( wm, WIIMOTE_STATE_SPEAKER );
    WIIMOTE_DISABLE_STATE( wm, WIIMOTE_STATE_SPEAKER_MUTE );
    WIIC_DEBUG("Speaker disabled");
    byte buf = 0x00;
    wiic_send( wm, WM_CMD_SPEAKER_ENABLE, &buf, 1 );
  }
  
  /* set the wiimote report type */
  wiic_set_report_type( wm );
  
  /* wait for the wiimote to catch up */
  usleep( 50000 );
}
Exemplo n.º 4
0
/**
 *	@brief	Set if the wiimote should track IR targets.
 *
 *	@param wm		Pointer to a wiimote_t structure.
 *	@param status	1 to enable, 0 to disable.
 */
void wiic_set_ir( struct wiimote_t* wm, int status ) {
  byte buf;
  char* block1 = NULL;
  char* block2 = NULL;
  int ir_level;
  
  if ( !wm ) return;
  
  /*
   *	Wait for the handshake to finish first.
   *	When it handshake finishes and sees that
   *	IR is enabled, it will call this function
   *	again to actually enable IR.
   */
  if ( !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_HANDSHAKE_COMPLETE) ) {
    WIIC_DEBUG("Tried to enable IR, will wait until handshake finishes.");
    WIIMOTE_ENABLE_STATE( wm, WIIMOTE_STATE_IR );
    return;
  }
  
  /*
   *	Check to make sure a sensitivity setting is selected.
   */
  ir_level = get_ir_sens( wm, &block1, &block2 );
  if ( !ir_level ) {
    WIIC_ERROR( "No IR sensitivity setting selected." );
    return;
  }
  
  if ( status ) {
    /* if already enabled then stop */
    if ( WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR) ) return;
    WIIMOTE_ENABLE_STATE( wm, WIIMOTE_STATE_IR );
  } else {
    /* if already disabled then stop */
    if ( !WIIMOTE_IS_SET(wm, WIIMOTE_STATE_IR) ) return;
    WIIMOTE_DISABLE_STATE( wm, WIIMOTE_STATE_IR );
  }
  
  /* set camera 1 and 2 */
  buf = (status ? 0x04 : 0x00);
  wiic_send( wm, WM_CMD_IR, &buf, 1 );
  wiic_send( wm, WM_CMD_IR_2, &buf, 1 );
  
  if ( !status ) {
    WIIC_DEBUG("Disabled IR cameras for wiimote id %i.", wm->unid);
    wiic_set_report_type( wm );
    return;
  }
  
  /* enable IR, set sensitivity */
  buf = 0x08;
  wiic_write_data( wm, WM_REG_IR, &buf, 1 );
  
  /* wait for the wiimote to catch up */
  usleep( 50000 );
  
  /* write sensitivity blocks */
  wiic_write_data( wm, WM_REG_IR_BLOCK1, (byte*) block1, 9 );
  wiic_write_data( wm, WM_REG_IR_BLOCK2, (byte*) block2, 2 );
  
  /* set the IR mode */
  if ( WIIMOTE_IS_SET(wm, WIIMOTE_STATE_EXP) ) buf = WM_IR_TYPE_BASIC;
  else buf = WM_IR_TYPE_EXTENDED;
  wiic_write_data( wm, WM_REG_IR_MODENUM, &buf, 1 );
  
  usleep( 50000 );
  
  /* set the wiimote report type */
  wiic_set_report_type( wm );
  
  WIIC_DEBUG("Enabled IR camera for wiimote id %i (sensitivity level %i).", wm->unid, ir_level);
}